ExternalLED.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. This program is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. */
  13. #include "ExternalLED.h"
  14. #include "AP_Notify.h"
  15. #include <AP_HAL/AP_HAL.h>
  16. #if (defined(EXTERNAL_LED_ARMED) && defined(EXTERNAL_LED_GPS) && \
  17. defined(EXTERNAL_LED_MOTOR1) && defined(EXTERNAL_LED_MOTOR2))
  18. extern const AP_HAL::HAL& hal;
  19. bool ExternalLED::init(void)
  20. {
  21. // setup the main LEDs as outputs
  22. hal.gpio->pinMode(EXTERNAL_LED_ARMED, HAL_GPIO_OUTPUT);
  23. hal.gpio->pinMode(EXTERNAL_LED_GPS, HAL_GPIO_OUTPUT);
  24. hal.gpio->pinMode(EXTERNAL_LED_MOTOR1, HAL_GPIO_OUTPUT);
  25. hal.gpio->pinMode(EXTERNAL_LED_MOTOR2, HAL_GPIO_OUTPUT);
  26. // turn leds off
  27. hal.gpio->write(EXTERNAL_LED_ARMED, HAL_GPIO_LED_OFF);
  28. hal.gpio->write(EXTERNAL_LED_GPS, HAL_GPIO_LED_OFF);
  29. hal.gpio->write(EXTERNAL_LED_MOTOR1, HAL_GPIO_LED_OFF);
  30. hal.gpio->write(EXTERNAL_LED_MOTOR2, HAL_GPIO_LED_OFF);
  31. return true;
  32. }
  33. /*
  34. main update function called at 50Hz
  35. */
  36. void ExternalLED::update(void)
  37. {
  38. // reduce update rate from 50hz to 10hz
  39. _counter++;
  40. if (_counter < 5) {
  41. return;
  42. }
  43. _counter = 0;
  44. // internal counter used to control step of armed and gps led
  45. _counter2++;
  46. if (_counter2 >= 10) {
  47. _counter2 = 0;
  48. }
  49. // initialising
  50. if (AP_Notify::flags.initialising) {
  51. // blink arming and gps leds at 5hz
  52. switch(_counter2) {
  53. case 0:
  54. case 2:
  55. case 4:
  56. case 6:
  57. case 8:
  58. armed_led(true);
  59. gps_led(false);
  60. break;
  61. case 1:
  62. case 3:
  63. case 5:
  64. case 7:
  65. case 9:
  66. armed_led(false);
  67. gps_led(true);
  68. break;
  69. }
  70. return;
  71. }
  72. // arming led control
  73. if (AP_Notify::flags.armed) {
  74. armed_led(true);
  75. }else{
  76. // blink arming led at 2hz
  77. switch(_counter2) {
  78. case 0:
  79. case 1:
  80. case 2:
  81. case 5:
  82. case 6:
  83. case 7:
  84. armed_led(false);
  85. break;
  86. case 3:
  87. case 4:
  88. case 8:
  89. case 9:
  90. armed_led(true);
  91. break;
  92. }
  93. }
  94. // GPS led control
  95. switch (AP_Notify::flags.gps_status) {
  96. case 0:
  97. // no GPS attached
  98. gps_led(false);
  99. break;
  100. case 1:
  101. case 2:
  102. // GPS attached but no lock, blink at 4Hz
  103. switch(_counter2) { // Pattern: 3(off), 2(on), 3(off), 2(on), repeat
  104. case 0:
  105. case 1:
  106. case 2:
  107. case 5:
  108. case 6:
  109. case 7:
  110. gps_led(false);
  111. break;
  112. case 3:
  113. case 4:
  114. case 8:
  115. case 9:
  116. gps_led(true);
  117. break;
  118. }
  119. break;
  120. case 3:
  121. // solid blue on gps lock
  122. gps_led(true);
  123. break;
  124. }
  125. // motor led control
  126. // if we are displaying a pattern complete it
  127. if (_pattern != NONE) {
  128. _pattern_counter++;
  129. switch(_pattern) {
  130. case NONE:
  131. // do nothing
  132. break;
  133. case FAST_FLASH:
  134. switch(_pattern_counter) {
  135. case 1:
  136. case 3:
  137. case 5:
  138. case 7:
  139. case 9:
  140. motor_led1(true);
  141. motor_led2(true);
  142. break;
  143. case 2:
  144. case 4:
  145. case 6:
  146. case 8:
  147. motor_led1(false);
  148. motor_led2(false);
  149. break;
  150. case 10:
  151. motor_led1(false);
  152. motor_led2(false);
  153. set_pattern(NONE);
  154. break;
  155. }
  156. break;
  157. case OSCILLATE:
  158. switch(_pattern_counter) {
  159. case 1:
  160. motor_led1(true);
  161. motor_led2(false);
  162. break;
  163. case 4:
  164. motor_led1(false);
  165. motor_led2(true);
  166. break;
  167. case 6:
  168. set_pattern(NONE);
  169. break;
  170. }
  171. break;
  172. }
  173. }else{
  174. if (AP_Notify::flags.failsafe_battery || AP_Notify::flags.failsafe_radio) {
  175. // radio or battery failsafe indicated by fast flashing
  176. set_pattern(FAST_FLASH);
  177. } else {
  178. // otherwise do whatever the armed led is doing
  179. motor_led1(_flags.armedled_on);
  180. motor_led2(_flags.armedled_on);
  181. }
  182. }
  183. }
  184. // set_pattern - sets pattern for motor leds
  185. void ExternalLED::set_pattern(LEDPattern pattern_id)
  186. {
  187. _pattern = pattern_id;
  188. _pattern_counter = 0;
  189. }
  190. // armed_led - set armed light on or off
  191. void ExternalLED::armed_led(bool on_off)
  192. {
  193. if (_flags.armedled_on != on_off) {
  194. _flags.armedled_on = on_off;
  195. hal.gpio->write(EXTERNAL_LED_ARMED, _flags.armedled_on);
  196. }
  197. }
  198. // gps_led - set gps light on or off
  199. void ExternalLED::gps_led(bool on_off)
  200. {
  201. if (_flags.gpsled_on != on_off) {
  202. _flags.gpsled_on = on_off;
  203. hal.gpio->write(EXTERNAL_LED_GPS, _flags.gpsled_on);
  204. }
  205. }
  206. // motor_led - set motor light on or off
  207. void ExternalLED::motor_led1(bool on_off)
  208. {
  209. if (_flags.motorled1_on != on_off) {
  210. _flags.motorled1_on = on_off;
  211. hal.gpio->write(EXTERNAL_LED_MOTOR1, _flags.motorled1_on);
  212. }
  213. }
  214. // motor_led - set motor light on or off
  215. void ExternalLED::motor_led2(bool on_off)
  216. {
  217. if (_flags.motorled2_on != on_off) {
  218. _flags.motorled2_on = on_off;
  219. hal.gpio->write(EXTERNAL_LED_MOTOR2, _flags.motorled2_on);
  220. }
  221. }
  222. #else
  223. bool ExternalLED::init(void) {return true;}
  224. void ExternalLED::update(void) {return;}
  225. #endif