RGBLed.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * AP_Notify Library.
  3. * based upon a prototype library by David "Buzz" Bussenschutt.
  4. */
  5. /*
  6. This program is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #pragma once
  18. #include <AP_HAL/AP_HAL.h>
  19. #include "NotifyDevice.h"
  20. class RGBLed: public NotifyDevice {
  21. public:
  22. RGBLed(uint8_t led_off, uint8_t led_bright, uint8_t led_medium, uint8_t led_dim);
  23. // init - initialised the LED
  24. virtual bool init(void) override;
  25. // set_rgb - set color as a combination of red, green and blue levels from 0 ~ 15
  26. virtual void set_rgb(uint8_t red, uint8_t green, uint8_t blue);
  27. // update - updates led according to timed_updated. Should be
  28. // called at 50Hz
  29. virtual void update() override;
  30. // handle LED control, only used when LED_OVERRIDE=1
  31. virtual void handle_led_control(const mavlink_message_t &msg) override;
  32. protected:
  33. // methods implemented in hardware specific classes
  34. virtual bool hw_init(void) = 0;
  35. virtual bool hw_set_rgb(uint8_t red, uint8_t green, uint8_t blue) = 0;
  36. // set_rgb - set color as a combination of red, green and blue levels from 0 ~ 15
  37. virtual void _set_rgb(uint8_t red, uint8_t green, uint8_t blue);
  38. void update_override();
  39. // meta-data common to all hw devices
  40. uint8_t _red_des, _green_des, _blue_des; // color requested by timed update
  41. uint8_t _red_curr, _green_curr, _blue_curr; // current colours displayed by the led
  42. uint8_t _led_off;
  43. uint8_t _led_bright;
  44. uint8_t _led_medium;
  45. uint8_t _led_dim;
  46. struct {
  47. uint8_t r, g, b;
  48. uint8_t rate_hz;
  49. uint32_t start_ms;
  50. } _led_override;
  51. private:
  52. void update_colours();
  53. uint32_t get_colour_sequence() const;
  54. uint32_t get_colour_sequence_obc() const;
  55. uint32_t get_colour_sequence_traffic_light() const;
  56. uint8_t get_brightness(void) const;
  57. #define DEFINE_COLOUR_SEQUENCE(S0, S1, S2, S3, S4, S5, S6, S7, S8, S9) \
  58. ((S0) << (0*3) | (S1) << (1*3) | (S2) << (2*3) | (S3) << (3*3) | (S4) << (4*3) | (S5) << (5*3) | (S6) << (6*3) | (S7) << (7*3) | (S8) << (8*3) | (S9) << (9*3))
  59. #define DEFINE_COLOUR_SEQUENCE_SLOW(colour) \
  60. DEFINE_COLOUR_SEQUENCE(colour,colour,colour,colour,colour,OFF,OFF,OFF,OFF,OFF)
  61. #define DEFINE_COLOUR_SEQUENCE_FAILSAFE(colour) \
  62. DEFINE_COLOUR_SEQUENCE(YELLOW,YELLOW,YELLOW,YELLOW,YELLOW,colour,colour,colour,colour,colour)
  63. #define DEFINE_COLOUR_SEQUENCE_SOLID(colour) \
  64. DEFINE_COLOUR_SEQUENCE(colour,colour,colour,colour,colour,colour,colour,colour,colour,colour)
  65. #define DEFINE_COLOUR_SEQUENCE_ALTERNATE(colour1, colour2) \
  66. DEFINE_COLOUR_SEQUENCE(colour1,colour2,colour1,colour2,colour1,colour2,colour1,colour2,colour1,colour2)
  67. #define OFF 0
  68. #define BLUE 1
  69. #define GREEN 2
  70. #define RED 4
  71. #define YELLOW (RED|GREEN)
  72. #define WHITE (RED|GREEN|BLUE)
  73. const uint32_t sequence_initialising = DEFINE_COLOUR_SEQUENCE_ALTERNATE(RED,BLUE);
  74. const uint32_t sequence_trim_or_esc = DEFINE_COLOUR_SEQUENCE(RED,BLUE,GREEN,RED,BLUE,GREEN,RED,BLUE,GREEN,OFF);
  75. const uint32_t sequence_failsafe_leak = DEFINE_COLOUR_SEQUENCE_FAILSAFE(WHITE);
  76. const uint32_t sequence_failsafe_ekf = DEFINE_COLOUR_SEQUENCE_FAILSAFE(RED);
  77. const uint32_t sequence_failsafe_gps_glitching = DEFINE_COLOUR_SEQUENCE_FAILSAFE(BLUE);
  78. const uint32_t sequence_failsafe_radio_or_battery = DEFINE_COLOUR_SEQUENCE_FAILSAFE(OFF);
  79. const uint32_t sequence_armed = DEFINE_COLOUR_SEQUENCE_SOLID(GREEN);
  80. const uint32_t sequence_armed_nogps = DEFINE_COLOUR_SEQUENCE_SOLID(BLUE);
  81. const uint32_t sequence_prearm_failing = DEFINE_COLOUR_SEQUENCE(YELLOW,YELLOW,OFF,OFF,YELLOW,YELLOW,OFF,OFF,OFF,OFF);
  82. const uint32_t sequence_disarmed_good_dgps = DEFINE_COLOUR_SEQUENCE_ALTERNATE(GREEN,OFF);
  83. const uint32_t sequence_disarmed_good_gps = DEFINE_COLOUR_SEQUENCE_SLOW(GREEN);
  84. const uint32_t sequence_disarmed_bad_gps = DEFINE_COLOUR_SEQUENCE_SLOW(BLUE);
  85. uint8_t last_step;
  86. enum rgb_source_t {
  87. standard = 0,
  88. mavlink = 1,
  89. obc = 2,
  90. traffic_light = 3,
  91. };
  92. rgb_source_t rgb_source() const;
  93. };