Merge pull request #11198 from SteveCEvans/sce_rc2
[betaflight.git] / src / main / io / ledstrip.h
blobe84b198dda0f3c92d619c3b0f33604497825b5ff
1 /*
2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
8 * any later version.
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
21 #pragma once
23 #include "common/color.h"
24 #include "common/time.h"
26 #include "drivers/io_types.h"
27 #include "drivers/light_ws2811strip.h"
29 #include "pg/pg.h"
31 #define LED_MAX_STRIP_LENGTH 32
32 #define LED_CONFIGURABLE_COLOR_COUNT 16
33 #define LED_MODE_COUNT 6
34 #define LED_DIRECTION_COUNT 6
35 #define LED_BASEFUNCTION_COUNT 7
36 #define LED_OVERLAY_COUNT 6
37 #define LED_SPECIAL_COLOR_COUNT 11
39 #define LED_POS_OFFSET 0
40 #define LED_FUNCTION_OFFSET 8
41 #define LED_OVERLAY_OFFSET 12
42 #define LED_COLOR_OFFSET 18
43 #define LED_DIRECTION_OFFSET 22
44 #define LED_PARAMS_OFFSET 28
46 #define LED_POS_BITCNT 8
47 #define LED_FUNCTION_BITCNT 4
48 #define LED_OVERLAY_BITCNT 6
49 #define LED_COLOR_BITCNT 4
50 #define LED_DIRECTION_BITCNT 6
51 #define LED_PARAMS_BITCNT 4
53 #define LED_FLAG_OVERLAY_MASK ((1 << LED_OVERLAY_BITCNT) - 1)
54 #define LED_FLAG_DIRECTION_MASK ((1 << LED_DIRECTION_BITCNT) - 1)
56 #define LED_MOV_POS(pos) ((pos) << LED_POS_OFFSET)
57 #define LED_MOV_FUNCTION(func) ((func) << LED_FUNCTION_OFFSET)
58 #define LED_MOV_OVERLAY(overlay) ((overlay) << LED_OVERLAY_OFFSET)
59 #define LED_MOV_COLOR(colorId) ((colorId) << LED_COLOR_OFFSET)
60 #define LED_MOV_DIRECTION(direction) ((direction) << LED_DIRECTION_OFFSET)
61 #define LED_MOV_PARAMS(param) ((param) << LED_PARAMS_OFFSET)
63 #define LED_BIT_MASK(len) ((1 << (len)) - 1)
65 #define LED_POS_MASK LED_MOV_POS(((1 << LED_POS_BITCNT) - 1))
66 #define LED_FUNCTION_MASK LED_MOV_FUNCTION(((1 << LED_FUNCTION_BITCNT) - 1))
67 #define LED_OVERLAY_MASK LED_MOV_OVERLAY(LED_FLAG_OVERLAY_MASK)
68 #define LED_COLOR_MASK LED_MOV_COLOR(((1 << LED_COLOR_BITCNT) - 1))
69 #define LED_DIRECTION_MASK LED_MOV_DIRECTION(LED_FLAG_DIRECTION_MASK)
70 #define LED_PARAMS_MASK LED_MOV_PARAMS(((1 << LED_PARAMS_BITCNT) - 1))
72 #define LED_FLAG_OVERLAY(id) (1 << (id))
73 #define LED_FLAG_DIRECTION(id) (1 << (id))
75 #define LED_X_BIT_OFFSET 4
76 #define LED_Y_BIT_OFFSET 0
77 #define LED_XY_MASK 0x0F
78 #define CALCULATE_LED_XY(x, y) ((((x) & LED_XY_MASK) << LED_X_BIT_OFFSET) | (((y) & LED_XY_MASK) << LED_Y_BIT_OFFSET))
80 typedef enum {
81 COLOR_BLACK = 0,
82 COLOR_WHITE,
83 COLOR_RED,
84 COLOR_ORANGE,
85 COLOR_YELLOW,
86 COLOR_LIME_GREEN,
87 COLOR_GREEN,
88 COLOR_MINT_GREEN,
89 COLOR_CYAN,
90 COLOR_LIGHT_BLUE,
91 COLOR_BLUE,
92 COLOR_DARK_VIOLET,
93 COLOR_MAGENTA,
94 COLOR_DEEP_PINK,
95 COLOR_COUNT
96 } colorId_e;
98 typedef enum {
99 LED_MODE_ORIENTATION = 0,
100 LED_MODE_HEADFREE,
101 LED_MODE_HORIZON,
102 LED_MODE_ANGLE,
103 LED_MODE_MAG,
104 LED_MODE_BARO,
105 LED_SPECIAL,
106 LED_AUX_CHANNEL
107 } ledModeIndex_e;
109 typedef enum {
110 LED_SCOLOR_DISARMED = 0,
111 LED_SCOLOR_ARMED,
112 LED_SCOLOR_ANIMATION,
113 LED_SCOLOR_BACKGROUND,
114 LED_SCOLOR_BLINKBACKGROUND,
115 LED_SCOLOR_GPSNOSATS,
116 LED_SCOLOR_GPSNOLOCK,
117 LED_SCOLOR_GPSLOCKED
118 } ledSpecialColorIds_e;
120 typedef enum {
121 LED_DIRECTION_NORTH = 0,
122 LED_DIRECTION_EAST,
123 LED_DIRECTION_SOUTH,
124 LED_DIRECTION_WEST,
125 LED_DIRECTION_UP,
126 LED_DIRECTION_DOWN
127 } ledDirectionId_e;
129 typedef enum {
130 LED_FUNCTION_COLOR,
131 LED_FUNCTION_FLIGHT_MODE,
132 LED_FUNCTION_ARM_STATE,
133 LED_FUNCTION_BATTERY,
134 LED_FUNCTION_RSSI,
135 LED_FUNCTION_GPS,
136 LED_FUNCTION_THRUST_RING
137 } ledBaseFunctionId_e;
139 typedef enum {
140 LED_OVERLAY_THROTTLE,
141 LED_OVERLAY_LARSON_SCANNER,
142 LED_OVERLAY_BLINK,
143 LED_OVERLAY_VTX,
144 LED_OVERLAY_INDICATOR,
145 LED_OVERLAY_WARNING
146 } ledOverlayId_e;
148 typedef enum {
149 LED_PROFILE_RACE = 0,
150 LED_PROFILE_BEACON,
151 #ifdef USE_LED_STRIP_STATUS_MODE
152 LED_PROFILE_STATUS,
153 #endif
154 LED_PROFILE_COUNT
155 } ledProfile_e;
157 typedef struct modeColorIndexes_s {
158 uint8_t color[LED_DIRECTION_COUNT];
159 } modeColorIndexes_t;
161 typedef struct specialColorIndexes_s {
162 uint8_t color[LED_SPECIAL_COLOR_COUNT];
163 } specialColorIndexes_t;
165 typedef uint32_t ledConfig_t;
167 typedef struct ledCounts_s {
168 uint8_t count;
169 uint8_t ring;
170 uint8_t larson;
171 uint8_t ringSeqLen;
172 } ledCounts_t;
174 typedef struct ledStripConfig_s {
175 uint8_t ledstrip_visual_beeper;
176 ioTag_t ioTag;
177 ledStripFormatRGB_e ledstrip_grb_rgb;
178 ledProfile_e ledstrip_profile;
179 colorId_e ledstrip_race_color;
180 colorId_e ledstrip_beacon_color;
181 uint16_t ledstrip_beacon_period_ms;
182 uint8_t ledstrip_beacon_percent;
183 uint8_t ledstrip_beacon_armed_only;
184 colorId_e ledstrip_visual_beeper_color;
185 uint8_t ledstrip_brightness;
186 } ledStripConfig_t;
188 PG_DECLARE(ledStripConfig_t, ledStripConfig);
190 #if defined(USE_LED_STRIP_STATUS_MODE)
191 typedef struct ledStripStatusModeConfig_s {
192 ledConfig_t ledConfigs[LED_MAX_STRIP_LENGTH];
193 hsvColor_t colors[LED_CONFIGURABLE_COLOR_COUNT];
194 modeColorIndexes_t modeColors[LED_MODE_COUNT];
195 specialColorIndexes_t specialColors;
196 uint8_t ledstrip_aux_channel;
197 } ledStripStatusModeConfig_t;
199 PG_DECLARE(ledStripStatusModeConfig_t, ledStripStatusModeConfig);
200 #endif
202 #define LF(name) LED_FUNCTION_ ## name
203 #define LO(name) LED_FLAG_OVERLAY(LED_OVERLAY_ ## name)
204 #define LD(name) LED_FLAG_DIRECTION(LED_DIRECTION_ ## name)
205 #define DEFINE_LED(x, y, col, dir, func, ol, params) (LED_MOV_POS(CALCULATE_LED_XY(x, y)) | LED_MOV_COLOR(col) | LED_MOV_DIRECTION(dir) | LED_MOV_FUNCTION(func) | LED_MOV_OVERLAY(ol) | LED_MOV_PARAMS(params))
207 static inline uint8_t ledGetXY(const ledConfig_t *lcfg) { return ((*lcfg >> LED_POS_OFFSET) & LED_BIT_MASK(LED_POS_BITCNT)); }
208 static inline uint8_t ledGetX(const ledConfig_t *lcfg) { return ((*lcfg >> (LED_POS_OFFSET + LED_X_BIT_OFFSET)) & LED_XY_MASK); }
209 static inline uint8_t ledGetY(const ledConfig_t *lcfg) { return ((*lcfg >> (LED_POS_OFFSET + LED_Y_BIT_OFFSET)) & LED_XY_MASK); }
210 static inline uint8_t ledGetFunction(const ledConfig_t *lcfg) { return ((*lcfg >> LED_FUNCTION_OFFSET) & LED_BIT_MASK(LED_FUNCTION_BITCNT)); }
211 static inline uint8_t ledGetOverlay(const ledConfig_t *lcfg) { return ((*lcfg >> LED_OVERLAY_OFFSET) & LED_BIT_MASK(LED_OVERLAY_BITCNT)); }
212 static inline uint8_t ledGetColor(const ledConfig_t *lcfg) { return ((*lcfg >> LED_COLOR_OFFSET) & LED_BIT_MASK(LED_COLOR_BITCNT)); }
213 static inline uint8_t ledGetDirection(const ledConfig_t *lcfg) { return ((*lcfg >> LED_DIRECTION_OFFSET) & LED_BIT_MASK(LED_DIRECTION_BITCNT)); }
214 static inline uint8_t ledGetParams(const ledConfig_t *lcfg) { return ((*lcfg >> LED_PARAMS_OFFSET) & LED_BIT_MASK(LED_PARAMS_BITCNT)); }
216 static inline bool ledGetOverlayBit(const ledConfig_t *lcfg, int id) { return ((ledGetOverlay(lcfg) >> id) & 1); }
217 static inline bool ledGetDirectionBit(const ledConfig_t *lcfg, int id) { return ((ledGetDirection(lcfg) >> id) & 1); }
219 bool parseColor(int index, const char *colorConfig);
221 bool parseLedStripConfig(int ledIndex, const char *config);
222 void generateLedConfig(ledConfig_t *ledConfig, char *ledConfigBuffer, size_t bufferSize);
223 void reevaluateLedConfig(void);
225 void ledStripInit(void);
226 void ledStripEnable(void);
227 void ledStripDisable(void);
228 void ledStripUpdate(timeUs_t currentTimeUs);
230 bool setModeColor(ledModeIndex_e modeIndex, int modeColorIndex, int colorIndex);
232 void applyDefaultLedStripConfig(ledConfig_t *ledConfig);
233 void applyDefaultColors(hsvColor_t *colors);
234 void applyDefaultModeColors(modeColorIndexes_t *modeColors);
235 void applyDefaultSpecialColors(specialColorIndexes_t *specialColors);
237 void updateRequiredOverlay(void);
239 uint8_t getLedProfile(void);
240 void setLedProfile(uint8_t profile);