APM:Libraries
AP_Notify.cpp
Go to the documentation of this file.
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 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program. If not, see <http://www.gnu.org/licenses/>.
14  */
15 
16 #include "AP_Notify.h"
17 
18 #include "AP_BoardLED.h"
19 #include "PixRacerLED.h"
20 #include "Buzzer.h"
21 #include "Display.h"
22 #include "ExternalLED.h"
23 #include "NavioLED_I2C.h"
24 #include "OreoLED_PX4.h"
25 #include "RCOutputRGBLed.h"
26 #include "ToneAlarm_Linux.h"
27 #include "ToneAlarm_ChibiOS.h"
28 #include "ToneAlarm_PX4.h"
29 #include "ToshibaLED.h"
30 #include "ToshibaLED_I2C.h"
31 #include "VRBoard_LED.h"
32 #include "DiscreteRGBLed.h"
33 #include "DiscoLED.h"
34 #include "Led_Sysfs.h"
35 #include "UAVCAN_RGB_LED.h"
36 #include <stdio.h>
37 #include "AP_BoardLED2.h"
38 
39 extern const AP_HAL::HAL& hal;
40 
42 
43 #define CONFIG_NOTIFY_DEVICES_MAX 6
44 
45 #define TOSHIBA_LED_I2C_BUS_INTERNAL 0
46 #define TOSHIBA_LED_I2C_BUS_EXTERNAL 1
47 
48 // table of user settable parameters
50 
51  // @Param: LED_BRIGHT
52  // @DisplayName: LED Brightness
53  // @Description: Select the RGB LED brightness level. When USB is connected brightness will never be higher than low regardless of the setting.
54  // @Values: 0:Off,1:Low,2:Medium,3:High
55  // @User: Advanced
56  AP_GROUPINFO("LED_BRIGHT", 0, AP_Notify, _rgb_led_brightness, RGB_LED_HIGH),
57 
58  // @Param: BUZZ_ENABLE
59  // @DisplayName: Buzzer enable
60  // @Description: Enable or disable the buzzer. Only for Linux and PX4 based boards.
61  // @Values: 0:Disable,1:Enable
62  // @User: Advanced
63  AP_GROUPINFO("BUZZ_ENABLE", 1, AP_Notify, _buzzer_enable, BUZZER_ON),
64 
65 
66  // @Param: LED_OVERRIDE
67  // @DisplayName: Setup for MAVLink LED override
68  // @Description: This sets up the board RGB LED for override by MAVLink. Normal notify LED control is disabled
69  // @Values: 0:Disable,1:Enable
70  // @User: Advanced
71  AP_GROUPINFO("LED_OVERRIDE", 2, AP_Notify, _rgb_led_override, 0),
72 
73  // @Param: DISPLAY_TYPE
74  // @DisplayName: Type of on-board I2C display
75  // @Description: This sets up the type of on-board I2C display. Disabled by default.
76  // @Values: 0:Disable,1:ssd1306,2:sh1106
77  // @User: Advanced
78  AP_GROUPINFO("DISPLAY_TYPE", 3, AP_Notify, _display_type, 0),
79 
80  // @Param: OREO_THEME
81  // @DisplayName: OreoLED Theme
82  // @Description: Enable/Disable Solo Oreo LED driver, 0 to disable, 1 for Aircraft theme, 2 for Rover theme
83  // @Values: 0:Disabled,1:Aircraft,2:Rover
84  // @User: Advanced
85  AP_GROUPINFO("OREO_THEME", 4, AP_Notify, _oreo_theme, 0),
86 
87 #if !defined(HAL_BUZZER_PIN)
88  // @Param: BUZZ_PIN
89  // @DisplayName: Buzzer pin
90  // @Description: Enables to connect active buzzer to arbitrary pin. Requires 3-pin buzzer or additional MOSFET!
91  // @Values: 0:Disabled
92  // @User: Advanced
93  AP_GROUPINFO("BUZZ_PIN", 5, AP_Notify, _buzzer_pin, 0),
94 #endif
95 
97 };
98 
99 // Default constructor
101 {
103  if (_instance != nullptr) {
104  AP_HAL::panic("AP_Notify must be singleton");
105  }
106  _instance = this;
107 }
108 
109 // static flags, to allow for direct class update from device drivers
112 
115 
116 #define ADD_BACKEND(backend) do { _devices[_num_devices++] = backend; if (_num_devices >= CONFIG_NOTIFY_DEVICES_MAX) return;} while(0)
117 
118 // add notify backends to _devices array
120 {
121  if (_num_devices != 0) {
122  return;
123  }
124 
125 // Notify devices for PX4 boards
126 #if CONFIG_HAL_BOARD == HAL_BOARD_PX4
127  #if CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_PX4_V3 // Has enough memory for Oreo LEDs
128  ADD_BACKEND(new AP_BoardLED());
131  ADD_BACKEND(new ToneAlarm_PX4());
132  ADD_BACKEND(new Display());
133 
134  // Oreo LED enable/disable by NTF_OREO_THEME parameter
135  if (_oreo_theme) {
137  }
138 
139  #elif CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_PX4_V4 // Has its own LED board
140  ADD_BACKEND(new PixRacerLED());
143  ADD_BACKEND(new ToneAlarm_PX4());
144  ADD_BACKEND(new Display());
145 
146  #else // All other px4 boards use standard devices.
147  ADD_BACKEND(new AP_BoardLED());
150  ADD_BACKEND(new ToneAlarm_PX4());
151  ADD_BACKEND(new Display());
152  #endif
153 
154 // Notify devices for ChibiOS boards
155 #elif CONFIG_HAL_BOARD == HAL_BOARD_CHIBIOS
157  ADD_BACKEND(new PixRacerLED());
160  ADD_BACKEND(new Display());
161 
162 // Notify devices for VRBRAIN boards
163 #elif CONFIG_HAL_BOARD == HAL_BOARD_VRBRAIN
164  #if CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_VRBRAIN_V45 // Uses px4 LED board
165  ADD_BACKEND(new AP_BoardLED());
168  ADD_BACKEND(new ToneAlarm_PX4());
169  ADD_BACKEND(new ExternalLED());
170  #else
171  ADD_BACKEND(new VRBoard_LED());
174  ADD_BACKEND(new ToneAlarm_PX4());
175  ADD_BACKEND(new ExternalLED());
176  #endif
177 
178 // Notify devices for linux boards
179 #elif CONFIG_HAL_BOARD == HAL_BOARD_LINUX
180  #if CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_NAVIO
181  ADD_BACKEND(new AP_BoardLED());
182  ADD_BACKEND(new NavioLED_I2C());
185 
186  #elif CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_NAVIO2
187  ADD_BACKEND(new Led_Sysfs("rgb_led0", "rgb_led2", "rgb_led1"));
190 
191  #elif CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_EDGE
192  ADD_BACKEND(new RCOutputRGBLedInverted(12, 13, 14));
195  ADD_BACKEND(new UAVCAN_RGB_LED(0));
196 
197  #elif CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_BBBMINI
198  ADD_BACKEND(new AP_BoardLED());
199  ADD_BACKEND(new Buzzer());
200  ADD_BACKEND(new Display());
201 
202  #elif CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_BLUE
203  ADD_BACKEND(new AP_BoardLED());
204  ADD_BACKEND(new Display());
205 
206  #elif CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_POCKET
207  ADD_BACKEND(new AP_BoardLED());
208  ADD_BACKEND(new Buzzer());
209  ADD_BACKEND(new Display());
210 
211  #elif CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_MINLURE
212  ADD_BACKEND(new RCOutputRGBLedOff(15, 13, 14, 255));
213 
214  #elif CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_ERLEBRAIN2 || \
215  CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_PXFMINI
216  ADD_BACKEND(new AP_BoardLED());
217 
218  #elif CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_BH
219  ADD_BACKEND(new AP_BoardLED());
220  ADD_BACKEND(new RCOutputRGBLed(HAL_RCOUT_RGBLED_RED, HAL_RCOUT_RGBLED_GREEN, HAL_RCOUT_RGBLED_BLUE));
221 
222  #elif CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_DISCO
223  ADD_BACKEND(new DiscoLED());
225 
226  #elif CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_RST_ZYNQ
228 
229  #else // other linux
230  ADD_BACKEND(new AP_BoardLED());
234  #endif
235 
236 #elif CONFIG_HAL_BOARD == HAL_BOARD_CHIBIOS
237 # ifdef HAL_HAVE_PIXRACER_LED
238  ADD_BACKEND(new PixRacerLED());
239 # else
240  ADD_BACKEND(new AP_BoardLED());
241 # endif
242 #ifdef HAL_BUZZER_PIN
243  ADD_BACKEND(new Buzzer());
244 #endif
247  ADD_BACKEND(new Display());
248 #elif CONFIG_HAL_BOARD == HAL_BOARD_F4LIGHT
250  ADD_BACKEND(new Display());
251  ADD_BACKEND(new Buzzer());
252  ADD_BACKEND(new AP_BoardLED2()); // needs AP_BoardLED2 in master
253 #else
254  ADD_BACKEND(new AP_BoardLED());
257  ADD_BACKEND(new Display());
258 #endif
259 }
260 
261 // initialisation
262 void AP_Notify::init(bool enable_external_leds)
263 {
264  // add all the backends
265  add_backends();
266 
267  // clear all flags and events
268  memset(&AP_Notify::flags, 0, sizeof(AP_Notify::flags));
269  memset(&AP_Notify::events, 0, sizeof(AP_Notify::events));
270 
271  // clear flight mode string and text buffer
272  memset(_flight_mode_str, 0, sizeof(_flight_mode_str));
273  memset(_send_text, 0, sizeof(_send_text));
275 
276  AP_Notify::flags.external_leds = enable_external_leds;
277 
278  for (uint8_t i = 0; i < _num_devices; i++) {
279  if (_devices[i] != nullptr) {
280  _devices[i]->pNotify = this;
281  _devices[i]->init();
282  }
283  }
284 }
285 
286 // main update function, called at 50Hz
288 {
289  for (uint8_t i = 0; i < _num_devices; i++) {
290  if (_devices[i] != nullptr) {
291  _devices[i]->update();
292  }
293  }
294 
295  //reset the events
296  memset(&AP_Notify::events, 0, sizeof(AP_Notify::events));
297 }
298 
299 // handle a LED_CONTROL message
300 void AP_Notify::handle_led_control(mavlink_message_t *msg)
301 {
302  for (uint8_t i = 0; i < _num_devices; i++) {
303  if (_devices[i] != nullptr) {
304  _devices[i]->handle_led_control(msg);
305  }
306  }
307 }
308 
309 // handle a PLAY_TUNE message
310 void AP_Notify::handle_play_tune(mavlink_message_t *msg)
311 {
312  for (uint8_t i = 0; i < _num_devices; i++) {
313  if (_devices[i] != nullptr) {
314  _devices[i]->handle_play_tune(msg);
315  }
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 
326 void AP_Notify::send_text(const char *str)
327 {
328  strncpy(_send_text, str, sizeof(_send_text));
329  _send_text[sizeof(_send_text)-1] = 0;
331 }
static void handle_led_control(mavlink_message_t *msg)
Definition: AP_Notify.cpp:300
const AP_HAL::HAL & hal
-*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
Definition: AC_PID_test.cpp:14
static uint8_t _num_devices
Definition: AP_Notify.h:165
AP_Int8 _oreo_theme
Definition: AP_Notify.h:157
static const struct AP_Param::GroupInfo var_info[]
Definition: AP_Notify.h:145
#define AP_GROUPINFO(name, idx, clazz, element, def)
Definition: AP_Param.h:102
char _flight_mode_str[5]
Definition: AP_Notify.h:162
Definition: Buzzer.h:26
virtual void handle_led_control(mavlink_message_t *msg)
Definition: NotifyDevice.h:18
virtual void handle_play_tune(mavlink_message_t *msg)
Definition: NotifyDevice.h:21
notify_flags_type - bitmask of notification flags
Definition: AP_Notify.h:62
#define TOSHIBA_LED_I2C_BUS_INTERNAL
Definition: AP_Notify.cpp:45
#define RGB_LED_HIGH
Definition: AP_Notify.h:27
virtual void update()=0
notify_events_type - bitmask of active events.
Definition: AP_Notify.h:95
void init(bool enable_external_leds)
Definition: AP_Notify.cpp:262
#define TOSHIBA_LED_I2C_BUS_EXTERNAL
Definition: AP_Notify.cpp:46
void add_backends(void)
Definition: AP_Notify.cpp:119
uint32_t millis()
Definition: system.cpp:157
virtual bool init(void)=0
uint32_t _send_text_updated_millis
Definition: AP_Notify.h:161
static void handle_play_tune(mavlink_message_t *msg)
Definition: AP_Notify.cpp:310
const AP_Notify * pNotify
Definition: NotifyDevice.h:24
void set_flight_mode_str(const char *str)
Definition: AP_Notify.cpp:320
static AP_Notify * _instance
Definition: AP_Notify.h:150
static struct notify_flags_and_values_type flags
Definition: AP_Notify.h:117
#define ADD_BACKEND(backend)
Definition: AP_Notify.cpp:116
#define CONFIG_NOTIFY_DEVICES_MAX
Definition: AP_Notify.cpp:43
void update(void)
update - allow updates of leds that cannot be updated during a timed interrupt
Definition: AP_Notify.cpp:287
void send_text(const char *str)
Definition: AP_Notify.cpp:326
char _send_text[NOTIFY_TEXT_BUFFER_SIZE]
Definition: AP_Notify.h:160
void panic(const char *errormsg,...) FMT_PRINTF(1
Definition: system.cpp:140
static struct notify_events_type events
Definition: AP_Notify.h:118
#define BUZZER_ON
Definition: AP_Notify.h:28
friend class Display
Definition: AP_Notify.h:41
#define AP_GROUPEND
Definition: AP_Param.h:121
static void setup_object_defaults(const void *object_pointer, const struct GroupInfo *group_info)
Definition: AP_Param.cpp:1214
static NotifyDevice * _devices[]
Definition: AP_Notify.h:164