PX4 Firmware
PX4 Autopilot Software http://px4.io
rgbled_pwm.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2015, 2016 Airmind 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 Airmind 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 rgbled_pwm.cpp
36  *
37  * Driver for the onboard RGB LED controller by PWM.
38  * this driver is based the PX4 led driver
39  *
40  */
41 
42 #include <string.h>
43 
44 #include <lib/led/led.h>
45 #include <px4_platform_common/getopt.h>
46 #include <px4_platform_common/px4_work_queue/ScheduledWorkItem.hpp>
47 
48 class RGBLED_PWM : public px4::ScheduledWorkItem
49 {
50 public:
51  RGBLED_PWM();
52  virtual ~RGBLED_PWM();
53 
54  int init();
55  int status();
56 
57 private:
58 
59  uint8_t _r{0};
60  uint8_t _g{0};
61  uint8_t _b{0};
62 
63  volatile bool _running{false};
64  volatile bool _should_run{true};
65 
67 
68  void Run() override;
69 
70  int send_led_rgb();
71  int get(bool &on, bool &powersave, uint8_t &r, uint8_t &g, uint8_t &b);
72 };
73 
74 extern int led_pwm_servo_set(unsigned channel, uint8_t value);
75 extern unsigned led_pwm_servo_get(unsigned channel);
76 extern int led_pwm_servo_init(void);
77 extern void led_pwm_servo_deinit(void);
78 
79 
80 /* for now, we only support one RGBLED */
81 namespace
82 {
83 RGBLED_PWM *g_rgbled = nullptr;
84 }
85 
87  ScheduledWorkItem(MODULE_NAME, px4::wq_configurations::lp_default)
88 {
89 }
90 
92 {
93  _should_run = false;
94  int counter = 0;
95 
96  while (_running && ++counter < 10) {
97  px4_usleep(100000);
98  }
99 }
100 
101 int
103 {
104  /* switch off LED on start */
106  send_led_rgb();
107 
108  _running = true;
109 
110  // kick off work queue
111  ScheduleNow();
112 
113  return OK;
114 }
115 
116 int
118 {
119  bool on, powersave;
120  uint8_t r, g, b;
121 
122  int ret = get(on, powersave, r, g, b);
123 
124  if (ret == OK) {
125  /* we don't care about power-save mode */
126  PX4_INFO("state: %s", on ? "ON" : "OFF");
127  PX4_INFO("red: %u, green: %u, blue: %u", (unsigned)r, (unsigned)g, (unsigned)b);
128 
129  } else {
130  PX4_WARN("failed to read led");
131  }
132 
133  return ret;
134 }
135 
136 /**
137  * Main loop function
138  */
139 void
141 {
142  if (!_should_run) {
143  _running = false;
144  return;
145  }
146 
147  LedControlData led_control_data;
148 
149  if (_led_controller.update(led_control_data) == 1) {
150  uint8_t brightness = led_control_data.leds[0].brightness;
151 
152  switch (led_control_data.leds[0].color) {
153  case led_control_s::COLOR_RED:
154  _r = brightness; _g = 0; _b = 0;
155  break;
156 
157  case led_control_s::COLOR_GREEN:
158  _r = 0; _g = brightness; _b = 0;
159  break;
160 
161  case led_control_s::COLOR_BLUE:
162  _r = 0; _g = 0; _b = brightness;
163  break;
164 
165  case led_control_s::COLOR_AMBER: //make it the same as yellow
166  case led_control_s::COLOR_YELLOW:
167  _r = brightness; _g = brightness; _b = 0;
168  break;
169 
170  case led_control_s::COLOR_PURPLE:
171  _r = brightness; _g = 0; _b = brightness;
172  break;
173 
174  case led_control_s::COLOR_CYAN:
175  _r = 0; _g = brightness; _b = brightness;
176  break;
177 
178  case led_control_s::COLOR_WHITE:
179  _r = brightness; _g = brightness; _b = brightness;
180  break;
181 
182  default: // led_control_s::COLOR_OFF
183  _r = 0; _g = 0; _b = 0;
184  break;
185  }
186 
187  send_led_rgb();
188  }
189 
190  /* re-queue ourselves to run again later */
191  ScheduleDelayed(_led_controller.maximum_update_interval());
192 }
193 
194 /**
195  * Send RGB PWM settings to LED driver according to current color and brightness
196  */
197 int
199 {
200 #if defined(BOARD_HAS_LED_PWM)
201  led_pwm_servo_set(0, _r);
202  led_pwm_servo_set(1, _g);
203  led_pwm_servo_set(2, _b);
204 #endif
205 
206 #if defined(BOARD_HAS_UI_LED_PWM)
207  led_pwm_servo_set(3, _r);
208  led_pwm_servo_set(4, _g);
209  led_pwm_servo_set(5, _b);
210 #endif
211 
212  return (OK);
213 }
214 
215 int
216 RGBLED_PWM::get(bool &on, bool &powersave, uint8_t &r, uint8_t &g, uint8_t &b)
217 {
218  powersave = OK;
219  on = _r > 0 || _g > 0 || _b > 0;
220  r = _r;
221  g = _g;
222  b = _b;
223  return OK;
224 }
225 
226 static void
228 {
229  PX4_INFO("missing command: try 'start', 'status', 'stop'");
230 }
231 
232 extern "C" __EXPORT int
233 rgbled_pwm_main(int argc, char *argv[])
234 {
235  int myoptind = 1;
236  int ch;
237  const char *myoptarg = nullptr;
238 
239  /* jump over start/off/etc and look at options first */
240  while ((ch = px4_getopt(argc, argv, "a:b:", &myoptind, &myoptarg)) != EOF) {
241  switch (ch) {
242  case 'a':
243  break;
244 
245  case 'b':
246  break;
247 
248  default:
249  rgbled_usage();
250  return 1;
251  }
252  }
253 
254  if (myoptind >= argc) {
255  rgbled_usage();
256  return 1;
257  }
258 
259  const char *verb = argv[myoptind];
260 
261  if (!strcmp(verb, "start")) {
262  if (g_rgbled != nullptr) {
263  PX4_WARN("already started");
264  return 1;
265  }
266 
267  if (g_rgbled == nullptr) {
268  g_rgbled = new RGBLED_PWM();
269 
270  if (g_rgbled == nullptr) {
271  PX4_WARN("alloc failed");
272  return 1;
273  }
274 
275  if (OK != g_rgbled->init()) {
276  delete g_rgbled;
277  g_rgbled = nullptr;
278  PX4_ERR("init failed");
279  return 1;
280  }
281  }
282 
283  return 0;
284  }
285 
286  /* need the driver past this point */
287  if (g_rgbled == nullptr) {
288  PX4_WARN("not started");
289  rgbled_usage();
290  return 1;
291  }
292 
293  if (!strcmp(verb, "status")) {
294  g_rgbled->status();
295  return 0;
296  }
297 
298  if (!strcmp(verb, "stop")) {
299  delete g_rgbled;
300  g_rgbled = nullptr;
301  return 0;
302  }
303 
304  rgbled_usage();
305  return 1;
306 }
int maximum_update_interval() const
get maxium time between two consecutive calls to update() in us.
Definition: led.h:72
LedControlDataSingle leds[BOARD_MAX_LEDS]
Definition: led.h:55
Definition: I2C.hpp:51
static void rgbled_usage()
Definition: rgbled_pwm.cpp:227
void Run() override
Main loop function.
Definition: rgbled_pwm.cpp:140
volatile bool _running
Definition: rgbled_pwm.cpp:63
uint8_t _b
Definition: rgbled_pwm.cpp:61
uint8_t brightness
brightness in [0, 255]
Definition: led.h:52
uint8_t color
one of led_control_s::COLOR_*
Definition: led.h:51
int get(bool &on, bool &powersave, uint8_t &r, uint8_t &g, uint8_t &b)
Definition: rgbled_pwm.cpp:216
int status()
Definition: rgbled_pwm.cpp:117
LedController _led_controller
Definition: rgbled_pwm.cpp:66
uint8_t _r
Definition: rgbled_pwm.cpp:59
static unsigned counter
Definition: safety.c:56
uint8_t _g
Definition: rgbled_pwm.cpp:60
int led_pwm_servo_init(void)
unsigned led_pwm_servo_get(unsigned channel)
int update(LedControlData &control_data)
Update and retrieve the Led state.
Definition: led.cpp:41
Definition: bst.cpp:62
Led controller helper class, used by Led drivers.
void led_pwm_servo_deinit(void)
#define OK
Definition: uavcan_main.cpp:71
virtual ~RGBLED_PWM()
Definition: rgbled_pwm.cpp:91
int led_pwm_servo_set(unsigned channel, uint8_t value)
__EXPORT int rgbled_pwm_main(int argc, char *argv[])
Definition: rgbled_pwm.cpp:233
int send_led_rgb()
Send RGB PWM settings to LED driver according to current color and brightness.
Definition: rgbled_pwm.cpp:198
class LedController Handles the led_control topic: blinking, priorities and state updates...
Definition: led.h:63
volatile bool _should_run
Definition: rgbled_pwm.cpp:64