Commit | Line | Data |
---|---|---|
14b7602a JH |
1 | /* Copyright 2017 Jason Williams |
2 | * Copyright 2017 Jack Humbert | |
fdd0f915 | 3 | * Copyright 2018 Yiancar |
14b7602a JH |
4 | * |
5 | * This program is free software: you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation, either version 2 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 | */ | |
18 | ||
19 | ||
20 | #include "rgb_matrix.h" | |
14b7602a JH |
21 | #include "progmem.h" |
22 | #include "config.h" | |
23 | #include "eeprom.h" | |
14b7602a JH |
24 | #include <math.h> |
25 | ||
26 | rgb_config_t rgb_matrix_config; | |
27 | ||
fdd0f915 | 28 | #ifndef MAX |
29 | #define MAX(X, Y) ((X) > (Y) ? (X) : (Y)) | |
30 | #endif | |
31 | ||
32 | #ifndef MIN | |
33 | #define MIN(a,b) ((a) < (b)? (a): (b)) | |
34 | #endif | |
35 | ||
14b7602a JH |
36 | #ifndef RGB_DISABLE_AFTER_TIMEOUT |
37 | #define RGB_DISABLE_AFTER_TIMEOUT 0 | |
38 | #endif | |
39 | ||
40 | #ifndef RGB_DISABLE_WHEN_USB_SUSPENDED | |
41 | #define RGB_DISABLE_WHEN_USB_SUSPENDED false | |
42 | #endif | |
43 | ||
44 | #ifndef EECONFIG_RGB_MATRIX | |
45 | #define EECONFIG_RGB_MATRIX EECONFIG_RGBLIGHT | |
46 | #endif | |
47 | ||
a7df9027 | 48 | #if !defined(RGB_MATRIX_MAXIMUM_BRIGHTNESS) || RGB_MATRIX_MAXIMUM_BRIGHTNESS > 255 |
49 | #define RGB_MATRIX_MAXIMUM_BRIGHTNESS 255 | |
50 | #endif | |
51 | ||
14b7602a JH |
52 | bool g_suspend_state = false; |
53 | ||
54 | // Global tick at 20 Hz | |
55 | uint32_t g_tick = 0; | |
56 | ||
57 | // Ticks since this key was last hit. | |
58 | uint8_t g_key_hit[DRIVER_LED_TOTAL]; | |
59 | ||
60 | // Ticks since any key was last hit. | |
61 | uint32_t g_any_key_hit = 0; | |
62 | ||
63 | #ifndef PI | |
64 | #define PI 3.14159265 | |
65 | #endif | |
66 | ||
67 | uint32_t eeconfig_read_rgb_matrix(void) { | |
68 | return eeprom_read_dword(EECONFIG_RGB_MATRIX); | |
69 | } | |
70 | void eeconfig_update_rgb_matrix(uint32_t val) { | |
71 | eeprom_update_dword(EECONFIG_RGB_MATRIX, val); | |
72 | } | |
73 | void eeconfig_update_rgb_matrix_default(void) { | |
74 | dprintf("eeconfig_update_rgb_matrix_default\n"); | |
75 | rgb_matrix_config.enable = 1; | |
76 | rgb_matrix_config.mode = RGB_MATRIX_CYCLE_LEFT_RIGHT; | |
77 | rgb_matrix_config.hue = 0; | |
78 | rgb_matrix_config.sat = 255; | |
a7df9027 | 79 | rgb_matrix_config.val = RGB_MATRIX_MAXIMUM_BRIGHTNESS; |
afacd423 | 80 | rgb_matrix_config.speed = 0; |
14b7602a JH |
81 | eeconfig_update_rgb_matrix(rgb_matrix_config.raw); |
82 | } | |
83 | void eeconfig_debug_rgb_matrix(void) { | |
84 | dprintf("rgb_matrix_config eprom\n"); | |
85 | dprintf("rgb_matrix_config.enable = %d\n", rgb_matrix_config.enable); | |
86 | dprintf("rgb_matrix_config.mode = %d\n", rgb_matrix_config.mode); | |
87 | dprintf("rgb_matrix_config.hue = %d\n", rgb_matrix_config.hue); | |
88 | dprintf("rgb_matrix_config.sat = %d\n", rgb_matrix_config.sat); | |
89 | dprintf("rgb_matrix_config.val = %d\n", rgb_matrix_config.val); | |
afacd423 | 90 | dprintf("rgb_matrix_config.speed = %d\n", rgb_matrix_config.speed); |
14b7602a JH |
91 | } |
92 | ||
93 | // Last led hit | |
94 | #define LED_HITS_TO_REMEMBER 8 | |
95 | uint8_t g_last_led_hit[LED_HITS_TO_REMEMBER] = {255}; | |
96 | uint8_t g_last_led_count = 0; | |
97 | ||
98 | void map_row_column_to_led( uint8_t row, uint8_t column, uint8_t *led_i, uint8_t *led_count) { | |
99 | rgb_led led; | |
100 | *led_count = 0; | |
101 | ||
102 | for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) { | |
103 | // map_index_to_led(i, &led); | |
104 | led = g_rgb_leds[i]; | |
105 | if (row == led.matrix_co.row && column == led.matrix_co.col) { | |
106 | led_i[*led_count] = i; | |
107 | (*led_count)++; | |
108 | } | |
109 | } | |
110 | } | |
111 | ||
14b7602a | 112 | void rgb_matrix_update_pwm_buffers(void) { |
f70f45ee | 113 | rgb_matrix_driver.flush(); |
14b7602a JH |
114 | } |
115 | ||
116 | void rgb_matrix_set_color( int index, uint8_t red, uint8_t green, uint8_t blue ) { | |
f70f45ee | 117 | rgb_matrix_driver.set_color(index, red, green, blue); |
14b7602a JH |
118 | } |
119 | ||
120 | void rgb_matrix_set_color_all( uint8_t red, uint8_t green, uint8_t blue ) { | |
f70f45ee | 121 | rgb_matrix_driver.set_color_all(red, green, blue); |
14b7602a JH |
122 | } |
123 | ||
14b7602a JH |
124 | bool process_rgb_matrix(uint16_t keycode, keyrecord_t *record) { |
125 | if ( record->event.pressed ) { | |
126 | uint8_t led[8], led_count; | |
127 | map_row_column_to_led(record->event.key.row, record->event.key.col, led, &led_count); | |
128 | if (led_count > 0) { | |
129 | for (uint8_t i = LED_HITS_TO_REMEMBER; i > 1; i--) { | |
130 | g_last_led_hit[i - 1] = g_last_led_hit[i - 2]; | |
131 | } | |
132 | g_last_led_hit[0] = led[0]; | |
133 | g_last_led_count = MIN(LED_HITS_TO_REMEMBER, g_last_led_count + 1); | |
134 | } | |
135 | for(uint8_t i = 0; i < led_count; i++) | |
136 | g_key_hit[led[i]] = 0; | |
137 | g_any_key_hit = 0; | |
138 | } else { | |
139 | #ifdef RGB_MATRIX_KEYRELEASES | |
140 | uint8_t led[8], led_count; | |
141 | map_row_column_to_led(record->event.key.row, record->event.key.col, led, &led_count); | |
142 | for(uint8_t i = 0; i < led_count; i++) | |
143 | g_key_hit[led[i]] = 255; | |
144 | ||
145 | g_any_key_hit = 255; | |
146 | #endif | |
147 | } | |
148 | return true; | |
149 | } | |
150 | ||
151 | void rgb_matrix_set_suspend_state(bool state) { | |
152 | g_suspend_state = state; | |
153 | } | |
154 | ||
155 | void rgb_matrix_test(void) { | |
156 | // Mask out bits 4 and 5 | |
21ad968a EZ |
157 | // Increase the factor to make the test animation slower (and reduce to make it faster) |
158 | uint8_t factor = 10; | |
159 | switch ( (g_tick & (0b11 << factor)) >> factor ) | |
14b7602a JH |
160 | { |
161 | case 0: | |
162 | { | |
163 | rgb_matrix_set_color_all( 20, 0, 0 ); | |
164 | break; | |
165 | } | |
166 | case 1: | |
167 | { | |
168 | rgb_matrix_set_color_all( 0, 20, 0 ); | |
169 | break; | |
170 | } | |
171 | case 2: | |
172 | { | |
173 | rgb_matrix_set_color_all( 0, 0, 20 ); | |
174 | break; | |
175 | } | |
176 | case 3: | |
177 | { | |
178 | rgb_matrix_set_color_all( 20, 20, 20 ); | |
179 | break; | |
180 | } | |
181 | } | |
182 | } | |
183 | ||
14b7602a | 184 | // All LEDs off |
bad56a4f | 185 | void rgb_matrix_all_off(void) { |
14b7602a JH |
186 | rgb_matrix_set_color_all( 0, 0, 0 ); |
187 | } | |
188 | ||
189 | // Solid color | |
190 | void rgb_matrix_solid_color(void) { | |
191 | HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val }; | |
192 | RGB rgb = hsv_to_rgb( hsv ); | |
193 | rgb_matrix_set_color_all( rgb.r, rgb.g, rgb.b ); | |
194 | } | |
195 | ||
196 | void rgb_matrix_solid_reactive(void) { | |
197 | // Relies on hue being 8-bit and wrapping | |
198 | for ( int i=0; i<DRIVER_LED_TOTAL; i++ ) | |
199 | { | |
200 | uint16_t offset2 = g_key_hit[i]<<2; | |
201 | offset2 = (offset2<=130) ? (130-offset2) : 0; | |
202 | ||
203 | HSV hsv = { .h = rgb_matrix_config.hue+offset2, .s = 255, .v = rgb_matrix_config.val }; | |
204 | RGB rgb = hsv_to_rgb( hsv ); | |
205 | rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b ); | |
206 | } | |
207 | } | |
208 | ||
209 | // alphas = color1, mods = color2 | |
210 | void rgb_matrix_alphas_mods(void) { | |
bad56a4f | 211 | |
14b7602a JH |
212 | RGB rgb1 = hsv_to_rgb( (HSV){ .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val } ); |
213 | RGB rgb2 = hsv_to_rgb( (HSV){ .h = (rgb_matrix_config.hue + 180) % 360, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val } ); | |
214 | ||
215 | rgb_led led; | |
216 | for (int i = 0; i < DRIVER_LED_TOTAL; i++) { | |
217 | led = g_rgb_leds[i]; | |
218 | if ( led.matrix_co.raw < 0xFF ) { | |
219 | if ( led.modifier ) | |
220 | { | |
221 | rgb_matrix_set_color( i, rgb2.r, rgb2.g, rgb2.b ); | |
222 | } | |
223 | else | |
224 | { | |
225 | rgb_matrix_set_color( i, rgb1.r, rgb1.g, rgb1.b ); | |
226 | } | |
227 | } | |
228 | } | |
229 | } | |
230 | ||
231 | void rgb_matrix_gradient_up_down(void) { | |
232 | int16_t h1 = rgb_matrix_config.hue; | |
233 | int16_t h2 = (rgb_matrix_config.hue + 180) % 360; | |
234 | int16_t deltaH = h2 - h1; | |
235 | ||
236 | // Take the shortest path between hues | |
237 | if ( deltaH > 127 ) | |
238 | { | |
239 | deltaH -= 256; | |
240 | } | |
241 | else if ( deltaH < -127 ) | |
242 | { | |
243 | deltaH += 256; | |
244 | } | |
245 | // Divide delta by 4, this gives the delta per row | |
246 | deltaH /= 4; | |
247 | ||
248 | int16_t s1 = rgb_matrix_config.sat; | |
249 | int16_t s2 = rgb_matrix_config.hue; | |
250 | int16_t deltaS = ( s2 - s1 ) / 4; | |
251 | ||
252 | HSV hsv = { .h = 0, .s = 255, .v = rgb_matrix_config.val }; | |
253 | RGB rgb; | |
254 | Point point; | |
255 | for ( int i=0; i<DRIVER_LED_TOTAL; i++ ) | |
256 | { | |
257 | // map_led_to_point( i, &point ); | |
258 | point = g_rgb_leds[i].point; | |
259 | // The y range will be 0..64, map this to 0..4 | |
260 | uint8_t y = (point.y>>4); | |
261 | // Relies on hue being 8-bit and wrapping | |
262 | hsv.h = rgb_matrix_config.hue + ( deltaH * y ); | |
263 | hsv.s = rgb_matrix_config.sat + ( deltaS * y ); | |
264 | rgb = hsv_to_rgb( hsv ); | |
265 | rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b ); | |
266 | } | |
267 | } | |
268 | ||
269 | void rgb_matrix_raindrops(bool initialize) { | |
270 | int16_t h1 = rgb_matrix_config.hue; | |
271 | int16_t h2 = (rgb_matrix_config.hue + 180) % 360; | |
272 | int16_t deltaH = h2 - h1; | |
273 | deltaH /= 4; | |
274 | ||
275 | // Take the shortest path between hues | |
276 | if ( deltaH > 127 ) | |
277 | { | |
278 | deltaH -= 256; | |
279 | } | |
280 | else if ( deltaH < -127 ) | |
281 | { | |
282 | deltaH += 256; | |
283 | } | |
284 | ||
285 | int16_t s1 = rgb_matrix_config.sat; | |
286 | int16_t s2 = rgb_matrix_config.sat; | |
287 | int16_t deltaS = ( s2 - s1 ) / 4; | |
288 | ||
289 | HSV hsv; | |
290 | RGB rgb; | |
291 | ||
c89565cc | 292 | // Change one LED every tick, make sure speed is not 0 |
293 | uint8_t led_to_change = ( g_tick & ( 0x0A / (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed) ) ) == 0 ? rand() % (DRIVER_LED_TOTAL) : 255; | |
14b7602a JH |
294 | |
295 | for ( int i=0; i<DRIVER_LED_TOTAL; i++ ) | |
296 | { | |
297 | // If initialize, all get set to random colors | |
298 | // If not, all but one will stay the same as before. | |
299 | if ( initialize || i == led_to_change ) | |
300 | { | |
301 | hsv.h = h1 + ( deltaH * ( rand() & 0x03 ) ); | |
302 | hsv.s = s1 + ( deltaS * ( rand() & 0x03 ) ); | |
303 | // Override brightness with global brightness control | |
304 | hsv.v = rgb_matrix_config.val; | |
305 | ||
306 | rgb = hsv_to_rgb( hsv ); | |
307 | rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b ); | |
308 | } | |
309 | } | |
310 | } | |
311 | ||
312 | void rgb_matrix_cycle_all(void) { | |
afacd423 | 313 | uint8_t offset = ( g_tick << rgb_matrix_config.speed ) & 0xFF; |
14b7602a JH |
314 | |
315 | rgb_led led; | |
316 | ||
317 | // Relies on hue being 8-bit and wrapping | |
318 | for ( int i=0; i<DRIVER_LED_TOTAL; i++ ) | |
319 | { | |
320 | // map_index_to_led(i, &led); | |
321 | led = g_rgb_leds[i]; | |
322 | if (led.matrix_co.raw < 0xFF) { | |
323 | uint16_t offset2 = g_key_hit[i]<<2; | |
324 | offset2 = (offset2<=63) ? (63-offset2) : 0; | |
325 | ||
326 | HSV hsv = { .h = offset+offset2, .s = 255, .v = rgb_matrix_config.val }; | |
327 | RGB rgb = hsv_to_rgb( hsv ); | |
328 | rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b ); | |
329 | } | |
330 | } | |
331 | } | |
332 | ||
333 | void rgb_matrix_cycle_left_right(void) { | |
afacd423 | 334 | uint8_t offset = ( g_tick << rgb_matrix_config.speed ) & 0xFF; |
14b7602a JH |
335 | HSV hsv = { .h = 0, .s = 255, .v = rgb_matrix_config.val }; |
336 | RGB rgb; | |
337 | Point point; | |
338 | rgb_led led; | |
339 | for ( int i=0; i<DRIVER_LED_TOTAL; i++ ) | |
340 | { | |
341 | // map_index_to_led(i, &led); | |
342 | led = g_rgb_leds[i]; | |
343 | if (led.matrix_co.raw < 0xFF) { | |
344 | uint16_t offset2 = g_key_hit[i]<<2; | |
345 | offset2 = (offset2<=63) ? (63-offset2) : 0; | |
346 | ||
347 | // map_led_to_point( i, &point ); | |
348 | point = g_rgb_leds[i].point; | |
349 | // Relies on hue being 8-bit and wrapping | |
350 | hsv.h = point.x + offset + offset2; | |
351 | rgb = hsv_to_rgb( hsv ); | |
352 | rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b ); | |
353 | } | |
354 | } | |
355 | } | |
356 | ||
357 | void rgb_matrix_cycle_up_down(void) { | |
afacd423 | 358 | uint8_t offset = ( g_tick << rgb_matrix_config.speed ) & 0xFF; |
14b7602a JH |
359 | HSV hsv = { .h = 0, .s = 255, .v = rgb_matrix_config.val }; |
360 | RGB rgb; | |
361 | Point point; | |
362 | rgb_led led; | |
363 | for ( int i=0; i<DRIVER_LED_TOTAL; i++ ) | |
364 | { | |
365 | // map_index_to_led(i, &led); | |
366 | led = g_rgb_leds[i]; | |
367 | if (led.matrix_co.raw < 0xFF) { | |
368 | uint16_t offset2 = g_key_hit[i]<<2; | |
369 | offset2 = (offset2<=63) ? (63-offset2) : 0; | |
370 | ||
371 | // map_led_to_point( i, &point ); | |
372 | point = g_rgb_leds[i].point; | |
373 | // Relies on hue being 8-bit and wrapping | |
374 | hsv.h = point.y + offset + offset2; | |
375 | rgb = hsv_to_rgb( hsv ); | |
376 | rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b ); | |
377 | } | |
378 | } | |
379 | } | |
380 | ||
381 | ||
382 | void rgb_matrix_dual_beacon(void) { | |
383 | HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val }; | |
384 | RGB rgb; | |
c7ce0d21 FD |
385 | Point point; |
386 | double cos_value = cos(g_tick * PI / 128) / 32; | |
387 | double sin_value = sin(g_tick * PI / 128) / 112; | |
14b7602a | 388 | for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) { |
c7ce0d21 FD |
389 | point = g_rgb_leds[i].point; |
390 | hsv.h = ((point.y - 32.0)* cos_value + (point.x - 112.0) * sin_value) * (180) + rgb_matrix_config.hue; | |
14b7602a JH |
391 | rgb = hsv_to_rgb( hsv ); |
392 | rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b ); | |
393 | } | |
394 | } | |
395 | ||
396 | void rgb_matrix_rainbow_beacon(void) { | |
397 | HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val }; | |
398 | RGB rgb; | |
c7ce0d21 FD |
399 | Point point; |
400 | double cos_value = cos(g_tick * PI / 128); | |
401 | double sin_value = sin(g_tick * PI / 128); | |
14b7602a | 402 | for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) { |
c7ce0d21 FD |
403 | point = g_rgb_leds[i].point; |
404 | hsv.h = (1.5 * (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed)) * (point.y - 32.0)* cos_value + (1.5 * (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed)) * (point.x - 112.0) * sin_value + rgb_matrix_config.hue; | |
14b7602a JH |
405 | rgb = hsv_to_rgb( hsv ); |
406 | rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b ); | |
407 | } | |
408 | } | |
409 | ||
410 | void rgb_matrix_rainbow_pinwheels(void) { | |
411 | HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val }; | |
412 | RGB rgb; | |
c7ce0d21 FD |
413 | Point point; |
414 | double cos_value = cos(g_tick * PI / 128); | |
415 | double sin_value = sin(g_tick * PI / 128); | |
14b7602a | 416 | for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) { |
c7ce0d21 FD |
417 | point = g_rgb_leds[i].point; |
418 | hsv.h = (2 * (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed)) * (point.y - 32.0)* cos_value + (2 * (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed)) * (66 - abs(point.x - 112.0)) * sin_value + rgb_matrix_config.hue; | |
14b7602a JH |
419 | rgb = hsv_to_rgb( hsv ); |
420 | rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b ); | |
421 | } | |
422 | } | |
423 | ||
424 | void rgb_matrix_rainbow_moving_chevron(void) { | |
425 | HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val }; | |
426 | RGB rgb; | |
c7ce0d21 FD |
427 | Point point; |
428 | uint8_t r = 128; | |
429 | double cos_value = cos(r * PI / 128); | |
430 | double sin_value = sin(r * PI / 128); | |
431 | double multiplier = (g_tick / 256.0 * 224); | |
14b7602a | 432 | for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) { |
c7ce0d21 FD |
433 | point = g_rgb_leds[i].point; |
434 | hsv.h = (1.5 * (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed)) * abs(point.y - 32.0)* sin_value + (1.5 * (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed)) * (point.x - multiplier) * cos_value + rgb_matrix_config.hue; | |
14b7602a JH |
435 | rgb = hsv_to_rgb( hsv ); |
436 | rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b ); | |
437 | } | |
438 | } | |
439 | ||
440 | ||
441 | void rgb_matrix_jellybean_raindrops( bool initialize ) { | |
442 | HSV hsv; | |
443 | RGB rgb; | |
444 | ||
c89565cc | 445 | // Change one LED every tick, make sure speed is not 0 |
446 | uint8_t led_to_change = ( g_tick & ( 0x0A / (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed) ) ) == 0 ? rand() % (DRIVER_LED_TOTAL) : 255; | |
14b7602a JH |
447 | |
448 | for ( int i=0; i<DRIVER_LED_TOTAL; i++ ) | |
449 | { | |
450 | // If initialize, all get set to random colors | |
451 | // If not, all but one will stay the same as before. | |
452 | if ( initialize || i == led_to_change ) | |
453 | { | |
454 | hsv.h = rand() & 0xFF; | |
455 | hsv.s = rand() & 0xFF; | |
456 | // Override brightness with global brightness control | |
457 | hsv.v = rgb_matrix_config.val; | |
458 | ||
459 | rgb = hsv_to_rgb( hsv ); | |
460 | rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b ); | |
461 | } | |
462 | } | |
463 | } | |
464 | ||
4d5705ea DS |
465 | void rgb_matrix_digital_rain( const bool initialize ) { |
466 | // algorithm ported from https://github.com/tremby/Kaleidoscope-LEDEffect-DigitalRain | |
467 | const uint8_t drop_ticks = 28; | |
468 | const uint8_t new_drop_probability = 24; | |
469 | const uint8_t pure_green_intensity = 0xd0; | |
470 | const uint8_t max_brightness_boost = 0xc0; | |
471 | const uint8_t max_intensity = 0xff; | |
472 | ||
473 | static uint8_t map[MATRIX_COLS][MATRIX_ROWS] = {{0}}; | |
474 | static uint8_t drop = 0; | |
475 | ||
476 | if (initialize) { | |
477 | rgb_matrix_set_color_all(0, 0, 0); | |
478 | memset(map, 0, sizeof map); | |
479 | drop = 0; | |
480 | } | |
481 | for (uint8_t col = 0; col < MATRIX_COLS; col++) { | |
482 | for (uint8_t row = 0; row < MATRIX_ROWS; row++) { | |
483 | if (row == 0 && drop == 0 && rand() < RAND_MAX / new_drop_probability) { | |
484 | // top row, pixels have just fallen and we're | |
485 | // making a new rain drop in this column | |
486 | map[col][row] = max_intensity; | |
487 | } | |
488 | else if (map[col][row] > 0 && map[col][row] < max_intensity) { | |
489 | // neither fully bright nor dark, decay it | |
490 | map[col][row]--; | |
491 | } | |
492 | // set the pixel colour | |
493 | uint8_t led, led_count; | |
494 | map_row_column_to_led(row, col, &led, &led_count); | |
495 | ||
496 | if (map[col][row] > pure_green_intensity) { | |
497 | const uint8_t boost = (uint8_t) ((uint16_t) max_brightness_boost | |
498 | * (map[col][row] - pure_green_intensity) / (max_intensity - pure_green_intensity)); | |
499 | rgb_matrix_set_color(led, boost, max_intensity, boost); | |
500 | } | |
501 | else { | |
502 | const uint8_t green = (uint8_t) ((uint16_t) max_intensity * map[col][row] / pure_green_intensity); | |
503 | rgb_matrix_set_color(led, 0, green, 0); | |
504 | } | |
505 | } | |
506 | } | |
507 | if (++drop > drop_ticks) { | |
508 | // reset drop timer | |
509 | drop = 0; | |
510 | for (uint8_t row = MATRIX_ROWS - 1; row > 0; row--) { | |
511 | for (uint8_t col = 0; col < MATRIX_COLS; col++) { | |
512 | // if ths is on the bottom row and bright allow decay | |
513 | if (row == MATRIX_ROWS - 1 && map[col][row] == max_intensity) { | |
514 | map[col][row]--; | |
515 | } | |
516 | // check if the pixel above is bright | |
517 | if (map[col][row - 1] == max_intensity) { | |
518 | // allow old bright pixel to decay | |
519 | map[col][row - 1]--; | |
520 | // make this pixel bright | |
521 | map[col][row] = max_intensity; | |
522 | } | |
523 | } | |
524 | } | |
525 | } | |
526 | } | |
527 | ||
14b7602a JH |
528 | void rgb_matrix_multisplash(void) { |
529 | // if (g_any_key_hit < 0xFF) { | |
530 | HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val }; | |
531 | RGB rgb; | |
532 | rgb_led led; | |
533 | for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) { | |
534 | led = g_rgb_leds[i]; | |
535 | uint16_t c = 0, d = 0; | |
536 | rgb_led last_led; | |
537 | // if (g_last_led_count) { | |
538 | for (uint8_t last_i = 0; last_i < g_last_led_count; last_i++) { | |
539 | last_led = g_rgb_leds[g_last_led_hit[last_i]]; | |
540 | uint16_t dist = (uint16_t)sqrt(pow(led.point.x - last_led.point.x, 2) + pow(led.point.y - last_led.point.y, 2)); | |
541 | uint16_t effect = (g_key_hit[g_last_led_hit[last_i]] << 2) - dist; | |
542 | c += MIN(MAX(effect, 0), 255); | |
543 | d += 255 - MIN(MAX(effect, 0), 255); | |
544 | } | |
545 | // } else { | |
546 | // d = 255; | |
547 | // } | |
548 | hsv.h = (rgb_matrix_config.hue + c) % 256; | |
549 | hsv.v = MAX(MIN(d, 255), 0); | |
550 | rgb = hsv_to_rgb( hsv ); | |
551 | rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b ); | |
552 | } | |
553 | // } else { | |
554 | // rgb_matrix_set_color_all( 0, 0, 0 ); | |
555 | // } | |
556 | } | |
557 | ||
558 | ||
559 | void rgb_matrix_splash(void) { | |
560 | g_last_led_count = MIN(g_last_led_count, 1); | |
561 | rgb_matrix_multisplash(); | |
562 | } | |
563 | ||
564 | ||
565 | void rgb_matrix_solid_multisplash(void) { | |
566 | // if (g_any_key_hit < 0xFF) { | |
567 | HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val }; | |
568 | RGB rgb; | |
569 | rgb_led led; | |
570 | for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) { | |
571 | led = g_rgb_leds[i]; | |
572 | uint16_t d = 0; | |
573 | rgb_led last_led; | |
574 | // if (g_last_led_count) { | |
575 | for (uint8_t last_i = 0; last_i < g_last_led_count; last_i++) { | |
576 | last_led = g_rgb_leds[g_last_led_hit[last_i]]; | |
577 | uint16_t dist = (uint16_t)sqrt(pow(led.point.x - last_led.point.x, 2) + pow(led.point.y - last_led.point.y, 2)); | |
578 | uint16_t effect = (g_key_hit[g_last_led_hit[last_i]] << 2) - dist; | |
579 | d += 255 - MIN(MAX(effect, 0), 255); | |
580 | } | |
581 | // } else { | |
582 | // d = 255; | |
583 | // } | |
584 | hsv.v = MAX(MIN(d, 255), 0); | |
585 | rgb = hsv_to_rgb( hsv ); | |
586 | rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b ); | |
587 | } | |
588 | // } else { | |
589 | // rgb_matrix_set_color_all( 0, 0, 0 ); | |
590 | // } | |
591 | } | |
592 | ||
593 | ||
594 | void rgb_matrix_solid_splash(void) { | |
595 | g_last_led_count = MIN(g_last_led_count, 1); | |
596 | rgb_matrix_solid_multisplash(); | |
597 | } | |
598 | ||
599 | ||
600 | // Needs eeprom access that we don't have setup currently | |
601 | ||
602 | void rgb_matrix_custom(void) { | |
603 | // HSV hsv; | |
604 | // RGB rgb; | |
605 | // for ( int i=0; i<DRIVER_LED_TOTAL; i++ ) | |
606 | // { | |
607 | // backlight_get_key_color(i, &hsv); | |
608 | // // Override brightness with global brightness control | |
609 | // hsv.v = rgb_matrix_config.val; | |
610 | // rgb = hsv_to_rgb( hsv ); | |
611 | // rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b ); | |
612 | // } | |
613 | } | |
614 | ||
615 | void rgb_matrix_task(void) { | |
c89565cc | 616 | static uint8_t toggle_enable_last = 255; |
14b7602a JH |
617 | if (!rgb_matrix_config.enable) { |
618 | rgb_matrix_all_off(); | |
c89565cc | 619 | toggle_enable_last = rgb_matrix_config.enable; |
14b7602a JH |
620 | return; |
621 | } | |
622 | // delay 1 second before driving LEDs or doing anything else | |
623 | static uint8_t startup_tick = 0; | |
624 | if ( startup_tick < 20 ) { | |
625 | startup_tick++; | |
626 | return; | |
627 | } | |
628 | ||
629 | g_tick++; | |
630 | ||
631 | if ( g_any_key_hit < 0xFFFFFFFF ) { | |
632 | g_any_key_hit++; | |
633 | } | |
634 | ||
635 | for ( int led = 0; led < DRIVER_LED_TOTAL; led++ ) { | |
636 | if ( g_key_hit[led] < 255 ) { | |
637 | if (g_key_hit[led] == 254) | |
638 | g_last_led_count = MAX(g_last_led_count - 1, 0); | |
639 | g_key_hit[led]++; | |
640 | } | |
641 | } | |
642 | ||
643 | // Factory default magic value | |
644 | if ( rgb_matrix_config.mode == 255 ) { | |
645 | rgb_matrix_test(); | |
646 | return; | |
647 | } | |
648 | ||
649 | // Ideally we would also stop sending zeros to the LED driver PWM buffers | |
650 | // while suspended and just do a software shutdown. This is a cheap hack for now. | |
651 | bool suspend_backlight = ((g_suspend_state && RGB_DISABLE_WHEN_USB_SUSPENDED) || | |
652 | (RGB_DISABLE_AFTER_TIMEOUT > 0 && g_any_key_hit > RGB_DISABLE_AFTER_TIMEOUT * 60 * 20)); | |
653 | uint8_t effect = suspend_backlight ? 0 : rgb_matrix_config.mode; | |
654 | ||
655 | // Keep track of the effect used last time, | |
656 | // detect change in effect, so each effect can | |
657 | // have an optional initialization. | |
658 | static uint8_t effect_last = 255; | |
c89565cc | 659 | bool initialize = (effect != effect_last) || (rgb_matrix_config.enable != toggle_enable_last); |
14b7602a | 660 | effect_last = effect; |
c89565cc | 661 | toggle_enable_last = rgb_matrix_config.enable; |
14b7602a JH |
662 | |
663 | // this gets ticked at 20 Hz. | |
664 | // each effect can opt to do calculations | |
665 | // and/or request PWM buffer updates. | |
666 | switch ( effect ) { | |
667 | case RGB_MATRIX_SOLID_COLOR: | |
668 | rgb_matrix_solid_color(); | |
669 | break; | |
14b7602a JH |
670 | case RGB_MATRIX_ALPHAS_MODS: |
671 | rgb_matrix_alphas_mods(); | |
672 | break; | |
673 | case RGB_MATRIX_DUAL_BEACON: | |
674 | rgb_matrix_dual_beacon(); | |
675 | break; | |
676 | case RGB_MATRIX_GRADIENT_UP_DOWN: | |
677 | rgb_matrix_gradient_up_down(); | |
678 | break; | |
679 | case RGB_MATRIX_RAINDROPS: | |
680 | rgb_matrix_raindrops( initialize ); | |
681 | break; | |
682 | case RGB_MATRIX_CYCLE_ALL: | |
683 | rgb_matrix_cycle_all(); | |
684 | break; | |
685 | case RGB_MATRIX_CYCLE_LEFT_RIGHT: | |
686 | rgb_matrix_cycle_left_right(); | |
687 | break; | |
688 | case RGB_MATRIX_CYCLE_UP_DOWN: | |
689 | rgb_matrix_cycle_up_down(); | |
690 | break; | |
691 | case RGB_MATRIX_RAINBOW_BEACON: | |
692 | rgb_matrix_rainbow_beacon(); | |
693 | break; | |
694 | case RGB_MATRIX_RAINBOW_PINWHEELS: | |
695 | rgb_matrix_rainbow_pinwheels(); | |
696 | break; | |
697 | case RGB_MATRIX_RAINBOW_MOVING_CHEVRON: | |
698 | rgb_matrix_rainbow_moving_chevron(); | |
699 | break; | |
700 | case RGB_MATRIX_JELLYBEAN_RAINDROPS: | |
701 | rgb_matrix_jellybean_raindrops( initialize ); | |
702 | break; | |
4d5705ea DS |
703 | case RGB_MATRIX_DIGITAL_RAIN: |
704 | rgb_matrix_digital_rain( initialize ); | |
705 | break; | |
14b7602a | 706 | #ifdef RGB_MATRIX_KEYPRESSES |
c89565cc | 707 | case RGB_MATRIX_SOLID_REACTIVE: |
708 | rgb_matrix_solid_reactive(); | |
709 | break; | |
14b7602a JH |
710 | case RGB_MATRIX_SPLASH: |
711 | rgb_matrix_splash(); | |
712 | break; | |
713 | case RGB_MATRIX_MULTISPLASH: | |
714 | rgb_matrix_multisplash(); | |
715 | break; | |
716 | case RGB_MATRIX_SOLID_SPLASH: | |
717 | rgb_matrix_solid_splash(); | |
718 | break; | |
719 | case RGB_MATRIX_SOLID_MULTISPLASH: | |
720 | rgb_matrix_solid_multisplash(); | |
721 | break; | |
722 | #endif | |
723 | default: | |
724 | rgb_matrix_custom(); | |
725 | break; | |
726 | } | |
727 | ||
728 | if ( ! suspend_backlight ) { | |
729 | rgb_matrix_indicators(); | |
730 | } | |
731 | ||
732 | } | |
733 | ||
734 | void rgb_matrix_indicators(void) { | |
735 | rgb_matrix_indicators_kb(); | |
736 | rgb_matrix_indicators_user(); | |
737 | } | |
738 | ||
739 | __attribute__((weak)) | |
740 | void rgb_matrix_indicators_kb(void) {} | |
741 | ||
742 | __attribute__((weak)) | |
743 | void rgb_matrix_indicators_user(void) {} | |
744 | ||
745 | ||
746 | // void rgb_matrix_set_indicator_index( uint8_t *index, uint8_t row, uint8_t column ) | |
747 | // { | |
748 | // if ( row >= MATRIX_ROWS ) | |
749 | // { | |
750 | // // Special value, 255=none, 254=all | |
751 | // *index = row; | |
752 | // } | |
753 | // else | |
754 | // { | |
755 | // // This needs updated to something like | |
756 | // // uint8_t led[8], led_count; | |
757 | // // map_row_column_to_led(row,column,led,&led_count); | |
758 | // // for(uint8_t i = 0; i < led_count; i++) | |
759 | // map_row_column_to_led( row, column, index ); | |
760 | // } | |
761 | // } | |
762 | ||
bad56a4f | 763 | void rgb_matrix_init(void) { |
f70f45ee | 764 | rgb_matrix_driver.init(); |
bad56a4f JH |
765 | |
766 | // TODO: put the 1 second startup delay here? | |
767 | ||
768 | // clear the key hits | |
769 | for ( int led=0; led<DRIVER_LED_TOTAL; led++ ) { | |
770 | g_key_hit[led] = 255; | |
771 | } | |
772 | ||
773 | ||
774 | if (!eeconfig_is_enabled()) { | |
775 | dprintf("rgb_matrix_init_drivers eeconfig is not enabled.\n"); | |
776 | eeconfig_init(); | |
777 | eeconfig_update_rgb_matrix_default(); | |
778 | } | |
779 | rgb_matrix_config.raw = eeconfig_read_rgb_matrix(); | |
780 | if (!rgb_matrix_config.mode) { | |
781 | dprintf("rgb_matrix_init_drivers rgb_matrix_config.mode = 0. Write default values to EEPROM.\n"); | |
782 | eeconfig_update_rgb_matrix_default(); | |
783 | rgb_matrix_config.raw = eeconfig_read_rgb_matrix(); | |
784 | } | |
785 | eeconfig_debug_rgb_matrix(); // display current eeprom values | |
786 | } | |
787 | ||
14b7602a JH |
788 | // Deals with the messy details of incrementing an integer |
789 | uint8_t increment( uint8_t value, uint8_t step, uint8_t min, uint8_t max ) { | |
790 | int16_t new_value = value; | |
791 | new_value += step; | |
792 | return MIN( MAX( new_value, min ), max ); | |
793 | } | |
794 | ||
795 | uint8_t decrement( uint8_t value, uint8_t step, uint8_t min, uint8_t max ) { | |
796 | int16_t new_value = value; | |
797 | new_value -= step; | |
798 | return MIN( MAX( new_value, min ), max ); | |
799 | } | |
800 | ||
801 | // void *backlight_get_custom_key_color_eeprom_address( uint8_t led ) | |
802 | // { | |
803 | // // 3 bytes per color | |
804 | // return EECONFIG_RGB_MATRIX + ( led * 3 ); | |
805 | // } | |
806 | ||
807 | // void backlight_get_key_color( uint8_t led, HSV *hsv ) | |
808 | // { | |
809 | // void *address = backlight_get_custom_key_color_eeprom_address( led ); | |
810 | // hsv->h = eeprom_read_byte(address); | |
811 | // hsv->s = eeprom_read_byte(address+1); | |
812 | // hsv->v = eeprom_read_byte(address+2); | |
813 | // } | |
814 | ||
815 | // void backlight_set_key_color( uint8_t row, uint8_t column, HSV hsv ) | |
816 | // { | |
817 | // uint8_t led[8], led_count; | |
818 | // map_row_column_to_led(row,column,led,&led_count); | |
819 | // for(uint8_t i = 0; i < led_count; i++) { | |
820 | // if ( led[i] < DRIVER_LED_TOTAL ) | |
821 | // { | |
822 | // void *address = backlight_get_custom_key_color_eeprom_address(led[i]); | |
823 | // eeprom_update_byte(address, hsv.h); | |
824 | // eeprom_update_byte(address+1, hsv.s); | |
825 | // eeprom_update_byte(address+2, hsv.v); | |
826 | // } | |
827 | // } | |
828 | // } | |
829 | ||
14b7602a JH |
830 | uint32_t rgb_matrix_get_tick(void) { |
831 | return g_tick; | |
832 | } | |
833 | ||
834 | void rgblight_toggle(void) { | |
835 | rgb_matrix_config.enable ^= 1; | |
836 | eeconfig_update_rgb_matrix(rgb_matrix_config.raw); | |
837 | } | |
838 | ||
839 | void rgblight_step(void) { | |
840 | rgb_matrix_config.mode++; | |
841 | if (rgb_matrix_config.mode >= RGB_MATRIX_EFFECT_MAX) | |
842 | rgb_matrix_config.mode = 1; | |
843 | eeconfig_update_rgb_matrix(rgb_matrix_config.raw); | |
844 | } | |
845 | ||
846 | void rgblight_step_reverse(void) { | |
847 | rgb_matrix_config.mode--; | |
c89565cc | 848 | if (rgb_matrix_config.mode < 1) |
849 | rgb_matrix_config.mode = RGB_MATRIX_EFFECT_MAX - 1; | |
14b7602a JH |
850 | eeconfig_update_rgb_matrix(rgb_matrix_config.raw); |
851 | } | |
852 | ||
853 | void rgblight_increase_hue(void) { | |
854 | rgb_matrix_config.hue = increment( rgb_matrix_config.hue, 8, 0, 255 ); | |
855 | eeconfig_update_rgb_matrix(rgb_matrix_config.raw); | |
856 | } | |
857 | ||
858 | void rgblight_decrease_hue(void) { | |
859 | rgb_matrix_config.hue = decrement( rgb_matrix_config.hue, 8, 0, 255 ); | |
860 | eeconfig_update_rgb_matrix(rgb_matrix_config.raw); | |
861 | } | |
862 | ||
863 | void rgblight_increase_sat(void) { | |
864 | rgb_matrix_config.sat = increment( rgb_matrix_config.sat, 8, 0, 255 ); | |
865 | eeconfig_update_rgb_matrix(rgb_matrix_config.raw); | |
866 | } | |
867 | ||
868 | void rgblight_decrease_sat(void) { | |
869 | rgb_matrix_config.sat = decrement( rgb_matrix_config.sat, 8, 0, 255 ); | |
870 | eeconfig_update_rgb_matrix(rgb_matrix_config.raw); | |
871 | } | |
872 | ||
873 | void rgblight_increase_val(void) { | |
a7df9027 | 874 | rgb_matrix_config.val = increment( rgb_matrix_config.val, 8, 0, RGB_MATRIX_MAXIMUM_BRIGHTNESS ); |
14b7602a JH |
875 | eeconfig_update_rgb_matrix(rgb_matrix_config.raw); |
876 | } | |
877 | ||
878 | void rgblight_decrease_val(void) { | |
a7df9027 | 879 | rgb_matrix_config.val = decrement( rgb_matrix_config.val, 8, 0, RGB_MATRIX_MAXIMUM_BRIGHTNESS ); |
14b7602a JH |
880 | eeconfig_update_rgb_matrix(rgb_matrix_config.raw); |
881 | } | |
882 | ||
afacd423 | 883 | void rgblight_increase_speed(void) { |
884 | rgb_matrix_config.speed = increment( rgb_matrix_config.speed, 1, 0, 3 ); | |
885 | eeconfig_update_rgb_matrix(rgb_matrix_config.raw);//EECONFIG needs to be increased to support this | |
886 | } | |
887 | ||
888 | void rgblight_decrease_speed(void) { | |
889 | rgb_matrix_config.speed = decrement( rgb_matrix_config.speed, 1, 0, 3 ); | |
890 | eeconfig_update_rgb_matrix(rgb_matrix_config.raw);//EECONFIG needs to be increased to support this | |
891 | } | |
892 | ||
14b7602a JH |
893 | void rgblight_mode(uint8_t mode) { |
894 | rgb_matrix_config.mode = mode; | |
895 | eeconfig_update_rgb_matrix(rgb_matrix_config.raw); | |
896 | } | |
897 | ||
898 | uint32_t rgblight_get_mode(void) { | |
899 | return rgb_matrix_config.mode; | |
900 | } |