From 0c0ae1855dd358c64f15ef6868efa5803c96b6a7 Mon Sep 17 00:00:00 2001 From: MrKevinWeiss Date: Wed, 27 Jun 2018 08:25:51 +0200 Subject: [PATCH] drivers/srf08: cleanup and fixes for new api --- drivers/include/srf08.h | 15 ++++++++------- drivers/srf08/srf08.c | 32 +++++++++++++------------------- tests/driver_srf08/main.c | 9 ++++++--- 3 files changed, 27 insertions(+), 29 deletions(-) diff --git a/drivers/include/srf08.h b/drivers/include/srf08.h index 59b54a9297..31268b913d 100644 --- a/drivers/include/srf08.h +++ b/drivers/include/srf08.h @@ -22,6 +22,7 @@ * * @author Zakaria Kasmi * @author Peter Kietzmann + * @author Kevin Weiss */ #ifndef SRF08_H @@ -79,9 +80,9 @@ typedef enum { SRF08_MODE_INCH = 0x50, /**< result in inches */ SRF08_MODE_CM = 0x51, /**< result in centimeters */ SRF08_MODE_MICRO_SEC = 0x52, /**< result in centimeters */ - SRF08_ANN_MODE_INCH = 0x53, /**< synchronous measurement in inches */ - SRF08_ANN_MODE_CM = 0x54, /**< synchronous measurement in centimeters */ - SRF08_ANN_MODE_MICRO_SEC = 0x55 /**< synchronous measurement in microseconds */ + SRF08_ANN_MODE_INCH = 0x53, /**< synchronous measurement in inch */ + SRF08_ANN_MODE_CM = 0x54, /**< synchronous measurement in cm */ + SRF08_ANN_MODE_MICRO_SEC = 0x55 /**< synchronous measurement in us */ }srf08_mode_t; /** @@ -99,7 +100,7 @@ typedef enum { * @return -4 on max. gain error * */ -int srf08_init(srf08_t *dev, i2c_t i2c, uint8_t addr, i2c_speed_t speed); +int srf08_init(srf08_t *dev, i2c_t i2c, uint8_t addr); /** * @brief Set the maximum range of the SRF08. @@ -119,9 +120,9 @@ int srf08_set_max_range(const srf08_t *dev, uint8_t max_range); * @brief Set the maximum of the analog stages. * * @ note - * This value is just a limitation of the maximum amplification and not the actual. - * While measuing, this value starts at its minimum and increases approx. each 75 us - * until the maximum value is reached. + * This value is just a limitation of the maximum amplification and not the + * actual. While measuring, this value starts at its minimum and increases + * approx. each 75 us until the maximum value is reached. * * @param[in] dev device descriptor of an SRF08 sensor * @param[in] max_gain the maximal gain value. diff --git a/drivers/srf08/srf08.c b/drivers/srf08/srf08.c index 2e8f546bf7..4fa9408b01 100644 --- a/drivers/srf08/srf08.c +++ b/drivers/srf08/srf08.c @@ -17,6 +17,7 @@ * * @author Zakaria Kasmi * @author Peter Kietzmann + * @author Kevin Weiss * * @} */ @@ -27,30 +28,20 @@ #include "srf08.h" #include "periph/i2c.h" -#define ENABLE_DEBUG (0) +#define ENABLE_DEBUG (1) #include "debug.h" -int srf08_init(srf08_t *dev, i2c_t i2c, uint8_t addr, i2c_speed_t speed) +int srf08_init(srf08_t *dev, i2c_t i2c, uint8_t addr) { int status = 0; - (void)speed; (void)status; + (void)addr; + (void)i2c; dev->i2c = i2c; dev->addr = addr; - /* Acquire exclusive access to the bus. */ - i2c_acquire(dev->i2c); - /* initialize i2c interface */ - i2c_init(dev->i2c); - /* Release the bus for other threads. */ - i2c_release(dev->i2c); - - if(status < 0) { - return -1; - } - /* set the maximum range */ if (srf08_set_max_range(dev, SRF08_MAX_RANGE_6M) < 0) { return -3; @@ -93,7 +84,8 @@ int srf08_set_max_gain(const srf08_t *dev, uint8_t gain) } -int srf08_get_distances(const srf08_t *dev, uint16_t *range_array, int num_echos, srf08_mode_t ranging_mode) +int srf08_get_distances(const srf08_t *dev, uint16_t *range_array, + int num_echos, srf08_mode_t ranging_mode) { int status; int echo_number = 0; @@ -104,11 +96,12 @@ int srf08_get_distances(const srf08_t *dev, uint16_t *range_array, int num_echos /* Acquire exclusive access to the bus. */ i2c_acquire(dev->i2c); /* set ranging mode */ - status = i2c_write_reg(dev->i2c, dev->addr, SRF08_COMMAND_REG, ranging_mode, 0); + status = i2c_write_reg(dev->i2c, dev->addr, SRF08_COMMAND_REG, + ranging_mode, 0); /* Release the bus for other threads. */ i2c_release(dev->i2c); - if (!status) { + if (status != 0) { DEBUG("Write the ranging command to the i2c-interface is failed\n"); return -1; } @@ -127,11 +120,12 @@ int srf08_get_distances(const srf08_t *dev, uint16_t *range_array, int num_echos /* Acquire exclusive access to the bus. */ i2c_acquire(dev->i2c); /* read the echo bytes */ - status = i2c_read_regs(dev->i2c, dev->addr, register_location, range_bytes, sizeof(range_bytes), 0); + status = i2c_read_regs(dev->i2c, dev->addr, register_location, + range_bytes, sizeof(range_bytes), 0); /* Release the bus for other threads. */ i2c_release(dev->i2c); - if (!status) { + if (status != 0) { DEBUG("Read the echo bytes from the i2c-interface is failed\n"); return -3; } diff --git a/tests/driver_srf08/main.c b/tests/driver_srf08/main.c index 0be8dded17..4439443959 100644 --- a/tests/driver_srf08/main.c +++ b/tests/driver_srf08/main.c @@ -15,6 +15,7 @@ * * @author Peter Kietzmann * @author Zakaria Kasmi + * @author Kevin Weiss * * @} */ @@ -49,7 +50,7 @@ int main(void) int res; uint16_t range_array[TEST_NUM_ECHOS]; - res = srf08_init(&srf08_0, TEST_SRF08_I2C, SRF08_DEFAULT_ADDR, TEST_SRF08_SPEED); + res = srf08_init(&srf08_0, TEST_SRF08_I2C, SRF08_DEFAULT_ADDR); if (res < 0) { printf("[Failed]"); @@ -61,11 +62,13 @@ int main(void) while(1) { - int echo_number = srf08_get_distances(&srf08_0, range_array, TEST_NUM_ECHOS, TEST_MODE); + int echo_number = srf08_get_distances(&srf08_0, range_array, + TEST_NUM_ECHOS, TEST_MODE); if (echo_number > 0) { for (int i = 0; i < echo_number; i++) { - printf("stored distance = %i cm , echo%i\n", range_array[i], i + 1); + printf("stored distance = %i cm , echo%i\n", + range_array[i], i + 1); } puts("--------------------------------------------"); }