1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

drivers/srf02: expose trigger and read to API

This commit is contained in:
Hauke Petersen 2016-02-12 17:23:06 +01:00
parent 0f31419f98
commit ff2ff5d008
2 changed files with 49 additions and 12 deletions

View File

@ -34,7 +34,12 @@ extern "C" {
/**
* @brief Default I2C address of SRF02 sensors
*/
#define SRF02_DEFAULT_ADDR (0xe0)
#define SRF02_DEFAULT_ADDR (0xe0) /* 224 decimal */
/**
* @brief The datasheet tells us, that ranging takes 70ms
*/
#define SRF02_RANGE_DELAY (70000U)
/**
* @brief Device descriptor for SRF02 sensors
@ -69,11 +74,38 @@ typedef enum {
int srf02_init(srf02_t *dev, i2c_t i2c, uint8_t addr);
/**
* @brief Get the distance measured from the SRF02 ultrasonic sensor
* @brief Trigger a new measurement
*
* This function triggers a new ranging operation. After triggering this
* operation, you have to wait at least 70ms for the result to be ready.
*
* The result of the ranging operation is returned in inches, centimeters or
* microseconds - depending on the given @p mode parameter.
*
* @param[in] dev device to trigger
* @param[in] mode there are three real ranging modes, which return
* the result in inches, centimeters or microseconds.
* Another set of three fake ranging modes do the same
* but without transmitting the burst
*/
void srf02_trigger(srf02_t *dev, srf02_mode_t mode);
/**
* @brief Read the results of the last ranging operation
*
* @param[in] dev device to read from
*
* @return result of the last ranging operation, meaning depends on the mode
* parameter given to the srf02_trigger function
*/
uint16_t srf02_read(srf02_t *dev);
/**
* @brief Get the distance measured from the SRF02 ultrasonic sensor
*
* This function combines the srf02_trigger and the srf02_read functions for
* simplified usage in simple (single sensor) setups.
*
* @param[in] dev device descriptor of an SRF02 sensor
* @param[in] mode there are three real ranging modes, which return
* the result in inches, centimeters or microseconds.

View File

@ -31,11 +31,6 @@
#define ENABLE_DEBUG (0)
#include "debug.h"
/**
* @brief The datasheet tells us, that ranging takes 70ms
*/
#define RANGE_DELAY (70000U)
/**
* @brief Per default use normal speed on the I2C bus
*/
@ -90,18 +85,18 @@ int srf02_init(srf02_t *dev, i2c_t i2c, uint8_t addr)
return 0;
}
uint16_t srf02_get_distance(srf02_t *dev, srf02_mode_t mode)
void srf02_trigger(srf02_t *dev, srf02_mode_t mode)
{
char res[2];
/* trigger a new measurement by writing the mode to the CMD register */
DEBUG("[srf02] trigger new reading\n");
i2c_acquire(dev->i2c);
i2c_write_reg(dev->i2c, dev->addr, REG_CMD, mode);
i2c_release(dev->i2c);
}
/* give the sensor the required time for sampling */
xtimer_usleep(RANGE_DELAY);
uint16_t srf02_read(srf02_t *dev)
{
char res[2];
/* read the results */
i2c_acquire(dev->i2c);
@ -113,6 +108,16 @@ uint16_t srf02_get_distance(srf02_t *dev, srf02_mode_t mode)
return ((((uint16_t)res[0]) << 8) | (res[1] & 0xff));
}
uint16_t srf02_get_distance(srf02_t *dev, srf02_mode_t mode)
{
/* trigger a new reading */
srf02_trigger(dev, mode);
/* give the sensor the required time for sampling */
xtimer_usleep(SRF02_RANGE_DELAY);
/* get the results */
return srf02_read(dev);
}
void srf02_set_addr(srf02_t *dev, uint8_t new_addr)
{
/* get access to the bus */