Minor LED fixes, catchup with betaflight (#533)
[inav/snaewe.git] / src / main / io / ledstrip.h
blob30450cf05d9caafe5f43568b0e4f534bffdb97cc
1 /*
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/>.
18 #pragma once
20 #define LED_MAX_STRIP_LENGTH 32
21 #define LED_CONFIGURABLE_COLOR_COUNT 16
22 #define LED_MODE_COUNT 6
23 #define LED_DIRECTION_COUNT 6
24 #define LED_BASEFUNCTION_COUNT 7
25 #define LED_OVERLAY_COUNT 6
26 #define LED_SPECIAL_COLOR_COUNT 11
28 #define LED_POS_OFFSET 0
29 #define LED_FUNCTION_OFFSET 8
30 #define LED_OVERLAY_OFFSET 12
31 #define LED_COLOR_OFFSET 18
32 #define LED_DIRECTION_OFFSET 22
33 #define LED_PARAMS_OFFSET 28
35 #define LED_POS_BITCNT 8
36 #define LED_FUNCTION_BITCNT 4
37 #define LED_OVERLAY_BITCNT 6
38 #define LED_COLOR_BITCNT 4
39 #define LED_DIRECTION_BITCNT 6
40 #define LED_PARAMS_BITCNT 4
42 #define LED_FLAG_OVERLAY_MASK ((1 << LED_OVERLAY_BITCNT) - 1)
43 #define LED_FLAG_DIRECTION_MASK ((1 << LED_DIRECTION_BITCNT) - 1)
45 #define LED_MOV_POS(pos) ((pos) << LED_POS_OFFSET)
46 #define LED_MOV_FUNCTION(func) ((func) << LED_FUNCTION_OFFSET)
47 #define LED_MOV_OVERLAY(overlay) ((overlay) << LED_OVERLAY_OFFSET)
48 #define LED_MOV_COLOR(colorId) ((colorId) << LED_COLOR_OFFSET)
49 #define LED_MOV_DIRECTION(direction) ((direction) << LED_DIRECTION_OFFSET)
50 #define LED_MOV_PARAMS(param) ((param) << LED_PARAMS_OFFSET)
52 #define LED_BIT_MASK(len) ((1 << (len)) - 1)
54 #define LED_POS_MASK LED_MOV_POS(((1 << LED_POS_BITCNT) - 1))
55 #define LED_FUNCTION_MASK LED_MOV_FUNCTION(((1 << LED_FUNCTION_BITCNT) - 1))
56 #define LED_OVERLAY_MASK LED_MOV_OVERLAY(LED_FLAG_OVERLAY_MASK)
57 #define LED_COLOR_MASK LED_MOV_COLOR(((1 << LED_COLOR_BITCNT) - 1))
58 #define LED_DIRECTION_MASK LED_MOV_DIRECTION(LED_FLAG_DIRECTION_MASK)
59 #define LED_PARAMS_MASK LED_MOV_PARAMS(((1 << LED_PARAMS_BITCNT) - 1))
61 #define LED_FLAG_OVERLAY(id) (1 << (id))
62 #define LED_FLAG_DIRECTION(id) (1 << (id))
64 #define LED_X_BIT_OFFSET 4
65 #define LED_Y_BIT_OFFSET 0
66 #define LED_XY_MASK 0x0F
67 #define CALCULATE_LED_XY(x, y) ((((x) & LED_XY_MASK) << LED_X_BIT_OFFSET) | (((y) & LED_XY_MASK) << LED_Y_BIT_OFFSET))
69 typedef enum {
70 LED_MODE_ORIENTATION = 0,
71 LED_MODE_HEADFREE,
72 LED_MODE_HORIZON,
73 LED_MODE_ANGLE,
74 LED_MODE_MAG,
75 LED_MODE_BARO,
76 LED_SPECIAL
77 } ledModeIndex_e;
79 typedef enum {
80 LED_SCOLOR_DISARMED = 0,
81 LED_SCOLOR_ARMED,
82 LED_SCOLOR_ANIMATION,
83 LED_SCOLOR_BACKGROUND,
84 LED_SCOLOR_BLINKBACKGROUND,
85 LED_SCOLOR_GPSNOSATS,
86 LED_SCOLOR_GPSNOLOCK,
87 LED_SCOLOR_GPSLOCKED
88 } ledSpecialColorIds_e;
90 typedef enum {
91 LED_DIRECTION_NORTH = 0,
92 LED_DIRECTION_EAST,
93 LED_DIRECTION_SOUTH,
94 LED_DIRECTION_WEST,
95 LED_DIRECTION_UP,
96 LED_DIRECTION_DOWN
97 } ledDirectionId_e;
99 typedef enum {
100 LED_FUNCTION_COLOR,
101 LED_FUNCTION_FLIGHT_MODE,
102 LED_FUNCTION_ARM_STATE,
103 LED_FUNCTION_BATTERY,
104 LED_FUNCTION_RSSI,
105 LED_FUNCTION_GPS,
106 LED_FUNCTION_THRUST_RING,
107 } ledBaseFunctionId_e;
109 typedef enum {
110 LED_OVERLAY_THROTTLE,
111 LED_OVERLAY_LARSON_SCANNER,
112 LED_OVERLAY_BLINK,
113 LED_OVERLAY_LANDING_FLASH,
114 LED_OVERLAY_INDICATOR,
115 LED_OVERLAY_WARNING,
116 } ledOverlayId_e;
118 typedef struct modeColorIndexes_s {
119 uint8_t color[LED_DIRECTION_COUNT];
120 } modeColorIndexes_t;
122 typedef struct specialColorIndexes_s {
123 uint8_t color[LED_SPECIAL_COLOR_COUNT];
124 } specialColorIndexes_t;
126 typedef uint32_t ledConfig_t;
128 typedef struct ledCounts_s {
129 uint8_t count;
130 uint8_t ring;
131 uint8_t larson;
132 uint8_t ringSeqLen;
133 } ledCounts_t;
136 ledConfig_t *ledConfigs;
137 hsvColor_t *colors;
138 modeColorIndexes_t *modeColors;
139 specialColorIndexes_t specialColors;
141 #define DEFINE_LED(x, y, col, dir, func, ol, params) (LED_MOV_POS(CALCULATE_LED_XY(x, y)) | LED_MOV_COLOR(col) | LED_MOV_DIRECTION(dir) | LED_MOV_FUNCTION(func) | LED_MOV_OVERLAY(ol) | LED_MOV_PARAMS(params))
143 static inline uint8_t ledGetXY(const ledConfig_t *lcfg) { return ((*lcfg >> LED_POS_OFFSET) & LED_BIT_MASK(LED_POS_BITCNT)); }
144 static inline uint8_t ledGetX(const ledConfig_t *lcfg) { return ((*lcfg >> (LED_POS_OFFSET + LED_X_BIT_OFFSET)) & LED_XY_MASK); }
145 static inline uint8_t ledGetY(const ledConfig_t *lcfg) { return ((*lcfg >> (LED_POS_OFFSET + LED_Y_BIT_OFFSET)) & LED_XY_MASK); }
146 static inline uint8_t ledGetFunction(const ledConfig_t *lcfg) { return ((*lcfg >> LED_FUNCTION_OFFSET) & LED_BIT_MASK(LED_FUNCTION_BITCNT)); }
147 static inline uint8_t ledGetOverlay(const ledConfig_t *lcfg) { return ((*lcfg >> LED_OVERLAY_OFFSET) & LED_BIT_MASK(LED_OVERLAY_BITCNT)); }
148 static inline uint8_t ledGetColor(const ledConfig_t *lcfg) { return ((*lcfg >> LED_COLOR_OFFSET) & LED_BIT_MASK(LED_COLOR_BITCNT)); }
149 static inline uint8_t ledGetDirection(const ledConfig_t *lcfg) { return ((*lcfg >> LED_DIRECTION_OFFSET) & LED_BIT_MASK(LED_DIRECTION_BITCNT)); }
150 static inline uint8_t ledGetParams(const ledConfig_t *lcfg) { return ((*lcfg >> LED_PARAMS_OFFSET) & LED_BIT_MASK(LED_PARAMS_BITCNT)); }
152 static inline bool ledGetOverlayBit(const ledConfig_t *lcfg, int id) { return ((ledGetOverlay(lcfg) >> id) & 1); }
153 static inline bool ledGetDirectionBit(const ledConfig_t *lcfg, int id) { return ((ledGetDirection(lcfg) >> id) & 1); }
155 PG_DECLARE_ARR(ledConfig_t, LED_MAX_STRIP_LENGTH, ledConfigs);
156 PG_DECLARE_ARR(hsvColor_t, LED_CONFIGURABLE_COLOR_COUNT, colors);
157 PG_DECLARE_ARR(modeColorIndexes_t, LED_MODE_COUNT, modeColors);
158 PG_DECLARE(specialColorIndexes_t, specialColors);
160 bool parseColor(int index, const char *colorConfig);
162 bool parseLedStripConfig(int ledIndex, const char *config);
163 void generateLedConfig(ledConfig_t *ledConfig, char *ledConfigBuffer, size_t bufferSize);
164 void reevaluateLedConfig(void);
166 void ledStripInit(ledConfig_t *ledConfigsToUse, hsvColor_t *colorsToUse, modeColorIndexes_t *modeColorsToUse, specialColorIndexes_t *specialColorsToUse);
167 void ledStripEnable(void);
168 void updateLedStrip(void);
170 bool setModeColor(ledModeIndex_e modeIndex, int modeColorIndex, int colorIndex);
172 extern uint16_t rssi; // FIXME dependency on mw.c
175 void applyDefaultLedStripConfig(ledConfig_t *ledConfig);
176 void applyDefaultColors(hsvColor_t *colors);
177 void applyDefaultModeColors(modeColorIndexes_t *modeColors);
178 void applyDefaultSpecialColors(specialColorIndexes_t *specialColors);