APM:Libraries
OreoLED_PX4.h
Go to the documentation of this file.
1 /*
2  OreoLED I2C driver
3 
4  This program is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
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 
19 #if CONFIG_HAL_BOARD == HAL_BOARD_PX4
20 
21 #include <AP_HAL/AP_HAL.h>
22 #include "NotifyDevice.h"
23 #include <drivers/drv_oreoled.h>
24 
25 #define OREOLED_NUM_LEDS 4 // maximum number of individual LEDs connected to the oreo led cpu
26 #define OREOLED_INSTANCE_ALL 0xff // instance number to indicate all LEDs (used for set_rgb and set_macro)
27 #define OREOLED_BRIGHT 0xff // maximum brightness when flying (disconnected from usb)
28 
29 #define CUSTOM_HEADER_LENGTH 4 // number of bytes in the custom LED buffer that are used to identify the command
30 
31 class OreoLED_PX4 : public NotifyDevice
32 {
33 public:
34  // constuctor
35  OreoLED_PX4(uint8_t theme);
36 
37  // init - initialised the device
38  bool init(void);
39 
40  // update - updates device according to timed_updated. Should be
41  // called at 50Hz
42  void update();
43 
44  // healthy - return true if at least one LED is responding
45  bool healthy() const { return _overall_health; }
46 
47  // handle a LED_CONTROL message, by default device ignore message
48  void handle_led_control(mavlink_message_t *msg);
49 
50 private:
51  // update_timer - called by scheduler and updates PX4 driver with commands
52  void update_timer(void);
53 
54  // set_rgb - set color as a combination of red, green and blue values for one or all LEDs, pattern defaults to solid color
55  void set_rgb(uint8_t instance, uint8_t red, uint8_t green, uint8_t blue);
56 
57  // set_rgb - set color as a combination of red, green and blue values for one or all LEDs, using the specified pattern
58  void set_rgb(uint8_t instance, enum oreoled_pattern pattern, uint8_t red, uint8_t green, uint8_t blue);
59 
60  // 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
61  void set_rgb(uint8_t instance, oreoled_pattern pattern, uint8_t red, uint8_t green, uint8_t blue,
62  uint8_t amplitude_red, uint8_t amplitude_green, uint8_t amplitude_blue,
63  uint16_t period, uint16_t phase_offset);
64 
65  // set_macro - set macro for one or all LEDs
66  void set_macro(uint8_t instance, enum oreoled_macro macro);
67 
68  // send_sync - force a syncronisation of the all LED's
69  void send_sync();
70 
71  // functions to set LEDs to specific patterns. These functions return true if no further updates should be made to LEDs this iteration
72  bool slow_counter(void);
73  void sync_counter(void);
74  bool mode_firmware_update(void);
75  bool mode_init(void);
76  bool mode_failsafe_radio(void);
77  bool set_standard_colors(void);
78  bool mode_failsafe_batt(void);
79  bool mode_auto_flight(void);
80  bool mode_pilot_flight(void);
81 
82  // Clear the desired state
83  void clear_state(void);
84 
85  // oreo led modes (pattern, macro or rgb)
86  enum oreoled_mode {
92  };
93 
94  // Oreo LED modes
99  };
100 
101  // oreo_state structure holds possible state of an led
102  struct oreo_state {
104  enum oreoled_pattern pattern;
105  enum oreoled_macro macro;
106  uint8_t red;
107  uint8_t green;
108  uint8_t blue;
109  uint8_t amplitude_red;
111  uint8_t amplitude_blue;
112  uint16_t period;
113  int8_t repeat;
114  uint16_t phase_offset;
115 
116  oreo_state();
117 
118  void clear_state();
119 
120  void send_sync();
121 
122  void set_macro(oreoled_macro new_macro);
123 
124  void set_rgb(enum oreoled_pattern new_pattern, uint8_t new_red, uint8_t new_green, uint8_t new_blue);
125 
126  void set_rgb(enum oreoled_pattern new_pattern, uint8_t new_red, uint8_t new_green,
127  uint8_t new_blue, uint8_t new_amplitude_red, uint8_t new_amplitude_green, uint8_t new_amplitude_blue,
128  uint16_t new_period, uint16_t new_phase_offset);
129 
130  bool operator==(const oreo_state &os);
131  };
132 
133  // private members
134  bool _overall_health; // overall health
135  int _oreoled_fd; // file descriptor
136  bool _send_required; // true when we need to send an update to at least one led
137  volatile bool _state_desired_semaphore; // true when we are updating the state desired values to ensure they are not sent prematurely
139  oreo_state _state_sent[OREOLED_NUM_LEDS]; // last state sent to led
140  uint8_t _pattern_override; // holds last processed pattern override, 0 if we are not overriding a pattern
141  uint8_t _oreo_theme; // theme (1=AirCraft, 2=Ground Vehicle)
142  uint8_t _rear_color_r = 255; // the rear LED red value
143  uint8_t _rear_color_g = 255; // the rear LED green value
144  uint8_t _rear_color_b = 255; // the rear LED blue value
145 };
146 
147 #endif // CONFIG_HAL_BOARD == HAL_BOARD_PX4
bool init(void)
Definition: OreoLED_PX4.cpp:65
void set_rgb(uint8_t instance, uint8_t red, uint8_t green, uint8_t blue)
enum oreoled_macro macro
Definition: OreoLED_PX4.h:105
void set_macro(oreoled_macro new_macro)
uint8_t _oreo_theme
Definition: OreoLED_PX4.h:141
void clear_state(void)
bool _send_required
Definition: OreoLED_PX4.h:136
void sync_counter(void)
void send_sync()
oreo_state _state_sent[OREOLED_NUM_LEDS]
Definition: OreoLED_PX4.h:139
bool operator==(const oreo_state &os)
void set_rgb(enum oreoled_pattern new_pattern, uint8_t new_red, uint8_t new_green, uint8_t new_blue)
OreoLED_PX4(uint8_t theme)
Definition: OreoLED_PX4.cpp:46
bool mode_failsafe_radio(void)
bool mode_init(void)
volatile bool _state_desired_semaphore
Definition: OreoLED_PX4.h:137
bool mode_failsafe_batt(void)
uint8_t _pattern_override
Definition: OreoLED_PX4.h:140
void update_timer(void)
enum oreoled_mode mode
Definition: OreoLED_PX4.h:103
uint8_t _rear_color_g
Definition: OreoLED_PX4.h:143
bool _overall_health
Definition: OreoLED_PX4.h:134
void set_macro(uint8_t instance, enum oreoled_macro macro)
bool healthy() const
Definition: OreoLED_PX4.h:45
void handle_led_control(mavlink_message_t *msg)
#define OREOLED_NUM_LEDS
Definition: OreoLED_PX4.h:25
bool mode_firmware_update(void)
uint8_t _rear_color_r
Definition: OreoLED_PX4.h:142
bool mode_auto_flight(void)
bool mode_pilot_flight(void)
uint8_t _rear_color_b
Definition: OreoLED_PX4.h:144
enum oreoled_pattern pattern
Definition: OreoLED_PX4.h:104
oreo_state _state_desired[OREOLED_NUM_LEDS]
Definition: OreoLED_PX4.h:138
bool slow_counter(void)
void update()
Definition: OreoLED_PX4.cpp:95
bool set_standard_colors(void)