AP_Notify.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. #pragma once
  14. #include <AP_Common/AP_Common.h>
  15. #include <AP_Param/AP_Param.h>
  16. #include <GCS_MAVLink/GCS_MAVLink.h>
  17. #include "NotifyDevice.h"
  18. // Device parameters values
  19. #define RGB_LED_OFF 0
  20. #define RGB_LED_LOW 1
  21. #define RGB_LED_MEDIUM 2
  22. #define RGB_LED_HIGH 3
  23. #define BUZZER_ON 1
  24. #define BUZZER_OFF 0
  25. #define NOTIFY_TEXT_BUFFER_SIZE 51
  26. //Type of on-board display
  27. #define DISPLAY_OFF 0
  28. #define DISPLAY_SSD1306 1
  29. #define DISPLAY_SH1106 2
  30. #define DISPLAY_SITL 10
  31. class AP_Notify
  32. {
  33. friend class RGBLed; // RGBLed needs access to notify parameters
  34. friend class Display; // Display needs access to notify parameters
  35. public:
  36. AP_Notify();
  37. /* Do not allow copies */
  38. AP_Notify(const AP_Notify &other) = delete;
  39. AP_Notify &operator=(const AP_Notify&) = delete;
  40. // get singleton instance
  41. static AP_Notify *get_singleton(void) {
  42. return _singleton;
  43. }
  44. // Oreo LED Themes
  45. enum Oreo_LED_Theme {
  46. OreoLED_Disabled = 0, // Disabled the OLED driver entirely
  47. OreoLED_Aircraft = 1, // Standard aviation themed lighting
  48. OreoLED_Automobile = 2, // Automobile themed lighting (white front, red back)
  49. };
  50. enum Notify_LED_Type {
  51. Notify_LED_None = 0, // not enabled
  52. Notify_LED_Board = (1 << 0), // Built in board LED's
  53. Notify_LED_ToshibaLED_I2C_Internal = (1 << 1), // Internal ToshibaLED_I2C
  54. Notify_LED_ToshibaLED_I2C_External = (1 << 2), // External ToshibaLED_I2C
  55. Notify_LED_PCA9685LED_I2C_External = (1 << 3), // External PCA9685_I2C
  56. Notify_LED_OreoLED = (1 << 4), // Oreo
  57. Notify_LED_UAVCAN = (1 << 5), // UAVCAN RGB LED
  58. Notify_LED_NCP5623_I2C_External = (1 << 6), // External NCP5623
  59. Notify_LED_NCP5623_I2C_Internal = (1 << 7), // Internal NCP5623
  60. Notify_LED_NeoPixel = (1 << 8), // NeoPixel 5050 AdaFruit 1655 SK6812 Worldsemi WS2812B
  61. Notify_LED_MAX
  62. };
  63. /// notify_flags_type - bitmask of notification flags
  64. struct notify_flags_and_values_type {
  65. bool initialising; // true if initialising and the vehicle should not be moved
  66. uint8_t gps_status; // see the GPS_0 = no gps, 1 = no lock, 2 = 2d lock, 3 = 3d lock, 4 = dgps lock, 5 = rtk lock
  67. uint8_t gps_num_sats; // number of sats
  68. uint8_t flight_mode; // flight mode
  69. bool armed; // 0 = disarmed, 1 = armed
  70. bool flying; // 0 = not flying, 1 = flying/driving/diving/tracking
  71. bool pre_arm_check; // true if passing pre arm checks
  72. bool pre_arm_gps_check; // true if passing pre arm gps checks
  73. bool save_trim; // true if gathering trim data
  74. bool esc_calibration; // true if calibrating escs
  75. bool failsafe_radio; // true if radio failsafe
  76. bool failsafe_battery; // true if battery failsafe
  77. bool parachute_release; // true if parachute is being released
  78. bool ekf_bad; // true if ekf is reporting problems
  79. bool autopilot_mode; // true if vehicle is in an autopilot flight mode (only used by OreoLEDs)
  80. bool firmware_update; // true just before vehicle firmware is updated
  81. bool compass_cal_running; // true if a compass calibration is running
  82. bool leak_detected; // true if leak detected
  83. bool gps_fusion; // true if the GPS is in use by EKF, usable for flight
  84. bool gps_glitching; // true f the GPS is believed to be glitching is affecting navigation accuracy
  85. bool have_pos_abs; // true if absolute position is available
  86. bool vehicle_lost; // true when lost copter tone is requested (normally only used for copter)
  87. bool waiting_for_throw; // true when copter is in THROW mode and waiting to detect the user hand launch
  88. bool powering_off; // true when the vehicle is powering off
  89. bool video_recording; // true when the vehicle is recording video
  90. };
  91. /// notify_events_type - bitmask of active events.
  92. // Notify library is responsible for setting back to zero after notification has been completed
  93. struct notify_events_type {
  94. uint32_t arming_failed : 1; // 1 if copter failed to arm after user input
  95. uint32_t user_mode_change : 1; // 1 if user has initiated a flight mode change
  96. uint32_t user_mode_change_failed: 1; // 1 when user initiated flight mode change fails
  97. uint32_t failsafe_mode_change : 1; // 1 when failsafe has triggered a flight mode change
  98. uint32_t autotune_complete : 1; // 1 when autotune has successfully completed
  99. uint32_t autotune_failed : 1; // 1 when autotune has failed
  100. uint32_t autotune_next_axis : 1; // 1 when autotune has completed one axis and is moving onto the next
  101. uint32_t mission_complete : 1; // 1 when the mission has completed successfully
  102. uint32_t waypoint_complete : 1; // 1 as vehicle completes a waypoint
  103. uint32_t initiated_compass_cal : 1; // 1 when user input to begin compass cal was accepted
  104. uint32_t compass_cal_saved : 1; // 1 when compass calibration was just saved
  105. uint32_t compass_cal_failed : 1; // 1 when compass calibration has just failed
  106. uint32_t compass_cal_canceled : 1; // 1 when compass calibration was just canceled
  107. uint32_t tune_started : 1; // tuning a parameter has started
  108. uint32_t tune_next : 3; // tuning switched to next parameter
  109. uint32_t tune_save : 1; // tuning saved parameters
  110. uint32_t tune_error : 1; // tuning controller error
  111. };
  112. // The notify flags and values are static to allow direct class access
  113. // without declaring the object.
  114. static struct notify_flags_and_values_type flags;
  115. static struct notify_events_type events;
  116. // initialisation
  117. void init(void);
  118. /// update - allow updates of leds that cannot be updated during a timed interrupt
  119. void update(void);
  120. // handle a LED_CONTROL message
  121. static void handle_led_control(const mavlink_message_t &msg);
  122. // handle a PLAY_TUNE message
  123. static void handle_play_tune(const mavlink_message_t &msg);
  124. // play a tune string
  125. static void play_tune(const char *tune);
  126. bool buzzer_enabled() const { return _buzzer_enable; }
  127. // set flight mode string
  128. void set_flight_mode_str(const char *str);
  129. const char* get_flight_mode_str() const { return _flight_mode_str; }
  130. // send text to display
  131. void send_text(const char *str);
  132. const char* get_text() const { return _send_text; }
  133. uint32_t get_text_updated_millis() const {return _send_text_updated_millis; }
  134. static const struct AP_Param::GroupInfo var_info[];
  135. uint8_t get_buzz_pin() const { return _buzzer_pin; }
  136. uint8_t get_buzz_level() const { return _buzzer_level; }
  137. uint8_t get_buzz_volume() const { return _buzzer_volume; }
  138. #if CONFIG_HAL_BOARD == HAL_BOARD_SITL
  139. HAL_Semaphore sf_window_mutex;
  140. #endif
  141. private:
  142. static AP_Notify *_singleton;
  143. void add_backend_helper(NotifyDevice *backend);
  144. // add all backends
  145. void add_backends(void);
  146. // parameters
  147. AP_Int8 _rgb_led_brightness;
  148. AP_Int8 _rgb_led_override;
  149. AP_Int8 _buzzer_enable;
  150. AP_Int8 _display_type;
  151. AP_Int8 _oreo_theme;
  152. AP_Int8 _buzzer_pin;
  153. AP_Int32 _led_type;
  154. AP_Int8 _buzzer_level;
  155. AP_Int8 _buzzer_volume;
  156. char _send_text[NOTIFY_TEXT_BUFFER_SIZE];
  157. uint32_t _send_text_updated_millis; // last time text changed
  158. char _flight_mode_str[5];
  159. static NotifyDevice* _devices[];
  160. static uint8_t _num_devices;
  161. };
  162. namespace AP {
  163. AP_Notify &notify();
  164. };