AP_Notify.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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 "AP_Notify.h"
  14. #include "AP_BoardLED.h"
  15. #include "PixRacerLED.h"
  16. #include "Buzzer.h"
  17. #include "Display.h"
  18. #include "ExternalLED.h"
  19. #include "PCA9685LED_I2C.h"
  20. #include "NeoPixel.h"
  21. #include "NCP5623.h"
  22. #include "OreoLED_I2C.h"
  23. #include "RCOutputRGBLed.h"
  24. #include "ToneAlarm.h"
  25. #include "ToshibaLED_I2C.h"
  26. #include "VRBoard_LED.h"
  27. #include "DiscreteRGBLed.h"
  28. #include "DiscoLED.h"
  29. #include "Led_Sysfs.h"
  30. #include "UAVCAN_RGB_LED.h"
  31. #include "SITL_SFML_LED.h"
  32. #include <stdio.h>
  33. #include "AP_BoardLED2.h"
  34. extern const AP_HAL::HAL& hal;
  35. AP_Notify *AP_Notify::_singleton;
  36. #define CONFIG_NOTIFY_DEVICES_MAX 6
  37. #define TOSHIBA_LED_I2C_BUS_INTERNAL 0
  38. #define TOSHIBA_LED_I2C_BUS_EXTERNAL 1
  39. // all I2C_LEDS
  40. #define I2C_LEDS (Notify_LED_ToshibaLED_I2C_Internal | Notify_LED_ToshibaLED_I2C_External | \
  41. Notify_LED_NCP5623_I2C_Internal | Notify_LED_NCP5623_I2C_External)
  42. #ifndef BUILD_DEFAULT_LED_TYPE
  43. #if CONFIG_HAL_BOARD == HAL_BOARD_CHIBIOS
  44. #define BUILD_DEFAULT_LED_TYPE (Notify_LED_Board | I2C_LEDS)
  45. // Linux boards
  46. #elif CONFIG_HAL_BOARD == HAL_BOARD_LINUX
  47. #if CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_NAVIO
  48. #define BUILD_DEFAULT_LED_TYPE (Notify_LED_Board | I2C_LEDS |\
  49. Notify_LED_PCA9685LED_I2C_External)
  50. #elif CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_NAVIO2
  51. #define BUILD_DEFAULT_LED_TYPE (Notify_LED_Board | I2C_LEDS)
  52. #elif CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_EDGE
  53. #define BUILD_DEFAULT_LED_TYPE (Notify_LED_Board | I2C_LEDS |\
  54. Notify_LED_UAVCAN)
  55. #elif CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_BBBMINI || \
  56. CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_BLUE || \
  57. CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_POCKET || \
  58. CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_ERLEBRAIN2 || \
  59. CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_PXFMINI || \
  60. CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_BH || \
  61. CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_DISCO
  62. #define BUILD_DEFAULT_LED_TYPE (Notify_LED_Board)
  63. #elif CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_RST_ZYNQ
  64. #define BUILD_DEFAULT_LED_TYPE (Notify_LED_ToshibaLED_I2C_External)
  65. #else // other linux
  66. #define BUILD_DEFAULT_LED_TYPE (Notify_LED_Board | I2C_LEDS)
  67. #endif
  68. // All other builds
  69. #else
  70. #define BUILD_DEFAULT_LED_TYPE (Notify_LED_Board | I2C_LEDS)
  71. #endif // board selection
  72. #endif // BUILD_DEFAULT_LED_TYPE
  73. #ifndef BUZZER_ENABLE_DEFAULT
  74. #define BUZZER_ENABLE_DEFAULT 1
  75. #endif
  76. // table of user settable parameters
  77. const AP_Param::GroupInfo AP_Notify::var_info[] = {
  78. // @Param: LED_BRIGHT
  79. // @DisplayName: LED Brightness
  80. // @Description: Select the RGB LED brightness level. When USB is connected brightness will never be higher than low regardless of the setting.
  81. // @Values: 0:Off,1:Low,2:Medium,3:High
  82. // @User: Advanced
  83. AP_GROUPINFO("LED_BRIGHT", 0, AP_Notify, _rgb_led_brightness, RGB_LED_HIGH),
  84. // @Param: BUZZ_ENABLE
  85. // @DisplayName: Buzzer enable
  86. // @Description: Enable or disable the buzzer.
  87. // @Values: 0:Disable,1:Enable
  88. // @User: Advanced
  89. AP_GROUPINFO("BUZZ_ENABLE", 1, AP_Notify, _buzzer_enable, BUZZER_ENABLE_DEFAULT),
  90. // @Param: LED_OVERRIDE
  91. // @DisplayName: Specifies colour source for the RGBLed
  92. // @Description: Specifies the source for the colours and brightness for the LED. OutbackChallenge conforms to the MedicalExpress (https://uavchallenge.org/medical-express/) rules, essentially "Green" is disarmed (safe-to-approach), "Red" is armed (not safe-to-approach). Traffic light is a simplified color set, red when armed, yellow when the safety switch is not surpressing outputs (but disarmed), and green when outputs are surpressed and disarmed, the LED will blink faster if disarmed and failing arming checks.
  93. // @Values: 0:Standard,1:MAVLink,2:OutbackChallenge,3:TrafficLight
  94. // @User: Advanced
  95. AP_GROUPINFO("LED_OVERRIDE", 2, AP_Notify, _rgb_led_override, 0),
  96. // @Param: DISPLAY_TYPE
  97. // @DisplayName: Type of on-board I2C display
  98. // @Description: This sets up the type of on-board I2C display. Disabled by default.
  99. // @Values: 0:Disable,1:ssd1306,2:sh1106,10:SITL
  100. // @User: Advanced
  101. AP_GROUPINFO("DISPLAY_TYPE", 3, AP_Notify, _display_type, 0),
  102. #if !HAL_MINIMIZE_FEATURES
  103. // @Param: OREO_THEME
  104. // @DisplayName: OreoLED Theme
  105. // @Description: Enable/Disable Solo Oreo LED driver, 0 to disable, 1 for Aircraft theme, 2 for Rover theme
  106. // @Values: 0:Disabled,1:Aircraft,2:Rover
  107. // @User: Advanced
  108. AP_GROUPINFO("OREO_THEME", 4, AP_Notify, _oreo_theme, 0),
  109. #endif
  110. #if !defined(HAL_BUZZER_PIN)
  111. // @Param: BUZZ_PIN
  112. // @DisplayName: Buzzer pin
  113. // @Description: Enables to connect active buzzer to arbitrary pin. Requires 3-pin buzzer or additional MOSFET!
  114. // @Values: 0:Disabled
  115. // @User: Advanced
  116. AP_GROUPINFO("BUZZ_PIN", 5, AP_Notify, _buzzer_pin, 0),
  117. #endif
  118. // @Param: LED_TYPES
  119. // @DisplayName: LED Driver Types
  120. // @Description: Controls what types of LEDs will be enabled
  121. // @Bitmask: 0:Build in LED, 1:Internal ToshibaLED, 2:External ToshibaLED, 3:External PCA9685, 4:Oreo LED, 5:UAVCAN, 6:NCP5623 External, 7:NCP5623 Internal, 8:NeoPixel
  122. // @User: Advanced
  123. AP_GROUPINFO("LED_TYPES", 6, AP_Notify, _led_type, BUILD_DEFAULT_LED_TYPE),
  124. #if !defined(HAL_BUZZER_PIN)
  125. // @Param: BUZZ_ON_LVL
  126. // @DisplayName: Buzzer-on pin logic level
  127. // @Description: Specifies pin level that indicates buzzer should play
  128. // @Values: 0:LowIsOn,1:HighIsOn
  129. // @User: Advanced
  130. AP_GROUPINFO("BUZZ_ON_LVL", 7, AP_Notify, _buzzer_level, 1),
  131. #endif
  132. // @Param: BUZZ_VOLUME
  133. // @DisplayName: Buzzer volume
  134. // @Description: Enable or disable the buzzer.
  135. // @Range: 0 100
  136. // @Units: %
  137. AP_GROUPINFO("BUZZ_VOLUME", 8, AP_Notify, _buzzer_volume, 100),
  138. AP_GROUPEND
  139. };
  140. // Default constructor
  141. AP_Notify::AP_Notify()
  142. {
  143. AP_Param::setup_object_defaults(this, var_info);
  144. if (_singleton != nullptr) {
  145. AP_HAL::panic("AP_Notify must be singleton");
  146. }
  147. _singleton = this;
  148. }
  149. // static flags, to allow for direct class update from device drivers
  150. struct AP_Notify::notify_flags_and_values_type AP_Notify::flags;
  151. struct AP_Notify::notify_events_type AP_Notify::events;
  152. NotifyDevice *AP_Notify::_devices[CONFIG_NOTIFY_DEVICES_MAX];
  153. uint8_t AP_Notify::_num_devices;
  154. void AP_Notify::add_backend_helper(NotifyDevice *backend)
  155. {
  156. _devices[_num_devices] = backend;
  157. _devices[_num_devices]->pNotify = this;
  158. if(!_devices[_num_devices]->init()) {
  159. delete _devices[_num_devices];
  160. _devices[_num_devices] = nullptr;
  161. } else {
  162. _num_devices++;
  163. }
  164. }
  165. #define ADD_BACKEND(backend) do { add_backend_helper(backend); if (_num_devices >= CONFIG_NOTIFY_DEVICES_MAX) return;} while(0)
  166. // add notify backends to _devices array
  167. void AP_Notify::add_backends(void)
  168. {
  169. if (_num_devices != 0) {
  170. return;
  171. }
  172. for (uint32_t i = 1; i < Notify_LED_MAX; i = i << 1) {
  173. switch(_led_type & i) {
  174. case Notify_LED_None:
  175. break;
  176. case Notify_LED_Board:
  177. // select the most appropriate built in LED driver type
  178. #if CONFIG_HAL_BOARD == HAL_BOARD_LINUX
  179. #if CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_NAVIO2
  180. ADD_BACKEND(new Led_Sysfs("rgb_led0", "rgb_led2", "rgb_led1"));
  181. #elif CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_EDGE
  182. ADD_BACKEND(new RCOutputRGBLedInverted(12, 13, 14));
  183. #elif CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_BH
  184. ADD_BACKEND(new RCOutputRGBLed(HAL_RCOUT_RGBLED_RED, HAL_RCOUT_RGBLED_GREEN, HAL_RCOUT_RGBLED_BLUE));
  185. #elif CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_DISCO
  186. ADD_BACKEND(new DiscoLED());
  187. #elif CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_NAVIGATOR
  188. ADD_BACKEND(new DiscreteRGBLed(HAL_RGBLED_RED, HAL_RGBLED_GREEN, HAL_RGBLED_BLUE, HAL_RGBLED_NORMAL_POLARITY));
  189. #endif
  190. #endif // CONFIG_HAL_BOARD == HAL_BOARD_LINUX
  191. #if CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_CHIBIOS_VRBRAIN_V51 || CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_CHIBIOS_VRBRAIN_V52 || CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_CHIBIOS_VRUBRAIN_V51 || CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_CHIBIOS_VRCORE_V10 || CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_CHIBIOS_VRBRAIN_V54
  192. ADD_BACKEND(new ExternalLED()); // despite the name this is a built in set of onboard LED's
  193. #endif // CONFIG_HAL_BOARD_SUBTYPE == various CHIBIOS-VRBRAINs
  194. #if defined(HAL_HAVE_PIXRACER_LED)
  195. ADD_BACKEND(new PixRacerLED());
  196. #elif (defined(HAL_GPIO_A_LED_PIN) && defined(HAL_GPIO_B_LED_PIN) && defined(HAL_GPIO_C_LED_PIN))
  197. #if CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_CHIBIOS_VRBRAIN_V51 || CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_CHIBIOS_VRBRAIN_V52 || CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_CHIBIOS_VRUBRAIN_V51 || CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_CHIBIOS_VRCORE_V10 || CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_CHIBIOS_VRBRAIN_V54
  198. ADD_BACKEND(new VRBoard_LED());
  199. #else
  200. ADD_BACKEND(new AP_BoardLED());
  201. #endif
  202. #elif (defined(HAL_GPIO_A_LED_PIN) && defined(HAL_GPIO_B_LED_PIN))
  203. ADD_BACKEND(new AP_BoardLED2());
  204. #endif
  205. break;
  206. case Notify_LED_ToshibaLED_I2C_Internal:
  207. ADD_BACKEND(new ToshibaLED_I2C(TOSHIBA_LED_I2C_BUS_INTERNAL));
  208. break;
  209. case Notify_LED_ToshibaLED_I2C_External:
  210. ADD_BACKEND(new ToshibaLED_I2C(TOSHIBA_LED_I2C_BUS_EXTERNAL));
  211. break;
  212. #if !HAL_MINIMIZE_FEATURES
  213. case Notify_LED_NCP5623_I2C_External:
  214. FOREACH_I2C_EXTERNAL(b) {
  215. ADD_BACKEND(new NCP5623(b));
  216. }
  217. break;
  218. case Notify_LED_NCP5623_I2C_Internal:
  219. ADD_BACKEND(new NCP5623(TOSHIBA_LED_I2C_BUS_INTERNAL));
  220. break;
  221. #endif
  222. case Notify_LED_PCA9685LED_I2C_External:
  223. ADD_BACKEND(new PCA9685LED_I2C());
  224. break;
  225. case Notify_LED_NeoPixel:
  226. ADD_BACKEND(new NeoPixel());
  227. break;
  228. case Notify_LED_OreoLED:
  229. #if !HAL_MINIMIZE_FEATURES
  230. if (_oreo_theme) {
  231. ADD_BACKEND(new OreoLED_I2C(0, _oreo_theme));
  232. }
  233. #endif
  234. break;
  235. case Notify_LED_UAVCAN:
  236. #if HAL_WITH_UAVCAN
  237. ADD_BACKEND(new UAVCAN_RGB_LED(0));
  238. #endif // HAL_WITH_UAVCAN
  239. break;
  240. }
  241. }
  242. // Always try and add a display backend
  243. ADD_BACKEND(new Display());
  244. // ChibiOS noise makers
  245. #if CONFIG_HAL_BOARD == HAL_BOARD_CHIBIOS
  246. ADD_BACKEND(new Buzzer());
  247. #ifdef HAL_PWM_ALARM
  248. ADD_BACKEND(new AP_ToneAlarm());
  249. #endif
  250. // Linux noise makers
  251. #elif CONFIG_HAL_BOARD == HAL_BOARD_LINUX
  252. #if CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_NAVIO || \
  253. CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_NAVIO2 || \
  254. CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_EDGE || \
  255. CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_ERLEBRAIN2 || \
  256. CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_PXFMINI || \
  257. CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_RST_ZYNQ || \
  258. CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_BLUE
  259. // No noise makers, keep this though to ensure that the final else is safe
  260. #elif CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_BBBMINI || \
  261. CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_POCKET
  262. ADD_BACKEND(new Buzzer());
  263. #else // other linux
  264. ADD_BACKEND(new AP_ToneAlarm());
  265. #endif
  266. #elif CONFIG_HAL_BOARD == HAL_BOARD_SITL
  267. ADD_BACKEND(new AP_ToneAlarm());
  268. #ifdef WITH_SITL_RGBLED
  269. ADD_BACKEND(new SITL_SFML_LED());
  270. #endif
  271. #endif // Noise makers
  272. }
  273. // initialisation
  274. void AP_Notify::init(void)
  275. {
  276. // clear all flags and events
  277. memset(&AP_Notify::flags, 0, sizeof(AP_Notify::flags));
  278. memset(&AP_Notify::events, 0, sizeof(AP_Notify::events));
  279. // add all the backends
  280. add_backends();
  281. }
  282. // main update function, called at 50Hz
  283. void AP_Notify::update(void)
  284. {
  285. for (uint8_t i = 0; i < _num_devices; i++) {
  286. if (_devices[i] != nullptr) {
  287. _devices[i]->update();
  288. }
  289. }
  290. //reset the events
  291. memset(&AP_Notify::events, 0, sizeof(AP_Notify::events));
  292. }
  293. // handle a LED_CONTROL message
  294. void AP_Notify::handle_led_control(const mavlink_message_t &msg)
  295. {
  296. for (uint8_t i = 0; i < _num_devices; i++) {
  297. if (_devices[i] != nullptr) {
  298. _devices[i]->handle_led_control(msg);
  299. }
  300. }
  301. }
  302. // handle a PLAY_TUNE message
  303. void AP_Notify::handle_play_tune(const mavlink_message_t &msg)
  304. {
  305. for (uint8_t i = 0; i < _num_devices; i++) {
  306. if (_devices[i] != nullptr) {
  307. _devices[i]->handle_play_tune(msg);
  308. }
  309. }
  310. }
  311. void AP_Notify::play_tune(const char *tune)
  312. {
  313. for (uint8_t i = 0; i < _num_devices; i++) {
  314. if (_devices[i] != nullptr) {
  315. _devices[i]->play_tune(tune);
  316. }
  317. }
  318. }
  319. // set flight mode string
  320. void AP_Notify::set_flight_mode_str(const char *str)
  321. {
  322. strncpy(_flight_mode_str, str, 4);
  323. _flight_mode_str[sizeof(_flight_mode_str)-1] = 0;
  324. }
  325. void AP_Notify::send_text(const char *str)
  326. {
  327. strncpy(_send_text, str, sizeof(_send_text));
  328. _send_text[sizeof(_send_text)-1] = 0;
  329. _send_text_updated_millis = AP_HAL::millis();
  330. }
  331. namespace AP {
  332. AP_Notify &notify()
  333. {
  334. return *AP_Notify::get_singleton();
  335. }
  336. };