2 * This file is part of Cleanflight.
4 * Cleanflight 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.
9 * Cleanflight 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.
14 * You should have received a copy of the GNU General Public License
15 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
20 #include "config/parameter_group.h"
21 #include "common/color.h"
24 #define LED_MAX_STRIP_LENGTH 128
25 #define LED_CONFIGURABLE_COLOR_COUNT 16
26 #define LED_MODE_COUNT 6
27 #define LED_DIRECTION_COUNT 6
28 #define LED_BASEFUNCTION_COUNT 8
29 #define LED_OVERLAY_COUNT 7
30 #define LED_SPECIAL_COLOR_COUNT 9
32 #define LED_FUNCTION_OFFSET 8
33 #define LED_OVERLAY_OFFSET 16
35 #define LED_POS_BITCNT 8
36 #define LED_FUNCTION_BITCNT 8
37 #define LED_OVERLAY_BITCNT 8
38 #define LED_COLOR_BITCNT 4
39 #define LED_DIRECTION_BITCNT 6
40 #define LED_PARAMS_BITCNT 6
42 #define LED_FLAG_OVERLAY_MASK ((1 << LED_OVERLAY_BITCNT) - 1)
44 #define LED_MOV_FUNCTION(func) ((func) << LED_FUNCTION_OFFSET)
45 #define LED_MOV_OVERLAY(overlay) ((overlay) << LED_OVERLAY_OFFSET)
47 #define LED_FUNCTION_MASK LED_MOV_FUNCTION(((1 << LED_FUNCTION_BITCNT) - 1))
48 #define LED_OVERLAY_MASK LED_MOV_OVERLAY(LED_FLAG_OVERLAY_MASK)
50 #define LED_FLAG_OVERLAY(id) (1 << (id))
51 #define LED_FLAG_DIRECTION(id) (1 << (id))
53 #define LED_X_BIT_OFFSET 4
54 #define LED_Y_BIT_OFFSET 0
55 #define LED_XY_MASK 0x0F
56 #define CALCULATE_LED_XY(x, y) ((((x) & LED_XY_MASK) << LED_X_BIT_OFFSET) | (((y) & LED_XY_MASK) << LED_Y_BIT_OFFSET))
76 LED_MODE_ORIENTATION
= 0,
86 LED_SCOLOR_DISARMED
= 0,
89 LED_SCOLOR_BACKGROUND
,
90 LED_SCOLOR_BLINKBACKGROUND
,
95 } ledSpecialColorIds_e
;
98 LED_DIRECTION_NORTH
= 0,
108 LED_FUNCTION_FLIGHT_MODE
,
109 LED_FUNCTION_ARM_STATE
,
110 LED_FUNCTION_BATTERY
,
113 LED_FUNCTION_THRUST_RING
,
114 LED_FUNCTION_CHANNEL
,
115 } ledBaseFunctionId_e
;
118 LED_OVERLAY_THROTTLE
,
119 LED_OVERLAY_LARSON_SCANNER
,
121 LED_OVERLAY_LANDING_FLASH
,
122 LED_OVERLAY_INDICATOR
,
127 typedef struct modeColorIndexes_s
{
128 uint8_t color
[LED_DIRECTION_COUNT
];
129 } modeColorIndexes_t
;
131 typedef struct specialColorIndexes_s
{
132 uint8_t color
[LED_SPECIAL_COLOR_COUNT
];
133 } specialColorIndexes_t
;
135 typedef struct ledConfig_s
{
136 uint16_t led_position
: LED_POS_BITCNT
;
137 uint16_t led_function
: LED_FUNCTION_BITCNT
;
138 uint16_t led_overlay
: LED_OVERLAY_BITCNT
;
139 uint16_t led_color
: LED_COLOR_BITCNT
;
140 uint16_t led_direction
: LED_DIRECTION_BITCNT
;
141 uint16_t led_params
: LED_PARAMS_BITCNT
;
142 } __attribute__((packed
)) ledConfig_t
;
144 typedef struct ledCounts_s
{
151 typedef struct ledStripConfig_s
{
152 ledConfig_t ledConfigs
[LED_MAX_STRIP_LENGTH
];
153 hsvColor_t colors
[LED_CONFIGURABLE_COLOR_COUNT
];
154 modeColorIndexes_t modeColors
[LED_MODE_COUNT
];
155 specialColorIndexes_t specialColors
;
158 PG_DECLARE(ledStripConfig_t
, ledStripConfig
);
160 #define DEFINE_LED(ledConfigPtr, x, y, col, dir, func, ol, params) { \
161 (ledConfigPtr)->led_position = CALCULATE_LED_XY(x, y); \
162 (ledConfigPtr)->led_color = (col); \
163 (ledConfigPtr)->led_direction = (dir); \
164 (ledConfigPtr)->led_function = (func); \
165 (ledConfigPtr)->led_overlay = (ol); \
166 (ledConfigPtr)->led_params = (params); }
168 static inline uint8_t ledGetXY(const ledConfig_t
*lcfg
) { return (lcfg
->led_position
); }
169 static inline uint8_t ledGetX(const ledConfig_t
*lcfg
) { return ((lcfg
->led_position
>> (LED_X_BIT_OFFSET
)) & LED_XY_MASK
); }
170 static inline uint8_t ledGetY(const ledConfig_t
*lcfg
) { return ((lcfg
->led_position
>> (LED_Y_BIT_OFFSET
)) & LED_XY_MASK
); }
171 static inline uint8_t ledGetFunction(const ledConfig_t
*lcfg
) { return (lcfg
->led_function
); }
172 static inline uint8_t ledGetOverlay(const ledConfig_t
*lcfg
) { return (lcfg
->led_overlay
); }
173 static inline uint8_t ledGetColor(const ledConfig_t
*lcfg
) { return (lcfg
->led_color
); }
174 static inline uint8_t ledGetDirection(const ledConfig_t
*lcfg
) { return (lcfg
->led_direction
); }
175 static inline uint8_t ledGetParams(const ledConfig_t
*lcfg
) { return (lcfg
->led_params
); }
177 static inline bool ledGetOverlayBit(const ledConfig_t
*lcfg
, int id
) { return ((ledGetOverlay(lcfg
) >> id
) & 1); }
178 static inline bool ledGetDirectionBit(const ledConfig_t
*lcfg
, int id
) { return ((ledGetDirection(lcfg
) >> id
) & 1); }
180 bool parseColor(int index
, const char *colorConfig
);
182 bool parseLedStripConfig(int ledIndex
, const char *config
);
183 void generateLedConfig(ledConfig_t
*ledConfig
, char *ledConfigBuffer
, size_t bufferSize
);
184 void reevaluateLedConfig(void);
186 void ledStripInit(void);
187 void ledStripEnable(void);
188 void ledStripUpdate(timeUs_t currentTimeUs
);
190 bool setModeColor(ledModeIndex_e modeIndex
, int modeColorIndex
, int colorIndex
);