1 /* Copyright 2017 Jason Williams (Wilba)
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.
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.
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/>.
16 #if RGB_BACKLIGHT_ENABLED
18 #if defined (RGB_BACKLIGHT_ZEAL60) || defined (RGB_BACKLIGHT_ZEAL65) || defined (RGB_BACKLIGHT_M60_A) || defined(RGB_BACKLIGHT_M6_B) || defined(RGB_BACKLIGHT_KOYU)
20 #error None of the following was defined: RGB_BACKLIGHT_ZEAL60, RGB_BACKLIGHT_ZEAL65, RGB_BACKLIGHT_M60_A, RGB_BACKLIGHT_M6_B, RGB_BACKLIGHT_KOYU
24 #include "rgb_backlight.h"
25 #include "rgb_backlight_api.h"
26 #include "rgb_backlight_keycodes.h"
29 #include <util/delay.h>
30 #include <avr/interrupt.h>
33 #include "quantum/color.h"
34 #include "drivers/avr/i2c_master.h"
36 #if defined (RGB_BACKLIGHT_M6_B)
37 #include "drivers/issi/is31fl3218.h"
38 #define BACKLIGHT_LED_COUNT 6
40 #include "drivers/issi/is31fl3731.h"
41 #define BACKLIGHT_LED_COUNT 72
44 #define BACKLIGHT_EFFECT_MAX 10
46 backlight_config g_config = {
47 .use_split_backspace = RGB_BACKLIGHT_USE_SPLIT_BACKSPACE,
48 .use_split_left_shift = RGB_BACKLIGHT_USE_SPLIT_LEFT_SHIFT,
49 .use_split_right_shift = RGB_BACKLIGHT_USE_SPLIT_RIGHT_SHIFT,
50 .use_7u_spacebar = RGB_BACKLIGHT_USE_7U_SPACEBAR,
51 .use_iso_enter = RGB_BACKLIGHT_USE_ISO_ENTER,
52 .disable_hhkb_blocker_leds = RGB_BACKLIGHT_DISABLE_HHKB_BLOCKER_LEDS,
53 .disable_when_usb_suspended = RGB_BACKLIGHT_DISABLE_WHEN_USB_SUSPENDED,
54 .disable_after_timeout = RGB_BACKLIGHT_DISABLE_AFTER_TIMEOUT,
56 .effect = RGB_BACKLIGHT_EFFECT,
58 .color_1 = { .h = 0, .s = 255 },
59 .color_2 = { .h = 127, .s = 255 },
60 .caps_lock_indicator = { .color = { .h = 0, .s = 0 }, .index = 255 },
61 .layer_1_indicator = { .color = { .h = 0, .s = 0 }, .index = 255 },
62 .layer_2_indicator = { .color = { .h = 0, .s = 0 }, .index = 255 },
63 .layer_3_indicator = { .color = { .h = 0, .s = 0 }, .index = 255 },
65 RGB_BACKLIGHT_ALPHAS_MODS_ROW_0,
66 RGB_BACKLIGHT_ALPHAS_MODS_ROW_1,
67 RGB_BACKLIGHT_ALPHAS_MODS_ROW_2,
68 RGB_BACKLIGHT_ALPHAS_MODS_ROW_3,
69 RGB_BACKLIGHT_ALPHAS_MODS_ROW_4 },
70 #if defined(RGB_BACKLIGHT_M6_B)
71 .custom_color = { { 0, 255 }, { 43, 255 }, { 85, 255 }, { 128, 255 }, { 171, 255 }, { 213, 255 } }
75 bool g_suspend_state = false;
76 uint8_t g_indicator_state = 0;
78 // Global tick at 20 Hz
81 // Ticks since this key was last hit.
82 uint8_t g_key_hit[BACKLIGHT_LED_COUNT];
84 // Ticks since any key was last hit.
85 uint32_t g_any_key_hit = 0;
87 #if !defined(RGB_BACKLIGHT_M6_B)
88 // This is a 7-bit address, that gets left-shifted and bit 0
89 // set to 0 for write, 1 for read (as per I2C protocol)
90 #define ISSI_ADDR_1 0x74
91 #define ISSI_ADDR_2 0x76
93 const is31_led g_is31_leds[DRIVER_LED_TOTAL] = {
94 /* Refer to IS31 manual for these locations
100 {0, C2_1, C3_1, C4_1}, // LA0
101 {0, C1_1, C3_2, C4_2}, // LA1
102 {0, C1_2, C2_2, C4_3}, // LA2
103 {0, C1_3, C2_3, C3_3}, // LA3
104 {0, C1_4, C2_4, C3_4}, // LA4
105 {0, C1_5, C2_5, C3_5}, // LA5
106 {0, C1_6, C2_6, C3_6}, // LA6
107 {0, C1_7, C2_7, C3_7}, // LA7
108 {0, C1_8, C2_8, C3_8}, // LA8
109 {0, C9_1, C8_1, C7_1}, // LA9
110 {0, C9_2, C8_2, C7_2}, // LA10
111 {0, C9_3, C8_3, C7_3}, // LA11
112 {0, C9_4, C8_4, C7_4}, // LA12
113 {0, C9_5, C8_5, C7_5}, // LA13
114 {0, C9_6, C8_6, C7_6}, // LA14
115 {0, C9_7, C8_7, C6_6}, // LA15
116 {0, C9_8, C7_7, C6_7}, // LA16
117 {0, C8_8, C7_8, C6_8}, // LA17
119 {0, C2_9, C3_9, C4_9}, // LB0
120 {0, C1_9, C3_10, C4_10}, // LB1
121 {0, C1_10, C2_10, C4_11}, // LB2
122 {0, C1_11, C2_11, C3_11}, // LB3
123 {0, C1_12, C2_12, C3_12}, // LB4
124 {0, C1_13, C2_13, C3_13}, // LB5
125 {0, C1_14, C2_14, C3_14}, // LB6
126 {0, C1_15, C2_15, C3_15}, // LB7
127 {0, C1_16, C2_16, C3_16}, // LB8
128 {0, C9_9, C8_9, C7_9}, // LB9
129 {0, C9_10, C8_10, C7_10}, // LB10
130 {0, C9_11, C8_11, C7_11}, // LB11
131 {0, C9_12, C8_12, C7_12}, // LB12
132 {0, C9_13, C8_13, C7_13}, // LB13
133 {0, C9_14, C8_14, C7_14}, // LB14
134 {0, C9_15, C8_15, C6_14}, // LB15
135 {0, C9_16, C7_15, C6_15}, // LB16
136 {0, C8_16, C7_16, C6_16}, // LB17
138 {1, C2_1, C3_1, C4_1}, // LC0
139 {1, C1_1, C3_2, C4_2}, // LC1
140 {1, C1_2, C2_2, C4_3}, // LC2
141 {1, C1_3, C2_3, C3_3}, // LC3
142 {1, C1_4, C2_4, C3_4}, // LC4
143 {1, C1_5, C2_5, C3_5}, // LC5
144 {1, C1_6, C2_6, C3_6}, // LC6
145 {1, C1_7, C2_7, C3_7}, // LC7
146 {1, C1_8, C2_8, C3_8}, // LC8
147 {1, C9_1, C8_1, C7_1}, // LC9
148 {1, C9_2, C8_2, C7_2}, // LC10
149 {1, C9_3, C8_3, C7_3}, // LC11
150 {1, C9_4, C8_4, C7_4}, // LC12
151 {1, C9_5, C8_5, C7_5}, // LC13
152 {1, C9_6, C8_6, C7_6}, // LC14
153 {1, C9_7, C8_7, C6_6}, // LC15
154 {1, C9_8, C7_7, C6_7}, // LC16
155 {1, C8_8, C7_8, C6_8}, // LC17
157 {1, C2_9, C3_9, C4_9}, // LD0
158 {1, C1_9, C3_10, C4_10}, // LD1
159 {1, C1_10, C2_10, C4_11}, // LD2
160 {1, C1_11, C2_11, C3_11}, // LD3
161 {1, C1_12, C2_12, C3_12}, // LD4
162 {1, C1_13, C2_13, C3_13}, // LD5
163 {1, C1_14, C2_14, C3_14}, // LD6
164 {1, C1_15, C2_15, C3_15}, // LD7
165 {1, C1_16, C2_16, C3_16}, // LD8
166 {1, C9_9, C8_9, C7_9}, // LD9
167 {1, C9_10, C8_10, C7_10}, // LD10
168 {1, C9_11, C8_11, C7_11}, // LD11
169 {1, C9_12, C8_12, C7_12}, // LD12
170 {1, C9_13, C8_13, C7_13}, // LD13
171 {1, C9_14, C8_14, C7_14}, // LD14
172 {1, C9_15, C8_15, C6_14}, // LD15
173 {1, C9_16, C7_15, C6_15}, // LD16
174 {1, C8_16, C7_16, C6_16}, // LD17
176 #endif // !defined(RGB_BACKLIGHT_M6_B)
179 typedef struct Point {
185 // index in range 0..71 (LA0..LA17, LB0..LB17, LC0..LC17, LD0..LD17)
186 // point values in range x=0..224 y=0..64
187 // origin is center of top-left key (i.e Esc)
188 #if defined (RGB_BACKLIGHT_ZEAL65)
189 const Point g_map_led_to_point[BACKLIGHT_LED_COUNT] PROGMEM = {
191 {120,16}, {104,16}, {88,16}, {72,16}, {56,16}, {40,16}, {24,16}, {4,16}, {4,32},
192 {128,0}, {112,0}, {96,0}, {80,0}, {64,0}, {48,0}, {32,0}, {16,0}, {0,0},
194 {144,0}, {160,0}, {176,0}, {192,0}, {216,0}, {224,0}, {240,0}, {240,16}, {240,32},
195 {136,16}, {152,16}, {168,16}, {184,16}, {200,16}, {220,16}, {240,48}, {240,64}, {224,64},
197 {96,64}, {100,48}, {84,48}, {68,48}, {52,48}, {36,48}, {255,255}, {48,60}, {28,64},
198 {108,32}, {92,32}, {76,32}, {60,32}, {44,32}, {28,32}, {20,44}, {10,48}, {4,64},
200 {124,32}, {140,32}, {156,32}, {172,32}, {188,32}, {214,32}, {180,48}, {202,48}, {224,48},
201 {116,48}, {132,48}, {148,48}, {164,48}, {255,255}, {144,60}, {164,64}, {188,64}, {208,64}
203 const Point g_map_led_to_point_polar[BACKLIGHT_LED_COUNT] PROGMEM = {
205 {64,128}, {75,132}, {84,145}, {91,164}, {97,187}, {102,213}, {105,242}, {109,255}, {128,243},
206 {61,255}, {67,255}, {72,255}, {77,255}, {82,255}, {86,255}, {90,255}, {93,255}, {96,255},
208 {56,255}, {51,255}, {46,255}, {42,255}, {37,255}, {35,255}, {32,255}, {19,255}, {0,255},
209 {53,132}, {44,145}, {37,164}, {31,187}, {26,213}, {22,249}, {237,255}, {224,255}, {221,255},
211 {184,255}, {179,135}, {170,149}, {163,169}, {157,193}, {153,220}, {255,255}, {167,255}, {165,255},
212 {128,26}, {128,60}, {128,94}, {128,128}, {128,162}, {128,196}, {145,233}, {148,255}, {161,255},
214 {0,9}, {0,43}, {0,77}, {0,111}, {0,145}, {0,201}, {224,181}, {230,217}, {235,255},
215 {189,128}, {200,131}, {210,141}, {218,159}, {255,255}, {201,228}, {206,255}, {213,255}, {218,255}
217 #elif defined (RGB_BACKLIGHT_KOYU)
218 const Point g_map_led_to_point[BACKLIGHT_LED_COUNT] PROGMEM = {
220 {120,16}, {104,16}, {88,16}, {72,16}, {56,16}, {40,16}, {24,16}, {4,16}, {4,32},
221 {128,0}, {112,0}, {96,0}, {80,0}, {64,0}, {48,0}, {32,0}, {16,0}, {0,0},
223 {144,0}, {160,0}, {176,0}, {192,0}, {208,0}, {224,0}, {240,0}, {240,16}, {240,32},
224 {136,16}, {152,16}, {168,16}, {184,16}, {200,16}, {220,16}, {240,48}, {240,64}, {224,64},
226 {112,64}, {100,48}, {84,48}, {68,48}, {52,48}, {36,48}, {64,60}, {44,60}, {24,64},
227 {108,32}, {92,32}, {76,32}, {60,32}, {44,32}, {28,32}, {255,255}, {10,48}, {4,64},
229 {124,32}, {140,32}, {156,32}, {172,32}, {188,32}, {214,32}, {180,48}, {202,48}, {224,48},
230 {116,48}, {132,48}, {148,48}, {164,48}, {255,255}, {160,60}, {180,64}, {208,64}, {255,255}
232 const Point g_map_led_to_point_polar[BACKLIGHT_LED_COUNT] PROGMEM = {
234 {64,128}, {75,132}, {84,145}, {91,164}, {97,187}, {102,213}, {105,242}, {109,255}, {128,243},
235 {61,255}, {67,255}, {72,255}, {77,255}, {82,255}, {86,255}, {90,255}, {93,255}, {96,255},
237 {56,255}, {51,255}, {46,255}, {42,255}, {38,255}, {35,255}, {32,255}, {19,255}, {0,255},
238 {53,132}, {44,145}, {37,164}, {31,187}, {26,213}, {22,249}, {237,255}, {224,255}, {221,255},
240 {189,255}, {179,135}, {170,149}, {163,169}, {157,193}, {153,220}, {172,252}, {169,255}, {165,255},
241 {128,26}, {128,60}, {128,94}, {128,128}, {128,162}, {128,196}, {255,255}, {148,255}, {161,255},
243 {0,9}, {0,43}, {0,77}, {0,111}, {0,145}, {0,201}, {224,181}, {230,217}, {235,255},
244 {189,128}, {200,131}, {210,141}, {218,159}, {255,255}, {207,238}, {211,255}, {218,255}, {255,255}
246 #elif defined (RGB_BACKLIGHT_ZEAL60) || defined (RGB_BACKLIGHT_M60_A)
247 const Point g_map_led_to_point[BACKLIGHT_LED_COUNT] PROGMEM = {
249 {120,16}, {104,16}, {88,16}, {72,16}, {56,16}, {40,16}, {24,16}, {4,16}, {4,32},
250 {128,0}, {112,0}, {96,0}, {80,0}, {64,0}, {48,0}, {32,0}, {16,0}, {0,0},
252 {144,0}, {160,0}, {176,0}, {192,0}, {216,0}, {224,0}, {255,255}, {255,255}, {255,255},
253 {136,16}, {152,16}, {168,16}, {184,16}, {200,16}, {220,16}, {255,255}, {255,255}, {255,255},
255 {102,64}, {100,48}, {84,48}, {68,48}, {52,48}, {36,48}, {60,64}, {43,64}, {23,64},
256 {108,32}, {92,32}, {76,32}, {60,32}, {44,32}, {28,32}, {20,48}, {2,48}, {3,64},
258 {124,32}, {140,32}, {156,32}, {172,32}, {188,32}, {214,32}, {180,48}, {210,48}, {224,48},
259 {116,48}, {132,48}, {148,48}, {164,48}, {144,64}, {161,64}, {181,64}, {201,64}, {221,64}
261 const Point g_map_led_to_point_polar[BACKLIGHT_LED_COUNT] PROGMEM = {
263 {58,129}, {70,129}, {80,139}, {89,157}, {96,181}, {101,208}, {105,238}, {109,255}, {128,247}, {58,255},
264 {64,255}, {70,255}, {75,255}, {80,255}, {85,255}, {89,255}, {93,255}, {96,255},
266 {53,255}, {48,255}, {43,255}, {39,255}, {34,255}, {32,255}, {255,255}, {255,255}, {255,255},
267 {48,139}, {39,157}, {32,181}, {27,208}, {23,238}, {19,255}, {255,255}, {255,255}, {255,255},
269 {188,255}, {183,131}, {173,143}, {165,163}, {159,188}, {154,216}, {172,252}, {170,255}, {165,255},
270 {128,9}, {128,46}, {128,82}, {128,119}, {128,155}, {128,192}, {150,244}, {147,255}, {161,255},
272 {0,27}, {0,64}, {0,101}, {0,137}, {0,174}, {255,233}, {228,201}, {235,255}, {237,255},
273 {195,128}, {206,136}, {215,152}, {222,175}, {205,234}, {209,255}, {214,255}, {219,255}, {223,255}
275 #elif defined (RGB_BACKLIGHT_M6_B)
276 // M6-B is really simple:
279 const Point g_map_led_to_point[BACKLIGHT_LED_COUNT] PROGMEM = {
280 {0,0}, {0,16}, {16,16}, {16,0}, {32,16}, {32,0}
282 const Point g_map_led_to_point_polar[BACKLIGHT_LED_COUNT] PROGMEM = {
283 {160,255}, {96,255}, {77,255}, {179,255}, {51,255}, {205,255}
287 // This may seem counter-intuitive, but it's quite flexible.
288 // For each LED, get it's position to decide what color to make it.
289 // This solves the issue of LEDs (and switches) not aligning to a grid,
290 // or having a large "bitmap" and sampling them.
291 void map_led_to_point( uint8_t index, Point *point )
293 // Slightly messy way to get Point structs out of progmem.
294 uint8_t *addr = (uint8_t*)&g_map_led_to_point[index];
295 point->x = pgm_read_byte(addr);
296 point->y = pgm_read_byte(addr+1);
298 #if defined (RGB_BACKLIGHT_M6_B)
305 if ( g_config.use_split_backspace )
308 #if defined (RGB_BACKLIGHT_ZEAL60)
310 if ( g_config.use_iso_enter )
311 point->y += 8; // extremely pedantic
314 if ( !g_config.use_iso_enter )
318 if ( !g_config.use_split_left_shift )
322 #if defined (RGB_BACKLIGHT_ZEAL60) || defined (RGB_BACKLIGHT_M60_A)
324 if ( g_config.use_7u_spacebar )
328 if ( g_config.use_7u_spacebar )
332 if ( !g_config.use_split_right_shift )
339 void map_led_to_point_polar( uint8_t index, Point *point )
341 // Slightly messy way to get Point structs out of progmem.
342 uint8_t *addr = (uint8_t*)&g_map_led_to_point_polar[index];
343 point->x = pgm_read_byte(addr);
344 point->y = pgm_read_byte(addr+1);
348 // Maps switch matrix coordinate (row,col) to LED index
352 #if defined (RGB_BACKLIGHT_ZEAL65)
353 // Note: Left spacebar stab is at 4,2 (LC7)
354 // Right spacebar stab is at 4,9 (D14)
356 // A17, A16, A15, A14, A13, A12, A11, A10, A9, B0, B1, B2, B3, B4, B6
357 // A7, A6, A5, A4, A3, A2, A1, A0, B9, B10, B11, B12, B13, B14, B7
358 // A8, C14, C13, C12, C11, C10, C9, D0, D1, D2, D3, D4, D5, B5, B8
359 // C16, C15, C5, C4, C3, C2, C1, D9, D10, D11, D12, D6, D7, D8, B15
360 // C17, C8, C7, ---, ---, ---, ---, C0, ---, D14, D15, D16, D17, B17, B16
361 const uint8_t g_map_row_column_to_led[MATRIX_ROWS][MATRIX_COLS] PROGMEM = {
362 { 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 },
363 { 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 },
364 { 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 },
365 { 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 },
366 { 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 }
368 #elif defined(RGB_BACKLIGHT_KOYU)
369 // Note: Left spacebar stab is at 4,4 (LC6)
370 // Right spacebar stab is at 4,10 (D14)
372 // A17, A16, A15, A14, A13, A12, A11, A10, A9, B0, B1, B2, B3, B4, B6
373 // A7, A6, A5, A4, A3, A2, A1, A0, B9, B10, B11, B12, B13, B14, B7
374 // A8, C14, C13, C12, C11, C10, C9, D0, D1, D2, D3, D4, D5, B5, B8
375 // C16, C15, C5, C4, C3, C2, C1, D9, D10, D11, D12, D6, D7, D8, B15
376 // C17, C8, C7, C6, ---, ---, ---, C0, ---, ---, D14, D15, D16, B17, B16
377 const uint8_t g_map_row_column_to_led[MATRIX_ROWS][MATRIX_COLS] PROGMEM = {
378 { 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 },
379 { 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 },
380 { 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 },
381 { 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 },
382 { 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 }
384 #elif defined (RGB_BACKLIGHT_ZEAL60) || defined (RGB_BACKLIGHT_M60_A)
385 // Note: Left spacebar stab is at 4,3 (LC6)
386 // Right spacebar stab is at 4,9 (LD13) or 4,10 (LD14)
388 // A17, A16, A15, A14, A13, A12, A11, A10, A9, B0, B1, B2, B3, B4,
389 // A7, A6, A5, A4, A3, A2, A1, A0, B9, B10, B11, B12, B13, B14,
390 // A8, C14, C13, C12, C11, C10, C9, D0, D1, D2, D3, D4, D5, B5,
391 // C16, C15, C5, C4, C3, C2, C1, D9, D10, D11, D12, D6, D7, D8,
392 // C17, C8, C7, C6, ---, ---, ---, C0, ---, D13, D14, D15, D16, D17,
393 const uint8_t g_map_row_column_to_led[MATRIX_ROWS][MATRIX_COLS] PROGMEM = {
394 { 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 },
395 { 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 },
396 { 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 },
397 { 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 },
398 { 36+17, 36+8, 36+7, 36+6, 255, 255, 255, 36+0, 255, 54+13, 54+14, 54+15, 54+16, 54+17 }
400 #elif defined (RGB_BACKLIGHT_M6_B)
401 // M6-B is really simple:
404 const uint8_t g_map_row_column_to_led[MATRIX_ROWS][MATRIX_COLS] PROGMEM = {
409 void map_row_column_to_led( uint8_t row, uint8_t column, uint8_t *led )
412 if ( row < MATRIX_ROWS && column < MATRIX_COLS )
414 *led = pgm_read_byte(&g_map_row_column_to_led[row][column]);
418 void backlight_update_pwm_buffers(void)
420 #if defined (RGB_BACKLIGHT_M6_B)
421 IS31FL3218_update_pwm_buffers();
423 IS31FL3731_update_pwm_buffers( ISSI_ADDR_1, ISSI_ADDR_2 );
424 IS31FL3731_update_led_control_registers( ISSI_ADDR_1, ISSI_ADDR_2 );
428 void backlight_set_color( int index, uint8_t red, uint8_t green, uint8_t blue )
430 #if defined (RGB_BACKLIGHT_M6_B)
431 IS31FL3218_set_color( index, red, green, blue );
433 IS31FL3731_set_color( index, red, green, blue );
437 void backlight_set_color_all( uint8_t red, uint8_t green, uint8_t blue )
439 #if defined (RGB_BACKLIGHT_M6_B)
440 IS31FL3218_set_color_all( red, green, blue );
442 IS31FL3731_set_color_all( red, green, blue );
446 void backlight_set_key_hit(uint8_t row, uint8_t column)
449 map_row_column_to_led(row,column,&led);
455 // This is (F_CPU/1024) / 20 Hz
456 // = 15625 Hz / 20 Hz
458 #define TIMER3_TOP 781
460 void backlight_timer_init(void)
462 static uint8_t backlight_timer_is_init = 0;
463 if ( backlight_timer_is_init )
467 backlight_timer_is_init = 1;
470 TCCR3B = _BV(WGM32) | // CTC mode OCR3A as TOP
471 _BV(CS32) | _BV(CS30); // prescale by /1024
476 OCR3AH = (TIMER3_TOP >> 8) & 0xff;
477 OCR3AL = TIMER3_TOP & 0xff;
481 void backlight_timer_enable(void)
483 TIMSK3 |= _BV(OCIE3A);
486 void backlight_timer_disable(void)
488 TIMSK3 &= ~_BV(OCIE3A);
491 void backlight_set_suspend_state(bool state)
493 g_suspend_state = state;
496 void backlight_set_indicator_state(uint8_t state)
498 g_indicator_state = state;
501 void backlight_effect_rgb_test(void)
503 // Mask out bits 4 and 5
504 // This 2-bit value will stay the same for 16 ticks.
505 switch ( (g_tick & 0x30) >> 4 )
509 backlight_set_color_all( 255, 0, 0 );
514 backlight_set_color_all( 0, 255, 0 );
519 backlight_set_color_all( 0, 0, 255 );
524 backlight_set_color_all( 255, 255, 255 );
530 #if defined(RGB_DEBUGGING_ONLY)
531 // This tests the LEDs
532 // Note that it will change the LED control registers
533 // in the LED drivers, and leave them in an invalid
534 // state for other backlight effects.
535 // ONLY USE THIS FOR TESTING LEDS!
536 void backlight_effect_single_LED_test(void)
538 static uint8_t color = 0; // 0,1,2 for R,G,B
539 static uint8_t row = 0;
540 static uint8_t column = 0;
542 static uint8_t tick = 0;
566 map_row_column_to_led( row, column, &led );
567 backlight_set_color_all( 255, 255, 255 );
568 backlight_test_led( led, color==0, color==1, color==2 );
570 #endif // defined(RGB_DEBUGGING_ONLY)
573 void backlight_effect_all_off(void)
575 backlight_set_color_all( 0, 0, 0 );
579 void backlight_effect_solid_color(void)
581 HSV hsv = { .h = g_config.color_1.h, .s = g_config.color_1.s, .v = g_config.brightness };
582 RGB rgb = hsv_to_rgb( hsv );
583 backlight_set_color_all( rgb.r, rgb.g, rgb.b );
586 // alphas = color1, mods = color2
587 void backlight_effect_alphas_mods(void)
589 RGB rgb1 = hsv_to_rgb( (HSV){ .h = g_config.color_1.h, .s = g_config.color_1.s, .v = g_config.brightness } );
590 RGB rgb2 = hsv_to_rgb( (HSV){ .h = g_config.color_2.h, .s = g_config.color_2.s, .v = g_config.brightness } );
592 for ( int row = 0; row < MATRIX_ROWS; row++ )
594 for ( int column = 0; column < MATRIX_COLS; column++ )
597 map_row_column_to_led( row, column, &index );
598 if ( index < BACKLIGHT_LED_COUNT )
600 if ( ( g_config.alphas_mods[row] & (1<<column) ) == 0 )
602 backlight_set_color( index, rgb1.r, rgb1.g, rgb1.b );
606 backlight_set_color( index, rgb2.r, rgb2.g, rgb2.b );
613 void backlight_effect_gradient_up_down(void)
615 int16_t h1 = g_config.color_1.h;
616 int16_t h2 = g_config.color_2.h;
617 int16_t deltaH = h2 - h1;
619 // Take the shortest path between hues
624 else if ( deltaH < -127 )
628 // Divide delta by 4, this gives the delta per row
631 int16_t s1 = g_config.color_1.s;
632 int16_t s2 = g_config.color_2.s;
633 int16_t deltaS = ( s2 - s1 ) / 4;
635 HSV hsv = { .h = 0, .s = 255, .v = g_config.brightness };
638 for ( int i=0; i<BACKLIGHT_LED_COUNT; i++ )
640 map_led_to_point( i, &point );
641 // The y range will be 0..64, map this to 0..4
642 uint8_t y = (point.y>>4);
643 // Relies on hue being 8-bit and wrapping
644 hsv.h = g_config.color_1.h + ( deltaH * y );
645 hsv.s = g_config.color_1.s + ( deltaS * y );
646 rgb = hsv_to_rgb( hsv );
647 backlight_set_color( i, rgb.r, rgb.g, rgb.b );
651 void backlight_effect_raindrops(bool initialize)
653 int16_t h1 = g_config.color_1.h;
654 int16_t h2 = g_config.color_2.h;
655 int16_t deltaH = h2 - h1;
658 // Take the shortest path between hues
663 else if ( deltaH < -127 )
668 int16_t s1 = g_config.color_1.s;
669 int16_t s2 = g_config.color_2.s;
670 int16_t deltaS = ( s2 - s1 ) / 4;
675 // Change one LED every tick
676 uint8_t led_to_change = ( g_tick & 0x000 ) == 0 ? rand() % BACKLIGHT_LED_COUNT : 255;
678 for ( int i=0; i<BACKLIGHT_LED_COUNT; i++ )
680 // If initialize, all get set to random colors
681 // If not, all but one will stay the same as before.
682 if ( initialize || i == led_to_change )
684 hsv.h = h1 + ( deltaH * ( rand() & 0x03 ) );
685 hsv.s = s1 + ( deltaS * ( rand() & 0x03 ) );
686 // Override brightness with global brightness control
687 hsv.v = g_config.brightness;;
689 rgb = hsv_to_rgb( hsv );
690 backlight_set_color( i, rgb.r, rgb.g, rgb.b );
695 void backlight_effect_cycle_all(void)
697 uint8_t offset = ( g_tick << g_config.effect_speed ) & 0xFF;
699 // Relies on hue being 8-bit and wrapping
700 for ( int i=0; i<BACKLIGHT_LED_COUNT; i++ )
702 uint16_t offset2 = g_key_hit[i]<<2;
703 // stabilizer LEDs use spacebar hits
704 if ( i == 36+6 || i == 54+13 || // LC6, LD13
705 ( g_config.use_7u_spacebar && i == 54+14 ) ) // LD14
707 offset2 = g_key_hit[36+0]<<2;
709 offset2 = (offset2<=63) ? (63-offset2) : 0;
711 HSV hsv = { .h = offset+offset2, .s = 255, .v = g_config.brightness };
712 RGB rgb = hsv_to_rgb( hsv );
713 backlight_set_color( i, rgb.r, rgb.g, rgb.b );
717 void backlight_effect_cycle_left_right(void)
719 uint8_t offset = ( g_tick << g_config.effect_speed ) & 0xFF;
720 HSV hsv = { .h = 0, .s = 255, .v = g_config.brightness };
723 for ( int i=0; i<BACKLIGHT_LED_COUNT; i++ )
725 uint16_t offset2 = g_key_hit[i]<<2;
726 // stabilizer LEDs use spacebar hits
727 if ( i == 36+6 || i == 54+13 || // LC6, LD13
728 ( g_config.use_7u_spacebar && i == 54+14 ) ) // LD14
730 offset2 = g_key_hit[36+0]<<2;
732 offset2 = (offset2<=63) ? (63-offset2) : 0;
734 map_led_to_point( i, &point );
735 // Relies on hue being 8-bit and wrapping
736 hsv.h = point.x + offset + offset2;
737 rgb = hsv_to_rgb( hsv );
738 backlight_set_color( i, rgb.r, rgb.g, rgb.b );
742 void backlight_effect_cycle_up_down(void)
744 uint8_t offset = ( g_tick << g_config.effect_speed ) & 0xFF;
745 HSV hsv = { .h = 0, .s = 255, .v = g_config.brightness };
748 for ( int i=0; i<BACKLIGHT_LED_COUNT; i++ )
750 uint16_t offset2 = g_key_hit[i]<<2;
751 // stabilizer LEDs use spacebar hits
752 if ( i == 36+6 || i == 54+13 || // LC6, LD13
753 ( g_config.use_7u_spacebar && i == 54+14 ) ) // LD14
755 offset2 = g_key_hit[36+0]<<2;
757 offset2 = (offset2<=63) ? (63-offset2) : 0;
759 map_led_to_point( i, &point );
760 // Relies on hue being 8-bit and wrapping
761 hsv.h = point.y + offset + offset2;
762 rgb = hsv_to_rgb( hsv );
763 backlight_set_color( i, rgb.r, rgb.g, rgb.b );
767 void backlight_effect_jellybean_raindrops( bool initialize )
772 // Change one LED every tick
773 uint8_t led_to_change = ( g_tick & 0x000 ) == 0 ? rand() % BACKLIGHT_LED_COUNT : 255;
775 for ( int i=0; i<BACKLIGHT_LED_COUNT; i++ )
777 // If initialize, all get set to random colors
778 // If not, all but one will stay the same as before.
779 if ( initialize || i == led_to_change )
781 hsv.h = rand() & 0xFF;
782 hsv.s = rand() & 0xFF;
783 // Override brightness with global brightness control
784 hsv.v = g_config.brightness;;
786 rgb = hsv_to_rgb( hsv );
787 backlight_set_color( i, rgb.r, rgb.g, rgb.b );
792 void backlight_effect_cycle_radial1(void)
794 uint8_t offset = ( g_tick << g_config.effect_speed ) & 0xFF;
795 HSV hsv = { .h = 0, .s = 255, .v = g_config.brightness };
798 for ( int i=0; i<BACKLIGHT_LED_COUNT; i++ )
800 map_led_to_point_polar( i, &point );
801 // Relies on hue being 8-bit and wrapping
802 hsv.h = point.x + offset;
804 rgb = hsv_to_rgb( hsv );
805 backlight_set_color( i, rgb.r, rgb.g, rgb.b );
809 void backlight_effect_cycle_radial2(void)
811 uint8_t offset = ( g_tick << g_config.effect_speed ) & 0xFF;
813 HSV hsv = { .h = 0, .s = g_config.color_1.s, .v = g_config.brightness };
816 for ( int i=0; i<BACKLIGHT_LED_COUNT; i++ )
818 map_led_to_point_polar( i, &point );
819 uint8_t offset2 = offset + point.x;
820 if ( offset2 & 0x80 )
824 offset2 = offset2 >> 2;
825 hsv.h = g_config.color_1.h + offset2;
826 hsv.s = 127 + ( point.y >> 1 );
827 rgb = hsv_to_rgb( hsv );
828 backlight_set_color( i, rgb.r, rgb.g, rgb.b );
832 #if defined(RGB_BACKLIGHT_M6_B)
833 void backlight_effect_custom_colors(void)
836 for ( uint8_t i = 0; i < 6; i++ )
838 HSV hsv = { .h = g_config.custom_color[i].h, .s = g_config.custom_color[i].s, .v = g_config.brightness };
839 rgb = hsv_to_rgb( hsv );
841 map_row_column_to_led( 0, i, &led );
842 backlight_set_color( led, rgb.r, rgb.g, rgb.b );
847 void backlight_effect_indicators_set_colors( uint8_t index, HS color )
849 HSV hsv = { .h = color.h, .s = color.s, .v = g_config.brightness };
850 RGB rgb = hsv_to_rgb( hsv );
853 backlight_set_color_all( rgb.r, rgb.g, rgb.b );
857 backlight_set_color( index, rgb.r, rgb.g, rgb.b );
859 // If the spacebar LED is the indicator,
860 // do the same for the spacebar stabilizers
861 if ( index == 36+0 ) // LC0
863 #if defined (RGB_BACKLIGHT_ZEAL65)
864 backlight_set_color( 36+7, rgb.r, rgb.g, rgb.b ); // LC7
865 backlight_set_color( 54+14, rgb.r, rgb.g, rgb.b ); // LD14
866 #elif defined (RGB_BACKLIGHT_KOYU)
867 backlight_set_color( 36+6, rgb.r, rgb.g, rgb.b ); // LC6
868 backlight_set_color( 54+14, rgb.r, rgb.g, rgb.b ); // LD14
869 #elif defined (RGB_BACKLIGHT_ZEAL60) || defined (RGB_BACKLIGHT_M60_A)
870 backlight_set_color( 36+6, rgb.r, rgb.g, rgb.b ); // LC6
871 backlight_set_color( 54+13, rgb.r, rgb.g, rgb.b ); // LD13
872 if ( g_config.use_7u_spacebar )
874 backlight_set_color( 54+14, rgb.r, rgb.g, rgb.b ); // LD14
881 // This runs after another backlight effect and replaces
882 // colors already set
883 void backlight_effect_indicators(void)
885 if ( g_config.caps_lock_indicator.index != 255 &&
886 ( g_indicator_state & (1<<USB_LED_CAPS_LOCK) ) )
888 backlight_effect_indicators_set_colors( g_config.caps_lock_indicator.index, g_config.caps_lock_indicator.color );
891 // This if/else if structure allows higher layers to
892 // override lower ones. If we set layer 3's indicator
893 // to none, then it will NOT show layer 2 or layer 1
894 // indicators, even if those layers are on via the
895 // MO13/MO23 Fn combo magic.
897 // Basically we want to handle the case where layer 3 is
898 // still the backlight configuration layer and we don't
899 // want "all LEDs" indicators hiding the backlight effect,
900 // but still allow end users to do whatever they want.
901 if ( IS_LAYER_ON(3) )
903 if ( g_config.layer_3_indicator.index != 255 )
905 backlight_effect_indicators_set_colors( g_config.layer_3_indicator.index, g_config.layer_3_indicator.color );
908 else if ( IS_LAYER_ON(2) )
910 if ( g_config.layer_2_indicator.index != 255 )
912 backlight_effect_indicators_set_colors( g_config.layer_2_indicator.index, g_config.layer_2_indicator.color );
915 else if ( IS_LAYER_ON(1) )
917 if ( g_config.layer_1_indicator.index != 255 )
919 backlight_effect_indicators_set_colors( g_config.layer_1_indicator.index, g_config.layer_1_indicator.color );
924 ISR(TIMER3_COMPA_vect)
926 // delay 1 second before driving LEDs or doing anything else
927 static uint8_t startup_tick = 0;
928 if ( startup_tick < 20 )
936 if ( g_any_key_hit < 0xFFFFFFFF )
941 for ( int led = 0; led < BACKLIGHT_LED_COUNT; led++ )
943 if ( g_key_hit[led] < 255 )
949 // Factory default magic value
950 if ( g_config.effect == 255 )
952 backlight_effect_rgb_test();
956 // Ideally we would also stop sending zeros to the LED driver PWM buffers
957 // while suspended and just do a software shutdown. This is a cheap hack for now.
958 bool suspend_backlight = ((g_suspend_state && g_config.disable_when_usb_suspended) ||
959 (g_config.disable_after_timeout > 0 && g_any_key_hit > g_config.disable_after_timeout * 60 * 20));
960 uint8_t effect = suspend_backlight ? 0 : g_config.effect;
962 // Keep track of the effect used last time,
963 // detect change in effect, so each effect can
964 // have an optional initialization.
965 static uint8_t effect_last = 255;
966 bool initialize = effect != effect_last;
967 effect_last = effect;
969 // this gets ticked at 20 Hz.
970 // each effect can opt to do calculations
971 // and/or request PWM buffer updates.
975 backlight_effect_all_off();
978 backlight_effect_solid_color();
981 #if defined(RGB_BACKLIGHT_M6_B)
982 backlight_effect_custom_colors();
984 backlight_effect_alphas_mods();
988 backlight_effect_gradient_up_down();
991 backlight_effect_raindrops( initialize );
994 backlight_effect_cycle_all();
997 backlight_effect_cycle_left_right();
1000 backlight_effect_cycle_up_down();
1003 backlight_effect_jellybean_raindrops( initialize );
1006 backlight_effect_cycle_radial1();
1009 backlight_effect_cycle_radial2();
1012 backlight_effect_all_off();
1016 if ( ! suspend_backlight )
1018 #if !defined(RGB_BACKLIGHT_M6_B)
1019 backlight_effect_indicators();
1024 void backlight_set_indicator_index( uint8_t *index, uint8_t row, uint8_t column )
1026 if ( row >= MATRIX_ROWS )
1028 // Special value, 255=none, 254=all
1033 map_row_column_to_led( row, column, index );
1037 void backlight_get_indicator_row_col( uint8_t index, uint8_t *row, uint8_t *column )
1039 if ( index == 255 || index == 254 )
1041 // Special value, 255=none, 254=all
1046 for ( uint8_t r = 0; r < MATRIX_ROWS; r++ )
1048 for ( uint8_t c = 0; c < MATRIX_COLS; c++ )
1051 map_row_column_to_led( r, c, &i );
1062 // Some helpers for setting/getting HSV
1063 void _set_color( HS *color, uint8_t *data )
1069 void _get_color( HS *color, uint8_t *data )
1075 void backlight_config_set_value( uint8_t *data )
1077 bool reinitialize = false;
1078 uint8_t *value_id = &(data[0]);
1079 uint8_t *value_data = &(data[1]);
1080 switch ( *value_id )
1082 #if defined (RGB_BACKLIGHT_ZEAL60) || defined(RGB_BACKLIGHT_ZEAL65)
1083 case id_use_split_backspace:
1085 g_config.use_split_backspace = (bool)*value_data;
1086 reinitialize = true;
1090 #if defined (RGB_BACKLIGHT_ZEAL60)
1091 case id_use_split_left_shift:
1093 g_config.use_split_left_shift = (bool)*value_data;
1094 reinitialize = true;
1097 case id_use_split_right_shift:
1099 g_config.use_split_right_shift = (bool)*value_data;
1100 reinitialize = true;
1103 case id_use_7u_spacebar:
1105 g_config.use_7u_spacebar = (bool)*value_data;
1106 reinitialize = true;
1109 case id_use_iso_enter:
1111 g_config.use_iso_enter = (bool)*value_data;
1112 reinitialize = true;
1115 case id_disable_hhkb_blocker_leds:
1117 g_config.disable_hhkb_blocker_leds = (bool)*value_data;
1118 reinitialize = true;
1122 case id_disable_when_usb_suspended:
1124 g_config.disable_when_usb_suspended = (bool)*value_data;
1127 case id_disable_after_timeout:
1129 g_config.disable_after_timeout = *value_data;
1134 g_config.brightness = *value_data;
1139 g_config.effect = *value_data;
1142 case id_effect_speed:
1144 g_config.effect_speed = *value_data;
1149 _set_color( &(g_config.color_1), value_data );
1154 _set_color( &(g_config.color_2), value_data );
1157 case id_caps_lock_indicator_color:
1159 _set_color( &(g_config.caps_lock_indicator.color), value_data );
1162 case id_caps_lock_indicator_row_col:
1164 backlight_set_indicator_index( &(g_config.caps_lock_indicator.index), value_data[0], value_data[1] );
1167 case id_layer_1_indicator_color:
1169 _set_color( &(g_config.layer_1_indicator.color), value_data );
1172 case id_layer_1_indicator_row_col:
1174 backlight_set_indicator_index( &(g_config.layer_1_indicator.index), value_data[0], value_data[1] );
1177 case id_layer_2_indicator_color:
1179 _set_color( &(g_config.layer_2_indicator.color), value_data );
1182 case id_layer_2_indicator_row_col:
1184 backlight_set_indicator_index( &(g_config.layer_2_indicator.index), value_data[0], value_data[1] );
1187 case id_layer_3_indicator_color:
1189 _set_color( &(g_config.layer_3_indicator.color), value_data );
1192 case id_layer_3_indicator_row_col:
1194 backlight_set_indicator_index( &(g_config.layer_3_indicator.index), value_data[0], value_data[1] );
1197 case id_alphas_mods:
1199 for ( int i=0; i<5; i++ )
1201 g_config.alphas_mods[i] = ( *(value_data+i*2) << 8 ) | ( *(value_data+i*2+1) );
1204 #if defined(RGB_BACKLIGHT_M6_B)
1205 case id_custom_color:
1207 uint8_t index = value_data[0];
1208 if ( index >= 0 && index <= 6 )
1210 _set_color( &(g_config.custom_color[index]), &(value_data[1]) );
1218 backlight_init_drivers();
1222 void backlight_config_get_value( uint8_t *data )
1224 uint8_t *value_id = &(data[0]);
1225 uint8_t *value_data = &(data[1]);
1226 switch ( *value_id )
1228 case id_use_split_backspace:
1230 *value_data = ( g_config.use_split_backspace ? 1 : 0 );
1233 case id_use_split_left_shift:
1235 *value_data = ( g_config.use_split_left_shift ? 1 : 0 );
1238 case id_use_split_right_shift:
1240 *value_data = ( g_config.use_split_right_shift ? 1 : 0 );
1243 case id_use_7u_spacebar:
1245 *value_data = ( g_config.use_7u_spacebar ? 1 : 0 );
1248 case id_use_iso_enter:
1250 *value_data = ( g_config.use_iso_enter ? 1 : 0 );
1253 case id_disable_when_usb_suspended:
1255 *value_data = ( g_config.disable_when_usb_suspended ? 1 : 0 );
1258 case id_disable_hhkb_blocker_leds:
1260 *value_data = ( g_config.disable_hhkb_blocker_leds ? 1 : 0 );
1263 case id_disable_after_timeout:
1265 *value_data = g_config.disable_after_timeout;
1270 *value_data = g_config.brightness;
1275 *value_data = g_config.effect;
1278 case id_effect_speed:
1280 *value_data = g_config.effect_speed;
1285 _get_color( &(g_config.color_1), value_data );
1290 _get_color( &(g_config.color_2), value_data );
1293 case id_caps_lock_indicator_color:
1295 _get_color( &(g_config.caps_lock_indicator.color), value_data );
1298 case id_caps_lock_indicator_row_col:
1300 backlight_get_indicator_row_col( g_config.caps_lock_indicator.index, &(value_data[0]), &(value_data[1]) );
1303 case id_layer_1_indicator_color:
1305 _get_color( &(g_config.layer_1_indicator.color), value_data );
1308 case id_layer_1_indicator_row_col:
1310 backlight_get_indicator_row_col( g_config.layer_1_indicator.index, &(value_data[0]), &(value_data[1]) );
1313 case id_layer_2_indicator_color:
1315 _get_color( &(g_config.layer_2_indicator.color), value_data );
1318 case id_layer_2_indicator_row_col:
1320 backlight_get_indicator_row_col( g_config.layer_2_indicator.index, &(value_data[0]), &(value_data[1]) );
1323 case id_layer_3_indicator_color:
1325 _get_color( &(g_config.layer_3_indicator.color), value_data );
1328 case id_layer_3_indicator_row_col:
1330 backlight_get_indicator_row_col( g_config.layer_3_indicator.index, &(value_data[0]), &(value_data[1]) );
1333 case id_alphas_mods:
1335 for ( int i=0; i<5; i++ )
1337 *(value_data+i*2) = g_config.alphas_mods[i] >> 8;
1338 *(value_data+i*2+1) = g_config.alphas_mods[i] & 0xFF;
1341 #if defined(RGB_BACKLIGHT_M6_B)
1342 case id_custom_color:
1344 uint8_t index = value_data[0];
1345 if ( index >= 0 && index <= 6 )
1347 _get_color( &(g_config.custom_color[index]), &(value_data[1]) );
1354 void backlight_config_set_alphas_mods( uint16_t *alphas_mods )
1356 for ( int i=0; i<5; i++ )
1358 g_config.alphas_mods[i] = alphas_mods[i];
1361 backlight_config_save();
1364 void backlight_config_load(void)
1366 eeprom_read_block( &g_config, ((void*)RGB_BACKLIGHT_CONFIG_EEPROM_ADDR), sizeof(backlight_config) );
1369 void backlight_config_save(void)
1371 eeprom_update_block( &g_config, ((void*)RGB_BACKLIGHT_CONFIG_EEPROM_ADDR), sizeof(backlight_config) );
1374 void backlight_init_drivers(void)
1379 #if defined(RGB_BACKLIGHT_M6_B)
1382 IS31FL3731_init( ISSI_ADDR_1 );
1383 IS31FL3731_init( ISSI_ADDR_2 );
1385 for ( int index = 0; index < BACKLIGHT_LED_COUNT; index++ )
1387 // OR the possible "disabled" cases together, then NOT the result to get the enabled state
1388 // LC6 LD13 not present on Zeal65
1389 #if defined (RGB_BACKLIGHT_ZEAL65)
1390 bool enabled = !( ( index == 18+5 && !g_config.use_split_backspace ) || // LB5
1391 ( index == 36+6 ) || // LC6
1392 ( index == 54+13 ) ); // LD13
1393 #elif defined (RGB_BACKLIGHT_KOYU)
1394 bool enabled = !( ( index == 36+15 ) || // LC15
1395 ( index == 54+13 ) || // LD13
1396 ( index == 54+17 ) ); // LD17
1397 #elif defined (RGB_BACKLIGHT_M60_A)
1399 // LB6 LB7 LB8 LB15 LB16 LB17 not present on M60-A
1400 ( index == 18+6 ) || // LB6
1401 ( index == 18+7 ) || // LB7
1402 ( index == 18+8 ) || // LB8
1403 ( index == 18+15 ) || // LB15
1404 ( index == 18+16 ) || // LB16
1405 ( index == 18+17 ) || // LB17
1406 // HHKB blockers (LC17, LD17) and ISO extra keys (LC15,LD13) not present on M60-A
1407 ( index == 36+17 ) || // LC17
1408 ( index == 54+17 ) || // LD17
1409 ( index == 36+15 ) || // LC15
1410 ( index == 54+13 ) ); // LD13
1411 #elif defined (RGB_BACKLIGHT_ZEAL60)
1412 // LB6 LB7 LB8 LB15 LB16 LB17 not present on Zeal60
1413 bool enabled = !( ( index == 18+5 && !g_config.use_split_backspace ) || // LB5
1414 ( index == 36+15 && !g_config.use_split_left_shift ) || // LC15
1415 ( index == 54+8 && !g_config.use_split_right_shift ) || // LD8
1416 ( index == 54+13 && g_config.use_7u_spacebar ) || // LD13
1417 ( index == 36+17 && g_config.disable_hhkb_blocker_leds ) || // LC17
1418 ( index == 54+17 && g_config.disable_hhkb_blocker_leds ) || // LD17
1419 ( index == 18+6 ) || // LB6
1420 ( index == 18+7 ) || // LB7
1421 ( index == 18+8 ) || // LB8
1422 ( index == 18+15 ) || // LB15
1423 ( index == 18+16 ) || // LB16
1424 ( index == 18+17 ) ); // LB17
1426 // This only caches it for later
1427 IS31FL3731_set_led_control_register( index, enabled, enabled, enabled );
1429 // This actually updates the LED drivers
1430 IS31FL3731_update_led_control_registers( ISSI_ADDR_1, ISSI_ADDR_2 );
1431 #endif // !defined(RGB_BACKLIGHT_M6_B)
1433 // TODO: put the 1 second startup delay here?
1435 // clear the key hits
1436 for ( int led=0; led<BACKLIGHT_LED_COUNT; led++ )
1438 g_key_hit[led] = 255;
1442 bool process_record_backlight(uint16_t keycode, keyrecord_t *record)
1444 // Record keypresses for backlight effects
1445 if ( record->event.pressed )
1447 backlight_set_key_hit( record->event.key.row, record->event.key.col );
1453 if (record->event.pressed)
1455 backlight_brightness_increase();
1460 if (record->event.pressed)
1462 backlight_brightness_decrease();
1467 if (record->event.pressed)
1469 backlight_effect_increase();
1474 if (record->event.pressed)
1476 backlight_effect_decrease();
1481 if (record->event.pressed)
1483 backlight_effect_speed_increase();
1488 if (record->event.pressed)
1490 backlight_effect_speed_decrease();
1495 if (record->event.pressed)
1497 backlight_color_1_hue_increase();
1502 if (record->event.pressed)
1504 backlight_color_1_hue_decrease();
1509 if (record->event.pressed)
1511 backlight_color_1_sat_increase();
1516 if (record->event.pressed)
1518 backlight_color_1_sat_decrease();
1524 if (record->event.pressed)
1526 backlight_color_2_hue_increase();
1531 if (record->event.pressed)
1533 backlight_color_2_hue_decrease();
1538 if (record->event.pressed)
1540 backlight_color_2_sat_increase();
1545 if (record->event.pressed)
1547 backlight_color_2_sat_decrease();
1557 // Deals with the messy details of incrementing an integer
1558 uint8_t increment( uint8_t value, uint8_t step, uint8_t min, uint8_t max )
1560 int16_t new_value = value;
1562 return MIN( MAX( new_value, min ), max );
1565 uint8_t decrement( uint8_t value, uint8_t step, uint8_t min, uint8_t max )
1567 int16_t new_value = value;
1569 return MIN( MAX( new_value, min ), max );
1572 void backlight_effect_increase(void)
1574 g_config.effect = increment( g_config.effect, 1, 0, BACKLIGHT_EFFECT_MAX );
1575 backlight_config_save();
1578 void backlight_effect_decrease(void)
1580 g_config.effect = decrement( g_config.effect, 1, 0, BACKLIGHT_EFFECT_MAX );
1581 backlight_config_save();
1584 void backlight_effect_speed_increase(void)
1586 g_config.effect_speed = increment( g_config.effect_speed, 1, 0, 3 );
1587 backlight_config_save();
1590 void backlight_effect_speed_decrease(void)
1592 g_config.effect_speed = decrement( g_config.effect_speed, 1, 0, 3 );
1593 backlight_config_save();
1596 void backlight_brightness_increase(void)
1598 g_config.brightness = increment( g_config.brightness, 8, 0, 255 );
1599 backlight_config_save();
1602 void backlight_brightness_decrease(void)
1604 g_config.brightness = decrement( g_config.brightness, 8, 0, 255 );
1605 backlight_config_save();
1608 void backlight_color_1_hue_increase(void)
1610 g_config.color_1.h = increment( g_config.color_1.h, 8, 0, 255 );
1611 backlight_config_save();
1614 void backlight_color_1_hue_decrease(void)
1616 g_config.color_1.h = decrement( g_config.color_1.h, 8, 0, 255 );
1617 backlight_config_save();
1620 void backlight_color_1_sat_increase(void)
1622 g_config.color_1.s = increment( g_config.color_1.s, 8, 0, 255 );
1623 backlight_config_save();
1626 void backlight_color_1_sat_decrease(void)
1628 g_config.color_1.s = decrement( g_config.color_1.s, 8, 0, 255 );
1629 backlight_config_save();
1632 void backlight_color_2_hue_increase(void)
1634 g_config.color_2.h = increment( g_config.color_2.h, 8, 0, 255 );
1635 backlight_config_save();
1638 void backlight_color_2_hue_decrease(void)
1640 g_config.color_2.h = decrement( g_config.color_2.h, 8, 0, 255 );
1641 backlight_config_save();
1644 void backlight_color_2_sat_increase(void)
1646 g_config.color_2.s = increment( g_config.color_2.s, 8, 0, 255 );
1647 backlight_config_save();
1650 void backlight_color_2_sat_decrease(void)
1652 g_config.color_2.s = decrement( g_config.color_2.s, 8, 0, 255 );
1653 backlight_config_save();
1656 #if defined(RGB_DEBUGGING_ONLY)
1657 void backlight_test_led( uint8_t index, bool red, bool green, bool blue )
1659 for ( int i=0; i<BACKLIGHT_LED_COUNT; i++ )
1663 IS31FL3731_set_led_control_register( i, red, green, blue );
1667 IS31FL3731_set_led_control_register( i, false, false, false );
1671 #endif // defined(RGB_DEBUGGING_ONLY)
1673 void backlight_debug_led( bool state )
1689 #endif // BACKLIGHT_ENABLED