PX4 Firmware
PX4 Autopilot Software http://px4.io
led_control.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2017 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 /**
35  * @file led_control.cpp
36  */
37 
38 #include <px4_platform_common/getopt.h>
39 #include <px4_platform_common/module.h>
40 #include <px4_platform_common/log.h>
41 
42 #include <stdlib.h>
43 #include <unistd.h>
44 
45 #include <drivers/drv_hrt.h>
46 #include <drivers/drv_led.h>
47 
50 
51 static void usage();
52 
53 extern "C" {
54  __EXPORT int led_control_main(int argc, char *argv[]);
55 }
56 
58 {
59  led_control.timestamp = hrt_absolute_time();
60 
62  led_control_pub.publish(led_control);
63 }
64 
65 static void run_led_test1()
66 {
67  PX4_INFO("generating LED pattern...");
68 
70  led_control.led_mask = 0xff;
71  led_control.mode = led_control_s::MODE_OFF;
72  led_control.priority = led_control_s::MAX_PRIORITY;
73  publish_led_control(led_control);
74 
75  px4_usleep(200 * 1000);
76 
77  // generate some pattern
78  for (int round = led_control_s::COLOR_RED; round <= led_control_s::COLOR_WHITE; ++round) {
79  for (int led = 0; led < BOARD_MAX_LEDS; ++led) {
80  led_control.led_mask = 1 << led;
81  led_control.mode = led_control_s::MODE_ON;
82  led_control.color = round;
83  publish_led_control(led_control);
84  px4_usleep(80 * 1000);
85  }
86 
87  px4_usleep(100 * 1000);
88  led_control.led_mask = 0xff;
89 
90  for (int i = 0; i < 3; ++i) {
91  led_control.mode = led_control_s::MODE_ON;
92  publish_led_control(led_control);
93  px4_usleep(100 * 1000);
94  led_control.mode = led_control_s::MODE_OFF;
95  publish_led_control(led_control);
96  px4_usleep(100 * 1000);
97  }
98 
99  px4_usleep(200 * 1000);
100  }
101 
102  px4_usleep(500 * 1000);
103 
104  // reset
105  led_control.led_mask = 0xff;
106  led_control.mode = led_control_s::MODE_DISABLED;
107  publish_led_control(led_control);
108 
109  PX4_INFO("Done");
110 }
111 
112 int
113 led_control_main(int argc, char *argv[])
114 {
115  int myoptind = 1;
116  int ch;
117  const char *myoptarg = nullptr;
118  uint8_t blink_speed = led_control_s::MODE_BLINK_NORMAL;
120  led_control.num_blinks = 3;
121  led_control.priority = led_control_s::MAX_PRIORITY;
122  led_control.mode = 0xff;
123  led_control.led_mask = 0xff;
124  led_control.color = led_control_s::COLOR_WHITE;
125 
126  while ((ch = px4_getopt(argc, argv, "c:l:n:s:p:", &myoptind, &myoptarg)) != EOF) {
127  switch (ch) {
128  case 'c':
129  if (!strcmp(myoptarg, "red")) {
130  led_control.color = led_control_s::COLOR_RED;
131 
132  } else if (!strcmp(myoptarg, "blue")) {
133  led_control.color = led_control_s::COLOR_BLUE;
134 
135  } else if (!strcmp(myoptarg, "green")) {
136  led_control.color = led_control_s::COLOR_GREEN;
137 
138  } else if (!strcmp(myoptarg, "yellow")) {
139  led_control.color = led_control_s::COLOR_YELLOW;
140 
141  } else if (!strcmp(myoptarg, "purple")) {
142  led_control.color = led_control_s::COLOR_PURPLE;
143 
144  } else if (!strcmp(myoptarg, "amber")) {
145  led_control.color = led_control_s::COLOR_AMBER;
146 
147  } else if (!strcmp(myoptarg, "cyan")) {
148  led_control.color = led_control_s::COLOR_CYAN;
149 
150  } else if (!strcmp(myoptarg, "white")) {
151  led_control.color = led_control_s::COLOR_WHITE;
152 
153  } else {
154  usage();
155  return 1;
156  }
157 
158  break;
159 
160  case 'l':
161  led_control.led_mask = 1 << strtol(myoptarg, nullptr, 0);
162  break;
163 
164  case 'n':
165  led_control.num_blinks = strtol(myoptarg, nullptr, 0);
166  break;
167 
168  case 's':
169  if (!strcmp(myoptarg, "fast")) {
170  blink_speed = led_control_s::MODE_BLINK_FAST;
171 
172  } else if (!strcmp(myoptarg, "normal")) {
173  blink_speed = led_control_s::MODE_BLINK_NORMAL;
174 
175  } else if (!strcmp(myoptarg, "slow")) {
176  blink_speed = led_control_s::MODE_BLINK_SLOW;
177 
178  } else {
179  usage();
180  return 1;
181  }
182 
183  break;
184 
185  case 'p':
186  led_control.priority = strtol(myoptarg, nullptr, 0);
187  break;
188 
189  default:
190  usage();
191  return -1;
192  break;
193  }
194  }
195 
196  if (led_control.priority > led_control_s::MAX_PRIORITY) {
197  led_control.priority = led_control_s::MAX_PRIORITY;
198  }
199 
200  if (myoptind >= argc) {
201  usage();
202  return 1;
203  }
204 
205  if (!strcmp(argv[myoptind], "test")) {
206  run_led_test1();
207 
208  } else if (!strcmp(argv[myoptind], "on")) {
209  led_control.mode = led_control_s::MODE_ON;
210 
211  } else if (!strcmp(argv[myoptind], "off")) {
212  led_control.mode = led_control_s::MODE_OFF;
213 
214  } else if (!strcmp(argv[myoptind], "reset")) {
215  led_control.mode = led_control_s::MODE_DISABLED;
216 
217  } else if (!strcmp(argv[myoptind], "blink")) {
218  led_control.mode = blink_speed;
219 
220  } else if (!strcmp(argv[myoptind], "breathe")) {
221  led_control.mode = led_control_s::MODE_BREATHE;
222 
223  } else if (!strcmp(argv[myoptind], "flash")) {
224  led_control.mode = led_control_s::MODE_FLASH;
225 
226  } else {
227  usage();
228  return 1;
229  }
230 
231  if (led_control.mode != 0xff) {
232  publish_led_control(led_control);
233  }
234 
235  return 0;
236 }
237 
238 static void
240 {
241  PRINT_MODULE_DESCRIPTION(
242  R"DESCR_STR(
243 ### Description
244 Command-line tool to control & test the (external) LED's.
245 
246 To use it make sure there's a driver running, which handles the led_control uorb topic.
247 
248 There are different priorities, such that for example one module can set a color with low priority, and another
249 module can blink N times with high priority, and the LED's automatically return to the lower priority state
250 after the blinking. The `reset` command can also be used to return to a lower priority.
251 
252 ### Examples
253 Blink the first LED 5 times in blue:
254 $ led_control blink -c blue -l 0 -n 5
255 
256 )DESCR_STR");
257 
258  PRINT_MODULE_USAGE_NAME("led_control", "command");
259 
260  PRINT_MODULE_USAGE_COMMAND_DESCR("test", "Run a test pattern");
261  PRINT_MODULE_USAGE_COMMAND_DESCR("on", "Turn LED on");
262  PRINT_MODULE_USAGE_COMMAND_DESCR("off", "Turn LED off");
263  PRINT_MODULE_USAGE_COMMAND_DESCR("reset", "Reset LED priority");
264  PRINT_MODULE_USAGE_COMMAND_DESCR("blink", "Blink LED N times");
265  PRINT_MODULE_USAGE_PARAM_INT('n', 3, 1, 20, "Number of blinks", true);
266  PRINT_MODULE_USAGE_PARAM_STRING('s', "normal", "fast|normal|slow", "Set blinking speed", true);
267  PRINT_MODULE_USAGE_COMMAND_DESCR("breathe", "Continuously fade LED in & out");
268  PRINT_MODULE_USAGE_COMMAND_DESCR("flash", "Two fast blinks and then off with frequency of 1Hz");
269 
270  PRINT_MODULE_USAGE_PARAM_COMMENT("The following arguments apply to all of the above commands except for 'test':");
271  PRINT_MODULE_USAGE_PARAM_STRING('c', "white", "red|blue|green|yellow|purple|amber|cyan|white", "color", true);
272  PRINT_MODULE_USAGE_PARAM_INT('l', -1, 0, 100, "Which LED to control: 0, 1, 2, ... (default=all)", true);
273  PRINT_MODULE_USAGE_PARAM_INT('p', 2, 0, 2, "Priority", true);
274 }
__EXPORT int led_control_main(int argc, char *argv[])
uint8_t priority
Definition: led_control.h:77
static led_control_s led_control
Definition: I2C.hpp:51
static orb_advert_t led_control_pub
static void publish_led_control(led_control_s &led_control)
Definition: led_control.cpp:57
uint8_t led_mask
Definition: led_control.h:73
uint8_t color
Definition: led_control.h:74
High-resolution timer with callouts and timekeeping.
#define ORB_ID(_name)
Generates a pointer to the uORB metadata structure for a given topic.
Definition: uORB.h:87
uint8_t mode
Definition: led_control.h:75
Led device API to control the external LED(s) via uORB interface.
uint64_t timestamp
Definition: led_control.h:72
#define BOARD_MAX_LEDS
Definition: drv_led.h:49
uint8_t num_blinks
Definition: led_control.h:76
static void usage()
__EXPORT hrt_abstime hrt_absolute_time(void)
Get absolute time in [us] (does not wrap).
static void run_led_test1()
Definition: led_control.cpp:65