mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2025-01-18 12:52:44 +01:00
test/driver_srf02: added some commands
- added shell - made init manual (by giving address) - added command to change device address
This commit is contained in:
parent
d6b8336cb9
commit
2581b2c8a2
@ -5,6 +5,7 @@ FEATURES_REQUIRED = periph_i2c
|
||||
|
||||
USEMODULE += xtimer
|
||||
USEMODULE += srf02
|
||||
USEMODULE += shell
|
||||
|
||||
# set default device parameters in case they are undefined
|
||||
TEST_SRF02_I2C ?= I2C_0
|
||||
|
@ -21,6 +21,14 @@
|
||||
* @}
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "shell.h"
|
||||
#include "xtimer.h"
|
||||
#include "srf02.h"
|
||||
|
||||
#ifndef TEST_SRF02_I2C
|
||||
#error "TEST_SRF02_I2C not defined"
|
||||
#endif
|
||||
@ -28,26 +36,35 @@
|
||||
#error "TEST_MODE not defined"
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#define SAMPLE_PERIOD (100 * 1000U)
|
||||
|
||||
#include "xtimer.h"
|
||||
#include "srf02.h"
|
||||
#include "periph/i2c.h"
|
||||
static srf02_t dev;
|
||||
|
||||
#define SAMPLE_PERIOD (1000 * 1000U)
|
||||
|
||||
int main(void)
|
||||
static void sample_loop(void)
|
||||
{
|
||||
int res;
|
||||
srf02_t dev;
|
||||
uint32_t wakeup = xtimer_now();
|
||||
|
||||
puts("\nSRF02 Ultrasonic Range Sensor Test\n");
|
||||
puts("This test will sample the sensor once per second and display the\n"
|
||||
"result\n");
|
||||
while(1) {
|
||||
uint16_t distance = srf02_get_distance(&dev, TEST_MODE);
|
||||
printf("distance = %3i cm\n", distance);
|
||||
xtimer_usleep_until(&wakeup, SAMPLE_PERIOD);
|
||||
}
|
||||
}
|
||||
|
||||
printf("Initializing SRF02 sensor at I2C_%i... ", TEST_SRF02_I2C);
|
||||
res = srf02_init(&dev, TEST_SRF02_I2C, SRF02_DEFAULT_ADDR);
|
||||
static int cmd_init(int argc, char **argv)
|
||||
{
|
||||
int res;
|
||||
|
||||
if (argc < 2) {
|
||||
printf("usage: %s <addr (decimal)>\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
uint8_t addr = (uint8_t)atoi(argv[1]);
|
||||
|
||||
printf("Initializing SRF02 sensor at I2C_DEV(%i), address is 0x%02x\n... ",
|
||||
TEST_SRF02_I2C, (int)addr);
|
||||
res = srf02_init(&dev, TEST_SRF02_I2C, addr);
|
||||
if (res < 0) {
|
||||
puts("[Failed]");
|
||||
return 1;
|
||||
@ -55,10 +72,46 @@ int main(void)
|
||||
else {
|
||||
puts("[Ok]\n");
|
||||
}
|
||||
|
||||
while(1) {
|
||||
xtimer_usleep_until(&wakeup, SAMPLE_PERIOD);
|
||||
uint16_t distance = srf02_get_distance(&dev, TEST_MODE);
|
||||
printf("distance = %i cm\n", distance);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int cmd_sample(int argc, char **argv)
|
||||
{
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
sample_loop();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int cmd_set_addr(int argc, char **argv)
|
||||
{
|
||||
uint8_t new_addr;
|
||||
|
||||
if (argc < 2) {
|
||||
printf("usage: %s <new_addr (decimal)>\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
new_addr = (uint8_t)atoi(argv[1]);
|
||||
srf02_set_addr(&dev, new_addr);
|
||||
printf("Set address to %i (0x%02x)\n", (int)new_addr, (int)new_addr);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const shell_command_t shell_commands[] = {
|
||||
{ "init", "initialize a device", cmd_init },
|
||||
{ "sample", "start sampling", cmd_sample },
|
||||
{ "addr", "reprogram the devices address", cmd_set_addr },
|
||||
{ NULL, NULL, NULL }
|
||||
};
|
||||
|
||||
int main(void)
|
||||
{
|
||||
puts("\nSRF02 Ultrasonic Range Sensor Test\n");
|
||||
puts("This test will sample the sensor once per second and display the\n"
|
||||
"result\n");
|
||||
|
||||
char line_buf[SHELL_DEFAULT_BUFSIZE];
|
||||
shell_run(shell_commands, line_buf, SHELL_DEFAULT_BUFSIZE);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user