more cleanup changes
[inav.git] / src / main / io / ledstrip.h
blob0311f5f4382a18bc7826db1bc685b4c3a74e6177
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 #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))
57 typedef enum {
58 COLOR_BLACK = 0,
59 COLOR_WHITE,
60 COLOR_RED,
61 COLOR_ORANGE,
62 COLOR_YELLOW,
63 COLOR_LIME_GREEN,
64 COLOR_GREEN,
65 COLOR_MINT_GREEN,
66 COLOR_CYAN,
67 COLOR_LIGHT_BLUE,
68 COLOR_BLUE,
69 COLOR_DARK_VIOLET,
70 COLOR_MAGENTA,
71 COLOR_DEEP_PINK,
72 } colorId_e;
74 typedef enum {
75 LED_MODE_ORIENTATION = 0,
76 LED_MODE_HEADFREE,
77 LED_MODE_HORIZON,
78 LED_MODE_ANGLE,
79 LED_MODE_MAG,
80 LED_MODE_BARO,
81 LED_SPECIAL
82 } ledModeIndex_e;
84 typedef enum {
85 LED_SCOLOR_DISARMED = 0,
86 LED_SCOLOR_ARMED,
87 LED_SCOLOR_ANIMATION,
88 LED_SCOLOR_BACKGROUND,
89 LED_SCOLOR_BLINKBACKGROUND,
90 LED_SCOLOR_GPSNOSATS,
91 LED_SCOLOR_GPSNOLOCK,
92 LED_SCOLOR_GPSLOCKED,
93 LED_SCOLOR_STROBE
94 } ledSpecialColorIds_e;
96 typedef enum {
97 LED_DIRECTION_NORTH = 0,
98 LED_DIRECTION_EAST,
99 LED_DIRECTION_SOUTH,
100 LED_DIRECTION_WEST,
101 LED_DIRECTION_UP,
102 LED_DIRECTION_DOWN
103 } ledDirectionId_e;
105 typedef enum {
106 LED_FUNCTION_COLOR,
107 LED_FUNCTION_FLIGHT_MODE,
108 LED_FUNCTION_ARM_STATE,
109 LED_FUNCTION_BATTERY,
110 LED_FUNCTION_RSSI,
111 LED_FUNCTION_GPS,
112 LED_FUNCTION_THRUST_RING,
113 LED_FUNCTION_CHANNEL,
114 } ledBaseFunctionId_e;
116 typedef enum {
117 LED_OVERLAY_THROTTLE,
118 LED_OVERLAY_LARSON_SCANNER,
119 LED_OVERLAY_BLINK,
120 LED_OVERLAY_LANDING_FLASH,
121 LED_OVERLAY_INDICATOR,
122 LED_OVERLAY_WARNING,
123 LED_OVERLAY_STROBE
124 } ledOverlayId_e;
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 {
146 uint8_t count;
147 uint8_t ring;
148 uint8_t larson;
149 uint8_t ringSeqLen;
150 } ledCounts_t;
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
158 } ledStripConfig_t;
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);