Skip to content

Commit ff2ff5d

Browse files
committed
drivers/srf02: expose trigger and read to API
1 parent 0f31419 commit ff2ff5d

File tree

2 files changed

+49
-12
lines changed

2 files changed

+49
-12
lines changed

drivers/include/srf02.h

+34-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,12 @@ extern "C" {
3434
/**
3535
* @brief Default I2C address of SRF02 sensors
3636
*/
37-
#define SRF02_DEFAULT_ADDR (0xe0)
37+
#define SRF02_DEFAULT_ADDR (0xe0) /* 224 decimal */
38+
39+
/**
40+
* @brief The datasheet tells us, that ranging takes 70ms
41+
*/
42+
#define SRF02_RANGE_DELAY (70000U)
3843

3944
/**
4045
* @brief Device descriptor for SRF02 sensors
@@ -69,11 +74,38 @@ typedef enum {
6974
int srf02_init(srf02_t *dev, i2c_t i2c, uint8_t addr);
7075

7176
/**
72-
* @brief Get the distance measured from the SRF02 ultrasonic sensor
77+
* @brief Trigger a new measurement
78+
*
79+
* This function triggers a new ranging operation. After triggering this
80+
* operation, you have to wait at least 70ms for the result to be ready.
7381
*
7482
* The result of the ranging operation is returned in inches, centimeters or
7583
* microseconds - depending on the given @p mode parameter.
7684
*
85+
* @param[in] dev device to trigger
86+
* @param[in] mode there are three real ranging modes, which return
87+
* the result in inches, centimeters or microseconds.
88+
* Another set of three fake ranging modes do the same
89+
* but without transmitting the burst
90+
*/
91+
void srf02_trigger(srf02_t *dev, srf02_mode_t mode);
92+
93+
/**
94+
* @brief Read the results of the last ranging operation
95+
*
96+
* @param[in] dev device to read from
97+
*
98+
* @return result of the last ranging operation, meaning depends on the mode
99+
* parameter given to the srf02_trigger function
100+
*/
101+
uint16_t srf02_read(srf02_t *dev);
102+
103+
/**
104+
* @brief Get the distance measured from the SRF02 ultrasonic sensor
105+
*
106+
* This function combines the srf02_trigger and the srf02_read functions for
107+
* simplified usage in simple (single sensor) setups.
108+
*
77109
* @param[in] dev device descriptor of an SRF02 sensor
78110
* @param[in] mode there are three real ranging modes, which return
79111
* the result in inches, centimeters or microseconds.

drivers/srf02/srf02.c

+15-10
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,6 @@
3131
#define ENABLE_DEBUG (0)
3232
#include "debug.h"
3333

34-
/**
35-
* @brief The datasheet tells us, that ranging takes 70ms
36-
*/
37-
#define RANGE_DELAY (70000U)
38-
3934
/**
4035
* @brief Per default use normal speed on the I2C bus
4136
*/
@@ -90,18 +85,18 @@ int srf02_init(srf02_t *dev, i2c_t i2c, uint8_t addr)
9085
return 0;
9186
}
9287

93-
uint16_t srf02_get_distance(srf02_t *dev, srf02_mode_t mode)
88+
void srf02_trigger(srf02_t *dev, srf02_mode_t mode)
9489
{
95-
char res[2];
96-
9790
/* trigger a new measurement by writing the mode to the CMD register */
9891
DEBUG("[srf02] trigger new reading\n");
9992
i2c_acquire(dev->i2c);
10093
i2c_write_reg(dev->i2c, dev->addr, REG_CMD, mode);
10194
i2c_release(dev->i2c);
95+
}
10296

103-
/* give the sensor the required time for sampling */
104-
xtimer_usleep(RANGE_DELAY);
97+
uint16_t srf02_read(srf02_t *dev)
98+
{
99+
char res[2];
105100

106101
/* read the results */
107102
i2c_acquire(dev->i2c);
@@ -113,6 +108,16 @@ uint16_t srf02_get_distance(srf02_t *dev, srf02_mode_t mode)
113108
return ((((uint16_t)res[0]) << 8) | (res[1] & 0xff));
114109
}
115110

111+
uint16_t srf02_get_distance(srf02_t *dev, srf02_mode_t mode)
112+
{
113+
/* trigger a new reading */
114+
srf02_trigger(dev, mode);
115+
/* give the sensor the required time for sampling */
116+
xtimer_usleep(SRF02_RANGE_DELAY);
117+
/* get the results */
118+
return srf02_read(dev);
119+
}
120+
116121
void srf02_set_addr(srf02_t *dev, uint8_t new_addr)
117122
{
118123
/* get access to the bus */

0 commit comments

Comments
 (0)