Dshot RPM Telemetry Refactoring (#13012)
[betaflight.git] / src / main / io / ledstrip.h
blobe21e9c65787f12c7a1879ce832295b468b051bba
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 TASK_LEDSTRIP_RATE_HZ 100
33 #define LED_CONFIGURABLE_COLOR_COUNT 16
34 #define LED_MODE_COUNT 6
35 #define LED_DIRECTION_COUNT 6
36 #define LED_BASEFUNCTION_COUNT 7
37 #define LED_OVERLAY_COUNT 7
38 #define LED_SPECIAL_COLOR_COUNT 11
40 #define LED_POS_OFFSET 0
41 #define LED_FUNCTION_OFFSET 8
42 #define LED_OVERLAY_OFFSET 12
43 #define LED_COLOR_OFFSET 22
44 #define LED_DIRECTION_OFFSET 26
46 #define LED_POS_BITCNT 8
47 #define LED_FUNCTION_BITCNT 4
48 #define LED_OVERLAY_BITCNT 10
49 #define LED_COLOR_BITCNT 4
50 #define LED_DIRECTION_BITCNT 6
52 #define LED_MOV_POS(pos) ((pos) << LED_POS_OFFSET)
53 #define LED_MOV_FUNCTION(func) ((func) << LED_FUNCTION_OFFSET)
54 #define LED_MOV_OVERLAY(overlay) ((overlay) << LED_OVERLAY_OFFSET)
55 #define LED_MOV_COLOR(colorId) ((colorId) << LED_COLOR_OFFSET)
56 #define LED_MOV_DIRECTION(direction) ((direction) << LED_DIRECTION_OFFSET)
58 #define LED_BIT_MASK(len) ((1 << (len)) - 1)
60 #define LED_POS_MASK LED_MOV_POS(((1 << LED_POS_BITCNT) - 1))
61 #define LED_FUNCTION_MASK LED_MOV_FUNCTION(((1 << LED_FUNCTION_BITCNT) - 1))
62 #define LED_OVERLAY_MASK LED_MOV_OVERLAY(((1 << LED_OVERLAY_BITCNT) - 1))
63 #define LED_COLOR_MASK LED_MOV_COLOR(((1 << LED_COLOR_BITCNT) - 1))
64 #define LED_DIRECTION_MASK LED_MOV_DIRECTION(((1 << LED_DIRECTION_BITCNT) - 1))
66 #define LED_FLAG_OVERLAY(id) (1 << (id))
67 #define LED_FLAG_DIRECTION(id) (1 << (id))
69 #define LED_X_BIT_OFFSET 4
70 #define LED_Y_BIT_OFFSET 0
71 #define LED_XY_MASK 0x0F
72 #define CALCULATE_LED_XY(x, y) ((((x) & LED_XY_MASK) << LED_X_BIT_OFFSET) | (((y) & LED_XY_MASK) << LED_Y_BIT_OFFSET))
74 typedef enum {
75 COLOR_BLACK = 0,
76 COLOR_WHITE,
77 COLOR_RED,
78 COLOR_ORANGE,
79 COLOR_YELLOW,
80 COLOR_LIME_GREEN,
81 COLOR_GREEN,
82 COLOR_MINT_GREEN,
83 COLOR_CYAN,
84 COLOR_LIGHT_BLUE,
85 COLOR_BLUE,
86 COLOR_DARK_VIOLET,
87 COLOR_MAGENTA,
88 COLOR_DEEP_PINK,
89 COLOR_COUNT
90 } colorId_e;
92 typedef enum {
93 LED_MODE_ORIENTATION = 0,
94 LED_MODE_HEADFREE,
95 LED_MODE_HORIZON,
96 LED_MODE_ANGLE,
97 LED_MODE_MAG,
98 LED_MODE_BARO,
99 LED_SPECIAL,
100 LED_AUX_CHANNEL
101 } ledModeIndex_e;
103 typedef enum {
104 LED_SCOLOR_DISARMED = 0,
105 LED_SCOLOR_ARMED,
106 LED_SCOLOR_ANIMATION,
107 LED_SCOLOR_BACKGROUND,
108 LED_SCOLOR_BLINKBACKGROUND,
109 LED_SCOLOR_GPSNOSATS,
110 LED_SCOLOR_GPSNOLOCK,
111 LED_SCOLOR_GPSLOCKED
112 } ledSpecialColorIds_e;
114 typedef enum {
115 LED_DIRECTION_NORTH = 0,
116 LED_DIRECTION_EAST,
117 LED_DIRECTION_SOUTH,
118 LED_DIRECTION_WEST,
119 LED_DIRECTION_UP,
120 LED_DIRECTION_DOWN
121 } ledDirectionId_e;
123 typedef enum {
124 LED_FUNCTION_COLOR,
125 LED_FUNCTION_FLIGHT_MODE,
126 LED_FUNCTION_ARM_STATE,
127 LED_FUNCTION_BATTERY,
128 LED_FUNCTION_RSSI,
129 LED_FUNCTION_GPS,
130 LED_FUNCTION_THRUST_RING
131 } ledBaseFunctionId_e;
133 typedef enum {
134 LED_OVERLAY_THROTTLE,
135 LED_OVERLAY_RAINBOW,
136 LED_OVERLAY_LARSON_SCANNER,
137 LED_OVERLAY_BLINK,
138 LED_OVERLAY_VTX,
139 LED_OVERLAY_INDICATOR,
140 LED_OVERLAY_WARNING
141 } ledOverlayId_e;
143 typedef enum {
144 LED_PROFILE_RACE = 0,
145 LED_PROFILE_BEACON,
146 #ifdef USE_LED_STRIP_STATUS_MODE
147 LED_PROFILE_STATUS,
148 #endif
149 LED_PROFILE_COUNT
150 } ledProfile_e;
152 typedef struct modeColorIndexes_s {
153 uint8_t color[LED_DIRECTION_COUNT];
154 } modeColorIndexes_t;
156 typedef struct specialColorIndexes_s {
157 uint8_t color[LED_SPECIAL_COLOR_COUNT];
158 } specialColorIndexes_t;
160 typedef uint32_t ledConfig_t;
162 typedef struct ledCounts_s {
163 uint8_t count;
164 uint8_t ring;
165 uint8_t larson;
166 uint8_t ringSeqLen;
167 } ledCounts_t;
169 typedef struct ledStripConfig_s {
170 uint8_t ledstrip_visual_beeper;
171 ioTag_t ioTag;
172 ledStripFormatRGB_e ledstrip_grb_rgb;
173 ledProfile_e ledstrip_profile;
174 colorId_e ledstrip_race_color;
175 colorId_e ledstrip_beacon_color;
176 uint16_t ledstrip_beacon_period_ms;
177 uint8_t ledstrip_beacon_percent;
178 uint8_t ledstrip_beacon_armed_only;
179 colorId_e ledstrip_visual_beeper_color;
180 uint8_t ledstrip_brightness;
181 uint16_t ledstrip_rainbow_delta;
182 uint16_t ledstrip_rainbow_freq;
183 } ledStripConfig_t;
185 PG_DECLARE(ledStripConfig_t, ledStripConfig);
187 #if defined(USE_LED_STRIP_STATUS_MODE)
188 typedef struct ledStripStatusModeConfig_s {
189 ledConfig_t ledConfigs[LED_STRIP_MAX_LENGTH];
190 hsvColor_t colors[LED_CONFIGURABLE_COLOR_COUNT];
191 modeColorIndexes_t modeColors[LED_MODE_COUNT];
192 specialColorIndexes_t specialColors;
193 uint8_t ledstrip_aux_channel;
194 } ledStripStatusModeConfig_t;
196 PG_DECLARE(ledStripStatusModeConfig_t, ledStripStatusModeConfig);
197 #endif
199 #define LF(name) LED_FUNCTION_ ## name
200 #define LO(name) LED_FLAG_OVERLAY(LED_OVERLAY_ ## name)
201 #define LD(name) LED_FLAG_DIRECTION(LED_DIRECTION_ ## name)
202 #define DEFINE_LED(x, y, col, dir, func, ol) (LED_MOV_POS(CALCULATE_LED_XY(x, y)) | LED_MOV_COLOR(col) | LED_MOV_DIRECTION(dir) | LED_MOV_FUNCTION(func) | LED_MOV_OVERLAY(ol))
204 static inline uint8_t ledGetXY(const ledConfig_t *lcfg) { return ((*lcfg >> LED_POS_OFFSET) & LED_BIT_MASK(LED_POS_BITCNT)); }
205 static inline uint8_t ledGetX(const ledConfig_t *lcfg) { return ((*lcfg >> (LED_POS_OFFSET + LED_X_BIT_OFFSET)) & LED_XY_MASK); }
206 static inline uint8_t ledGetY(const ledConfig_t *lcfg) { return ((*lcfg >> (LED_POS_OFFSET + LED_Y_BIT_OFFSET)) & LED_XY_MASK); }
207 static inline uint8_t ledGetFunction(const ledConfig_t *lcfg) { return ((*lcfg >> LED_FUNCTION_OFFSET) & LED_BIT_MASK(LED_FUNCTION_BITCNT)); }
208 static inline uint8_t ledGetOverlay(const ledConfig_t *lcfg) { return ((*lcfg >> LED_OVERLAY_OFFSET) & LED_BIT_MASK(LED_OVERLAY_BITCNT)); }
209 static inline uint8_t ledGetColor(const ledConfig_t *lcfg) { return ((*lcfg >> LED_COLOR_OFFSET) & LED_BIT_MASK(LED_COLOR_BITCNT)); }
210 static inline uint8_t ledGetDirection(const ledConfig_t *lcfg) { return ((*lcfg >> LED_DIRECTION_OFFSET) & LED_BIT_MASK(LED_DIRECTION_BITCNT)); }
212 static inline bool ledGetOverlayBit(const ledConfig_t *lcfg, int id) { return ((ledGetOverlay(lcfg) >> id) & 1); }
213 static inline bool ledGetDirectionBit(const ledConfig_t *lcfg, int id) { return ((ledGetDirection(lcfg) >> id) & 1); }
215 bool parseColor(int index, const char *colorConfig);
217 bool parseLedStripConfig(int ledIndex, const char *config);
218 void generateLedConfig(ledConfig_t *ledConfig, char *ledConfigBuffer, size_t bufferSize);
219 void reevaluateLedConfig(void);
221 void ledStripInit(void);
222 void ledStripEnable(void);
223 void ledStripDisable(void);
224 void ledStripUpdate(timeUs_t currentTimeUs);
226 bool setModeColor(ledModeIndex_e modeIndex, int modeColorIndex, int colorIndex);
228 void applyDefaultLedStripConfig(ledConfig_t *ledConfig);
229 void applyDefaultColors(hsvColor_t *colors);
230 void applyDefaultModeColors(modeColorIndexes_t *modeColors);
231 void applyDefaultSpecialColors(specialColorIndexes_t *specialColors);
233 void updateRequiredOverlay(void);
235 uint8_t getLedProfile(void);
236 void setLedProfile(uint8_t profile);