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"
23 #define LED_MAX_STRIP_LENGTH 32
24 #define LED_CONFIGURABLE_COLOR_COUNT 16
25 #define LED_MODE_COUNT 6
26 #define LED_DIRECTION_COUNT 6
27 #define LED_BASEFUNCTION_COUNT 8
28 #define LED_OVERLAY_COUNT 7
29 #define LED_SPECIAL_COLOR_COUNT 9
31 #define LED_FUNCTION_OFFSET 8
32 #define LED_OVERLAY_OFFSET 12
34 #define LED_POS_BITCNT 8
35 #define LED_FUNCTION_BITCNT 4
36 #define LED_OVERLAY_BITCNT 7
37 #define LED_COLOR_BITCNT 4
38 #define LED_DIRECTION_BITCNT 6
39 #define LED_PARAMS_BITCNT 4
41 #define LED_FLAG_OVERLAY_MASK ((1 << LED_OVERLAY_BITCNT) - 1)
43 #define LED_MOV_FUNCTION(func) ((func) << LED_FUNCTION_OFFSET)
44 #define LED_MOV_OVERLAY(overlay) ((overlay) << LED_OVERLAY_OFFSET)
46 #define LED_FUNCTION_MASK LED_MOV_FUNCTION(((1 << LED_FUNCTION_BITCNT) - 1))
47 #define LED_OVERLAY_MASK LED_MOV_OVERLAY(LED_FLAG_OVERLAY_MASK)
49 #define LED_FLAG_OVERLAY(id) (1 << (id))
50 #define LED_FLAG_DIRECTION(id) (1 << (id))
52 #define LED_X_BIT_OFFSET 4
53 #define LED_Y_BIT_OFFSET 0
54 #define LED_XY_MASK 0x0F
55 #define CALCULATE_LED_XY(x, y) ((((x) & LED_XY_MASK) << LED_X_BIT_OFFSET) | (((y) & LED_XY_MASK) << LED_Y_BIT_OFFSET))
75 LED_MODE_ORIENTATION
= 0,
85 LED_SCOLOR_DISARMED
= 0,
88 LED_SCOLOR_BACKGROUND
,
89 LED_SCOLOR_BLINKBACKGROUND
,
94 } ledSpecialColorIds_e
;
97 LED_DIRECTION_NORTH
= 0,
107 LED_FUNCTION_FLIGHT_MODE
,
108 LED_FUNCTION_ARM_STATE
,
109 LED_FUNCTION_BATTERY
,
112 LED_FUNCTION_THRUST_RING
,
113 LED_FUNCTION_CHANNEL
,
114 } ledBaseFunctionId_e
;
117 LED_OVERLAY_THROTTLE
,
118 LED_OVERLAY_LARSON_SCANNER
,
120 LED_OVERLAY_LANDING_FLASH
,
121 LED_OVERLAY_INDICATOR
,
126 typedef struct modeColorIndexes_s
{
127 uint8_t color
[LED_DIRECTION_COUNT
];
128 } modeColorIndexes_t
;
130 typedef struct specialColorIndexes_s
{
131 uint8_t color
[LED_SPECIAL_COLOR_COUNT
];
132 } specialColorIndexes_t
;
135 typedef struct ledConfig_s
{
136 uint16_t led_position
: 8;
137 uint16_t led_function
: 4;
138 uint16_t led_overlay
: 7;
139 uint16_t led_color
: 4;
140 uint16_t led_direction
: 6;
141 uint16_t led_params
: 4;
143 } __attribute__((packed
)) ledConfig_t
;
145 typedef struct ledCounts_s
{
152 typedef struct ledStripConfig_s
{
153 ledConfig_t ledConfigs
[LED_MAX_STRIP_LENGTH
];
154 hsvColor_t colors
[LED_CONFIGURABLE_COLOR_COUNT
];
155 modeColorIndexes_t modeColors
[LED_MODE_COUNT
];
156 specialColorIndexes_t specialColors
;
157 uint8_t ledstrip_visual_beeper
; // suppress LEDLOW mode if beeper is on
160 PG_DECLARE(ledStripConfig_t
, ledStripConfig
);
162 #define DEFINE_LED(ledConfigPtr, x, y, col, dir, func, ol, params) { \
163 ledConfig->led_position = CALCULATE_LED_XY(x, y); \
164 ledConfig->led_color = (col); \
165 ledConfig->led_direction = (dir); \
166 ledConfig->led_function = (func); \
167 ledConfig->led_overlay = (ol); \
168 ledConfig->led_params = (params); }
170 static inline uint8_t ledGetXY(const ledConfig_t
*lcfg
) { return (lcfg
->led_position
); }
171 static inline uint8_t ledGetX(const ledConfig_t
*lcfg
) { return ((lcfg
->led_position
>> (LED_X_BIT_OFFSET
)) & LED_XY_MASK
); }
172 static inline uint8_t ledGetY(const ledConfig_t
*lcfg
) { return ((lcfg
->led_position
>> (LED_Y_BIT_OFFSET
)) & LED_XY_MASK
); }
173 static inline uint8_t ledGetFunction(const ledConfig_t
*lcfg
) { return (lcfg
->led_function
); }
174 static inline uint8_t ledGetOverlay(const ledConfig_t
*lcfg
) { return (lcfg
->led_overlay
); }
175 static inline uint8_t ledGetColor(const ledConfig_t
*lcfg
) { return (lcfg
->led_color
); }
176 static inline uint8_t ledGetDirection(const ledConfig_t
*lcfg
) { return (lcfg
->led_direction
); }
177 static inline uint8_t ledGetParams(const ledConfig_t
*lcfg
) { return (lcfg
->led_params
); }
179 static inline bool ledGetOverlayBit(const ledConfig_t
*lcfg
, int id
) { return ((ledGetOverlay(lcfg
) >> id
) & 1); }
180 static inline bool ledGetDirectionBit(const ledConfig_t
*lcfg
, int id
) { return ((ledGetDirection(lcfg
) >> id
) & 1); }
182 bool parseColor(int index
, const char *colorConfig
);
184 bool parseLedStripConfig(int ledIndex
, const char *config
);
185 void generateLedConfig(ledConfig_t
*ledConfig
, char *ledConfigBuffer
, size_t bufferSize
);
186 void reevaluateLedConfig(void);
188 void ledStripInit(void);
189 void ledStripEnable(void);
190 void ledStripUpdate(timeUs_t currentTimeUs
);
192 bool setModeColor(ledModeIndex_e modeIndex
, int modeColorIndex
, int colorIndex
);