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)
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/>.
23 #include "common/color.h"
24 #include "common/time.h"
26 #include "drivers/io_types.h"
27 #include "drivers/light_ws2811strip.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 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 #define LED_TARGET_UPDATE_US 20
95 LED_MODE_ORIENTATION
= 0,
106 LED_SCOLOR_DISARMED
= 0,
108 LED_SCOLOR_ANIMATION
,
109 LED_SCOLOR_BACKGROUND
,
110 LED_SCOLOR_BLINKBACKGROUND
,
111 LED_SCOLOR_GPSNOSATS
,
112 LED_SCOLOR_GPSNOLOCK
,
114 } ledSpecialColorIds_e
;
117 LED_DIRECTION_NORTH
= 0,
127 LED_FUNCTION_FLIGHT_MODE
,
128 LED_FUNCTION_ARM_STATE
,
129 LED_FUNCTION_BATTERY
,
132 LED_FUNCTION_THRUST_RING
133 } ledBaseFunctionId_e
;
136 LED_OVERLAY_THROTTLE
,
138 LED_OVERLAY_LARSON_SCANNER
,
141 LED_OVERLAY_INDICATOR
,
146 LED_PROFILE_RACE
= 0,
148 #ifdef USE_LED_STRIP_STATUS_MODE
154 typedef struct modeColorIndexes_s
{
155 uint8_t color
[LED_DIRECTION_COUNT
];
156 } modeColorIndexes_t
;
158 typedef struct specialColorIndexes_s
{
159 uint8_t color
[LED_SPECIAL_COLOR_COUNT
];
160 } specialColorIndexes_t
;
162 typedef uint32_t ledConfig_t
;
164 typedef struct ledCounts_s
{
171 typedef struct ledStripConfig_s
{
172 uint8_t ledstrip_visual_beeper
;
174 ledStripFormatRGB_e ledstrip_grb_rgb
;
175 ledProfile_e ledstrip_profile
;
176 colorId_e ledstrip_race_color
;
177 colorId_e ledstrip_beacon_color
;
178 uint16_t ledstrip_beacon_period_ms
;
179 uint8_t ledstrip_beacon_percent
;
180 uint8_t ledstrip_beacon_armed_only
;
181 colorId_e ledstrip_visual_beeper_color
;
182 uint8_t ledstrip_brightness
;
183 uint16_t ledstrip_rainbow_delta
;
184 uint16_t ledstrip_rainbow_freq
;
187 PG_DECLARE(ledStripConfig_t
, ledStripConfig
);
189 #if defined(USE_LED_STRIP_STATUS_MODE)
190 typedef struct ledStripStatusModeConfig_s
{
191 ledConfig_t ledConfigs
[LED_STRIP_MAX_LENGTH
];
192 hsvColor_t colors
[LED_CONFIGURABLE_COLOR_COUNT
];
193 modeColorIndexes_t modeColors
[LED_MODE_COUNT
];
194 specialColorIndexes_t specialColors
;
195 uint8_t ledstrip_aux_channel
;
196 } ledStripStatusModeConfig_t
;
198 PG_DECLARE(ledStripStatusModeConfig_t
, ledStripStatusModeConfig
);
201 #define LF(name) LED_FUNCTION_ ## name
202 #define LO(name) LED_FLAG_OVERLAY(LED_OVERLAY_ ## name)
203 #define LD(name) LED_FLAG_DIRECTION(LED_DIRECTION_ ## name)
204 #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))
206 static inline uint8_t ledGetXY(const ledConfig_t
*lcfg
) { return ((*lcfg
>> LED_POS_OFFSET
) & LED_BIT_MASK(LED_POS_BITCNT
)); }
207 static inline uint8_t ledGetX(const ledConfig_t
*lcfg
) { return ((*lcfg
>> (LED_POS_OFFSET
+ LED_X_BIT_OFFSET
)) & LED_XY_MASK
); }
208 static inline uint8_t ledGetY(const ledConfig_t
*lcfg
) { return ((*lcfg
>> (LED_POS_OFFSET
+ LED_Y_BIT_OFFSET
)) & LED_XY_MASK
); }
209 static inline uint8_t ledGetFunction(const ledConfig_t
*lcfg
) { return ((*lcfg
>> LED_FUNCTION_OFFSET
) & LED_BIT_MASK(LED_FUNCTION_BITCNT
)); }
210 static inline uint8_t ledGetOverlay(const ledConfig_t
*lcfg
) { return ((*lcfg
>> LED_OVERLAY_OFFSET
) & LED_BIT_MASK(LED_OVERLAY_BITCNT
)); }
211 static inline uint8_t ledGetColor(const ledConfig_t
*lcfg
) { return ((*lcfg
>> LED_COLOR_OFFSET
) & LED_BIT_MASK(LED_COLOR_BITCNT
)); }
212 static inline uint8_t ledGetDirection(const ledConfig_t
*lcfg
) { return ((*lcfg
>> LED_DIRECTION_OFFSET
) & LED_BIT_MASK(LED_DIRECTION_BITCNT
)); }
214 static inline bool ledGetOverlayBit(const ledConfig_t
*lcfg
, int id
) { return ((ledGetOverlay(lcfg
) >> id
) & 1); }
215 static inline bool ledGetDirectionBit(const ledConfig_t
*lcfg
, int id
) { return ((ledGetDirection(lcfg
) >> id
) & 1); }
217 bool parseColor(int index
, const char *colorConfig
);
219 bool parseLedStripConfig(int ledIndex
, const char *config
);
220 void generateLedConfig(ledConfig_t
*ledConfig
, char *ledConfigBuffer
, size_t bufferSize
);
221 void reevaluateLedConfig(void);
223 void ledStripInit(void);
224 void ledStripEnable(void);
225 void ledStripDisable(void);
226 void ledStripUpdate(timeUs_t currentTimeUs
);
228 bool setModeColor(ledModeIndex_e modeIndex
, int modeColorIndex
, int colorIndex
);
230 void applyDefaultLedStripConfig(ledConfig_t
*ledConfig
);
231 void applyDefaultColors(hsvColor_t
*colors
);
232 void applyDefaultModeColors(modeColorIndexes_t
*modeColors
);
233 void applyDefaultSpecialColors(specialColorIndexes_t
*specialColors
);
235 void updateRequiredOverlay(void);
237 uint8_t getLedProfile(void);
238 void setLedProfile(uint8_t profile
);