]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/wilba_tech/wt_rgb_backlight.c
Refactoring wilba.tech PCBs, updating Rama Works U80-A (#6272)
[qmk_firmware.git] / keyboards / wilba_tech / wt_rgb_backlight.c
1 /* Copyright 2017 Jason Williams (Wilba)
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
17 #if defined(RGB_BACKLIGHT_ZEAL60) || defined(RGB_BACKLIGHT_ZEAL65) || defined(RGB_BACKLIGHT_M60_A) || defined(RGB_BACKLIGHT_M6_B) || defined(RGB_BACKLIGHT_KOYU) || defined(RGB_BACKLIGHT_HS60) || defined(RGB_BACKLIGHT_NK65) || defined(RGB_BACKLIGHT_U80_A)
18 #else
19 #error None of the following was defined: RGB_BACKLIGHT_ZEAL60, RGB_BACKLIGHT_ZEAL65, RGB_BACKLIGHT_M60_A, RGB_BACKLIGHT_M6_B, RGB_BACKLIGHT_KOYU, RGB_BACKLIGHT_HS60, RGB_BACKLIGHT_NK65, RGB_BACKLIGHT_U80_A
20 #endif
21
22 #ifndef MAX
23     #define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
24 #endif
25
26 #ifndef MIN
27     #define MIN(a,b) ((a) < (b)? (a): (b))
28 #endif
29
30 #include "quantum.h"
31 #include "wt_rgb_backlight.h"
32 #include "wt_rgb_backlight_api.h"
33 #include "wt_rgb_backlight_keycodes.h"
34
35 #if !defined(RGB_BACKLIGHT_HS60) && !defined(RGB_BACKLIGHT_NK65)
36 #include <avr/io.h>
37 #include <util/delay.h>
38 #include <avr/interrupt.h>
39 #include "drivers/avr/i2c_master.h"
40 #else
41 #include "ch.h"
42 #include "hal.h"
43 #include "drivers/arm/i2c_master.h"
44 #include "tmk_core/common/eeprom.h"
45 #endif
46 #include "progmem.h"
47 #include "quantum/color.h"
48
49 #if defined(RGB_BACKLIGHT_M6_B)
50 #include "drivers/issi/is31fl3218.h"
51 #define BACKLIGHT_LED_COUNT 6
52 #elif defined(RGB_BACKLIGHT_HS60)
53 #include "drivers/issi/is31fl3733.h"
54 #define BACKLIGHT_LED_COUNT 64
55 #elif defined(RGB_BACKLIGHT_NK65)
56 #include "drivers/issi/is31fl3733.h"
57 #define BACKLIGHT_LED_COUNT 69
58 #else
59 #include "drivers/issi/is31fl3731.h"
60 #if defined(RGB_BACKLIGHT_U80_A)
61 #define BACKLIGHT_LED_COUNT 108
62 #else
63 #define BACKLIGHT_LED_COUNT 72
64 #endif
65 #endif
66
67 #define BACKLIGHT_EFFECT_MAX 10
68
69 backlight_config g_config = {
70     .use_split_backspace = RGB_BACKLIGHT_USE_SPLIT_BACKSPACE,
71     .use_split_left_shift = RGB_BACKLIGHT_USE_SPLIT_LEFT_SHIFT,
72     .use_split_right_shift = RGB_BACKLIGHT_USE_SPLIT_RIGHT_SHIFT,
73     .use_7u_spacebar = RGB_BACKLIGHT_USE_7U_SPACEBAR,
74     .use_iso_enter = RGB_BACKLIGHT_USE_ISO_ENTER,
75     .disable_hhkb_blocker_leds = RGB_BACKLIGHT_DISABLE_HHKB_BLOCKER_LEDS,
76     .disable_when_usb_suspended = RGB_BACKLIGHT_DISABLE_WHEN_USB_SUSPENDED,
77     .disable_after_timeout = RGB_BACKLIGHT_DISABLE_AFTER_TIMEOUT,
78     .brightness = RGB_BACKLIGHT_BRIGHTNESS,
79     .effect = RGB_BACKLIGHT_EFFECT,
80     .effect_speed = RGB_BACKLIGHT_EFFECT_SPEED,
81     .color_1 = RGB_BACKLIGHT_COLOR_1,
82     .color_2 = RGB_BACKLIGHT_COLOR_2,
83     .caps_lock_indicator = RGB_BACKLIGHT_CAPS_LOCK_INDICATOR,
84     .layer_1_indicator = RGB_BACKLIGHT_LAYER_1_INDICATOR,
85     .layer_2_indicator = RGB_BACKLIGHT_LAYER_2_INDICATOR,
86     .layer_3_indicator = RGB_BACKLIGHT_LAYER_3_INDICATOR,
87     .alphas_mods = {
88         RGB_BACKLIGHT_ALPHAS_MODS_ROW_0,
89         RGB_BACKLIGHT_ALPHAS_MODS_ROW_1,
90         RGB_BACKLIGHT_ALPHAS_MODS_ROW_2,
91         RGB_BACKLIGHT_ALPHAS_MODS_ROW_3,
92         RGB_BACKLIGHT_ALPHAS_MODS_ROW_4 },
93 #if defined(RGB_BACKLIGHT_M6_B)
94     .custom_color = { { 0, 255 }, { 43, 255 }, { 85, 255 }, { 128, 255 }, { 171, 255 }, { 213, 255 } }
95 #endif
96 };
97
98 bool g_suspend_state = false;
99 uint8_t g_indicator_state = 0;
100
101 // Global tick at 20 Hz
102 uint32_t g_tick = 0;
103
104 // Ticks since this key was last hit.
105 uint8_t g_key_hit[BACKLIGHT_LED_COUNT];
106
107 // Ticks since any key was last hit.
108 uint32_t g_any_key_hit = 0;
109
110 #if defined(RGB_BACKLIGHT_HS60)
111
112 // This is a 7-bit address, that gets left-shifted and bit 0
113 // set to 0 for write, 1 for read (as per I2C protocol)
114 // ADDR_2 is not needed. it is here as a dummy
115 #define ISSI_ADDR_1 0x50
116
117 const is31_led g_is31_leds[DRIVER_LED_TOTAL] = {
118 /* Refer to IS31 manual for these locations
119  *   driver
120  *   |  R location
121  *   |  |      G location
122  *   |  |      |      B location
123  *   |  |      |      | */
124     {0, B_1,   A_1,   C_1}, //LA1
125     {0, E_1,   D_1,   F_1}, //LA2
126     {0, H_1,   G_1,   I_1}, //LA3
127     {0, K_1,   J_1,   L_1}, //LA4
128     {0, B_2,   A_2,   C_2}, //LA5
129     {0, E_2,   D_2,   F_2}, //LA6
130     {0, H_2,   G_2,   I_2}, //LA7
131     {0, K_2,   J_2,   L_2}, //LA8
132     {0, B_3,   A_3,   C_3}, //LA9
133     {0, E_3,   D_3,   F_3}, //LA10
134     {0, H_3,   G_3,   I_3}, //LA11
135     {0, K_3,   J_3,   L_3}, //LA12
136     {0, B_4,   A_4,   C_4}, //LA13
137     {0, E_4,   D_4,   F_4}, //LA14
138     {0, H_4,   G_4,   I_4}, //LA15
139     {0, K_4,   J_4,   L_4}, //LA16
140     {0, B_5,   A_5,   C_5}, //LA17
141     {0, E_5,   D_5,   F_5}, //LA18
142     {0, H_5,   G_5,   I_5}, //LA19
143     {0, K_5,   J_5,   L_5}, //LA20
144     {0, B_6,   A_6,   C_6}, //LA21
145     {0, E_6,   D_6,   F_6}, //LA22
146     {0, H_6,   G_6,   I_6}, //LA23
147     {0, K_6,   J_6,   L_6}, //LA24
148     {0, B_7,   A_7,   C_7}, //LA25
149     {0, E_7,   D_7,   F_7}, //LA26
150     {0, H_7,   G_7,   I_7}, //LA27
151     {0, K_7,   J_7,   L_7}, //LA28
152     {0, B_8,   A_8,   C_8}, //LA29
153     {0, E_8,   D_8,   F_8}, //LA30
154     {0, H_8,   G_8,   I_8}, //LA31
155     {0, K_8,   J_8,   L_8}, //LA32
156     {0, B_9,   A_9,   C_9}, //LA33
157     {0, E_9,   D_9,   F_9}, //LA34
158     {0, H_9,   G_9,   I_9}, //LA35
159     {0, K_9,   J_9,   L_9}, //LA36
160     {0, B_10,  A_10,  C_10}, //LA37
161     {0, E_10,  D_10,  F_10}, //LA38
162     {0, H_10,  G_10,  I_10}, //LA39
163     {0, K_10,  J_10,  L_10}, //LA40
164     {0, B_11,  A_11,  C_11}, //LA41
165     {0, E_11,  D_11,  F_11}, //LA42
166     {0, H_11,  G_11,  I_11}, //LA43
167     {0, K_11,  J_11,  L_11}, //LA44
168     {0, B_12,  A_12,  C_12}, //LA45
169     {0, E_12,  D_12,  F_12}, //LA46
170     {0, H_12,  G_12,  I_12}, //LA47
171     {0, K_12,  J_12,  L_12}, //LA48
172     {0, B_13,  A_13,  C_13}, //LA49
173     {0, E_13,  D_13,  F_13}, //LA50
174     {0, H_13,  G_13,  I_13}, //LA51
175     {0, K_13,  J_13,  L_13}, //LA52
176     {0, B_14,  A_14,  C_14}, //LA53
177     {0, E_14,  D_14,  F_14}, //LA54
178     {0, H_14,  G_14,  I_14}, //LA55
179     {0, K_14,  J_14,  L_14}, //LA56
180     {0, B_15,  A_15,  C_15}, //LA57
181     {0, E_15,  D_15,  F_15}, //LA58
182     {0, H_15,  G_15,  I_15}, //LA59
183     {0, K_15,  J_15,  L_15}, //LA60
184     {0, B_16,  A_16,  C_16}, //LA61
185     {0, E_16,  D_16,  F_16}, //LA62
186     {0, H_16,  G_16,  I_16}, //LA63
187     {0, K_16,  J_16,  L_16}, //LA64
188 };
189
190 #elif defined(RGB_BACKLIGHT_NK65)
191
192 // This is a 7-bit address, that gets left-shifted and bit 0
193 // set to 0 for write, 1 for read (as per I2C protocol)
194 // ADDR_2 is not needed. it is here as a dummy
195 #define ISSI_ADDR_1 0x50
196 #define ISSI_ADDR_2 0x52
197
198 const is31_led g_is31_leds[DRIVER_LED_TOTAL] = {
199 /* Refer to IS31 manual for these locations
200  *   driver
201  *   |  R location
202  *   |  |      G location
203  *   |  |      |      B location
204  *   |  |      |      | */
205     {0, B_1,   A_1,   C_1}, //LA1
206     {0, E_1,   D_1,   F_1}, //LA2
207     {0, H_1,   G_1,   I_1}, //LA3
208     {0, K_1,   J_1,   L_1}, //LA4
209     {0, B_2,   A_2,   C_2}, //LA5
210     {0, E_2,   D_2,   F_2}, //LA6
211     {0, H_2,   G_2,   I_2}, //LA7
212     {0, K_2,   J_2,   L_2}, //LA8
213     {0, B_3,   A_3,   C_3}, //LA9
214     {0, E_3,   D_3,   F_3}, //LA10
215     {0, H_3,   G_3,   I_3}, //LA11
216     {0, K_3,   J_3,   L_3}, //LA12
217     {0, B_4,   A_4,   C_4}, //LA13
218     {0, E_4,   D_4,   F_4}, //LA14
219     {0, H_4,   G_4,   I_4}, //LA15
220     {0, K_4,   J_4,   L_4}, //LA16
221     {0, B_5,   A_5,   C_5}, //LA17
222     {0, E_5,   D_5,   F_5}, //LA18
223     {0, H_5,   G_5,   I_5}, //LA19
224     {0, K_5,   J_5,   L_5}, //LA20
225     {0, B_6,   A_6,   C_6}, //LA21
226     {0, E_6,   D_6,   F_6}, //LA22
227     {0, H_6,   G_6,   I_6}, //LA23
228     {0, K_6,   J_6,   L_6}, //LA24
229     {0, B_7,   A_7,   C_7}, //LA25
230     {0, E_7,   D_7,   F_7}, //LA26
231     {0, H_7,   G_7,   I_7}, //LA27
232     {0, K_7,   J_7,   L_7}, //LA28
233     {0, B_8,   A_8,   C_8}, //LA29
234     {0, E_8,   D_8,   F_8}, //LA30
235     {0, H_8,   G_8,   I_8}, //LA31
236     {0, K_8,   J_8,   L_8}, //LA32
237     {0, B_9,   A_9,   C_9}, //LA33
238     {0, E_9,   D_9,   F_9}, //LA34
239     {0, H_9,   G_9,   I_9}, //LA35
240     {0, K_9,   J_9,   L_9}, //LA36
241     {0, B_10,  A_10,  C_10}, //LA37
242     {0, E_10,  D_10,  F_10}, //LA38
243     {0, H_10,  G_10,  I_10}, //LA39
244     {0, K_10,  J_10,  L_10}, //LA40
245     {0, B_11,  A_11,  C_11}, //LA41
246     {0, E_11,  D_11,  F_11}, //LA42
247     {0, H_11,  G_11,  I_11}, //LA43
248     {0, K_11,  J_11,  L_11}, //LA44
249     {0, B_12,  A_12,  C_12}, //LA45
250     {0, E_12,  D_12,  F_12}, //LA46
251     {0, H_12,  G_12,  I_12}, //LA47
252     {0, K_12,  J_12,  L_12}, //LA48
253     {0, B_13,  A_13,  C_13}, //LA49
254     {0, E_13,  D_13,  F_13}, //LA50
255     {0, H_13,  G_13,  I_13}, //LA51
256     {0, K_13,  J_13,  L_13}, //LA52
257     {0, B_14,  A_14,  C_14}, //LA53
258     {0, E_14,  D_14,  F_14}, //LA54
259     {0, H_14,  G_14,  I_14}, //LA55
260     {0, K_14,  J_14,  L_14}, //LA56
261     {0, B_15,  A_15,  C_15}, //LA57
262     {0, E_15,  D_15,  F_15}, //LA58
263     {0, H_15,  G_15,  I_15}, //LA59
264     {0, K_15,  J_15,  L_15}, //LA60
265     {0, B_16,  A_16,  C_16}, //LA61
266     {0, E_16,  D_16,  F_16}, //LA62
267     {0, H_16,  G_16,  I_16}, //LA63
268     {0, K_16,  J_16,  L_16}, //LA64
269
270     {1, B_1,   A_1,   C_1}, //LB1
271     {1, E_1,   D_1,   F_1}, //LB2
272     {1, H_1,   G_1,   I_1}, //LB3
273     {1, K_1,   J_1,   L_1}, //LB4
274     {1, B_2,   A_2,   C_2}, //LB5
275     {1, E_2,   D_2,   F_2}, //LB6
276     {1, H_2,   G_2,   I_2}, //LB7
277     {1, K_2,   J_2,   L_2}, //LB8
278     {1, B_3,   A_3,   C_3}, //LB9
279     {1, E_3,   D_3,   F_3}, //LB10
280     {1, H_3,   G_3,   I_3}, //LB11
281     {1, K_3,   J_3,   L_3}, //LB12
282     {1, B_4,   A_4,   C_4}, //LB13
283     {1, E_4,   D_4,   F_4}, //LB14
284     {1, H_4,   G_4,   I_4}, //LB15
285     {1, K_4,   J_4,   L_4}, //LB16
286     {1, B_5,   A_5,   C_5}, //LB17
287     {1, E_5,   D_5,   F_5}, //LB18
288     {1, H_5,   G_5,   I_5}, //LB19
289     {1, K_5,   J_5,   L_5}, //LB20
290     {1, B_6,   A_6,   C_6}, //LB21
291     {1, E_6,   D_6,   F_6}, //LB22
292     {1, H_6,   G_6,   I_6}, //LB23
293     {1, K_6,   J_6,   L_6}, //LB24
294     {1, B_7,   A_7,   C_7}, //LB25
295     {1, E_7,   D_7,   F_7}, //LB26
296     {1, H_7,   G_7,   I_7}, //LB27
297     {1, K_7,   J_7,   L_7}, //LB28
298     {1, B_8,   A_8,   C_8}, //LB29
299     {1, E_8,   D_8,   F_8}, //LB30
300     {1, H_8,   G_8,   I_8}, //LB31
301     {1, K_8,   J_8,   L_8}, //LB32
302     {1, B_9,   A_9,   C_9}, //LB33
303     {1, E_9,   D_9,   F_9}, //LB34
304     {1, H_9,   G_9,   I_9}, //LB35
305     {1, K_9,   J_9,   L_9}, //LB36
306     {1, B_10,  A_10,  C_10}, //LB37
307     {1, E_10,  D_10,  F_10}, //LB38
308     {1, H_10,  G_10,  I_10}, //LB39
309     {1, K_10,  J_10,  L_10}, //LB40
310     {1, B_11,  A_11,  C_11}, //LB41
311     {1, E_11,  D_11,  F_11}, //LB42
312     {1, H_11,  G_11,  I_11}, //LB43
313     {1, K_11,  J_11,  L_11}, //LB44
314     {1, B_12,  A_12,  C_12}, //LB45
315     {1, E_12,  D_12,  F_12}, //LB46
316     {1, H_12,  G_12,  I_12}, //LB47
317     {1, K_12,  J_12,  L_12}, //LB48
318     {1, B_13,  A_13,  C_13}, //LB49
319     {1, E_13,  D_13,  F_13}, //LB50
320     {1, H_13,  G_13,  I_13}, //LB51
321     {1, K_13,  J_13,  L_13}, //LB52
322     {1, B_14,  A_14,  C_14}, //LB53
323     {1, E_14,  D_14,  F_14}, //LB54
324     {1, H_14,  G_14,  I_14}, //LB55
325     {1, K_14,  J_14,  L_14}, //LB56
326     {1, B_15,  A_15,  C_15}, //LB57
327     {1, E_15,  D_15,  F_15}, //LB58
328     {1, H_15,  G_15,  I_15}, //LB59
329     {1, K_15,  J_15,  L_15}, //LB60
330     {1, B_16,  A_16,  C_16}, //LB61
331     {1, E_16,  D_16,  F_16}, //LB62
332     {1, H_16,  G_16,  I_16}, //LB63
333     {1, K_16,  J_16,  L_16}, //LB64
334 };
335
336 #elif defined(RGB_BACKLIGHT_U80_A)
337
338 // U80-A prototype uses 3 ISSI drivers
339 #define ISSI_ADDR_1 0x74  // 11101[00] <- GND
340 #define ISSI_ADDR_2 0x76  // 11101[10] <- SDA
341 #define ISSI_ADDR_3 0x75  // 11101[01] <- SCL
342
343 const is31_led g_is31_leds[DRIVER_LED_TOTAL] = {
344 /* Refer to IS31 manual for these locations
345  *   driver
346  *   |  R location
347  *   |  |      G location
348  *   |  |      |      B location
349  *   |  |      |      | */
350     {0, C2_1,  C3_1,  C4_1},  // LA0
351     {0, C1_1,  C3_2, C4_2},   // LA1
352     {0, C1_2,  C2_2, C4_3},   // LA2
353     {0, C1_3,  C2_3, C3_3},   // LA3
354     {0, C1_4,  C2_4, C3_4},   // LA4
355     {0, C1_5,  C2_5, C3_5},   // LA5
356     {0, C1_6,  C2_6, C3_6},   // LA6
357     {0, C1_7,  C2_7, C3_7},   // LA7
358     {0, C1_8,  C2_8, C3_8},   // LA8
359     {0, C9_1,  C8_1, C7_1},   // LA9
360     {0, C9_2,  C8_2, C7_2},   // LA10
361     {0, C9_3,  C8_3, C7_3},   // LA11
362     {0, C9_4,  C8_4, C7_4},   // LA12
363     {0, C9_5,  C8_5, C7_5},   // LA13
364     {0, C9_6,  C8_6, C7_6},   // LA14
365     {0, C9_7,  C8_7, C6_6},   // LA15
366     {0, C9_8,  C7_7, C6_7},   // LA16
367     {0, C8_8,  C7_8, C6_8},   // LA17
368
369     {0, C2_9,  C3_9,  C4_9},  // LB0
370     {0, C1_9,  C3_10, C4_10}, // LB1
371     {0, C1_10, C2_10, C4_11}, // LB2
372     {0, C1_11, C2_11, C3_11}, // LB3
373     {0, C1_12, C2_12, C3_12}, // LB4
374     {0, C1_13, C2_13, C3_13}, // LB5
375     {0, C1_14, C2_14, C3_14}, // LB6
376     {0, C1_15, C2_15, C3_15}, // LB7
377     {0, C1_16, C2_16, C3_16}, // LB8
378     {0, C9_9,  C8_9,  C7_9},  // LB9
379     {0, C9_10, C8_10, C7_10}, // LB10
380     {0, C9_11, C8_11, C7_11}, // LB11
381     {0, C9_12, C8_12, C7_12}, // LB12
382     {0, C9_13, C8_13, C7_13}, // LB13
383     {0, C9_14, C8_14, C7_14}, // LB14
384     {0, C9_15, C8_15, C6_14}, // LB15
385     {0, C9_16, C7_15, C6_15}, // LB16
386     {0, C8_16, C7_16, C6_16}, // LB17
387
388     {1, C2_1,  C3_1,  C4_1},  // LC0
389     {1, C1_1,  C3_2, C4_2},   // LC1
390     {1, C1_2,  C2_2, C4_3},   // LC2
391     {1, C1_3,  C2_3, C3_3},   // LC3
392     {1, C1_4,  C2_4, C3_4},   // LC4
393     {1, C1_5,  C2_5, C3_5},   // LC5
394     {1, C1_6,  C2_6, C3_6},   // LC6
395     {1, C1_7,  C2_7, C3_7},   // LC7
396     {1, C1_8,  C2_8, C3_8},   // LC8
397     {1, C9_1,  C8_1,  C7_1},  // LC9
398     {1, C9_2,  C8_2, C7_2},   // LC10
399     {1, C9_3,  C8_3, C7_3},   // LC11
400     {1, C9_4,  C8_4, C7_4},   // LC12
401     {1, C9_5,  C8_5, C7_5},   // LC13
402     {1, C9_6,  C8_6, C7_6},   // LC14
403     {1, C9_7,  C8_7, C6_6},   // LC15
404     {1, C9_8,  C7_7, C6_7},   // LC16
405     {1, C8_8,  C7_8, C6_8},   // LC17
406
407     {1, C2_9,  C3_9,  C4_9},  // LD0
408     {1, C1_9,  C3_10, C4_10}, // LD1
409     {1, C1_10, C2_10, C4_11}, // LD2
410     {1, C1_11, C2_11, C3_11}, // LD3
411     {1, C1_12, C2_12, C3_12}, // LD4
412     {1, C1_13, C2_13, C3_13}, // LD5
413     {1, C1_14, C2_14, C3_14}, // LD6
414     {1, C1_15, C2_15, C3_15}, // LD7
415     {1, C1_16, C2_16, C3_16}, // LD8
416     {1, C9_9,  C8_9,  C7_9},  // LD9
417     {1, C9_10, C8_10, C7_10}, // LD10
418     {1, C9_11, C8_11, C7_11}, // LD11
419     {1, C9_12, C8_12, C7_12}, // LD12
420     {1, C9_13, C8_13, C7_13}, // LD13
421     {1, C9_14, C8_14, C7_14}, // LD14
422     {1, C9_15, C8_15, C6_14}, // LD15
423     {1, C9_16, C7_15, C6_15}, // LD16
424     {1, C8_16, C7_16, C6_16}, // LD17
425
426     {2, C2_1,  C3_1,  C4_1},  // LE0
427     {2, C1_1,  C3_2, C4_2},   // LE1
428     {2, C1_2,  C2_2, C4_3},   // LE2
429     {2, C1_3,  C2_3, C3_3},   // LE3
430     {2, C1_4,  C2_4, C3_4},   // LE4
431     {2, C1_5,  C2_5, C3_5},   // LE5
432     {2, C1_6,  C2_6, C3_6},   // LE6
433     {2, C1_7,  C2_7, C3_7},   // LE7
434     {2, C1_8,  C2_8, C3_8},   // LE8
435     {2, C9_1,  C8_1,  C7_1},  // LE9
436     {2, C9_2,  C8_2, C7_2},   // LE10
437     {2, C9_3,  C8_3, C7_3},   // LE11
438     {2, C9_4,  C8_4, C7_4},   // LE12
439     {2, C9_5,  C8_5, C7_5},   // LE13
440     {2, C9_6,  C8_6, C7_6},   // LE14
441     {2, C9_7,  C8_7, C6_6},   // LE15
442     {2, C9_8,  C7_7, C6_7},   // LE16
443     {2, C8_8,  C7_8, C6_8},   // LE17
444
445     {2, C2_9,  C3_9,  C4_9},  // LF0
446     {2, C1_9,  C3_10, C4_10}, // LF1
447     {2, C1_10, C2_10, C4_11}, // LF2
448     {2, C1_11, C2_11, C3_11}, // LF3
449     {2, C1_12, C2_12, C3_12}, // LF4
450     {2, C1_13, C2_13, C3_13}, // LF5
451     {2, C1_14, C2_14, C3_14}, // LF6
452     {2, C1_15, C2_15, C3_15}, // LF7
453     {2, C1_16, C2_16, C3_16}, // LF8
454     {2, C9_9,  C8_9,  C7_9},  // LF9
455     {2, C9_10, C8_10, C7_10}, // LF10
456     {2, C9_11, C8_11, C7_11}, // LF11
457     {2, C9_12, C8_12, C7_12}, // LF12
458     {2, C9_13, C8_13, C7_13}, // LF13
459     {2, C9_14, C8_14, C7_14}, // LF14
460     {2, C9_15, C8_15, C6_14}, // LF15
461     {2, C9_16, C7_15, C6_15}, // LF16
462     {2, C8_16, C7_16, C6_16}, // LF17
463 };
464 #elif !defined(RGB_BACKLIGHT_M6_B)
465 // This is a 7-bit address, that gets left-shifted and bit 0
466 // set to 0 for write, 1 for read (as per I2C protocol)
467 #define ISSI_ADDR_1 0x74
468 #define ISSI_ADDR_2 0x76
469
470 const is31_led g_is31_leds[DRIVER_LED_TOTAL] = {
471 /* Refer to IS31 manual for these locations
472  *   driver
473  *   |  R location
474  *   |  |      G location
475  *   |  |      |      B location
476  *   |  |      |      | */
477     {0, C2_1,  C3_1,  C4_1},  // LA0
478     {0, C1_1,  C3_2, C4_2},   // LA1
479     {0, C1_2,  C2_2, C4_3},   // LA2
480     {0, C1_3,  C2_3, C3_3},   // LA3
481     {0, C1_4,  C2_4, C3_4},   // LA4
482     {0, C1_5,  C2_5, C3_5},   // LA5
483     {0, C1_6,  C2_6, C3_6},   // LA6
484     {0, C1_7,  C2_7, C3_7},   // LA7
485     {0, C1_8,  C2_8, C3_8},   // LA8
486     {0, C9_1,  C8_1, C7_1},   // LA9
487     {0, C9_2,  C8_2, C7_2},   // LA10
488     {0, C9_3,  C8_3, C7_3},   // LA11
489     {0, C9_4,  C8_4, C7_4},   // LA12
490     {0, C9_5,  C8_5, C7_5},   // LA13
491     {0, C9_6,  C8_6, C7_6},   // LA14
492     {0, C9_7,  C8_7, C6_6},   // LA15
493     {0, C9_8,  C7_7, C6_7},   // LA16
494     {0, C8_8,  C7_8, C6_8},   // LA17
495
496     {0, C2_9,  C3_9,  C4_9},  // LB0
497     {0, C1_9,  C3_10, C4_10}, // LB1
498     {0, C1_10, C2_10, C4_11}, // LB2
499     {0, C1_11, C2_11, C3_11}, // LB3
500     {0, C1_12, C2_12, C3_12}, // LB4
501     {0, C1_13, C2_13, C3_13}, // LB5
502     {0, C1_14, C2_14, C3_14}, // LB6
503     {0, C1_15, C2_15, C3_15}, // LB7
504     {0, C1_16, C2_16, C3_16}, // LB8
505     {0, C9_9,  C8_9,  C7_9},  // LB9
506     {0, C9_10, C8_10, C7_10}, // LB10
507     {0, C9_11, C8_11, C7_11}, // LB11
508     {0, C9_12, C8_12, C7_12}, // LB12
509     {0, C9_13, C8_13, C7_13}, // LB13
510     {0, C9_14, C8_14, C7_14}, // LB14
511     {0, C9_15, C8_15, C6_14}, // LB15
512     {0, C9_16, C7_15, C6_15}, // LB16
513     {0, C8_16, C7_16, C6_16}, // LB17
514
515     {1, C2_1,  C3_1,  C4_1},  // LC0
516     {1, C1_1,  C3_2, C4_2},   // LC1
517     {1, C1_2,  C2_2, C4_3},   // LC2
518     {1, C1_3,  C2_3, C3_3},   // LC3
519     {1, C1_4,  C2_4, C3_4},   // LC4
520     {1, C1_5,  C2_5, C3_5},   // LC5
521     {1, C1_6,  C2_6, C3_6},   // LC6
522     {1, C1_7,  C2_7, C3_7},   // LC7
523     {1, C1_8,  C2_8, C3_8},   // LC8
524     {1, C9_1,  C8_1,  C7_1},  // LC9
525     {1, C9_2,  C8_2, C7_2},   // LC10
526     {1, C9_3,  C8_3, C7_3},   // LC11
527     {1, C9_4,  C8_4, C7_4},   // LC12
528     {1, C9_5,  C8_5, C7_5},   // LC13
529     {1, C9_6,  C8_6, C7_6},   // LC14
530     {1, C9_7,  C8_7, C6_6},   // LC15
531     {1, C9_8,  C7_7, C6_7},   // LC16
532     {1, C8_8,  C7_8, C6_8},   // LC17
533
534     {1, C2_9,  C3_9,  C4_9},  // LD0
535     {1, C1_9,  C3_10, C4_10}, // LD1
536     {1, C1_10, C2_10, C4_11}, // LD2
537     {1, C1_11, C2_11, C3_11}, // LD3
538     {1, C1_12, C2_12, C3_12}, // LD4
539     {1, C1_13, C2_13, C3_13}, // LD5
540     {1, C1_14, C2_14, C3_14}, // LD6
541     {1, C1_15, C2_15, C3_15}, // LD7
542     {1, C1_16, C2_16, C3_16}, // LD8
543     {1, C9_9,  C8_9,  C7_9},  // LD9
544     {1, C9_10, C8_10, C7_10}, // LD10
545     {1, C9_11, C8_11, C7_11}, // LD11
546     {1, C9_12, C8_12, C7_12}, // LD12
547     {1, C9_13, C8_13, C7_13}, // LD13
548     {1, C9_14, C8_14, C7_14}, // LD14
549     {1, C9_15, C8_15, C6_14}, // LD15
550     {1, C9_16, C7_15, C6_15}, // LD16
551     {1, C8_16, C7_16, C6_16}, // LD17
552 };
553 #endif // !defined(RGB_BACKLIGHT_M6_B)
554
555
556 typedef struct Point {
557     uint8_t x;
558     uint8_t y;
559 } Point;
560
561
562 // index in range 0..71 (LA0..LA17, LB0..LB17, LC0..LC17, LD0..LD17)
563 // point values in range x=0..224 y=0..64
564 // origin is center of top-left key (i.e Esc)
565 #if defined(RGB_BACKLIGHT_ZEAL65)
566 const Point g_map_led_to_point[BACKLIGHT_LED_COUNT] PROGMEM = {
567     // LA0..LA17
568     {120,16}, {104,16}, {88,16}, {72,16}, {56,16}, {40,16}, {24,16}, {4,16}, {4,32},
569     {128,0}, {112,0}, {96,0}, {80,0}, {64,0}, {48,0}, {32,0}, {16,0}, {0,0},
570     // LB0..LB17
571     {144,0}, {160,0}, {176,0}, {192,0}, {216,0}, {224,0}, {240,0}, {240,16}, {240,32},
572     {136,16}, {152,16}, {168,16}, {184,16}, {200,16}, {220,16}, {240,48}, {240,64}, {224,64},
573     // LC0..LC17
574     {96,64}, {100,48}, {84,48}, {68,48}, {52,48}, {36,48}, {255,255}, {48,60}, {28,64},
575     {108,32}, {92,32}, {76,32}, {60,32}, {44,32}, {28,32}, {20,44}, {10,48}, {4,64},
576     // LD0..LD17
577     {124,32}, {140,32}, {156,32}, {172,32}, {188,32}, {214,32}, {180,48}, {202,48}, {224,48},
578     {116,48}, {132,48}, {148,48}, {164,48}, {255,255}, {144,60}, {164,64}, {188,64}, {208,64}
579 };
580 const Point g_map_led_to_point_polar[BACKLIGHT_LED_COUNT] PROGMEM = {
581     // LA0..LA17
582     {64,128}, {75,132}, {84,145}, {91,164}, {97,187}, {102,213}, {105,242}, {109,255}, {128,243},
583     {61,255}, {67,255}, {72,255}, {77,255}, {82,255}, {86,255}, {90,255}, {93,255}, {96,255},
584     // LB0..LB17
585     {56,255}, {51,255}, {46,255}, {42,255}, {37,255}, {35,255}, {32,255}, {19,255}, {0,255},
586     {53,132}, {44,145}, {37,164}, {31,187}, {26,213}, {22,249}, {237,255}, {224,255}, {221,255},
587     // LC0..LC17
588     {184,255}, {179,135}, {170,149}, {163,169}, {157,193}, {153,220}, {255,255}, {167,255}, {165,255},
589     {128,26}, {128,60}, {128,94}, {128,128}, {128,162}, {128,196}, {145,233}, {148,255}, {161,255},
590     // LD0..LD17
591     {0,9}, {0,43}, {0,77}, {0,111}, {0,145}, {0,201}, {224,181}, {230,217}, {235,255},
592     {189,128}, {200,131}, {210,141}, {218,159}, {255,255}, {201,228}, {206,255}, {213,255}, {218,255}
593 };
594 #elif defined(RGB_BACKLIGHT_KOYU)
595 const Point g_map_led_to_point[BACKLIGHT_LED_COUNT] PROGMEM = {
596     // LA0..LA17
597     {120,16}, {104,16}, {88,16}, {72,16}, {56,16}, {40,16}, {24,16}, {4,16}, {4,32},
598     {128,0}, {112,0}, {96,0}, {80,0}, {64,0}, {48,0}, {32,0}, {16,0}, {0,0},
599     // LB0..LB17
600     {144,0}, {160,0}, {176,0}, {192,0}, {208,0}, {224,0}, {240,0}, {240,16}, {240,32},
601     {136,16}, {152,16}, {168,16}, {184,16}, {200,16}, {220,16}, {240,48}, {240,64}, {224,64},
602     // LC0..LC17
603     {112,64}, {100,48}, {84,48}, {68,48}, {52,48}, {36,48}, {64,60}, {44,60}, {24,64},
604     {108,32}, {92,32}, {76,32}, {60,32}, {44,32}, {28,32}, {255,255}, {10,48}, {4,64},
605     // LD0..LD17
606     {124,32}, {140,32}, {156,32}, {172,32}, {188,32}, {214,32}, {180,48}, {202,48}, {224,48},
607     {116,48}, {132,48}, {148,48}, {164,48}, {255,255}, {160,60}, {180,64}, {208,64}, {255,255}
608 };
609 const Point g_map_led_to_point_polar[BACKLIGHT_LED_COUNT] PROGMEM = {
610     // LA0..LA17
611     {64,128}, {75,132}, {84,145}, {91,164}, {97,187}, {102,213}, {105,242}, {109,255}, {128,243},
612     {61,255}, {67,255}, {72,255}, {77,255}, {82,255}, {86,255}, {90,255}, {93,255}, {96,255},
613     // LB0..LB17
614     {56,255}, {51,255}, {46,255}, {42,255}, {38,255}, {35,255}, {32,255}, {19,255}, {0,255},
615     {53,132}, {44,145}, {37,164}, {31,187}, {26,213}, {22,249}, {237,255}, {224,255}, {221,255},
616     // LC0..LC17
617     {189,255}, {179,135}, {170,149}, {163,169}, {157,193}, {153,220}, {172,252}, {169,255}, {165,255},
618     {128,26}, {128,60}, {128,94}, {128,128}, {128,162}, {128,196}, {255,255}, {148,255}, {161,255},
619     // LD0..LD17
620     {0,9}, {0,43}, {0,77}, {0,111}, {0,145}, {0,201}, {224,181}, {230,217}, {235,255},
621     {189,128}, {200,131}, {210,141}, {218,159}, {255,255}, {207,238}, {211,255}, {218,255}, {255,255}
622 };
623 #elif defined(RGB_BACKLIGHT_ZEAL60) || defined(RGB_BACKLIGHT_M60_A)
624 const Point g_map_led_to_point[BACKLIGHT_LED_COUNT] PROGMEM = {
625     // LA0..LA17
626     {120,16}, {104,16}, {88,16}, {72,16}, {56,16}, {40,16}, {24,16}, {4,16}, {4,32},
627     {128,0}, {112,0}, {96,0}, {80,0}, {64,0}, {48,0}, {32,0}, {16,0}, {0,0},
628     // LB0..LB17
629     {144,0}, {160,0}, {176,0}, {192,0}, {216,0}, {224,0}, {255,255}, {255,255}, {255,255},
630     {136,16}, {152,16}, {168,16}, {184,16}, {200,16}, {220,16}, {255,255}, {255,255}, {255,255},
631     // LC0..LC17
632     {102,64}, {100,48}, {84,48}, {68,48}, {52,48}, {36,48}, {60,64}, {43,64}, {23,64},
633     {108,32}, {92,32}, {76,32}, {60,32}, {44,32}, {28,32}, {20,48}, {2,48}, {3,64},
634     // LD0..LD17
635     {124,32}, {140,32}, {156,32}, {172,32}, {188,32}, {214,32}, {180,48}, {210,48}, {224,48},
636     {116,48}, {132,48}, {148,48}, {164,48}, {144,64}, {161,64}, {181,64}, {201,64}, {221,64}
637 };
638 const Point g_map_led_to_point_polar[BACKLIGHT_LED_COUNT] PROGMEM = {
639     // LA0..LA17
640     {58,129}, {70,129}, {80,139}, {89,157}, {96,181}, {101,208}, {105,238}, {109,255}, {128,247},
641     {58,255}, {64,255}, {70,255}, {75,255}, {80,255}, {85,255}, {89,255}, {93,255}, {96,255},
642     // LB0..LB17
643     {53,255}, {48,255}, {43,255}, {39,255}, {34,255}, {32,255}, {255,255}, {255,255}, {255,255},
644     {48,139}, {39,157}, {32,181}, {27,208}, {23,238}, {19,255}, {255,255}, {255,255}, {255,255},
645     // LC0..LC17
646     {188,255}, {183,131}, {173,143}, {165,163}, {159,188}, {154,216}, {172,252}, {170,255}, {165,255},
647     {128,9}, {128,46}, {128,82}, {128,119}, {128,155}, {128,192}, {150,244}, {147,255}, {161,255},
648     // LD0..LD17
649     {0,27}, {0,64}, {0,101}, {0,137}, {0,174}, {255,233}, {228,201}, {235,255}, {237,255},
650     {195,128}, {206,136}, {215,152}, {222,175}, {205,234}, {209,255}, {214,255}, {219,255}, {223,255}
651 };
652 #elif defined(RGB_BACKLIGHT_U80_A)
653 const Point g_map_led_to_point[BACKLIGHT_LED_COUNT] PROGMEM = {
654     // Thse are scaled by 14.5 per U
655     // LA0..LA17
656     {109,36}, {94,36}, {80,36}, {65,36}, {51,36}, {36,36}, {22,36}, {4,36}, {5,51},
657     {116,22}, {102,22}, {87,22}, {73,22}, {58,22}, {44,22}, {29,22}, {15,22}, {0,22},
658     // LB0..LB17
659     {131,22}, {145,22}, {160,22}, {174,22}, {196,22}, {0,0}, {0,0}, {0,0}, {0,0},
660     {123,36}, {138,36}, {152,36}, {167,36}, {181,36}, {199,36}, {0,0}, {0,0}, {0,0},
661     // LC0..LC17
662     {102,80}, {91,65}, {76,65}, {62,65}, {47,65}, {33,65}, {58,76}, {40,80}, {22,80},
663     {98,51}, {83,51}, {69,51}, {54,51}, {40,51}, {25,51}, {0,0}, {9,65}, {4,80},
664     // LD0..LD17
665     {112,51}, {127,51}, {141,51}, {156,51}, {170,51}, {194,51}, {163,65}, {190,65}, {0,0},
666     {105,65}, {120,65}, {134,65}, {149,65}, {0,0}, {145,76}, {163,80}, {181,80}, {199,80},
667     // LE0..LE17
668     {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0},
669     {73,0}, {94,0}, {109,0}, {123,0}, {138,0}, {58,0}, {44,0}, {29,0}, {0,0},
670     // LF0..LF17
671     {160,0}, {174,0}, {189,0}, {203,0}, {225,0}, {239,0}, {254,0}, {254,22}, {254,36},
672     {239,22}, {239,36}, {225,22}, {225,36}, {0,0}, {239,65}, {225,80}, {239,80}, {254,80}
673 };
674 const Point g_map_led_to_point_polar[BACKLIGHT_LED_COUNT] PROGMEM = {
675     // LA0..LA17
676     {59,129}, {69,129}, {80,138}, {88,154}, {95,175}, {100,200}, {104,227}, {107,255}, {128,226},
677     {59,255}, {64,255}, {69,255}, {75,255}, {80,255}, {84,255}, {88,255}, {91,255}, {95,255},  
678     // LB0..LB17
679     {53,255}, {48,255}, {44,255}, {40,255}, {35,255}, {255,255}, {255,255}, {255,255}, {255,255},
680     {48,138}, {40,154}, {33,175}, {28,200}, {24,227}, {21,255}, {255,255}, {255,255}, {255,255},
681     // LC0..LC17
682     {192,255}, {184,131}, {174,141}, {166,159}, {160,181}, {155,207}, {174,244}, {171,255}, {166,255},
683     {128,9}, {128,43}, {128,77}, {128,111}, {128,145}, {128,179}, {255,255}, {150,252}, {162,255},
684     // LD0..LD17
685     {0,26}, {0,60}, {0,94}, {0,128}, {0,162}, {0,218}, {227,193}, {234,245}, {255,255},
686     {195,128}, {205,135}, {214,149}, {221,169}, {255,255}, {210,244}, {213,255}, {218,255}, {222,255},
687     // LE0..LE17
688     {255,255}, {255,255}, {255,255}, {255,255}, {255,255}, {255,255}, {255,255}, {255,255}, {255,255}, 
689     {70,255}, {66,255}, {62,255}, {59,255}, {56,255}, {73,255}, {76,255}, {79,255}, {84,255},
690     // LF0..LF17
691     {52,255}, {49,255}, {47,255}, {44,255}, {41,255}, {38,255}, {37,255}, {25,255}, {14,255},
692     {27,255}, {15,255}, {29,255}, {17,255}, {255,255}, {241,255}, {227,255}, {229,255}, {231,255}
693 };
694 #elif defined(RGB_BACKLIGHT_HS60) && defined(HS60_ANSI)
695 const Point g_map_led_to_point[BACKLIGHT_LED_COUNT] PROGMEM = {
696     // LA1..LA47
697     {0,0}, {4,16}, {6,32}, {10,48}, {16,0}, {24,16}, {28,32}, {36,48}, {32,0}, {40,16}, {44,32}, {52,48},
698     {48,0}, {56,16}, {60,32}, {68,48}, {64,0}, {72,16}, {76,32}, {84,48}, {80,0}, {88,16}, {92,32}, {100,48},
699     {96,0}, {104,16}, {108,32}, {116,48}, {112,0}, {120,16}, {124,32}, {132,48}, {128,0}, {136,16}, {140,32},
700     {148,48}, {144,0}, {152,16}, {156,32}, {164,48}, {160,0}, {168,16}, {172,32}, {180,48}, {176,0}, {184, 16}, {188,32},
701     {255,255},// LA48 does not exist, dummy
702     // LA49..LA50
703     {192,0}, {200,16},
704     {255,255},// LA51 does not exit, dummy
705     // LA52..LA60
706     {210,48}, {216,0}, {220,16}, {214,32}, {222,64}, {2,64}, {22,64}, {42,64}, {102,64},
707     {255,255},// LA61 does not exit, dummy
708     {162,64}, {182,64}, {202,64}
709 };
710 const Point g_map_led_to_point_polar[BACKLIGHT_LED_COUNT] PROGMEM = {
711     // LA1..LA47
712     {96,255}, {109,255}, {128,242}, {148,255}, {93,255}, {105,238}, {128,192}, {154,216}, {89,255}, {101,208}, {128,155}, {159,188},
713     {85,255}, {96,181}, {128,119}, {165,163}, {81,255}, {89,157}, {128,82}, {173,143}, {75,255}, {81,139}, {128,46}, {183,131},
714     {70,255}, {70,129}, {129,9}, {195,128}, {64,255}, {58,129}, {255,27}, {206,136}, {58,255}, {47,139}, {255,64}, {215,152},
715     {53,255}, {39,157}, {255,101}, {222,175}, {47,255}, {32,181}, {255,137}, {228,201}, {43,255}, {27,208}, {255, 174},
716     {255,255},// LA48 does not exist, dummy
717     // LA49..LA50
718     {39,255}, {23,238},
719     {255,255},// LA51 does not exit, dummy
720     // LA52..LA60
721     {235,255}, {33,255}, {19,255}, {255,233}, {224,255}, {160,255}, {164,255}, {169,255}, {188,255},
722     {255,255},// LA61 does not exit, dummy
723     {209,255}, {215,255}, {220,255}
724 };
725 #elif defined(RGB_BACKLIGHT_HS60) && defined(HS60_HHKB)
726 const Point g_map_led_to_point[BACKLIGHT_LED_COUNT] PROGMEM = {
727     // LA1..LA60
728     {0,0}, {4,16}, {6,32}, {10,48}, {16,0}, {24,16}, {28,32}, {36,48}, {32,0}, {40,16}, {44,32}, {52,48},
729     {48,0}, {56,16}, {60,32}, {68,48}, {64,0}, {72,16}, {76,32}, {84,48}, {80,0}, {88,16}, {92,32}, {100,48},
730     {96,0}, {104,16}, {108,32}, {116,48}, {112,0}, {120,16}, {124,32}, {132,48}, {128,0}, {136,16}, {140,32},
731     {148,48}, {144,0}, {152,16}, {156,32}, {164,48}, {160,0}, {168,16}, {172,32}, {180,48}, {176,0}, {184, 16}, {188,32},
732     {224,0}, {192,0}, {200,16}, {202,48}, {224,48}, {208,0}, {220,16}, {214,32}, {220,64}, {4,64}, {24,64}, {44,64}, {112,64},
733     {255,255}, {255,255}, // LA61..LA62 does not exit, dummy
734     // LA63..LA64
735     {180,64}, {200,64}
736 };
737 const Point g_map_led_to_point_polar[BACKLIGHT_LED_COUNT] PROGMEM = {
738     // LA1..LA60
739     {96,255}, {109,255}, {128,242}, {148,255}, {93,255}, {105,238}, {128,192}, {154,216}, {89,255}, {101,208}, {128,155}, {159,188},
740     {85,255}, {96,181}, {128,119}, {165,163}, {81,255}, {89,157}, {128,82}, {173,143}, {75,255}, {81,139}, {128,46}, {183,131},
741     {70,255}, {70,129}, {129,9}, {195,128}, {64,255}, {58,129}, {255,27}, {206,136}, {58,255}, {47,139}, {255,64}, {215,152},
742     {53,255}, {39,157}, {255,101}, {222,175}, {47,255}, {32,181}, {255,137}, {228,201}, {43,255}, {27,208}, {255, 174}, {32,255},
743     {39,255}, {23,238}, {233,242}, {237,255}, {35,255}, {19,255}, {255,233}, {223,255}, {161,255}, {165,255}, {170,255}, {192,255},
744     {255,255}, {255,255}, // LA61..LA62 does not exit, dummy
745     // LA63..LA64
746     {214,255}, {219,255}
747 };
748 #elif defined(RGB_BACKLIGHT_HS60) //HS60_ISO
749 const Point g_map_led_to_point[BACKLIGHT_LED_COUNT] PROGMEM = {
750     // LA1..LA50
751     {0,0}, {4,16}, {6,32}, {2,48}, {16,0}, {24,16}, {28,32}, {36,48}, {32,0}, {40,16}, {44,32}, {52,48}, {48,0},
752     {56,16}, {60,32}, {68,48}, {64,0}, {72,16}, {76,32}, {84,48}, {80,0}, {88,16}, {92,32}, {100,48}, {96,0}, {104,16}, 
753     {108,32}, {116,48}, {112,0}, {120,16}, {124,32}, {132,48}, {128,0}, {136,16}, {140,32}, {148,48}, {144,0}, {152,16},
754     {156,32}, {164,48}, {160,0}, {168,16}, {172,32}, {180,48}, {176,0}, {184, 16}, {188,32}, {20,48}, {192,0}, {200,16},
755     {255,255},// LA51 does not exit, dummy
756     // LA52..LA60
757     {210,48}, {216,0}, {220,16}, {222,24}, {222,64}, {2,64}, {22,64}, {42,64}, {102,64},
758     {255,255},// LA61 does not exit, dummy
759     {162,64}, {182,64}, {202,64}
760 };
761 const Point g_map_led_to_point_polar[BACKLIGHT_LED_COUNT] PROGMEM = {
762     // LA1..LA50
763     {96,255}, {109,255}, {128,242}, {147,255}, {93,255}, {105,238}, {128,192}, {154,216}, {89,255}, {101,208}, {128,155}, {159,188}, {85,255},
764     {96,181}, {128,119}, {165,163}, {81,255}, {89,157}, {128,82}, {173,143}, {75,255}, {81,139}, {128,46}, {183,131}, {70,255}, {70,129},
765     {129,9}, {195,128}, {64,255}, {58,129}, {255,27}, {206,136}, {58,255}, {47,139}, {255,64}, {215,152}, {53,255}, {39,157}, {255,101}, 
766     {222,175}, {47,255}, {32,181}, {255,137}, {228,201}, {43,255}, {27,208}, {255, 174}, {150,246}, {39,255}, {23,238},
767     {255,255},// LA51 does not exit, dummy
768     // LA52..LA60
769     {235,255}, {33,255}, {19,255}, {10,255}, {224,255}, {160,255}, {164,255}, {169,255}, {188,255},
770     {255,255},// LA61 does not exit, dummy
771     {209,255}, {215,255}, {220,255}
772 };
773 #elif defined(RGB_BACKLIGHT_NK65)
774 const Point g_map_led_to_point[BACKLIGHT_LED_COUNT] PROGMEM = {
775     // LA1..LA60
776     {0,0}, {4,16}, {6,32}, {10,48}, {16,0}, {24,16}, {28,32}, {36,48}, {32,0}, {40,16}, {44,32}, {52,48},
777     {48,0}, {56,16}, {60,32}, {68,48}, {64,0}, {72,16}, {76,32}, {84,48}, {80,0}, {88,16}, {92,32}, {100,48},
778     {96,0}, {104,16}, {108,32}, {116,48}, {112,0}, {120,16}, {124,32}, {132,48}, {128,0}, {136,16}, {140,32},
779     {148,48}, {144,0}, {152,16}, {156,32}, {164,48}, {160,0}, {168,16}, {172,32}, {180,48}, {176,0}, {184, 16}, {188,32},
780     {160,64}, {192,0}, {200,16}, {210,48}, {224,48}, {216,0}, {220,16}, {214,32}, {224,64}, {2,64}, {22,64}, {42,64}, {102,64},
781     {255,255},// LA61 does not exit, dummy
782     //LA62..LB5
783     {176,64}, {192,64}, {208,64}, {240,0}, {240,16}, {240,48}, {240,64}, {240,32}
784 };
785 const Point g_map_led_to_point_polar[BACKLIGHT_LED_COUNT] PROGMEM = {
786     // LA1..LA60
787     {96,255}, {109,255}, {128,242}, {148,255}, {93,255}, {105,238}, {128,192}, {154,216}, {89,255}, {101,208}, {128,155}, {159,188},
788     {85,255}, {96,181}, {128,119}, {165,163}, {81,255}, {89,157}, {128,82}, {173,143}, {75,255}, {81,139}, {128,46}, {183,131},
789     {70,255}, {70,129}, {129,9}, {195,128}, {64,255}, {58,129}, {255,27}, {206,136}, {58,255}, {47,139}, {255,64}, {215,152},
790     {53,255}, {39,157}, {255,101}, {222,175}, {47,255}, {32,181}, {255,137}, {228,201}, {43,255}, {27,208}, {255, 174},
791     {208,255}, {39,255}, {23,238}, {235,255}, {235,255}, {33,255}, {19,255}, {255,233}, {224,255}, {160,255}, {164,255}, {169,255}, {188,255},
792     {255,255},// LA61 does not exit, dummy
793     //LA62..LB5
794     {221,255}, {225,255}, {229,255}, {22,255}, {12,255}, {244,255}, {234,255}, {255,255},
795 };
796 #elif defined(RGB_BACKLIGHT_M6_B)
797 // M6-B is really simple:
798 // 0 3 5
799 // 1 2 4
800 const Point g_map_led_to_point[BACKLIGHT_LED_COUNT] PROGMEM = {
801     {0,0}, {0,16}, {16,16}, {16,0}, {32,16}, {32,0}
802 };
803 const Point g_map_led_to_point_polar[BACKLIGHT_LED_COUNT] PROGMEM = {
804     {160,255}, {96,255}, {77,255}, {179,255}, {51,255}, {205,255}
805 };
806 #endif
807
808 // This may seem counter-intuitive, but it's quite flexible.
809 // For each LED, get it's position to decide what color to make it.
810 // This solves the issue of LEDs (and switches) not aligning to a grid,
811 // or having a large "bitmap" and sampling them.
812 void map_led_to_point( uint8_t index, Point *point )
813 {
814     // Slightly messy way to get Point structs out of progmem.
815     uint8_t *addr = (uint8_t*)&g_map_led_to_point[index];
816     point->x = pgm_read_byte(addr);
817     point->y = pgm_read_byte(addr+1);
818
819 #if defined(RGB_BACKLIGHT_M6_B) || defined(RGB_BACKLIGHT_HS60) || defined(RGB_BACKLIGHT_NK65)
820     return;
821 #endif
822
823     switch (index)
824     {
825         case 18+4: // LB4A
826             if ( g_config.use_split_backspace )
827                 point->x -= 8;
828             break;
829 #if defined(RGB_BACKLIGHT_ZEAL60)
830         case 18+14: // LB14A
831             if ( g_config.use_iso_enter )
832                 point->y += 8; // extremely pedantic
833             break;
834         case 54+5: // LD5A
835             if ( !g_config.use_iso_enter )
836                 point->x -= 10;
837             break;
838         case 36+16: // LC16A
839             if ( !g_config.use_split_left_shift )
840                 point->x += 8;
841             break;
842 #endif
843 #if defined(RGB_BACKLIGHT_ZEAL60) || defined(RGB_BACKLIGHT_M60_A)
844         case 36+0: // LC0A
845             if ( g_config.use_7u_spacebar )
846                 point->x += 10;
847             break;
848         case 36+6: // LC6A
849             if ( g_config.use_7u_spacebar )
850                 point->x += 4;
851             break;
852         case 54+7: // LD7A
853             if ( !g_config.use_split_right_shift )
854                 point->x -= 8;
855             break;
856 #endif
857     }
858 }
859
860 void map_led_to_point_polar( uint8_t index, Point *point )
861 {
862     // Slightly messy way to get Point structs out of progmem.
863     uint8_t *addr = (uint8_t*)&g_map_led_to_point_polar[index];
864     point->x = pgm_read_byte(addr);
865     point->y = pgm_read_byte(addr+1);
866 }
867
868 //
869 // Maps switch matrix coordinate (row,col) to LED index
870 //
871
872
873 #if defined(RGB_BACKLIGHT_ZEAL65)
874 // Note: Left spacebar stab is at 4,2 (LC7)
875 // Right spacebar stab is at 4,9 (D14)
876 //
877 // A17, A16, A15, A14, A13, A12, A11, A10,  A9,  B0,  B1,  B2,  B3,  B4,  B6
878 //  A7,  A6,  A5,  A4,  A3,  A2,  A1,  A0,  B9, B10, B11, B12, B13, B14,  B7
879 //  A8, C14, C13, C12, C11, C10,  C9,  D0,  D1,  D2,  D3,  D4,  D5,  B5,  B8
880 // C16, C15,  C5,  C4,  C3,  C2,  C1,  D9, D10, D11, D12,  D6,  D7,  D8, B15
881 // C17,  C8,  C7, ---, ---, ---, ---,  C0, ---, D14, D15, D16, D17, B17, B16
882 const uint8_t g_map_row_column_to_led[MATRIX_ROWS][MATRIX_COLS] PROGMEM = {
883     {  0+17,  0+16,  0+15,  0+14,  0+13,  0+12,  0+11,  0+10,   0+9,  18+0,  18+1,  18+2,  18+3,  18+4,  18+6 },
884     {   0+7,   0+6,   0+5,   0+4,   0+3,   0+2,   0+1,   0+0,  18+9, 18+10, 18+11, 18+12, 18+13, 18+14,  18+7 },
885     {   0+8, 36+14, 36+13, 36+12, 36+11, 36+10,  36+9,  54+0,  54+1,  54+2,  54+3,  54+4,  54+5,  18+5,  18+8 },
886     { 36+16, 36+15,  36+5,  36+4,  36+3,  36+2,  36+1,  54+9, 54+10, 54+11, 54+12,  54+6,  54+7,  54+8, 18+15 },
887     { 36+17,  36+8,  36+7,   255,   255,   255,   255,  36+0,  255,  54+14, 54+15, 54+16, 54+17, 18+17, 18+16 }
888 };
889 #elif defined(RGB_BACKLIGHT_KOYU)
890 // Note: Left spacebar stab is at 4,4 (LC6)
891 // Right spacebar stab is at 4,10 (D14)
892 //
893 // A17, A16, A15, A14, A13, A12, A11, A10,  A9,  B0,  B1,  B2,  B3,  B4,  B6
894 //  A7,  A6,  A5,  A4,  A3,  A2,  A1,  A0,  B9, B10, B11, B12, B13, B14,  B7
895 //  A8, C14, C13, C12, C11, C10,  C9,  D0,  D1,  D2,  D3,  D4,  D5,  B5,  B8
896 // C16, C15,  C5,  C4,  C3,  C2,  C1,  D9, D10, D11, D12,  D6,  D7,  D8, B15
897 // C17,  C8,  C7,  C6, ---, ---, ---,  C0, ---, ---, D14, D15, D16, B17, B16
898 const uint8_t g_map_row_column_to_led[MATRIX_ROWS][MATRIX_COLS] PROGMEM = {
899     {  0+17,  0+16,  0+15,  0+14,  0+13,  0+12,  0+11,  0+10,   0+9,  18+0,  18+1,  18+2,  18+3,  18+4,  18+6 },
900     {   0+7,   0+6,   0+5,   0+4,   0+3,   0+2,   0+1,   0+0,  18+9, 18+10, 18+11, 18+12, 18+13, 18+14,  18+7 },
901     {   0+8, 36+14, 36+13, 36+12, 36+11, 36+10,  36+9,  54+0,  54+1,  54+2,  54+3,  54+4,  54+5,  18+5,  18+8 },
902     { 36+16, 36+15,  36+5,  36+4,  36+3,  36+2,  36+1,  54+9, 54+10, 54+11, 54+12,  54+6,  54+7,  54+8, 18+15 },
903     { 36+17,  36+8,  36+7,  36+6,   255,   255,   255,  36+0,  255,    255, 54+14, 54+15, 54+16, 18+17, 18+16 }
904 };
905 #elif defined(RGB_BACKLIGHT_ZEAL60) || defined(RGB_BACKLIGHT_M60_A)
906 // Note: Left spacebar stab is at 4,3 (LC6)
907 // Right spacebar stab is at 4,9 (LD13) or 4,10 (LD14)
908 //
909 // A17, A16, A15, A14, A13, A12, A11, A10,  A9,  B0,  B1,  B2,  B3,  B4,
910 //  A7,  A6,  A5,  A4,  A3,  A2,  A1,  A0,  B9, B10, B11, B12, B13, B14,
911 //  A8, C14, C13, C12, C11, C10,  C9,  D0,  D1,  D2,  D3,  D4,  D5,  B5,
912 // C16, C15,  C5,  C4,  C3,  C2,  C1,  D9, D10, D11, D12,  D6,  D7,  D8,
913 // C17,  C8,  C7,  C6, ---, ---, ---,  C0, ---, D13, D14, D15, D16, D17,
914 const uint8_t g_map_row_column_to_led[MATRIX_ROWS][MATRIX_COLS] PROGMEM = {
915     {  0+17,  0+16,  0+15,  0+14,  0+13,  0+12,  0+11,  0+10,   0+9,  18+0,  18+1,  18+2,  18+3,  18+4 },
916     {   0+7,   0+6,   0+5,   0+4,   0+3,   0+2,   0+1,   0+0,  18+9, 18+10, 18+11, 18+12, 18+13, 18+14 },
917     {   0+8, 36+14, 36+13, 36+12, 36+11, 36+10,  36+9,  54+0,  54+1,  54+2,  54+3,  54+4,  54+5,  18+5 },
918     { 36+16, 36+15,  36+5,  36+4,  36+3,  36+2,  36+1,  54+9, 54+10, 54+11, 54+12,  54+6,  54+7,  54+8 },
919     { 36+17,  36+8,  36+7,  36+6,   255,   255,   255,  36+0,  255,  54+13, 54+14, 54+15, 54+16, 54+17 }
920 };
921 #elif defined(RGB_BACKLIGHT_U80_A)
922 // Note: Left spacebar stab is at 5,3 (LC6)
923 // Right spacebar stab is at 5,10 (LD14)
924 const uint8_t g_map_row_column_to_led[MATRIX_ROWS][MATRIX_COLS] PROGMEM = {
925     { 72+17, 72+16, 72+15, 72+14,  72+9, 72+10, 72+11, 72+12, 72+13,  90+0,  90+1,  90+2,  90+3,   255,  90+4,  90+5,  90+6 },
926     {  0+17,  0+16,  0+15,  0+14,  0+13,  0+12,  0+11,  0+10,   0+9,  18+0,  18+1,  18+2,  18+3,  18+4, 90+11,  90+9,  90+7 },
927     {   0+7,   0+6,   0+5,   0+4,   0+3,   0+2,   0+1,   0+0,  18+9, 18+10, 18+11, 18+12, 18+13, 18+14, 90+12, 90+10,  90+8 },
928     {   0+8, 36+14, 36+13, 36+12, 36+11, 36+10,  36+9,  54+0,  54+1,  54+2,  54+3,  54+4,  54+5,   255,   255,   255,   255 },
929     { 36+16,   255,  36+5,  36+4,  36+3,  36+2,  36+1,  54+9, 54+10, 54+11, 54+12,  54+6,  54+7,   255,   255, 90+14,   255 },
930     { 36+17,  36+8,  36+7,  36+6,   255,   255,   255,  36+0,  255,    255, 54+14, 54+15, 54+16, 54+17, 90+15, 90+16, 90+17 }
931 };
932 #elif defined(RGB_BACKLIGHT_HS60) && defined(HS60_ANSI)
933 //
934 // LA1,  LA5,  LA9,  LA13, LA17, LA21, LA25, LA29, LA33, LA37, LA41, LA45, LA49, LA53,
935 // LA2,  LA6,  LA10, LA14, LA18, LA22, LA26, LA30, LA34, LA38, LA42, LA46, LA50,  ---,
936 // LA3,  LA7,  LA11, LA15, LA19, LA23, LA27, LA31, LA35, LA39, LA43, LA47, LA54, LA55,
937 // LA4,  ---,   LA8, LA12, LA16, LA20, LA24, LA28, LA32, LA36, LA40, LA44,  ---, LA52,
938 // LA57, LA58, LA59,  ---,  ---,  ---, LA60,  ---,  ---,  ---, LA62, LA63, LA64, LA56
939 const uint8_t g_map_row_column_to_led[MATRIX_ROWS][MATRIX_COLS] PROGMEM = {
940     {  1-1,  5-1,  9-1, 13-1, 17-1, 21-1, 25-1, 29-1, 33-1, 37-1, 41-1, 45-1, 49-1, 53-1 },
941     {  2-1,  6-1, 10-1, 14-1, 18-1, 22-1, 26-1, 30-1, 34-1, 38-1, 42-1, 46-1, 50-1,  255 },
942     {  3-1,  7-1, 11-1, 15-1, 19-1, 23-1, 27-1, 31-1, 35-1, 39-1, 43-1, 47-1, 54-1, 55-1 },
943     {  4-1,  255,  8-1, 12-1, 16-1, 20-1, 24-1, 28-1, 32-1, 36-1, 40-1, 44-1,  255, 52-1 },
944     { 57-1, 58-1, 59-1,  255,  255,  255, 60-1,  255,  255,  255, 62-1, 63-1, 64-1, 56-1 }
945 };
946 #elif defined(RGB_BACKLIGHT_HS60) && defined(HS60_HHKB)
947 //
948 // LA1,  LA5,   LA9, LA13, LA17, LA21, LA25, LA29, LA33, LA37, LA41, LA45, LA49, LA53,
949 // LA2,  LA6,  LA10, LA14, LA18, LA22, LA26, LA30, LA34, LA38, LA42, LA46, LA50, LA48,
950 // LA3,  LA7,  LA11, LA15, LA19, LA23, LA27, LA31, LA35, LA39, LA43, LA47, LA54, LA55,
951 // LA4,  ---,   LA8, LA12, LA16, LA20, LA24, LA28, LA32, LA36, LA40, LA44, LA51, LA52,
952 // LA57, LA58, LA59,  ---,  ---,  ---, LA60,  ---,  ---,  ---,  ---, LA63, LA64, LA56
953 const uint8_t g_map_row_column_to_led[MATRIX_ROWS][MATRIX_COLS] PROGMEM = {
954     {  1-1,  5-1,  9-1, 13-1, 17-1, 21-1, 25-1, 29-1, 33-1, 37-1, 41-1, 45-1, 49-1, 53-1 },
955     {  2-1,  6-1, 10-1, 14-1, 18-1, 22-1, 26-1, 30-1, 34-1, 38-1, 42-1, 46-1, 50-1, 48-1 },
956     {  3-1,  7-1, 11-1, 15-1, 19-1, 23-1, 27-1, 31-1, 35-1, 39-1, 43-1, 47-1, 54-1, 55-1 },
957     {  4-1,  255,  8-1, 12-1, 16-1, 20-1, 24-1, 28-1, 32-1, 36-1, 40-1, 44-1, 51-1, 52-1 },
958     { 57-1, 58-1, 59-1,  255,  255,  255, 60-1,  255,  255,  255,  255, 63-1, 64-1, 56-1 }
959 };
960 #elif defined(RGB_BACKLIGHT_HS60) //HS60_ISO
961 //
962 // LA1,  LA5,   LA9, LA13, LA17, LA21, LA25, LA29, LA33, LA37, LA41, LA45, LA49, LA53,
963 // LA2,  LA6,  LA10, LA14, LA18, LA22, LA26, LA30, LA34, LA38, LA42, LA46, LA50,  ---,
964 // LA3,  LA7,  LA11, LA15, LA19, LA23, LA27, LA31, LA35, LA39, LA43, LA47, LA54, LA55,
965 // LA4,  LA48,  LA8, LA12, LA16, LA20, LA24, LA28, LA32, LA36, LA40, LA44,  ---, LA52,
966 // LA57, LA58, LA59,  ---,  ---,  ---, LA60,  ---,  ---,  ---, LA62, LA63, LA64, LA56
967 const uint8_t g_map_row_column_to_led[MATRIX_ROWS][MATRIX_COLS] PROGMEM = {
968     {  1-1,  5-1,  9-1, 13-1, 17-1, 21-1, 25-1, 29-1, 33-1, 37-1, 41-1, 45-1, 49-1, 53-1 },
969     {  2-1,  6-1, 10-1, 14-1, 18-1, 22-1, 26-1, 30-1, 34-1, 38-1, 42-1, 46-1, 50-1,  255 },
970     {  3-1,  7-1, 11-1, 15-1, 19-1, 23-1, 27-1, 31-1, 35-1, 39-1, 43-1, 47-1, 54-1, 55-1 },
971     {  4-1, 48-1,  8-1, 12-1, 16-1, 20-1, 24-1, 28-1, 32-1, 36-1, 40-1, 44-1,  255, 52-1 },
972     { 57-1, 58-1, 59-1,  255,  255,  255, 60-1,  255,  255,  255, 62-1, 63-1, 64-1, 56-1 }
973 };
974 #elif defined(RGB_BACKLIGHT_NK65)
975 //
976 // LA1,  LA5,  LA9,  LA13, LA17, LA21, LA25, LA29, LA33, LA37, LA41, LA45, LA49, LA53, LB1,
977 // LA2,  LA6,  LA10, LA14, LA18, LA22, LA26, LA30, LA34, LA38, LA42, LA46, LA50,  ---, LB2,
978 // LA3,  LA7,  LA11, LA15, LA19, LA23, LA27, LA31, LA35, LA39, LA43, LA47, LA54, LA55, LB5,
979 // LA4,  ---,   LA8, LA12, LA16, LA20, LA24, LA28, LA32, LA36, LA40, LA44, LA51, LA52, LB3,
980 // LA57, LA58, LA59,  ---,  ---,  ---, LA60,  ---,  ---, LA48, LA62, LA63, LA64, LA56, LB4
981 const uint8_t g_map_row_column_to_led[MATRIX_ROWS][MATRIX_COLS] PROGMEM = {
982     {  1-1,  5-1,  9-1, 13-1, 17-1, 21-1, 25-1, 29-1, 33-1, 37-1, 41-1, 45-1, 49-1, 53-1, 1+64-1 },
983     {  2-1,  6-1, 10-1, 14-1, 18-1, 22-1, 26-1, 30-1, 34-1, 38-1, 42-1, 46-1, 50-1,  255, 2+64-1 },
984     {  3-1,  7-1, 11-1, 15-1, 19-1, 23-1, 27-1, 31-1, 35-1, 39-1, 43-1, 47-1, 54-1, 55-1, 5+64-1 },
985     {  4-1,  255,  8-1, 12-1, 16-1, 20-1, 24-1, 28-1, 32-1, 36-1, 40-1, 44-1, 51-1, 52-1, 3+64-1 },
986     { 57-1, 58-1, 59-1,  255,  255,  255, 60-1,  255,  255, 48-1, 62-1, 63-1, 64-1, 56-1, 4+64-1 }
987 };
988 #elif defined(RGB_BACKLIGHT_M6_B)
989 // M6-B is really simple:
990 // 0 3 5
991 // 1 2 4
992 const uint8_t g_map_row_column_to_led[MATRIX_ROWS][MATRIX_COLS] PROGMEM = {
993     {     0,     3,     5,     1,     2,     4 }
994 };
995 #endif
996
997 void map_row_column_to_led( uint8_t row, uint8_t column, uint8_t *led )
998 {
999     *led = 255;
1000     if ( row < MATRIX_ROWS && column < MATRIX_COLS )
1001     {
1002         *led = pgm_read_byte(&g_map_row_column_to_led[row][column]);
1003     }
1004 }
1005
1006 void backlight_update_pwm_buffers(void)
1007 {
1008 #if defined(RGB_BACKLIGHT_M6_B)
1009     IS31FL3218_update_pwm_buffers();
1010 #elif defined(RGB_BACKLIGHT_HS60)
1011     IS31FL3733_update_pwm_buffers( ISSI_ADDR_1, 0 );
1012     IS31FL3733_update_led_control_registers( ISSI_ADDR_1, 0 );
1013 #elif defined(RGB_BACKLIGHT_NK65)
1014     IS31FL3733_update_pwm_buffers( ISSI_ADDR_1, 0 );
1015     IS31FL3733_update_pwm_buffers( ISSI_ADDR_2, 1 );
1016     IS31FL3733_update_led_control_registers( ISSI_ADDR_1, 0 );
1017     IS31FL3733_update_led_control_registers( ISSI_ADDR_2, 1 );
1018 #elif defined(RGB_BACKLIGHT_U80_A)
1019     static uint8_t driver = 0;
1020     switch ( driver )
1021     {
1022         case 0:
1023             IS31FL3731_update_pwm_buffers( ISSI_ADDR_1, 0 );
1024             break;
1025         case 1:
1026             IS31FL3731_update_pwm_buffers( ISSI_ADDR_2, 1 );
1027             break;
1028         case 2:
1029             IS31FL3731_update_pwm_buffers( ISSI_ADDR_3, 2 );
1030             break;
1031     }
1032     if ( ++driver > 2 )
1033     {
1034         driver = 0;
1035     }
1036 #else
1037     IS31FL3731_update_pwm_buffers( ISSI_ADDR_1, 0 );
1038     IS31FL3731_update_pwm_buffers( ISSI_ADDR_2, 1 );
1039     IS31FL3731_update_led_control_registers( ISSI_ADDR_1, 0 );
1040     IS31FL3731_update_led_control_registers( ISSI_ADDR_2, 1 );
1041 #endif
1042 }
1043
1044 void backlight_set_color( int index, uint8_t red, uint8_t green, uint8_t blue )
1045 {
1046 #if defined(RGB_BACKLIGHT_M6_B)
1047     IS31FL3218_set_color( index, red, green, blue );
1048 #elif defined(RGB_BACKLIGHT_HS60) || defined(RGB_BACKLIGHT_NK65)
1049     IS31FL3733_set_color( index, red, green, blue );
1050 #else
1051     IS31FL3731_set_color( index, red, green, blue );
1052 #endif
1053 }
1054
1055 void backlight_set_color_all( uint8_t red, uint8_t green, uint8_t blue )
1056 {
1057 #if defined(RGB_BACKLIGHT_M6_B)
1058     IS31FL3218_set_color_all( red, green, blue );
1059 #elif defined(RGB_BACKLIGHT_HS60) || defined(RGB_BACKLIGHT_NK65)
1060     IS31FL3733_set_color_all( red, green, blue );
1061 #else
1062     IS31FL3731_set_color_all( red, green, blue );
1063 #endif
1064 }
1065
1066 void backlight_set_key_hit(uint8_t row, uint8_t column)
1067 {
1068     uint8_t led;
1069     map_row_column_to_led(row,column,&led);
1070     g_key_hit[led] = 0;
1071
1072     g_any_key_hit = 0;
1073 }
1074
1075 #if !defined(RGB_BACKLIGHT_HS60) && !defined(RGB_BACKLIGHT_NK65)
1076 // This is (F_CPU/1024) / 20 Hz
1077 // = 15625 Hz / 20 Hz
1078 // = 781
1079 #define TIMER3_TOP 781
1080
1081 void backlight_timer_init(void)
1082 {
1083     static uint8_t backlight_timer_is_init = 0;
1084     if ( backlight_timer_is_init )
1085     {
1086         return;
1087     }
1088     backlight_timer_is_init = 1;
1089
1090     // Timer 3 setup
1091     TCCR3B = _BV(WGM32) |           // CTC mode OCR3A as TOP
1092              _BV(CS32) | _BV(CS30); // prescale by /1024
1093     // Set TOP value
1094     uint8_t sreg = SREG;
1095     cli();
1096
1097     OCR3AH = (TIMER3_TOP >> 8) & 0xff;
1098     OCR3AL = TIMER3_TOP & 0xff;
1099     SREG = sreg;
1100 }
1101
1102 void backlight_timer_enable(void)
1103 {
1104     TIMSK3 |= _BV(OCIE3A);
1105 }
1106
1107 void backlight_timer_disable(void)
1108 {
1109     TIMSK3 &= ~_BV(OCIE3A);
1110 }
1111 #else //STM32, use GPT with TIM4. Enable in halconf.h
1112 static void gpt_backlight_timer_task(GPTDriver *gptp);
1113 // Timer setup at 200Khz, callback at 10k ticks = 20Hz
1114 static GPTConfig gpt4cfg1 = {
1115     .frequency = 200000U,
1116     .callback  = gpt_backlight_timer_task
1117 };
1118
1119 void backlight_timer_init(void)
1120 {
1121     gptStart(&GPTD4, &gpt4cfg1);
1122 }
1123
1124 void backlight_timer_enable(void)
1125 {
1126     gptStartContinuous(&GPTD4, 10000);
1127 }
1128
1129 void backlight_timer_disable(void)
1130 {
1131     gptStopTimer(&GPTD4);
1132 }
1133 #endif //!defined(RGB_BACKLIGHT_HS60) && !defined(RGB_BACKLIGHT_NK65)
1134
1135 void backlight_set_suspend_state(bool state)
1136 {
1137     g_suspend_state = state;
1138 }
1139
1140 void backlight_set_indicator_state(uint8_t state)
1141 {
1142     g_indicator_state = state;
1143 }
1144
1145 void backlight_effect_rgb_test(void)
1146 {
1147     // Mask out bits 4 and 5
1148     // This 2-bit value will stay the same for 16 ticks.
1149     switch ( (g_tick & 0x30) >> 4 )
1150     {
1151         case 0:
1152         {
1153             backlight_set_color_all( 255, 0, 0 );
1154             break;
1155         }
1156         case 1:
1157         {
1158             backlight_set_color_all( 0, 255, 0 );
1159             break;
1160         }
1161         case 2:
1162         {
1163             backlight_set_color_all( 0, 0, 255 );
1164             break;
1165         }
1166         case 3:
1167         {
1168             backlight_set_color_all( 255, 255, 255 );
1169             break;
1170         }
1171     }
1172 }
1173
1174 #if defined(RGB_DEBUGGING_ONLY)
1175 // This tests the LEDs
1176 // Note that it will change the LED control registers
1177 // in the LED drivers, and leave them in an invalid
1178 // state for other backlight effects.
1179 // ONLY USE THIS FOR TESTING LEDS!
1180 void backlight_effect_single_LED_test(void)
1181 {
1182     static uint8_t color = 0; // 0,1,2 for R,G,B
1183     static uint8_t row = 0;
1184     static uint8_t column = 0;
1185
1186     static uint8_t tick = 0;
1187     tick++;
1188
1189     if ( tick > 2 )
1190     {
1191         tick = 0;
1192         column++;
1193     }
1194     if ( column > 14 )
1195     {
1196         column = 0;
1197         row++;
1198     }
1199     if ( row > 4 )
1200     {
1201         row = 0;
1202         color++;
1203     }
1204     if ( color > 2 )
1205     {
1206         color = 0;
1207     }
1208
1209     uint8_t led;
1210     map_row_column_to_led( row, column, &led );
1211     backlight_set_color_all( 255, 255, 255 );
1212     backlight_test_led( led, color==0, color==1, color==2 );
1213 }
1214 #endif // defined(RGB_DEBUGGING_ONLY)
1215
1216 // All LEDs off
1217 void backlight_effect_all_off(void)
1218 {
1219     backlight_set_color_all( 0, 0, 0 );
1220 }
1221
1222 // Solid color
1223 void backlight_effect_solid_color(void)
1224 {
1225     HSV hsv = { .h = g_config.color_1.h, .s = g_config.color_1.s, .v = g_config.brightness };
1226     RGB rgb = hsv_to_rgb( hsv );
1227     backlight_set_color_all( rgb.r, rgb.g, rgb.b );
1228 }
1229
1230 // alphas = color1, mods = color2
1231 void backlight_effect_alphas_mods(void)
1232 {
1233     RGB rgb1 = hsv_to_rgb( (HSV){ .h = g_config.color_1.h, .s = g_config.color_1.s, .v = g_config.brightness } );
1234     RGB rgb2 = hsv_to_rgb( (HSV){ .h = g_config.color_2.h, .s = g_config.color_2.s, .v = g_config.brightness } );
1235     bool is_alpha = false;
1236     for ( int row = 0; row < MATRIX_ROWS; row++ )
1237     {
1238         for ( int column = 0; column < MATRIX_COLS; column++ )
1239         {
1240             uint8_t index;
1241             map_row_column_to_led( row, column, &index );
1242             if ( index < BACKLIGHT_LED_COUNT )
1243             {
1244 #if defined(RGB_BACKLIGHT_U80_A)
1245                 if ( row == 0 )
1246                 {
1247                     is_alpha = ( column < 16 ) && (( 0b1110000111100001 & (1<<column) ) == 0);
1248                 }
1249                 else
1250                 {
1251                     is_alpha = ( column < 16 ) && (( g_config.alphas_mods[row-1] & (1<<column) ) == 0);
1252                 }
1253 #else
1254                 is_alpha = ( g_config.alphas_mods[row] & (1<<column) ) == 0;
1255 #endif
1256                 if ( is_alpha )
1257                 {
1258                     backlight_set_color( index, rgb1.r, rgb1.g, rgb1.b );
1259                 }
1260                 else
1261                 {
1262                     backlight_set_color( index, rgb2.r, rgb2.g, rgb2.b );
1263                 }
1264             }
1265         }
1266     }
1267 }
1268
1269 void backlight_effect_gradient_up_down(void)
1270 {
1271     int16_t h1 = g_config.color_1.h;
1272     int16_t h2 = g_config.color_2.h;
1273     int16_t deltaH = h2 - h1;
1274
1275     // Take the shortest path between hues
1276     if ( deltaH > 127 )
1277     {
1278         deltaH -= 256;
1279     }
1280     else if ( deltaH < -127 )
1281     {
1282         deltaH += 256;
1283     }
1284     // Divide delta by 4, this gives the delta per row
1285     deltaH /= 4;
1286
1287     int16_t s1 = g_config.color_1.s;
1288     int16_t s2 = g_config.color_2.s;
1289     int16_t deltaS = ( s2 - s1 ) / 4;
1290
1291     HSV hsv = { .h = 0, .s = 255, .v = g_config.brightness };
1292     RGB rgb;
1293     Point point;
1294     for ( int i=0; i<BACKLIGHT_LED_COUNT; i++ )
1295     {
1296         map_led_to_point( i, &point );
1297         // The y range will be 0..64, map this to 0..4
1298         uint8_t y = (point.y>>4);
1299         // Relies on hue being 8-bit and wrapping
1300         hsv.h = g_config.color_1.h + ( deltaH * y );
1301         hsv.s = g_config.color_1.s + ( deltaS * y );
1302         rgb = hsv_to_rgb( hsv );
1303         backlight_set_color( i, rgb.r, rgb.g, rgb.b );
1304     }
1305 }
1306
1307 void backlight_effect_raindrops(bool initialize)
1308 {
1309     int16_t h1 = g_config.color_1.h;
1310     int16_t h2 = g_config.color_2.h;
1311     int16_t deltaH = h2 - h1;
1312     deltaH /= 4;
1313
1314     // Take the shortest path between hues
1315     if ( deltaH > 127 )
1316     {
1317         deltaH -= 256;
1318     }
1319     else if ( deltaH < -127 )
1320     {
1321         deltaH += 256;
1322     }
1323
1324     int16_t s1 = g_config.color_1.s;
1325     int16_t s2 = g_config.color_2.s;
1326     int16_t deltaS = ( s2 - s1 ) / 4;
1327
1328     HSV hsv;
1329     RGB rgb;
1330
1331     // Change one LED every tick
1332     uint8_t led_to_change = ( g_tick & 0x000 ) == 0 ? rand() % BACKLIGHT_LED_COUNT : 255;
1333
1334     for ( int i=0; i<BACKLIGHT_LED_COUNT; i++ )
1335     {
1336         // If initialize, all get set to random colors
1337         // If not, all but one will stay the same as before.
1338         if ( initialize || i == led_to_change )
1339         {
1340             hsv.h = h1 + ( deltaH * ( rand() & 0x03 ) );
1341             hsv.s = s1 + ( deltaS * ( rand() & 0x03 ) );
1342             // Override brightness with global brightness control
1343             hsv.v = g_config.brightness;;
1344
1345             rgb = hsv_to_rgb( hsv );
1346             backlight_set_color( i, rgb.r, rgb.g, rgb.b );
1347         }
1348     }
1349 }
1350
1351 void backlight_effect_cycle_all(void)
1352 {
1353     uint8_t offset = ( g_tick << g_config.effect_speed ) & 0xFF;
1354
1355     // Relies on hue being 8-bit and wrapping
1356     for ( int i=0; i<BACKLIGHT_LED_COUNT; i++ )
1357     {
1358         uint16_t offset2 = g_key_hit[i]<<2;
1359 #if !defined(RGB_BACKLIGHT_HS60) && !defined(RGB_BACKLIGHT_NK65)
1360         // stabilizer LEDs use spacebar hits
1361         if ( i == 36+6 || i == 54+13 || // LC6, LD13
1362                 ( g_config.use_7u_spacebar && i == 54+14 ) ) // LD14
1363         {
1364             offset2 = g_key_hit[36+0]<<2;
1365         }
1366 #endif
1367         offset2 = (offset2<=63) ? (63-offset2) : 0;
1368
1369         HSV hsv = { .h = offset+offset2, .s = 255, .v = g_config.brightness };
1370         RGB rgb = hsv_to_rgb( hsv );
1371         backlight_set_color( i, rgb.r, rgb.g, rgb.b );
1372     }
1373 }
1374
1375 void backlight_effect_cycle_left_right(void)
1376 {
1377     uint8_t offset = ( g_tick << g_config.effect_speed ) & 0xFF;
1378     HSV hsv = { .h = 0, .s = 255, .v = g_config.brightness };
1379     RGB rgb;
1380     Point point;
1381     for ( int i=0; i<BACKLIGHT_LED_COUNT; i++ )
1382     {
1383         uint16_t offset2 = g_key_hit[i]<<2;
1384 #if !defined(RGB_BACKLIGHT_HS60) && !defined(RGB_BACKLIGHT_NK65)
1385         // stabilizer LEDs use spacebar hits
1386         if ( i == 36+6 || i == 54+13 || // LC6, LD13
1387                 ( g_config.use_7u_spacebar && i == 54+14 ) ) // LD14
1388         {
1389             offset2 = g_key_hit[36+0]<<2;
1390         }
1391 #endif
1392         offset2 = (offset2<=63) ? (63-offset2) : 0;
1393
1394         map_led_to_point( i, &point );
1395         // Relies on hue being 8-bit and wrapping
1396         hsv.h = point.x + offset + offset2;
1397         rgb = hsv_to_rgb( hsv );
1398         backlight_set_color( i, rgb.r, rgb.g, rgb.b );
1399     }
1400 }
1401
1402 void backlight_effect_cycle_up_down(void)
1403 {
1404     uint8_t offset = ( g_tick << g_config.effect_speed ) & 0xFF;
1405     HSV hsv = { .h = 0, .s = 255, .v = g_config.brightness };
1406     RGB rgb;
1407     Point point;
1408     for ( int i=0; i<BACKLIGHT_LED_COUNT; i++ )
1409     {
1410         uint16_t offset2 = g_key_hit[i]<<2;
1411 #if !defined(RGB_BACKLIGHT_HS60) && !defined(RGB_BACKLIGHT_NK65)
1412         // stabilizer LEDs use spacebar hits
1413         if ( i == 36+6 || i == 54+13 || // LC6, LD13
1414                 ( g_config.use_7u_spacebar && i == 54+14 ) ) // LD14
1415         {
1416             offset2 = g_key_hit[36+0]<<2;
1417         }
1418 #endif
1419         offset2 = (offset2<=63) ? (63-offset2) : 0;
1420
1421         map_led_to_point( i, &point );
1422         // Relies on hue being 8-bit and wrapping
1423         hsv.h = point.y + offset + offset2;
1424         rgb = hsv_to_rgb( hsv );
1425         backlight_set_color( i, rgb.r, rgb.g, rgb.b );
1426     }
1427 }
1428
1429 void backlight_effect_jellybean_raindrops( bool initialize )
1430 {
1431     HSV hsv;
1432     RGB rgb;
1433
1434     // Change one LED every tick
1435     uint8_t led_to_change = ( g_tick & 0x000 ) == 0 ? rand() % BACKLIGHT_LED_COUNT : 255;
1436
1437     for ( int i=0; i<BACKLIGHT_LED_COUNT; i++ )
1438     {
1439         // If initialize, all get set to random colors
1440         // If not, all but one will stay the same as before.
1441         if ( initialize || i == led_to_change )
1442         {
1443             hsv.h = rand() & 0xFF;
1444             hsv.s = rand() & 0xFF;
1445             // Override brightness with global brightness control
1446             hsv.v = g_config.brightness;;
1447
1448             rgb = hsv_to_rgb( hsv );
1449             backlight_set_color( i, rgb.r, rgb.g, rgb.b );
1450         }
1451     }
1452 }
1453
1454 void backlight_effect_cycle_radial1(void)
1455 {
1456     uint8_t offset = ( g_tick << g_config.effect_speed ) & 0xFF;
1457     HSV hsv = { .h = 0, .s = 255, .v = g_config.brightness };
1458     RGB rgb;
1459     Point point;
1460     for ( int i=0; i<BACKLIGHT_LED_COUNT; i++ )
1461     {
1462         map_led_to_point_polar( i, &point );
1463         // Relies on hue being 8-bit and wrapping
1464         hsv.h = point.x + offset;
1465         hsv.s = point.y;
1466         rgb = hsv_to_rgb( hsv );
1467         backlight_set_color( i, rgb.r, rgb.g, rgb.b );
1468     }
1469 }
1470
1471 void backlight_effect_cycle_radial2(void)
1472 {
1473     uint8_t offset = ( g_tick << g_config.effect_speed ) & 0xFF;
1474
1475     HSV hsv = { .h = 0, .s = g_config.color_1.s, .v = g_config.brightness };
1476     RGB rgb;
1477     Point point;
1478     for ( int i=0; i<BACKLIGHT_LED_COUNT; i++ )
1479     {
1480         map_led_to_point_polar( i, &point );
1481         uint8_t offset2 = offset + point.x;
1482         if ( offset2 & 0x80 )
1483         {
1484             offset2 = ~offset2;
1485         }
1486         offset2 = offset2 >> 2;
1487         hsv.h = g_config.color_1.h + offset2;
1488         hsv.s = 127 + ( point.y >> 1 );
1489         rgb = hsv_to_rgb( hsv );
1490         backlight_set_color( i, rgb.r, rgb.g, rgb.b );
1491     }
1492 }
1493
1494 #if defined(RGB_BACKLIGHT_M6_B)
1495 void backlight_effect_custom_colors(void)
1496 {
1497     RGB rgb;
1498     for ( uint8_t i = 0; i < 6; i++ )
1499     {
1500         HSV hsv = { .h = g_config.custom_color[i].h, .s = g_config.custom_color[i].s, .v = g_config.brightness };
1501         rgb = hsv_to_rgb( hsv );
1502         uint8_t led;
1503         map_row_column_to_led( 0, i, &led );
1504         backlight_set_color( led, rgb.r, rgb.g, rgb.b );
1505     }
1506 }
1507 #endif
1508
1509 void backlight_effect_indicators_set_colors( uint8_t index, HS color )
1510 {
1511     HSV hsv = { .h = color.h, .s = color.s, .v = g_config.brightness };
1512     RGB rgb = hsv_to_rgb( hsv );
1513     if ( index == 254 )
1514     {
1515         backlight_set_color_all( rgb.r, rgb.g, rgb.b );
1516     }
1517     else
1518     {
1519         backlight_set_color( index, rgb.r, rgb.g, rgb.b );
1520
1521         // If the spacebar LED is the indicator,
1522         // do the same for the spacebar stabilizers
1523         if ( index == 36+0 ) // LC0
1524         {
1525 #if defined(RGB_BACKLIGHT_ZEAL65)
1526             backlight_set_color( 36+7, rgb.r, rgb.g, rgb.b ); // LC7
1527             backlight_set_color( 54+14, rgb.r, rgb.g, rgb.b ); // LD14
1528 #elif defined(RGB_BACKLIGHT_KOYU)
1529             backlight_set_color( 36+6, rgb.r, rgb.g, rgb.b ); // LC6
1530             backlight_set_color( 54+14, rgb.r, rgb.g, rgb.b ); // LD14
1531 #elif defined(RGB_BACKLIGHT_ZEAL60) || defined(RGB_BACKLIGHT_M60_A)
1532             backlight_set_color( 36+6, rgb.r, rgb.g, rgb.b ); // LC6
1533             backlight_set_color( 54+13, rgb.r, rgb.g, rgb.b ); // LD13
1534             if ( g_config.use_7u_spacebar )
1535             {
1536                 backlight_set_color( 54+14, rgb.r, rgb.g, rgb.b ); // LD14
1537             }
1538 #endif
1539         }
1540     }
1541 }
1542
1543 // This runs after another backlight effect and replaces
1544 // colors already set
1545 void backlight_effect_indicators(void)
1546 {
1547     if ( g_config.caps_lock_indicator.index != 255 &&
1548             ( g_indicator_state & (1<<USB_LED_CAPS_LOCK) ) )
1549     {
1550         backlight_effect_indicators_set_colors( g_config.caps_lock_indicator.index, g_config.caps_lock_indicator.color );
1551     }
1552
1553 #if defined(RGB_BACKLIGHT_NK65)
1554     if ( g_indicator_state & (1<<USB_LED_CAPS_LOCK) )
1555     {
1556         IS31FL3733_set_color( 7+64-1, 0, 255, 0 );
1557     } else {
1558         IS31FL3733_set_color( 7+64-1, 0, 0, 0 );
1559     }
1560     if ( g_indicator_state & (1<<USB_LED_SCROLL_LOCK) )
1561     {
1562         IS31FL3733_set_color( 6+64-1, 255, 0, 255 );
1563     } else {
1564         IS31FL3733_set_color( 6+64-1, 0, 0, 0 );
1565     }
1566     if ( g_indicator_state & (1<<USB_LED_NUM_LOCK) )
1567     {
1568         IS31FL3733_set_color( 6+64-1, 0, 255, 0 );
1569     } else {
1570         IS31FL3733_set_color( 6+64-1, 0, 0, 0 );
1571     }
1572 #endif
1573
1574     // This if/else if structure allows higher layers to
1575     // override lower ones. If we set layer 3's indicator
1576     // to none, then it will NOT show layer 2 or layer 1
1577     // indicators, even if those layers are on via the
1578     // MO13/MO23 Fn combo magic.
1579     //
1580     // Basically we want to handle the case where layer 3 is
1581     // still the backlight configuration layer and we don't
1582     // want "all LEDs" indicators hiding the backlight effect,
1583     // but still allow end users to do whatever they want.
1584     if ( IS_LAYER_ON(3) )
1585     {
1586         if ( g_config.layer_3_indicator.index != 255 )
1587         {
1588             backlight_effect_indicators_set_colors( g_config.layer_3_indicator.index, g_config.layer_3_indicator.color );
1589         }
1590     }
1591     else if ( IS_LAYER_ON(2) )
1592     {
1593         if ( g_config.layer_2_indicator.index != 255 )
1594         {
1595             backlight_effect_indicators_set_colors( g_config.layer_2_indicator.index, g_config.layer_2_indicator.color );
1596         }
1597     }
1598     else if ( IS_LAYER_ON(1) )
1599     {
1600         if ( g_config.layer_1_indicator.index != 255 )
1601         {
1602             backlight_effect_indicators_set_colors( g_config.layer_1_indicator.index, g_config.layer_1_indicator.color );
1603         }
1604     }
1605 }
1606
1607 #if !defined(RGB_BACKLIGHT_HS60) && !defined(RGB_BACKLIGHT_NK65)
1608 ISR(TIMER3_COMPA_vect)
1609 #else //STM32 interrupt
1610 static void gpt_backlight_timer_task(GPTDriver *gptp)
1611 #endif
1612 {
1613     // delay 1 second before driving LEDs or doing anything else
1614     static uint8_t startup_tick = 0;
1615     if ( startup_tick < 20 )
1616     {
1617         startup_tick++;
1618         return;
1619     }
1620
1621     g_tick++;
1622
1623     if ( g_any_key_hit < 0xFFFFFFFF )
1624     {
1625         g_any_key_hit++;
1626     }
1627
1628     for ( int led = 0; led < BACKLIGHT_LED_COUNT; led++ )
1629     {
1630         if ( g_key_hit[led] < 255 )
1631         {
1632             g_key_hit[led]++;
1633         }
1634     }
1635
1636     // Factory default magic value
1637     if ( g_config.effect == 255 )
1638     {
1639         backlight_effect_rgb_test();
1640         return;
1641     }
1642
1643     // Ideally we would also stop sending zeros to the LED driver PWM buffers
1644     // while suspended and just do a software shutdown. This is a cheap hack for now.
1645     bool suspend_backlight = ((g_suspend_state && g_config.disable_when_usb_suspended) ||
1646             (g_config.disable_after_timeout > 0 && g_any_key_hit > g_config.disable_after_timeout * 60 * 20));
1647     uint8_t effect = suspend_backlight ? 0 : g_config.effect;
1648
1649     // Keep track of the effect used last time,
1650     // detect change in effect, so each effect can
1651     // have an optional initialization.
1652     static uint8_t effect_last = 255;
1653     bool initialize = effect != effect_last;
1654     effect_last = effect;
1655
1656     // this gets ticked at 20 Hz.
1657     // each effect can opt to do calculations
1658     // and/or request PWM buffer updates.
1659     switch ( effect )
1660     {
1661         case 0:
1662             backlight_effect_all_off();
1663             break;
1664         case 1:
1665             backlight_effect_solid_color();
1666             break;
1667         case 2:
1668 #if defined(RGB_BACKLIGHT_M6_B)
1669             backlight_effect_custom_colors();
1670 #else
1671             backlight_effect_alphas_mods();
1672 #endif
1673             break;
1674         case 3:
1675             backlight_effect_gradient_up_down();
1676             break;
1677         case 4:
1678             backlight_effect_raindrops( initialize );
1679             break;
1680         case 5:
1681             backlight_effect_cycle_all();
1682             break;
1683         case 6:
1684             backlight_effect_cycle_left_right();
1685             break;
1686         case 7:
1687             backlight_effect_cycle_up_down();
1688             break;
1689         case 8:
1690             backlight_effect_jellybean_raindrops( initialize );
1691             break;
1692         case 9:
1693             backlight_effect_cycle_radial1();
1694             break;
1695         case 10:
1696             backlight_effect_cycle_radial2();
1697             break;
1698         default:
1699             backlight_effect_all_off();
1700             break;
1701     }
1702
1703     if ( ! suspend_backlight )
1704     {
1705 #if !defined(RGB_BACKLIGHT_M6_B)
1706         backlight_effect_indicators();
1707 #endif
1708     }
1709 }
1710
1711 void backlight_set_indicator_index( uint8_t *index, uint8_t row, uint8_t column )
1712 {
1713     if ( row >= MATRIX_ROWS )
1714     {
1715         // Special value, 255=none, 254=all
1716         *index = row;
1717     }
1718     else
1719     {
1720         map_row_column_to_led( row, column, index );
1721     }
1722 }
1723
1724 void backlight_get_indicator_row_col( uint8_t index, uint8_t *row, uint8_t *column )
1725 {
1726     if ( index == 255 || index == 254 )
1727     {
1728         // Special value, 255=none, 254=all
1729         *row = index;
1730         *column = 0;
1731         return;
1732     }
1733     for ( uint8_t r = 0; r < MATRIX_ROWS; r++ )
1734     {
1735         for ( uint8_t c = 0; c < MATRIX_COLS; c++ )
1736         {
1737             uint8_t i = 255;
1738             map_row_column_to_led( r, c, &i );
1739             if ( i == index )
1740             {
1741                 *row = r;
1742                 *column = c;
1743                 return;
1744             }
1745         }
1746     }
1747 }
1748
1749 // Some helpers for setting/getting HSV
1750 void _set_color( HS *color, uint8_t *data )
1751 {
1752     color->h = data[0];
1753     color->s = data[1];
1754 }
1755
1756 void _get_color( HS *color, uint8_t *data )
1757 {
1758     data[0] = color->h;
1759     data[1] = color->s;
1760 }
1761
1762 void backlight_config_set_value( uint8_t *data )
1763 {
1764     bool reinitialize = false;
1765     uint8_t *value_id = &(data[0]);
1766     uint8_t *value_data = &(data[1]);
1767     switch ( *value_id )
1768     {
1769 #if defined (RGB_BACKLIGHT_ZEAL60) || defined(RGB_BACKLIGHT_ZEAL65)
1770         case id_use_split_backspace:
1771         {
1772             g_config.use_split_backspace = (bool)*value_data;
1773             reinitialize = true;
1774             break;
1775         }
1776 #endif
1777 #if defined (RGB_BACKLIGHT_ZEAL60)
1778         case id_use_split_left_shift:
1779         {
1780             g_config.use_split_left_shift = (bool)*value_data;
1781             reinitialize = true;
1782             break;
1783         }
1784         case id_use_split_right_shift:
1785         {
1786             g_config.use_split_right_shift = (bool)*value_data;
1787             reinitialize = true;
1788             break;
1789         }
1790         case id_use_7u_spacebar:
1791         {
1792             g_config.use_7u_spacebar = (bool)*value_data;
1793             reinitialize = true;
1794             break;
1795         }
1796         case id_use_iso_enter:
1797         {
1798             g_config.use_iso_enter = (bool)*value_data;
1799             reinitialize = true;
1800             break;
1801         }
1802         case id_disable_hhkb_blocker_leds:
1803         {
1804             g_config.disable_hhkb_blocker_leds = (bool)*value_data;
1805             reinitialize = true;
1806             break;
1807         }
1808 #endif
1809         case id_disable_when_usb_suspended:
1810         {
1811             g_config.disable_when_usb_suspended = (bool)*value_data;
1812             break;
1813         }
1814         case id_disable_after_timeout:
1815         {
1816             g_config.disable_after_timeout = *value_data;
1817             break;
1818         }
1819         case id_brightness:
1820         {
1821             g_config.brightness = *value_data;
1822             break;
1823         }
1824         case id_effect:
1825         {
1826             g_config.effect = *value_data;
1827             break;
1828         }
1829         case id_effect_speed:
1830         {
1831             g_config.effect_speed = *value_data;
1832             break;
1833         }
1834         case id_color_1:
1835         {
1836             _set_color( &(g_config.color_1), value_data );
1837             break;
1838         }
1839         case id_color_2:
1840         {
1841             _set_color( &(g_config.color_2), value_data );
1842             break;
1843         }
1844         case id_caps_lock_indicator_color:
1845         {
1846             _set_color( &(g_config.caps_lock_indicator.color), value_data );
1847             break;
1848         }
1849         case id_caps_lock_indicator_row_col:
1850         {
1851             backlight_set_indicator_index( &(g_config.caps_lock_indicator.index), value_data[0], value_data[1] );
1852             break;
1853         }
1854         case id_layer_1_indicator_color:
1855         {
1856             _set_color( &(g_config.layer_1_indicator.color), value_data );
1857             break;
1858         }
1859         case id_layer_1_indicator_row_col:
1860         {
1861             backlight_set_indicator_index( &(g_config.layer_1_indicator.index), value_data[0], value_data[1] );
1862             break;
1863         }
1864         case id_layer_2_indicator_color:
1865         {
1866             _set_color( &(g_config.layer_2_indicator.color), value_data );
1867             break;
1868         }
1869         case id_layer_2_indicator_row_col:
1870         {
1871             backlight_set_indicator_index( &(g_config.layer_2_indicator.index), value_data[0], value_data[1] );
1872             break;
1873         }
1874         case id_layer_3_indicator_color:
1875         {
1876             _set_color( &(g_config.layer_3_indicator.color), value_data );
1877             break;
1878         }
1879         case id_layer_3_indicator_row_col:
1880         {
1881             backlight_set_indicator_index( &(g_config.layer_3_indicator.index), value_data[0], value_data[1] );
1882             break;
1883         }
1884         case id_alphas_mods:
1885         {
1886             for ( int i=0; i<5; i++ )
1887             {
1888                 g_config.alphas_mods[i] = ( *(value_data+i*2) << 8 ) | ( *(value_data+i*2+1) );
1889             }
1890         }
1891 #if defined(RGB_BACKLIGHT_M6_B)
1892         case id_custom_color:
1893         {
1894             uint8_t index = value_data[0];
1895             if ( index >= 0 && index <= 6 )
1896             {
1897                 _set_color( &(g_config.custom_color[index]), &(value_data[1]) );
1898             }
1899         }
1900 #endif
1901     }
1902
1903     if ( reinitialize )
1904     {
1905         backlight_init_drivers();
1906     }
1907 }
1908
1909 void backlight_config_get_value( uint8_t *data )
1910 {
1911     uint8_t *value_id = &(data[0]);
1912     uint8_t *value_data = &(data[1]);
1913     switch ( *value_id )
1914     {
1915         case id_use_split_backspace:
1916         {
1917             *value_data = ( g_config.use_split_backspace ? 1 : 0 );
1918             break;
1919         }
1920         case id_use_split_left_shift:
1921         {
1922             *value_data = ( g_config.use_split_left_shift ? 1 : 0 );
1923             break;
1924         }
1925         case id_use_split_right_shift:
1926         {
1927             *value_data = ( g_config.use_split_right_shift ? 1 : 0 );
1928             break;
1929         }
1930         case id_use_7u_spacebar:
1931         {
1932             *value_data = ( g_config.use_7u_spacebar ? 1 : 0 );
1933             break;
1934         }
1935         case id_use_iso_enter:
1936         {
1937             *value_data = ( g_config.use_iso_enter ? 1 : 0 );
1938             break;
1939         }
1940         case id_disable_when_usb_suspended:
1941         {
1942             *value_data = ( g_config.disable_when_usb_suspended ? 1 : 0 );
1943             break;
1944         }
1945         case id_disable_hhkb_blocker_leds:
1946         {
1947             *value_data = ( g_config.disable_hhkb_blocker_leds ? 1 : 0 );
1948             break;
1949         }
1950         case id_disable_after_timeout:
1951         {
1952             *value_data = g_config.disable_after_timeout;
1953             break;
1954         }
1955         case id_brightness:
1956         {
1957             *value_data = g_config.brightness;
1958             break;
1959         }
1960         case id_effect:
1961         {
1962             *value_data = g_config.effect;
1963             break;
1964         }
1965         case id_effect_speed:
1966         {
1967             *value_data = g_config.effect_speed;
1968             break;
1969         }
1970         case id_color_1:
1971         {
1972             _get_color( &(g_config.color_1), value_data );
1973             break;
1974         }
1975         case id_color_2:
1976         {
1977             _get_color( &(g_config.color_2), value_data );
1978             break;
1979         }
1980         case id_caps_lock_indicator_color:
1981         {
1982             _get_color( &(g_config.caps_lock_indicator.color), value_data );
1983             break;
1984         }
1985         case id_caps_lock_indicator_row_col:
1986         {
1987             backlight_get_indicator_row_col( g_config.caps_lock_indicator.index, &(value_data[0]), &(value_data[1]) );
1988             break;
1989         }
1990         case id_layer_1_indicator_color:
1991         {
1992             _get_color( &(g_config.layer_1_indicator.color), value_data );
1993             break;
1994         }
1995         case id_layer_1_indicator_row_col:
1996         {
1997             backlight_get_indicator_row_col( g_config.layer_1_indicator.index, &(value_data[0]), &(value_data[1]) );
1998             break;
1999         }
2000         case id_layer_2_indicator_color:
2001         {
2002             _get_color( &(g_config.layer_2_indicator.color), value_data );
2003             break;
2004         }
2005         case id_layer_2_indicator_row_col:
2006         {
2007             backlight_get_indicator_row_col( g_config.layer_2_indicator.index, &(value_data[0]), &(value_data[1]) );
2008             break;
2009         }
2010         case id_layer_3_indicator_color:
2011         {
2012             _get_color( &(g_config.layer_3_indicator.color), value_data );
2013             break;
2014         }
2015         case id_layer_3_indicator_row_col:
2016         {
2017             backlight_get_indicator_row_col( g_config.layer_3_indicator.index, &(value_data[0]), &(value_data[1]) );
2018             break;
2019         }
2020         case id_alphas_mods:
2021         {
2022             for ( int i=0; i<5; i++ )
2023             {
2024                 *(value_data+i*2) = g_config.alphas_mods[i] >> 8;
2025                 *(value_data+i*2+1) = g_config.alphas_mods[i] & 0xFF;
2026             }
2027         }
2028 #if defined(RGB_BACKLIGHT_M6_B)
2029         case id_custom_color:
2030         {
2031             uint8_t index = value_data[0];
2032             if ( index >= 0 && index <= 6 )
2033             {
2034                 _get_color( &(g_config.custom_color[index]), &(value_data[1]) );
2035             }
2036         }
2037 #endif
2038     }
2039 }
2040
2041 void backlight_config_set_alphas_mods( uint16_t *alphas_mods )
2042 {
2043     for ( int i=0; i<5; i++ )
2044     {
2045         g_config.alphas_mods[i] = alphas_mods[i];
2046     }
2047
2048     backlight_config_save();
2049 }
2050
2051 void backlight_config_load(void)
2052 {
2053     eeprom_read_block( &g_config, ((void*)RGB_BACKLIGHT_CONFIG_EEPROM_ADDR), sizeof(backlight_config) );
2054 }
2055
2056 void backlight_config_save(void)
2057 {
2058     eeprom_update_block( &g_config, ((void*)RGB_BACKLIGHT_CONFIG_EEPROM_ADDR), sizeof(backlight_config) );
2059 }
2060
2061 void backlight_init_drivers(void)
2062 {
2063     // Initialize I2C
2064     i2c_init();
2065
2066 #if defined(RGB_BACKLIGHT_M6_B)
2067     IS31FL3218_init();
2068 #elif defined(RGB_BACKLIGHT_HS60)
2069     IS31FL3733_init( ISSI_ADDR_1, 0 );
2070
2071     for ( int index = 0; index < DRIVER_LED_TOTAL; index++ )
2072     {
2073 #if defined(HS60_ANSI)
2074         bool enabled = !( ( index == 48-1 ) || //LA48
2075                           ( index == 51-1 ) || //LA51
2076                           ( index == 61-1 ) ); //LA61
2077 #elif defined(HS60_HHKB)
2078         bool enabled = !( ( index == 61-1 ) || //LA61
2079                           ( index == 62-1 ) ); //LA62
2080 #else //HS60_ISO
2081         bool enabled = !( ( index == 51-1 ) || //LA51
2082                           ( index == 61-1 ) ); //LA61
2083 #endif
2084                 // This only caches it for later
2085         IS31FL3733_set_led_control_register( index, enabled, enabled, enabled );
2086     }
2087     // This actually updates the LED drivers
2088     IS31FL3733_update_led_control_registers( ISSI_ADDR_1, 0 );
2089 #elif defined(RGB_BACKLIGHT_NK65)
2090     IS31FL3733_init( ISSI_ADDR_1, 0 );
2091     IS31FL3733_init( ISSI_ADDR_2, 0 );
2092
2093     for ( int index = 0; index < DRIVER_LED_TOTAL; index++ )
2094     {
2095         bool enabled = !( ( index == 61-1 )   || //LA61
2096                           ( index > 6+64-1 ) ); //LB7-LB64
2097         // This only caches it for later
2098         IS31FL3733_set_led_control_register( index, enabled, enabled, enabled );
2099     }
2100     IS31FL3733_set_led_control_register( 7+64-1, 0, 1, 0 ); //Enable LB7 green enable for indicators
2101     // This actually updates the LED drivers
2102     IS31FL3733_update_led_control_registers( ISSI_ADDR_1, 0 );
2103     IS31FL3733_update_led_control_registers( ISSI_ADDR_2, 1 );
2104 #else
2105     IS31FL3731_init( ISSI_ADDR_1 );
2106     IS31FL3731_init( ISSI_ADDR_2 );
2107
2108 #if defined(RGB_BACKLIGHT_U80_A)
2109     IS31FL3731_init( ISSI_ADDR_3 );
2110 #endif
2111
2112     // Experimental feature, not in configuration yet
2113 #if defined(RGB_BACKLIGHT_ZEAL60)
2114     bool disable_spacebar_stab_leds = false;
2115 #endif
2116
2117     for ( int index = 0; index < DRIVER_LED_TOTAL; index++ )
2118     {
2119         // OR the possible "disabled" cases together, then NOT the result to get the enabled state
2120         // LC6 LD13 not present on Zeal65
2121 #if defined(RGB_BACKLIGHT_ZEAL65)
2122         bool enabled = !( ( index == 18+5 && !g_config.use_split_backspace ) || // LB5
2123                           ( index == 36+6 ) || // LC6
2124                           ( index == 54+13 ) ); // LD13
2125 #elif defined(RGB_BACKLIGHT_KOYU)
2126         bool enabled = !( ( index == 36+15 ) || // LC15
2127                           ( index == 54+13 ) || // LD13
2128                           ( index == 54+17 ) ); // LD17
2129 #elif defined(RGB_BACKLIGHT_M60_A)
2130         bool enabled = !(
2131         // LB6 LB7 LB8 LB15 LB16 LB17 not present on M60-A
2132                           ( index == 18+6 ) || // LB6
2133                           ( index == 18+7 ) || // LB7
2134                           ( index == 18+8 ) || // LB8
2135                           ( index == 18+15 ) || // LB15
2136                           ( index == 18+16 ) || // LB16
2137                           ( index == 18+17 ) || // LB17
2138         // HHKB blockers (LC17, LD17) and ISO extra keys (LC15,LD13) not present on M60-A
2139                           ( index == 36+17 ) || // LC17
2140                           ( index == 54+17 ) || // LD17
2141                           ( index == 36+15 ) || // LC15
2142                           ( index == 54+13 ) ); // LD13
2143 #elif defined(RGB_BACKLIGHT_ZEAL60)
2144         // LB6 LB7 LB8 LB15 LB16 LB17 not present on Zeal60
2145         bool enabled = !( ( index == 18+5 && !g_config.use_split_backspace ) || // LB5
2146                           ( index == 36+15 && !g_config.use_split_left_shift ) || // LC15
2147                           ( index == 54+8 && !g_config.use_split_right_shift ) || // LD8
2148                           ( index == 54+13 && g_config.use_7u_spacebar ) || // LD13
2149                           ( index == 36+17 && g_config.disable_hhkb_blocker_leds ) || // LC17
2150                           ( index == 54+17 && g_config.disable_hhkb_blocker_leds ) ||  // LD17
2151                           ( index == 18+6 ) || // LB6
2152                           ( index == 18+7 ) || // LB7
2153                           ( index == 18+8 ) || // LB8
2154                           ( index == 18+15 ) || // LB15
2155                           ( index == 18+16 ) || // LB16
2156                           ( index == 18+17 ) || // LB17
2157                           ( index == 36+6 && disable_spacebar_stab_leds ) ||  // LC6
2158                           ( index == 54+13 && disable_spacebar_stab_leds ) ||  // LD13
2159                           ( index == 54+14 && disable_spacebar_stab_leds && g_config.use_7u_spacebar ) ); // LD14
2160 #elif defined(RGB_BACKLIGHT_U80_A)
2161 // LB5, LB6, LB7, LB8, LB15, LB16, LB17, LC15, LD8, LD13, LE0-LE8, LF13
2162         bool enabled = !(
2163                           ( index == 18+5 ) || // LB5
2164                           ( index == 18+6 ) || // LB6
2165                           ( index == 18+7 ) || // LB7
2166                           ( index == 18+8 ) || // LB8
2167                           ( index == 18+15 ) || // LB15
2168                           ( index == 18+16 ) || // LB16
2169                           ( index == 18+16 ) || // LB17
2170                           ( index == 36+15 ) || // LC15
2171                           ( index == 54+8 ) || // LD8
2172                           ( index == 54+13 ) ||  // LD13
2173                           ( index >= 72+0 && index <= 72+8 ) ||  // LE0-LE8
2174                           ( index == 90+13 ) ); // LF13
2175 #endif
2176         // This only caches it for later
2177         IS31FL3731_set_led_control_register( index, enabled, enabled, enabled );
2178     }
2179     // This actually updates the LED drivers
2180     IS31FL3731_update_led_control_registers( ISSI_ADDR_1, 0 );
2181     IS31FL3731_update_led_control_registers( ISSI_ADDR_2, 1 );
2182 #if defined(RGB_BACKLIGHT_U80_A)    
2183     IS31FL3731_update_led_control_registers( ISSI_ADDR_3, 2 );
2184 #endif
2185 #endif // !defined(RGB_BACKLIGHT_M6_B)
2186
2187     // TODO: put the 1 second startup delay here?
2188
2189     // clear the key hits
2190     for ( int led=0; led<BACKLIGHT_LED_COUNT; led++ )
2191     {
2192         g_key_hit[led] = 255;
2193     }
2194 }
2195
2196 bool process_record_backlight(uint16_t keycode, keyrecord_t *record)
2197 {
2198     // Record keypresses for backlight effects
2199     if ( record->event.pressed )
2200     {
2201         backlight_set_key_hit( record->event.key.row, record->event.key.col );
2202     }
2203
2204     switch(keycode)
2205     {
2206         case BR_INC:
2207             if (record->event.pressed)
2208             {
2209                 backlight_brightness_increase();
2210             }
2211             return false;
2212             break;
2213         case BR_DEC:
2214             if (record->event.pressed)
2215             {
2216                 backlight_brightness_decrease();
2217             }
2218             return false;
2219             break;
2220         case EF_INC:
2221             if (record->event.pressed)
2222             {
2223                 backlight_effect_increase();
2224             }
2225             return false;
2226             break;
2227         case EF_DEC:
2228             if (record->event.pressed)
2229             {
2230                 backlight_effect_decrease();
2231             }
2232             return false;
2233             break;
2234         case ES_INC:
2235             if (record->event.pressed)
2236             {
2237                 backlight_effect_speed_increase();
2238             }
2239             return false;
2240             break;
2241         case ES_DEC:
2242             if (record->event.pressed)
2243             {
2244                 backlight_effect_speed_decrease();
2245             }
2246             return false;
2247             break;
2248         case H1_INC:
2249             if (record->event.pressed)
2250             {
2251                 backlight_color_1_hue_increase();
2252             }
2253             return false;
2254             break;
2255         case H1_DEC:
2256             if (record->event.pressed)
2257             {
2258                 backlight_color_1_hue_decrease();
2259             }
2260             return false;
2261             break;
2262         case S1_INC:
2263             if (record->event.pressed)
2264             {
2265                 backlight_color_1_sat_increase();
2266             }
2267             return false;
2268             break;
2269         case S1_DEC:
2270             if (record->event.pressed)
2271             {
2272                 backlight_color_1_sat_decrease();
2273                 break;
2274             }
2275             return false;
2276             break;
2277         case H2_INC:
2278             if (record->event.pressed)
2279             {
2280                 backlight_color_2_hue_increase();
2281             }
2282             return false;
2283             break;
2284         case H2_DEC:
2285             if (record->event.pressed)
2286             {
2287                 backlight_color_2_hue_decrease();
2288             }
2289             return false;
2290             break;
2291         case S2_INC:
2292             if (record->event.pressed)
2293             {
2294                 backlight_color_2_sat_increase();
2295             }
2296             return false;
2297             break;
2298         case S2_DEC:
2299             if (record->event.pressed)
2300             {
2301                 backlight_color_2_sat_decrease();
2302                 break;
2303             }
2304             return false;
2305             break;
2306     }
2307
2308     return true;
2309 }
2310
2311 // Deals with the messy details of incrementing an integer
2312 uint8_t increment( uint8_t value, uint8_t step, uint8_t min, uint8_t max )
2313 {
2314     int16_t new_value = value;
2315     new_value += step;
2316     return MIN( MAX( new_value, min ), max );
2317 }
2318
2319 uint8_t decrement( uint8_t value, uint8_t step, uint8_t min, uint8_t max )
2320 {
2321     int16_t new_value = value;
2322     new_value -= step;
2323     return MIN( MAX( new_value, min ), max );
2324 }
2325
2326 void backlight_effect_increase(void)
2327 {
2328     g_config.effect = increment( g_config.effect, 1, 0, BACKLIGHT_EFFECT_MAX );
2329     backlight_config_save();
2330 }
2331
2332 void backlight_effect_decrease(void)
2333 {
2334     g_config.effect = decrement( g_config.effect, 1, 0, BACKLIGHT_EFFECT_MAX );
2335     backlight_config_save();
2336 }
2337
2338 void backlight_effect_speed_increase(void)
2339 {
2340     g_config.effect_speed = increment( g_config.effect_speed, 1, 0, 3 );
2341     backlight_config_save();
2342 }
2343
2344 void backlight_effect_speed_decrease(void)
2345 {
2346     g_config.effect_speed = decrement( g_config.effect_speed, 1, 0, 3 );
2347     backlight_config_save();
2348 }
2349
2350 void backlight_brightness_increase(void)
2351 {
2352     g_config.brightness = increment( g_config.brightness, 8, 0, 255 );
2353     backlight_config_save();
2354 }
2355
2356 void backlight_brightness_decrease(void)
2357 {
2358     g_config.brightness = decrement( g_config.brightness, 8, 0, 255 );
2359     backlight_config_save();
2360 }
2361
2362 void backlight_color_1_hue_increase(void)
2363 {
2364     g_config.color_1.h = increment( g_config.color_1.h, 8, 0, 255 );
2365     backlight_config_save();
2366 }
2367
2368 void backlight_color_1_hue_decrease(void)
2369 {
2370     g_config.color_1.h = decrement( g_config.color_1.h, 8, 0, 255 );
2371     backlight_config_save();
2372 }
2373
2374 void backlight_color_1_sat_increase(void)
2375 {
2376     g_config.color_1.s = increment( g_config.color_1.s, 8, 0, 255 );
2377     backlight_config_save();
2378 }
2379
2380 void backlight_color_1_sat_decrease(void)
2381 {
2382     g_config.color_1.s = decrement( g_config.color_1.s, 8, 0, 255 );
2383     backlight_config_save();
2384 }
2385
2386 void backlight_color_2_hue_increase(void)
2387 {
2388     g_config.color_2.h = increment( g_config.color_2.h, 8, 0, 255 );
2389     backlight_config_save();
2390 }
2391
2392 void backlight_color_2_hue_decrease(void)
2393 {
2394     g_config.color_2.h = decrement( g_config.color_2.h, 8, 0, 255 );
2395     backlight_config_save();
2396 }
2397
2398 void backlight_color_2_sat_increase(void)
2399 {
2400     g_config.color_2.s = increment( g_config.color_2.s, 8, 0, 255 );
2401     backlight_config_save();
2402 }
2403
2404 void backlight_color_2_sat_decrease(void)
2405 {
2406     g_config.color_2.s = decrement( g_config.color_2.s, 8, 0, 255 );
2407     backlight_config_save();
2408 }
2409
2410 #if defined(RGB_DEBUGGING_ONLY)
2411 void backlight_test_led( uint8_t index, bool red, bool green, bool blue )
2412 {
2413     for ( int i=0; i<BACKLIGHT_LED_COUNT; i++ )
2414     {
2415         if ( i == index )
2416         {
2417             IS31FL3731_set_led_control_register( i, red, green, blue );
2418         }
2419         else
2420         {
2421             IS31FL3731_set_led_control_register( i, false, false, false );
2422         }
2423     }
2424 }
2425
2426 void backlight_debug_led( bool state )
2427 {
2428     if (state)
2429     {
2430         // Output high.
2431         DDRE |= (1<<6);
2432         PORTE |= (1<<6);
2433     }
2434     else
2435     {
2436         // Output low.
2437         DDRE &= ~(1<<6);
2438         PORTE &= ~(1<<6);
2439     }
2440 }
2441 #endif // defined(RGB_DEBUGGING_ONLY)
2442