Fix unused variable compile time error (#13707)
[betaflight.git] / src / main / io / ledstrip.h
blobfc6d467a6c0d1ba3e2e459f9ebcd6311f19d4caa
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 60
33 #define LED_CONFIGURABLE_COLOR_COUNT 16
34 #define LED_MODE_COUNT 6
35 #define LED_DIRECTION_COUNT 6
36 #define LED_BASEFUNCTION_COUNT 10
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 #define LED_TARGET_UPDATE_US 20
76 typedef enum {
77 COLOR_BLACK = 0,
78 COLOR_WHITE,
79 COLOR_RED,
80 COLOR_ORANGE,
81 COLOR_YELLOW,
82 COLOR_LIME_GREEN,
83 COLOR_GREEN,
84 COLOR_MINT_GREEN,
85 COLOR_CYAN,
86 COLOR_LIGHT_BLUE,
87 COLOR_BLUE,
88 COLOR_DARK_VIOLET,
89 COLOR_MAGENTA,
90 COLOR_DEEP_PINK,
91 COLOR_COUNT
92 } colorId_e;
94 typedef enum {
95 LED_MODE_ORIENTATION = 0,
96 LED_MODE_HEADFREE,
97 LED_MODE_HORIZON,
98 LED_MODE_ANGLE,
99 LED_MODE_MAG,
100 LED_MODE_BARO,
101 LED_SPECIAL,
102 LED_AUX_CHANNEL
103 } ledModeIndex_e;
105 typedef enum {
106 LED_SCOLOR_DISARMED = 0,
107 LED_SCOLOR_ARMED,
108 LED_SCOLOR_ANIMATION,
109 LED_SCOLOR_BACKGROUND,
110 LED_SCOLOR_BLINKBACKGROUND,
111 LED_SCOLOR_GPSNOSATS,
112 LED_SCOLOR_GPSNOLOCK,
113 LED_SCOLOR_GPSLOCKED
114 } ledSpecialColorIds_e;
116 typedef enum {
117 LED_DIRECTION_NORTH = 0,
118 LED_DIRECTION_EAST,
119 LED_DIRECTION_SOUTH,
120 LED_DIRECTION_WEST,
121 LED_DIRECTION_UP,
122 LED_DIRECTION_DOWN
123 } ledDirectionId_e;
125 typedef enum {
126 LED_FUNCTION_COLOR,
127 LED_FUNCTION_FLIGHT_MODE,
128 LED_FUNCTION_ARM_STATE,
129 LED_FUNCTION_BATTERY,
130 LED_FUNCTION_RSSI,
131 LED_FUNCTION_GPS,
132 LED_FUNCTION_THRUST_RING,
133 LED_FUNCTION_GPS_BAR,
134 LED_FUNCTION_BATTERY_BAR,
135 LED_FUNCTION_ALTITUDE
136 } ledBaseFunctionId_e;
138 typedef enum {
139 LED_OVERLAY_THROTTLE,
140 LED_OVERLAY_RAINBOW,
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 uint16_t ledstrip_rainbow_delta;
187 uint16_t ledstrip_rainbow_freq;
188 } ledStripConfig_t;
190 PG_DECLARE(ledStripConfig_t, ledStripConfig);
192 #if defined(USE_LED_STRIP_STATUS_MODE)
193 typedef struct ledStripStatusModeConfig_s {
194 ledConfig_t ledConfigs[LED_STRIP_MAX_LENGTH];
195 hsvColor_t colors[LED_CONFIGURABLE_COLOR_COUNT];
196 modeColorIndexes_t modeColors[LED_MODE_COUNT];
197 specialColorIndexes_t specialColors;
198 uint8_t ledstrip_aux_channel;
199 } ledStripStatusModeConfig_t;
201 PG_DECLARE(ledStripStatusModeConfig_t, ledStripStatusModeConfig);
202 #endif
204 #define LF(name) LED_FUNCTION_ ## name
205 #define LO(name) LED_FLAG_OVERLAY(LED_OVERLAY_ ## name)
206 #define LD(name) LED_FLAG_DIRECTION(LED_DIRECTION_ ## name)
207 #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))
209 static inline uint8_t ledGetXY(const ledConfig_t *lcfg) { return ((*lcfg >> LED_POS_OFFSET) & LED_BIT_MASK(LED_POS_BITCNT)); }
210 static inline uint8_t ledGetX(const ledConfig_t *lcfg) { return ((*lcfg >> (LED_POS_OFFSET + LED_X_BIT_OFFSET)) & LED_XY_MASK); }
211 static inline uint8_t ledGetY(const ledConfig_t *lcfg) { return ((*lcfg >> (LED_POS_OFFSET + LED_Y_BIT_OFFSET)) & LED_XY_MASK); }
212 static inline uint8_t ledGetFunction(const ledConfig_t *lcfg) { return ((*lcfg >> LED_FUNCTION_OFFSET) & LED_BIT_MASK(LED_FUNCTION_BITCNT)); }
213 static inline uint8_t ledGetOverlay(const ledConfig_t *lcfg) { return ((*lcfg >> LED_OVERLAY_OFFSET) & LED_BIT_MASK(LED_OVERLAY_BITCNT)); }
214 static inline uint8_t ledGetColor(const ledConfig_t *lcfg) { return ((*lcfg >> LED_COLOR_OFFSET) & LED_BIT_MASK(LED_COLOR_BITCNT)); }
215 static inline uint8_t ledGetDirection(const ledConfig_t *lcfg) { return ((*lcfg >> LED_DIRECTION_OFFSET) & LED_BIT_MASK(LED_DIRECTION_BITCNT)); }
217 static inline bool ledGetOverlayBit(const ledConfig_t *lcfg, int id) { return ((ledGetOverlay(lcfg) >> id) & 1); }
218 static inline bool ledGetDirectionBit(const ledConfig_t *lcfg, int id) { return ((ledGetDirection(lcfg) >> id) & 1); }
220 bool parseColor(int index, const char *colorConfig);
222 bool parseLedStripConfig(int ledIndex, const char *config);
223 void generateLedConfig(ledConfig_t *ledConfig, char *ledConfigBuffer, size_t bufferSize);
224 void reevaluateLedConfig(void);
226 void ledStripInit(void);
227 void ledStripEnable(void);
228 void ledStripDisable(void);
229 void ledStripUpdate(timeUs_t currentTimeUs);
231 bool setModeColor(ledModeIndex_e modeIndex, int modeColorIndex, int colorIndex);
233 void applyDefaultLedStripConfig(ledConfig_t *ledConfig);
234 void applyDefaultColors(hsvColor_t *colors);
235 void applyDefaultModeColors(modeColorIndexes_t *modeColors);
236 void applyDefaultSpecialColors(specialColorIndexes_t *specialColors);
238 void updateRequiredOverlay(void);
240 uint8_t getLedProfile(void);
241 void setLedProfile(uint8_t profile);