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 {
|
typedef struct {
|
||||||
srf04_params_t p; /**< GPIO Ports of device */
|
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 */
|
uint32_t time; /**< timestamp of trigger or echo */
|
||||||
} srf04_t;
|
} srf04_t;
|
||||||
|
|
||||||
@ -87,7 +87,7 @@ void srf04_trigger(const srf04_t *dev);
|
|||||||
* @return SRF04_MEASURING if measurement is in progress
|
* @return SRF04_MEASURING if measurement is in progress
|
||||||
* @return SRF04_INVALID if no valid measurement is available
|
* @return SRF04_INVALID if no valid measurement is available
|
||||||
*/
|
*/
|
||||||
int srf04_read(const srf04_t* dev);
|
int srf04_read(const srf04_t *dev);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Convenience function triggers a measurement and returns distance
|
* @brief Convenience function triggers a measurement and returns distance
|
||||||
@ -100,7 +100,7 @@ int srf04_read(const srf04_t* dev);
|
|||||||
* @return SRF04_MEASURING if measurement is in progress
|
* @return SRF04_MEASURING if measurement is in progress
|
||||||
* @return SRF04_INVALID if no valid measurement is available
|
* @return SRF04_INVALID if no valid measurement is available
|
||||||
*/
|
*/
|
||||||
int srf04_get_distance(const srf04_t* dev);
|
int srf04_get_distance(const srf04_t *dev);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|||||||
@ -31,17 +31,19 @@ static void _cb(void *arg)
|
|||||||
{
|
{
|
||||||
uint32_t t = xtimer_now_usec();
|
uint32_t t = xtimer_now_usec();
|
||||||
|
|
||||||
srf04_t* dev = (srf04_t*)arg;
|
srf04_t *dev = (srf04_t *)arg;
|
||||||
|
|
||||||
if (dev->distance > SRF04_ERR_MEASURING) {
|
if (dev->distance > SRF04_ERR_MEASURING) {
|
||||||
dev->distance = SRF04_ERR_MEASURING;
|
dev->distance = SRF04_ERR_MEASURING;
|
||||||
dev->time = t;
|
dev->time = t;
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
gpio_irq_disable(dev->p.echo);
|
gpio_irq_disable(dev->p.echo);
|
||||||
dev->distance = (t - dev->time);
|
dev->distance = (t - dev->time);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int srf04_init(srf04_t* dev, const srf04_params_t *params)
|
int srf04_init(srf04_t *dev, const srf04_params_t *params)
|
||||||
{
|
{
|
||||||
dev->p = *params;
|
dev->p = *params;
|
||||||
|
|
||||||
@ -53,7 +55,7 @@ int srf04_init(srf04_t* dev, const srf04_params_t *params)
|
|||||||
return SRF04_ERR_GPIO;
|
return SRF04_ERR_GPIO;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (gpio_init_int(dev->p.echo, GPIO_IN, GPIO_BOTH, _cb, (void*)dev) != 0) {
|
if (gpio_init_int(dev->p.echo, GPIO_IN, GPIO_BOTH, _cb, (void *)dev) != 0) {
|
||||||
DEBUG("[srf04] Error: could not initialize GPIO echo pin\n");
|
DEBUG("[srf04] Error: could not initialize GPIO echo pin\n");
|
||||||
return SRF04_ERR_GPIO;
|
return SRF04_ERR_GPIO;
|
||||||
}
|
}
|
||||||
@ -63,7 +65,7 @@ int srf04_init(srf04_t* dev, const srf04_params_t *params)
|
|||||||
return SRF04_OK;
|
return SRF04_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
void srf04_trigger(const srf04_t* dev)
|
void srf04_trigger(const srf04_t *dev)
|
||||||
{
|
{
|
||||||
if (dev->distance == SRF04_ERR_MEASURING) {
|
if (dev->distance == SRF04_ERR_MEASURING) {
|
||||||
return;
|
return;
|
||||||
@ -76,18 +78,20 @@ void srf04_trigger(const srf04_t* dev)
|
|||||||
gpio_clear(dev->p.trigger);
|
gpio_clear(dev->p.trigger);
|
||||||
}
|
}
|
||||||
|
|
||||||
int srf04_read(const srf04_t* dev)
|
int srf04_read(const srf04_t *dev)
|
||||||
{
|
{
|
||||||
return dev->distance;
|
return dev->distance;
|
||||||
}
|
}
|
||||||
|
|
||||||
int srf04_get_distance(const srf04_t* dev)
|
int srf04_get_distance(const srf04_t *dev)
|
||||||
{
|
{
|
||||||
/* trigger new reading */
|
/* Trigger new reading */
|
||||||
srf04_trigger(dev);
|
srf04_trigger(dev);
|
||||||
/* give the sensor the required time for sampling */
|
|
||||||
|
/* Give the sensor the required time for sampling */
|
||||||
xtimer_usleep(SRF04_SAMPLE_PERIOD);
|
xtimer_usleep(SRF04_SAMPLE_PERIOD);
|
||||||
/* get the result */
|
|
||||||
|
/* Get the result */
|
||||||
if (dev->distance >= SRF04_OK) {
|
if (dev->distance >= SRF04_OK) {
|
||||||
return ((dev->distance * 100) / SRF04_DISTANCE);
|
return ((dev->distance * 100) / SRF04_DISTANCE);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -39,7 +39,8 @@ int main(void)
|
|||||||
int distance = srf04_get_distance(&dev);
|
int distance = srf04_get_distance(&dev);
|
||||||
if (distance < SRF04_OK) {
|
if (distance < SRF04_OK) {
|
||||||
puts("Error: no valid data available");
|
puts("Error: no valid data available");
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
printf("D: %d mm\n", distance);
|
printf("D: %d mm\n", distance);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user