RGBLed.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. Generic RGBLed driver
  3. */
  4. /*
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. */
  14. #include <AP_HAL/AP_HAL.h>
  15. #include <AP_GPS/AP_GPS.h>
  16. #include "RGBLed.h"
  17. #include "AP_Notify.h"
  18. extern const AP_HAL::HAL& hal;
  19. RGBLed::RGBLed(uint8_t led_off, uint8_t led_bright, uint8_t led_medium, uint8_t led_dim):
  20. _led_off(led_off),
  21. _led_bright(led_bright),
  22. _led_medium(led_medium),
  23. _led_dim(led_dim)
  24. {
  25. }
  26. bool RGBLed::init()
  27. {
  28. return hw_init();
  29. }
  30. // set_rgb - set color as a combination of red, green and blue values
  31. void RGBLed::_set_rgb(uint8_t red, uint8_t green, uint8_t blue)
  32. {
  33. if (red != _red_curr ||
  34. green != _green_curr ||
  35. blue != _blue_curr) {
  36. // call the hardware update routine
  37. if (hw_set_rgb(red, green, blue)) {
  38. _red_curr = red;
  39. _green_curr = green;
  40. _blue_curr = blue;
  41. }
  42. }
  43. }
  44. RGBLed::rgb_source_t RGBLed::rgb_source() const
  45. {
  46. return rgb_source_t(pNotify->_rgb_led_override.get());
  47. }
  48. // set_rgb - set color as a combination of red, green and blue values
  49. void RGBLed::set_rgb(uint8_t red, uint8_t green, uint8_t blue)
  50. {
  51. if (rgb_source() == mavlink) {
  52. // don't set if in override mode
  53. return;
  54. }
  55. _set_rgb(red, green, blue);
  56. }
  57. uint8_t RGBLed::get_brightness(void) const
  58. {
  59. uint8_t brightness = _led_bright;
  60. switch (pNotify->_rgb_led_brightness) {
  61. case RGB_LED_OFF:
  62. brightness = _led_off;
  63. break;
  64. case RGB_LED_LOW:
  65. brightness = _led_dim;
  66. break;
  67. case RGB_LED_MEDIUM:
  68. brightness = _led_medium;
  69. break;
  70. case RGB_LED_HIGH:
  71. brightness = _led_bright;
  72. break;
  73. }
  74. // use dim light when connected through USB
  75. if (hal.gpio->usb_connected() && brightness > _led_dim) {
  76. brightness = _led_dim;
  77. }
  78. return brightness;
  79. }
  80. uint32_t RGBLed::get_colour_sequence_obc(void) const
  81. {
  82. if (AP_Notify::flags.armed) {
  83. return DEFINE_COLOUR_SEQUENCE_SOLID(RED);
  84. }
  85. return DEFINE_COLOUR_SEQUENCE_SOLID(GREEN);
  86. }
  87. // _scheduled_update - updates _red, _green, _blue according to notify flags
  88. uint32_t RGBLed::get_colour_sequence(void) const
  89. {
  90. // initialising pattern
  91. if (AP_Notify::flags.initialising) {
  92. return sequence_initialising;
  93. }
  94. // save trim and esc calibration pattern
  95. if (AP_Notify::flags.save_trim || AP_Notify::flags.esc_calibration) {
  96. return sequence_trim_or_esc;
  97. }
  98. // radio and battery failsafe patter: flash yellow
  99. // gps failsafe pattern : flashing yellow and blue
  100. // ekf_bad pattern : flashing yellow and red
  101. if (AP_Notify::flags.failsafe_radio ||
  102. AP_Notify::flags.failsafe_battery ||
  103. AP_Notify::flags.ekf_bad ||
  104. AP_Notify::flags.gps_glitching ||
  105. AP_Notify::flags.leak_detected) {
  106. if (AP_Notify::flags.leak_detected) {
  107. // purple if leak detected
  108. return sequence_failsafe_leak;
  109. } else if (AP_Notify::flags.ekf_bad) {
  110. // red on if ekf bad
  111. return sequence_failsafe_ekf;
  112. } else if (AP_Notify::flags.gps_glitching) {
  113. // blue on gps glitch
  114. return sequence_failsafe_gps_glitching;
  115. }
  116. // all off for radio or battery failsafe
  117. return sequence_failsafe_radio_or_battery;
  118. }
  119. // solid green or blue if armed
  120. if (AP_Notify::flags.armed) {
  121. // solid green if armed with GPS 3d lock
  122. if (AP_Notify::flags.gps_status >= AP_GPS::GPS_OK_FIX_3D) {
  123. return sequence_armed;
  124. }
  125. // solid blue if armed with no GPS lock
  126. return sequence_armed_nogps;
  127. }
  128. // double flash yellow if failing pre-arm checks
  129. if (!AP_Notify::flags.pre_arm_check) {
  130. return sequence_prearm_failing;
  131. }
  132. if (AP_Notify::flags.gps_status >= AP_GPS::GPS_OK_FIX_3D_DGPS && AP_Notify::flags.pre_arm_gps_check) {
  133. return sequence_disarmed_good_dgps;
  134. }
  135. if (AP_Notify::flags.gps_status >= AP_GPS::GPS_OK_FIX_3D && AP_Notify::flags.pre_arm_gps_check) {
  136. return sequence_disarmed_good_gps;
  137. }
  138. return sequence_disarmed_bad_gps;
  139. }
  140. uint32_t RGBLed::get_colour_sequence_traffic_light(void) const
  141. {
  142. if (AP_Notify::flags.initialising) {
  143. return DEFINE_COLOUR_SEQUENCE(RED,GREEN,BLUE,RED,GREEN,BLUE,RED,GREEN,BLUE,OFF);
  144. }
  145. if (AP_Notify::flags.armed) {
  146. return DEFINE_COLOUR_SEQUENCE_SLOW(RED);
  147. }
  148. if (hal.util->safety_switch_state() != AP_HAL::Util::SAFETY_DISARMED) {
  149. if (!AP_Notify::flags.pre_arm_check) {
  150. return DEFINE_COLOUR_SEQUENCE_ALTERNATE(YELLOW, OFF);
  151. } else {
  152. return DEFINE_COLOUR_SEQUENCE_SLOW(YELLOW);
  153. }
  154. }
  155. if (!AP_Notify::flags.pre_arm_check) {
  156. return DEFINE_COLOUR_SEQUENCE_ALTERNATE(GREEN, OFF);
  157. }
  158. return DEFINE_COLOUR_SEQUENCE_SLOW(GREEN);
  159. }
  160. // update - updates led according to timed_updated. Should be called
  161. // at 50Hz
  162. void RGBLed::update()
  163. {
  164. uint32_t current_colour_sequence = 0;
  165. switch (rgb_source()) {
  166. case mavlink:
  167. update_override();
  168. return; // note this is a return not a break!
  169. case standard:
  170. current_colour_sequence = get_colour_sequence();
  171. break;
  172. case obc:
  173. current_colour_sequence = get_colour_sequence_obc();
  174. break;
  175. case traffic_light:
  176. current_colour_sequence = get_colour_sequence_traffic_light();
  177. break;
  178. }
  179. const uint8_t brightness = get_brightness();
  180. uint8_t step = (AP_HAL::millis()/100) % 10;
  181. // ensure we can't skip a step even with awful timing
  182. if (step != last_step) {
  183. step = (last_step+1) % 10;
  184. last_step = step;
  185. }
  186. const uint8_t colour = (current_colour_sequence >> (step*3)) & 7;
  187. _red_des = (colour & RED) ? brightness : 0;
  188. _green_des = (colour & GREEN) ? brightness : 0;
  189. _blue_des = (colour & BLUE) ? brightness : 0;
  190. set_rgb(_red_des, _green_des, _blue_des);
  191. }
  192. /*
  193. handle LED control, only used when LED_OVERRIDE=1
  194. */
  195. void RGBLed::handle_led_control(const mavlink_message_t &msg)
  196. {
  197. if (rgb_source() != mavlink) {
  198. // ignore LED_CONTROL commands if not in LED_OVERRIDE mode
  199. return;
  200. }
  201. // decode mavlink message
  202. mavlink_led_control_t packet;
  203. mavlink_msg_led_control_decode(&msg, &packet);
  204. _led_override.start_ms = AP_HAL::millis();
  205. switch (packet.custom_len) {
  206. case 3:
  207. _led_override.rate_hz = 0;
  208. _led_override.r = packet.custom_bytes[0];
  209. _led_override.g = packet.custom_bytes[1];
  210. _led_override.b = packet.custom_bytes[2];
  211. break;
  212. case 4:
  213. _led_override.rate_hz = packet.custom_bytes[3];
  214. _led_override.r = packet.custom_bytes[0];
  215. _led_override.g = packet.custom_bytes[1];
  216. _led_override.b = packet.custom_bytes[2];
  217. break;
  218. default:
  219. // not understood
  220. break;
  221. }
  222. }
  223. /*
  224. update LED when in override mode
  225. */
  226. void RGBLed::update_override(void)
  227. {
  228. if (_led_override.rate_hz == 0) {
  229. // solid colour
  230. _set_rgb(_led_override.r, _led_override.g, _led_override.b);
  231. return;
  232. }
  233. // blinking
  234. uint32_t ms_per_cycle = 1000 / _led_override.rate_hz;
  235. uint32_t cycle = (AP_HAL::millis() - _led_override.start_ms) % ms_per_cycle;
  236. if (cycle > ms_per_cycle / 2) {
  237. // on
  238. _set_rgb(_led_override.r, _led_override.g, _led_override.b);
  239. } else {
  240. _set_rgb(0, 0, 0);
  241. }
  242. }