Blame view

RIOT/tests/driver_srf02/main.c 3.22 KB
a752c7ab   elopes   add first test an...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
  /*
   * Copyright (C) 2014 Hamburg University of Applied Sciences
   *               2015 Freie Universitรคt Berlin
   *
   * This file is subject to the terms and conditions of the GNU Lesser
   * General Public License v2.1. See the file LICENSE in the top level
   * directory for more details.
   */
  
  /**
   * @ingroup     tests
   * @{
   *
   * @file
   * @brief       Test application for the SRF02 ultrasonic range sensor
   *
   * @author      Peter Kietzmann <peter.kietzmann@haw-hamburg.de>
   * @author      Zakaria Kasmi <zkasmi@inf.fu-berlin.de>
   * @author      Hauke Petersen <hauke.petersen@fu-berlin.de>
   *
   * @}
   */
  
  #include <stdio.h>
  #include <stdint.h>
  #include <stdlib.h>
  
  #include "shell.h"
  #include "xtimer.h"
  #include "timex.h"
  #include "srf02.h"
  
  #ifndef TEST_SRF02_I2C
  #error "TEST_SRF02_I2C not defined"
  #endif
  #ifndef TEST_MODE
  #error "TEST_MODE not defined"
  #endif
  
  #define SAMPLE_PERIOD       (100LU * US_PER_MS) /* 100 ms */
  
  static srf02_t dev;
  
  static void sample(void)
  {
      uint16_t distance = srf02_get_distance(&dev, TEST_MODE);
      printf("distance = %3i cm\n", distance);
  }
  
  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 = atoi(argv[1]);
  
      printf("Initializing SRF02 sensor at I2C_DEV(%i), address is %i\n... ",
             TEST_SRF02_I2C, (int)addr);
      res = srf02_init(&dev, TEST_SRF02_I2C, addr);
      if (res < 0) {
          puts("[Failed]");
          return 1;
      }
      else {
          puts("[Ok]\n");
      }
      return 0;
  }
  
  static int cmd_sample(int argc, char **argv)
  {
      (void)argc;
      (void)argv;
      xtimer_ticks32_t wakeup = xtimer_now();
  
      while(1) {
          sample();
          xtimer_periodic_wakeup(&wakeup, SAMPLE_PERIOD);
      }
  
      return 0;
  }
  
  static int cmd_shoot(int argc, char **argv)
  {
      (void)argc;
      (void)argv;
  
      sample();
      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 = atoi(argv[1]);
      srf02_set_addr(&dev, new_addr);
      printf("Set address to %i\n", (int)new_addr);
      return 0;
  }
  
  static const shell_command_t shell_commands[] = {
      { "init", "initialize a device", cmd_init },
      { "sample", "start sampling", cmd_sample },
      { "shoot", "get a single sample", cmd_shoot },
      { "addr", "reprogram the devices address", cmd_set_addr },
      { NULL, NULL, NULL }
  };
  
  int main(void)
  {
      puts("\nSRF02 Ultrasonic Range Sensor Test\n");
      puts("Use the following flow to test your device/setup. First you need to\n"
           "initialize your device (e.g. 'init 224'). Next you can sample your \n"
           "device continuously ('sample'), or get one value ('shoot').\n\n"
           "This application let's you also reprogram your device's I2C"
           "address:\n"
           " 1. initialize your device -> e.g. 'init 224'\n"
           " 2. specify the new address -> e.g.'addr 228'\n"
           "The device will be programmed with the new address and it is\n"
           "re-initialized with the new address, so you can use it right away\n");
  
      char line_buf[SHELL_DEFAULT_BUFSIZE];
      shell_run(shell_commands, line_buf, SHELL_DEFAULT_BUFSIZE);
  }