Relocate grave keycode processing (#8082)
[jackhill/qmk/firmware.git] / quantum / rgblight.h
1 /* Copyright 2017 Yang Liu
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16 #ifndef RGBLIGHT_H
17 #define RGBLIGHT_H
18
19 #include "rgblight_reconfig.h"
20
21 /***** rgblight_mode(mode)/rgblight_mode_noeeprom(mode) ****
22
23 old mode number (before 0.6.117) to new mode name table
24
25 |-----------------|-----------------------------------|
26 | old mode number | new mode name |
27 |-----------------|-----------------------------------|
28 | 1 | RGBLIGHT_MODE_STATIC_LIGHT |
29 | 2 | RGBLIGHT_MODE_BREATHING |
30 | 3 | RGBLIGHT_MODE_BREATHING + 1 |
31 | 4 | RGBLIGHT_MODE_BREATHING + 2 |
32 | 5 | RGBLIGHT_MODE_BREATHING + 3 |
33 | 6 | RGBLIGHT_MODE_RAINBOW_MOOD |
34 | 7 | RGBLIGHT_MODE_RAINBOW_MOOD + 1 |
35 | 8 | RGBLIGHT_MODE_RAINBOW_MOOD + 2 |
36 | 9 | RGBLIGHT_MODE_RAINBOW_SWIRL |
37 | 10 | RGBLIGHT_MODE_RAINBOW_SWIRL + 1 |
38 | 11 | RGBLIGHT_MODE_RAINBOW_SWIRL + 2 |
39 | 12 | RGBLIGHT_MODE_RAINBOW_SWIRL + 3 |
40 | 13 | RGBLIGHT_MODE_RAINBOW_SWIRL + 4 |
41 | 14 | RGBLIGHT_MODE_RAINBOW_SWIRL + 5 |
42 | 15 | RGBLIGHT_MODE_SNAKE |
43 | 16 | RGBLIGHT_MODE_SNAKE + 1 |
44 | 17 | RGBLIGHT_MODE_SNAKE + 2 |
45 | 18 | RGBLIGHT_MODE_SNAKE + 3 |
46 | 19 | RGBLIGHT_MODE_SNAKE + 4 |
47 | 20 | RGBLIGHT_MODE_SNAKE + 5 |
48 | 21 | RGBLIGHT_MODE_KNIGHT |
49 | 22 | RGBLIGHT_MODE_KNIGHT + 1 |
50 | 23 | RGBLIGHT_MODE_KNIGHT + 2 |
51 | 24 | RGBLIGHT_MODE_CHRISTMAS |
52 | 25 | RGBLIGHT_MODE_STATIC_GRADIENT |
53 | 26 | RGBLIGHT_MODE_STATIC_GRADIENT + 1 |
54 | 27 | RGBLIGHT_MODE_STATIC_GRADIENT + 2 |
55 | 28 | RGBLIGHT_MODE_STATIC_GRADIENT + 3 |
56 | 29 | RGBLIGHT_MODE_STATIC_GRADIENT + 4 |
57 | 30 | RGBLIGHT_MODE_STATIC_GRADIENT + 5 |
58 | 31 | RGBLIGHT_MODE_STATIC_GRADIENT + 6 |
59 | 32 | RGBLIGHT_MODE_STATIC_GRADIENT + 7 |
60 | 33 | RGBLIGHT_MODE_STATIC_GRADIENT + 8 |
61 | 34 | RGBLIGHT_MODE_STATIC_GRADIENT + 9 |
62 | 35 | RGBLIGHT_MODE_RGB_TEST |
63 | 36 | RGBLIGHT_MODE_ALTERNATING |
64 |-----------------|-----------------------------------|
65 *****/
66
67 #define _RGBM_SINGLE_STATIC(sym) RGBLIGHT_MODE_##sym,
68 #define _RGBM_SINGLE_DYNAMIC(sym) RGBLIGHT_MODE_##sym,
69 #define _RGBM_MULTI_STATIC(sym) RGBLIGHT_MODE_##sym,
70 #define _RGBM_MULTI_DYNAMIC(sym) RGBLIGHT_MODE_##sym,
71 #define _RGBM_TMP_STATIC(sym, msym) RGBLIGHT_MODE_##sym,
72 #define _RGBM_TMP_DYNAMIC(sym, msym) RGBLIGHT_MODE_##sym,
73 enum RGBLIGHT_EFFECT_MODE {
74 RGBLIGHT_MODE_zero = 0,
75 #include "rgblight_modes.h"
76 RGBLIGHT_MODE_last
77 };
78
79 #ifndef RGBLIGHT_H_DUMMY_DEFINE
80
81 # define RGBLIGHT_MODES (RGBLIGHT_MODE_last - 1)
82
83 // sample: #define RGBLIGHT_EFFECT_BREATHE_CENTER 1.85
84
85 # ifndef RGBLIGHT_EFFECT_BREATHE_MAX
86 # define RGBLIGHT_EFFECT_BREATHE_MAX 255 // 0-255
87 # endif
88
89 # ifndef RGBLIGHT_EFFECT_SNAKE_LENGTH
90 # define RGBLIGHT_EFFECT_SNAKE_LENGTH 4
91 # endif
92
93 # ifndef RGBLIGHT_EFFECT_KNIGHT_LENGTH
94 # define RGBLIGHT_EFFECT_KNIGHT_LENGTH 3
95 # endif
96
97 # ifndef RGBLIGHT_EFFECT_KNIGHT_OFFSET
98 # define RGBLIGHT_EFFECT_KNIGHT_OFFSET 0
99 # endif
100
101 # ifndef RGBLIGHT_EFFECT_KNIGHT_LED_NUM
102 # define RGBLIGHT_EFFECT_KNIGHT_LED_NUM (effect_num_leds)
103 # endif
104
105 # ifndef RGBLIGHT_EFFECT_CHRISTMAS_INTERVAL
106 # define RGBLIGHT_EFFECT_CHRISTMAS_INTERVAL 1000
107 # endif
108
109 # ifndef RGBLIGHT_EFFECT_CHRISTMAS_STEP
110 # define RGBLIGHT_EFFECT_CHRISTMAS_STEP 2
111 # endif
112
113 # ifndef RGBLIGHT_HUE_STEP
114 # define RGBLIGHT_HUE_STEP 8
115 # endif
116 # ifndef RGBLIGHT_SAT_STEP
117 # define RGBLIGHT_SAT_STEP 17
118 # endif
119 # ifndef RGBLIGHT_VAL_STEP
120 # define RGBLIGHT_VAL_STEP 17
121 # endif
122 # ifndef RGBLIGHT_LIMIT_VAL
123 # define RGBLIGHT_LIMIT_VAL 255
124 # endif
125
126 # define RGBLED_TIMER_TOP F_CPU / (256 * 64)
127 // #define RGBLED_TIMER_TOP 0xFF10
128
129 # include <stdint.h>
130 # include <stdbool.h>
131 # include "eeconfig.h"
132 # ifndef RGBLIGHT_CUSTOM_DRIVER
133 # include "ws2812.h"
134 # endif
135 # include "color.h"
136 # include "rgblight_list.h"
137
138 # if defined(__AVR__)
139 # include <avr/pgmspace.h>
140 # endif
141
142 extern LED_TYPE led[RGBLED_NUM];
143
144 extern const uint8_t RGBLED_BREATHING_INTERVALS[4] PROGMEM;
145 extern const uint8_t RGBLED_RAINBOW_MOOD_INTERVALS[3] PROGMEM;
146 extern const uint8_t RGBLED_RAINBOW_SWIRL_INTERVALS[3] PROGMEM;
147 extern const uint8_t RGBLED_SNAKE_INTERVALS[3] PROGMEM;
148 extern const uint8_t RGBLED_KNIGHT_INTERVALS[3] PROGMEM;
149 extern const uint16_t RGBLED_RGBTEST_INTERVALS[1] PROGMEM;
150 extern bool is_rgblight_initialized;
151
152 // Should stay in sycn with rgb matrix config as we reuse eeprom storage for both (for now)
153 typedef union {
154 uint32_t raw;
155 struct {
156 bool enable : 1;
157 uint8_t mode : 7;
158 uint8_t hue : 8;
159 uint8_t sat : 8;
160 uint8_t val : 8;
161 uint8_t speed : 8; // EECONFIG needs to be increased to support this
162 };
163 } rgblight_config_t;
164
165 typedef struct _rgblight_status_t {
166 uint8_t base_mode;
167 bool timer_enabled;
168 # ifdef RGBLIGHT_SPLIT
169 uint8_t change_flags;
170 # endif
171 } rgblight_status_t;
172
173 /* === Utility Functions ===*/
174 void sethsv(uint8_t hue, uint8_t sat, uint8_t val, LED_TYPE *led1);
175 void sethsv_raw(uint8_t hue, uint8_t sat, uint8_t val, LED_TYPE *led1); // without RGBLIGHT_LIMIT_VAL check
176 void setrgb(uint8_t r, uint8_t g, uint8_t b, LED_TYPE *led1);
177
178 /* === Low level Functions === */
179 void rgblight_set(void);
180 void rgblight_set_clipping_range(uint8_t start_pos, uint8_t num_leds);
181
182 /* === Effects and Animations Functions === */
183 /* effect range setting */
184 void rgblight_set_effect_range(uint8_t start_pos, uint8_t num_leds);
185
186 /* direct operation */
187 void rgblight_setrgb_at(uint8_t r, uint8_t g, uint8_t b, uint8_t index);
188 void rgblight_sethsv_at(uint8_t hue, uint8_t sat, uint8_t val, uint8_t index);
189 void rgblight_setrgb_range(uint8_t r, uint8_t g, uint8_t b, uint8_t start, uint8_t end);
190 void rgblight_sethsv_range(uint8_t hue, uint8_t sat, uint8_t val, uint8_t start, uint8_t end);
191 void rgblight_setrgb(uint8_t r, uint8_t g, uint8_t b);
192
193 # ifndef RGBLIGHT_SPLIT
194 void rgblight_setrgb_master(uint8_t r, uint8_t g, uint8_t b);
195 void rgblight_setrgb_slave(uint8_t r, uint8_t g, uint8_t b);
196 void rgblight_sethsv_master(uint8_t hue, uint8_t sat, uint8_t val);
197 void rgblight_sethsv_slave(uint8_t hue, uint8_t sat, uint8_t val);
198 # endif
199
200 /* effect mode change */
201 void rgblight_mode(uint8_t mode);
202 void rgblight_mode_noeeprom(uint8_t mode);
203 void rgblight_increase(void);
204 void rgblight_decrease(void);
205 void rgblight_step(void);
206 void rgblight_step_noeeprom(void);
207 void rgblight_step_reverse(void);
208 void rgblight_step_reverse_noeeprom(void);
209
210 /* effects mode disable/enable */
211 void rgblight_toggle(void);
212 void rgblight_toggle_noeeprom(void);
213 void rgblight_enable(void);
214 void rgblight_enable_noeeprom(void);
215 void rgblight_disable(void);
216 void rgblight_disable_noeeprom(void);
217
218 /* hue, sat, val change */
219 void rgblight_increase_hue(void);
220 void rgblight_increase_hue_noeeprom(void);
221 void rgblight_decrease_hue(void);
222 void rgblight_decrease_hue_noeeprom(void);
223 void rgblight_increase_sat(void);
224 void rgblight_increase_sat_noeeprom(void);
225 void rgblight_decrease_sat(void);
226 void rgblight_decrease_sat_noeeprom(void);
227 void rgblight_increase_val(void);
228 void rgblight_increase_val_noeeprom(void);
229 void rgblight_decrease_val(void);
230 void rgblight_decrease_val_noeeprom(void);
231 void rgblight_increase_speed(void);
232 void rgblight_decrease_speed(void);
233 void rgblight_sethsv(uint8_t hue, uint8_t sat, uint8_t val);
234 void rgblight_sethsv_noeeprom(uint8_t hue, uint8_t sat, uint8_t val);
235
236 /* effect speed */
237 uint8_t rgblight_get_speed(void);
238 void rgblight_set_speed(uint8_t speed);
239 void rgblight_set_speed_noeeprom(uint8_t speed);
240
241 /* query */
242 uint8_t rgblight_get_mode(void);
243 uint8_t rgblight_get_hue(void);
244 uint8_t rgblight_get_sat(void);
245 uint8_t rgblight_get_val(void);
246
247 /* === qmk_firmware (core)internal Functions === */
248 void rgblight_init(void);
249 uint32_t rgblight_read_dword(void);
250 void rgblight_update_dword(uint32_t dword);
251 uint32_t eeconfig_read_rgblight(void);
252 void eeconfig_update_rgblight(uint32_t val);
253 void eeconfig_update_rgblight_current(void);
254 void eeconfig_update_rgblight_default(void);
255 void eeconfig_debug_rgblight(void);
256
257 void rgb_matrix_increase(void);
258 void rgb_matrix_decrease(void);
259
260 void rgblight_sethsv_eeprom_helper(uint8_t hue, uint8_t sat, uint8_t val, bool write_to_eeprom);
261 void rgblight_mode_eeprom_helper(uint8_t mode, bool write_to_eeprom);
262
263 # define EZ_RGB(val) rgblight_show_solid_color((val >> 16) & 0xFF, (val >> 8) & 0xFF, val & 0xFF)
264 void rgblight_show_solid_color(uint8_t r, uint8_t g, uint8_t b);
265
266 void rgblight_task(void);
267
268 void rgblight_timer_init(void);
269 void rgblight_timer_enable(void);
270 void rgblight_timer_disable(void);
271 void rgblight_timer_toggle(void);
272
273 # ifdef RGBLIGHT_SPLIT
274 # define RGBLIGHT_STATUS_CHANGE_MODE (1 << 0)
275 # define RGBLIGHT_STATUS_CHANGE_HSVS (1 << 1)
276 # define RGBLIGHT_STATUS_CHANGE_TIMER (1 << 2)
277 # define RGBLIGHT_STATUS_ANIMATION_TICK (1 << 3)
278
279 typedef struct _rgblight_syncinfo_t {
280 rgblight_config_t config;
281 rgblight_status_t status;
282 } rgblight_syncinfo_t;
283
284 /* for split keyboard master side */
285 uint8_t rgblight_get_change_flags(void);
286 void rgblight_clear_change_flags(void);
287 void rgblight_get_syncinfo(rgblight_syncinfo_t *syncinfo);
288 /* for split keyboard slave side */
289 void rgblight_update_sync(rgblight_syncinfo_t *syncinfo, bool write_to_eeprom);
290 # endif
291
292 # ifdef RGBLIGHT_USE_TIMER
293
294 typedef struct _animation_status_t {
295 uint16_t last_timer;
296 uint8_t delta; /* mode - base_mode */
297 bool restart;
298 union {
299 uint16_t pos16;
300 uint8_t pos;
301 int8_t current_hue;
302 uint16_t current_offset;
303 };
304 } animation_status_t;
305
306 extern animation_status_t animation_status;
307
308 void rgblight_effect_breathing(animation_status_t *anim);
309 void rgblight_effect_rainbow_mood(animation_status_t *anim);
310 void rgblight_effect_rainbow_swirl(animation_status_t *anim);
311 void rgblight_effect_snake(animation_status_t *anim);
312 void rgblight_effect_knight(animation_status_t *anim);
313 void rgblight_effect_christmas(animation_status_t *anim);
314 void rgblight_effect_rgbtest(animation_status_t *anim);
315 void rgblight_effect_alternating(animation_status_t *anim);
316
317 # endif
318
319 #endif // #ifndef RGBLIGHT_H_DUMMY_DEFINE
320 #endif // RGBLIGHT_H