Merge pull request #15494 from gdiribarne/bugfix/sfr04-avr-overflow
drivers/srf04: fix overflow on AVR
This commit is contained in:
commit
14877443f7
@ -54,7 +54,7 @@ typedef struct {
|
||||
*/
|
||||
typedef struct {
|
||||
srf04_params_t p; /**< GPIO Ports of device */
|
||||
int distance; /**< raw time of flight distance */
|
||||
int32_t distance; /**< raw time of flight distance */
|
||||
uint32_t time; /**< timestamp of trigger or echo */
|
||||
} srf04_t;
|
||||
|
||||
|
||||
@ -32,10 +32,12 @@ static void _cb(void *arg)
|
||||
uint32_t t = xtimer_now_usec();
|
||||
|
||||
srf04_t *dev = (srf04_t *)arg;
|
||||
|
||||
if (dev->distance > SRF04_ERR_MEASURING) {
|
||||
dev->distance = SRF04_ERR_MEASURING;
|
||||
dev->time = t;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
gpio_irq_disable(dev->p.echo);
|
||||
dev->distance = (t - dev->time);
|
||||
}
|
||||
@ -83,11 +85,13 @@ int srf04_read(const srf04_t* dev)
|
||||
|
||||
int srf04_get_distance(const srf04_t *dev)
|
||||
{
|
||||
/* trigger new reading */
|
||||
/* Trigger new reading */
|
||||
srf04_trigger(dev);
|
||||
/* give the sensor the required time for sampling */
|
||||
|
||||
/* Give the sensor the required time for sampling */
|
||||
xtimer_usleep(SRF04_SAMPLE_PERIOD);
|
||||
/* get the result */
|
||||
|
||||
/* Get the result */
|
||||
if (dev->distance >= SRF04_OK) {
|
||||
return ((dev->distance * 100) / SRF04_DISTANCE);
|
||||
}
|
||||
|
||||
@ -39,7 +39,8 @@ int main(void)
|
||||
int distance = srf04_get_distance(&dev);
|
||||
if (distance < SRF04_OK) {
|
||||
puts("Error: no valid data available");
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
printf("D: %d mm\n", distance);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user