mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
drivers/srf08: cleanup and fixes for new api
This commit is contained in:
parent
98f6fd49a8
commit
0c0ae1855d
@ -22,6 +22,7 @@
|
||||
*
|
||||
* @author Zakaria Kasmi <zkasmi@inf.fu-berlin.de>
|
||||
* @author Peter Kietzmann <peter.kietzmann@haw-hamburg.de>
|
||||
* @author Kevin Weiss <kevin.weiss@haw-hamburg.de>
|
||||
*/
|
||||
|
||||
#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.
|
||||
|
@ -17,6 +17,7 @@
|
||||
*
|
||||
* @author Zakaria Kasmi <zkasmi@inf.fu-berlin.de>
|
||||
* @author Peter Kietzmann <peter.kietzmann@haw-hamburg.de>
|
||||
* @author Kevin Weiss <kevin.weiss@haw-hamburg.de>
|
||||
*
|
||||
* @}
|
||||
*/
|
||||
@ -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;
|
||||
}
|
||||
|
@ -15,6 +15,7 @@
|
||||
*
|
||||
* @author Peter Kietzmann <peter.kietzmann@haw-hamburg.de>
|
||||
* @author Zakaria Kasmi <zkasmi@inf.fu-berlin.de>
|
||||
* @author Kevin Weiss <kevin.weiss@haw-hamburg.de>
|
||||
*
|
||||
* @}
|
||||
*/
|
||||
@ -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("--------------------------------------------");
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user