OreoLED_I2C.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. OreoLED I2C driver
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  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. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #pragma once
  15. #include <AP_HAL/AP_HAL.h>
  16. #include "NotifyDevice.h"
  17. #define OREOLED_NUM_LEDS 4 // maximum number of individual LEDs connected to the oreo led cpu
  18. #define OREOLED_INSTANCE_ALL 0xff // instance number to indicate all LEDs (used for set_rgb and set_macro)
  19. #define OREOLED_BRIGHT 0xff // maximum brightness when flying (disconnected from usb)
  20. #define CUSTOM_HEADER_LENGTH 4 // number of bytes in the custom LED buffer that are used to identify the command
  21. class OreoLED_I2C : public NotifyDevice {
  22. public:
  23. // constuctor
  24. OreoLED_I2C(uint8_t bus, uint8_t theme);
  25. // init - initialised the device
  26. bool init(void) override;
  27. // update - updates device according to timed_updated. Should be
  28. // called at 50Hz
  29. void update() override;
  30. // handle a LED_CONTROL message, by default device ignore message
  31. void handle_led_control(const mavlink_message_t &msg) override;
  32. private:
  33. enum oreoled_pattern {
  34. OREOLED_PATTERN_OFF = 0,
  35. OREOLED_PATTERN_SINE = 1,
  36. OREOLED_PATTERN_SOLID = 2,
  37. OREOLED_PATTERN_SIREN = 3,
  38. OREOLED_PATTERN_STROBE = 4,
  39. OREOLED_PATTERN_FADEIN = 5,
  40. OREOLED_PATTERN_FADEOUT = 6,
  41. OREOLED_PATTERN_PARAMUPDATE = 7,
  42. OREOLED_PATTERN_ENUM_COUNT
  43. };
  44. /* enum of available macros defined by hardware */
  45. enum oreoled_macro {
  46. OREOLED_PARAM_MACRO_RESET = 0,
  47. OREOLED_PARAM_MACRO_COLOUR_CYCLE = 1,
  48. OREOLED_PARAM_MACRO_BREATH = 2,
  49. OREOLED_PARAM_MACRO_STROBE = 3,
  50. OREOLED_PARAM_MACRO_FADEIN = 4,
  51. OREOLED_PARAM_MACRO_FADEOUT = 5,
  52. OREOLED_PARAM_MACRO_RED = 6,
  53. OREOLED_PARAM_MACRO_GREEN = 7,
  54. OREOLED_PARAM_MACRO_BLUE = 8,
  55. OREOLED_PARAM_MACRO_YELLOW = 9,
  56. OREOLED_PARAM_MACRO_WHITE = 10,
  57. OREOLED_PARAM_MACRO_AUTOMOBILE = 11,
  58. OREOLED_PARAM_MACRO_AVIATION = 12,
  59. OREOLED_PARAM_MACRO_ENUM_COUNT
  60. };
  61. /* enum passed to OREOLED_SET_MODE defined by hardware */
  62. enum oreoled_param {
  63. OREOLED_PARAM_BIAS_RED = 0,
  64. OREOLED_PARAM_BIAS_GREEN = 1,
  65. OREOLED_PARAM_BIAS_BLUE = 2,
  66. OREOLED_PARAM_AMPLITUDE_RED = 3,
  67. OREOLED_PARAM_AMPLITUDE_GREEN = 4,
  68. OREOLED_PARAM_AMPLITUDE_BLUE = 5,
  69. OREOLED_PARAM_PERIOD = 6,
  70. OREOLED_PARAM_REPEAT = 7,
  71. OREOLED_PARAM_PHASEOFFSET = 8,
  72. OREOLED_PARAM_MACRO = 9,
  73. OREOLED_PARAM_RESET = 10,
  74. OREOLED_PARAM_APP_CHECKSUM = 11,
  75. OREOLED_PARAM_ENUM_COUNT
  76. };
  77. // update_timer - called by scheduler and updates driver with commands
  78. void update_timer(void);
  79. // set_rgb - set color as a combination of red, green and blue values for one or all LEDs, pattern defaults to solid color
  80. void set_rgb(uint8_t instance, uint8_t red, uint8_t green, uint8_t blue);
  81. // set_rgb - set color as a combination of red, green and blue values for one or all LEDs, using the specified pattern
  82. void set_rgb(uint8_t instance, enum oreoled_pattern pattern, uint8_t red, uint8_t green, uint8_t blue);
  83. // set_rgb - set color as a combination of red, green and blue values for one or all LEDs, using the specified pattern and other parameters
  84. void set_rgb(uint8_t instance, oreoled_pattern pattern, uint8_t red, uint8_t green, uint8_t blue,
  85. uint8_t amplitude_red, uint8_t amplitude_green, uint8_t amplitude_blue,
  86. uint16_t period, uint16_t phase_offset);
  87. // set_macro - set macro for one or all LEDs
  88. void set_macro(uint8_t instance, enum oreoled_macro macro);
  89. // send_sync - force a syncronisation of the all LED's
  90. void send_sync();
  91. // functions to set LEDs to specific patterns. These functions return true if no further updates should be made to LEDs this iteration
  92. bool slow_counter(void);
  93. bool mode_firmware_update(void);
  94. bool mode_init(void);
  95. bool mode_failsafe_radio(void);
  96. bool set_standard_colors(void);
  97. bool mode_failsafe_batt(void);
  98. bool mode_auto_flight(void);
  99. bool mode_pilot_flight(void);
  100. // Clear the desired state
  101. void clear_state(void);
  102. // oreo led modes (pattern, macro or rgb)
  103. enum oreoled_mode {
  104. OREOLED_MODE_NONE=0,
  105. OREOLED_MODE_MACRO,
  106. OREOLED_MODE_RGB,
  107. OREOLED_MODE_RGB_EXTENDED,
  108. };
  109. // Oreo LED modes
  110. enum Oreo_LED_Theme {
  111. OreoLED_Disabled = 0,
  112. OreoLED_Aircraft = 1,
  113. OreoLED_Automobile = 2,
  114. };
  115. // oreo_state structure holds possible state of an led
  116. struct oreo_state {
  117. enum oreoled_mode mode;
  118. enum oreoled_pattern pattern;
  119. enum oreoled_macro macro;
  120. uint8_t red;
  121. uint8_t green;
  122. uint8_t blue;
  123. uint8_t amplitude_red;
  124. uint8_t amplitude_green;
  125. uint8_t amplitude_blue;
  126. uint16_t period;
  127. int8_t repeat;
  128. uint16_t phase_offset;
  129. oreo_state();
  130. void clear_state();
  131. void set_macro(oreoled_macro new_macro);
  132. void set_rgb(enum oreoled_pattern new_pattern, uint8_t new_red, uint8_t new_green, uint8_t new_blue);
  133. void set_rgb(enum oreoled_pattern new_pattern, uint8_t new_red, uint8_t new_green,
  134. uint8_t new_blue, uint8_t new_amplitude_red, uint8_t new_amplitude_green, uint8_t new_amplitude_blue,
  135. uint16_t new_period, uint16_t new_phase_offset);
  136. bool operator==(const oreo_state &os);
  137. };
  138. typedef struct {
  139. uint8_t led_num;
  140. uint8_t num_bytes;
  141. uint8_t buff[32];
  142. } oreoled_cmd_t;
  143. // send a I2C command
  144. bool command_send(oreoled_cmd_t &cmd);
  145. void boot_leds(void);
  146. // private members
  147. uint8_t _bus;
  148. HAL_Semaphore_Recursive _sem;
  149. AP_HAL::OwnPtr<AP_HAL::I2CDevice> _dev;
  150. bool _send_required; // true when we need to send an update to at least one led
  151. oreo_state _state_desired[OREOLED_NUM_LEDS]; // desired state
  152. oreo_state _state_sent[OREOLED_NUM_LEDS]; // last state sent to led
  153. uint8_t _pattern_override; // holds last processed pattern override, 0 if we are not overriding a pattern
  154. uint8_t _oreo_theme; // theme (1=AirCraft, 2=Ground Vehicle)
  155. uint8_t _rear_color_r = 255; // the rear LED red value
  156. uint8_t _rear_color_g = 255; // the rear LED green value
  157. uint8_t _rear_color_b = 255; // the rear LED blue value
  158. uint8_t _slow_count;
  159. uint8_t _boot_count;
  160. uint32_t _last_boot_ms;
  161. uint32_t _last_sync_ms;
  162. };