5 #if defined(RGBLIGHT_ENABLE)
6 extern rgblight_config_t rgblight_config;
7 #elif defined(RGB_MATRIX_ENABLE)
8 extern rgb_config_t rgb_matrix_config;
11 #ifdef RGBLIGHT_ENABLE
12 void rgblight_sethsv_default_helper(uint8_t index) {
13 rgblight_sethsv_at(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, index);
15 #endif // RGBLIGHT_ENABLE
17 #ifdef INDICATOR_LIGHTS
18 void set_rgb_indicators(uint8_t this_mod, uint8_t this_led, uint8_t this_osm) {
19 if (userspace_config.rgb_layer_change && biton32(layer_state) == 0) {
20 if (this_mod & MODS_SHIFT_MASK || this_led & (1<<USB_LED_CAPS_LOCK) || this_osm & MODS_SHIFT_MASK) {
22 rgblight_sethsv_at(120, 255, 255, SHFT_LED1);
25 rgblight_sethsv_at(120, 255, 255, SHFT_LED2);
29 rgblight_sethsv_default_helper(SHFT_LED1);
32 rgblight_sethsv_default_helper(SHFT_LED2);
35 if (this_mod & MODS_CTRL_MASK || this_osm & MODS_CTRL_MASK) {
37 rgblight_sethsv_at(0, 255, 255, CTRL_LED1);
40 rgblight_sethsv_at(0, 255, 255, CTRL_LED2);
44 rgblight_sethsv_default_helper(CTRL_LED1);
47 rgblight_sethsv_default_helper(CTRL_LED2);
50 if (this_mod & MODS_GUI_MASK || this_osm & MODS_GUI_MASK) {
52 rgblight_sethsv_at(51, 255, 255, GUI_LED1);
55 rgblight_sethsv_at(51, 255, 255, GUI_LED2);
59 rgblight_sethsv_default_helper(GUI_LED1);
62 rgblight_sethsv_default_helper(GUI_LED2);
65 if (this_mod & MODS_ALT_MASK || this_osm & MODS_ALT_MASK) {
67 rgblight_sethsv_at(240, 255, 255, ALT_LED1);
70 rgblight_sethsv_at(240, 255, 255, ALT_LED2);
74 rgblight_sethsv_default_helper(ALT_LED1);
77 rgblight_sethsv_default_helper(ALT_LED2);
83 void matrix_scan_indicator(void) {
84 set_rgb_indicators(get_mods(), host_keyboard_leds(), get_oneshot_mods());
86 #endif //INDICATOR_LIGHTS
88 #ifdef RGBLIGHT_TWINKLE
89 static rgblight_fadeout lights[RGBLED_NUM];
91 __attribute__ ((weak))
92 bool rgblight_twinkle_is_led_used_keymap(uint8_t index) { return false; }
94 bool rgblight_twinkle_is_led_used(uint8_t index) {
96 #ifdef INDICATOR_LIGHTS
129 #endif //INDICATOR_LIGHTS
131 return rgblight_twinkle_is_led_used_keymap(index);
135 void scan_rgblight_fadeout(void) { // Don't effing change this function .... rgblight_sethsv is supppppper intensive
137 for (uint8_t light_index = 0 ; light_index < RGBLED_NUM ; ++light_index ) {
138 if (lights[light_index].enabled && timer_elapsed(lights[light_index].timer) > 10) {
139 rgblight_fadeout *light = &lights[light_index];
144 if (biton32(layer_state) == 0) {
145 sethsv(light->hue + rand() % 0xF, 255, light->life, (LED_TYPE *)&led[light_index]);
147 light->timer = timer_read();
150 if (light->enabled && biton32(layer_state) == 0) { rgblight_sethsv_default_helper(light_index); }
151 litup = light->enabled = false;
155 if (litup && biton32(layer_state) == 0) {
160 void start_rgb_light(void) {
162 uint8_t indices[RGBLED_NUM];
163 uint8_t indices_count = 0;
164 uint8_t min_life = 0xFF;
165 uint8_t min_life_index = -1;
166 for (uint8_t index = 0 ; index < RGBLED_NUM ; ++index ) {
167 if (rgblight_twinkle_is_led_used(index)) { continue; }
168 if (lights[index].enabled) {
169 if (min_life_index == -1 ||
170 lights[index].life < min_life)
172 min_life = lights[index].life;
173 min_life_index = index;
178 indices[indices_count] = index;
183 if (!indices_count) {
184 light_index = min_life_index;
187 light_index = indices[rand() % indices_count];
190 rgblight_fadeout *light = &lights[light_index];
191 light->enabled = true;
192 light->timer = timer_read();
193 light->life = 0xC0 + rand() % 0x40;
195 light->hue = rgblight_config.hue + (rand() % 0xB4) - 0x54;
197 rgblight_sethsv_at(light->hue, 255, light->life, light_index);
202 bool process_record_user_rgb(uint16_t keycode, keyrecord_t *record) {
203 if ((keycode >= QK_MOD_TAP && keycode <= QK_MOD_TAP_MAX) || (keycode >= QK_LAYER_TAP && keycode <= QK_LAYER_TAP_MAX)) {
204 keycode = keycode & 0xFF;
207 #ifdef RGBLIGHT_TWINKLE
208 case KC_A ... KC_SLASH:
209 case KC_F1 ... KC_F12:
210 case KC_INSERT ... KC_UP:
211 case KC_KP_SLASH ... KC_KP_DOT:
212 case KC_F13 ... KC_F24:
213 case KC_AUDIO_MUTE ... KC_MEDIA_REWIND:
214 if (record->event.pressed) { start_rgb_light(); }
216 #endif // RGBLIGHT_TWINKLE
217 case KC_RGB_T: // This allows me to use underglow as layer indication, or as normal
218 #if defined(RGBLIGHT_ENABLE) || defined(RGB_MATRIX_ENABLE)
219 if (record->event.pressed) {
220 userspace_config.rgb_layer_change ^= 1;
221 xprintf("rgblight layer change [EEPROM]: %u\n", userspace_config.rgb_layer_change);
222 eeconfig_update_user(userspace_config.raw);
223 if (userspace_config.rgb_layer_change) {
224 layer_state_set(layer_state); // This is needed to immediately set the layer color (looks better)
227 #endif // RGBLIGHT_ENABLE
229 #ifdef RGBLIGHT_ENABLE
230 case RGB_MODE_FORWARD ... RGB_MODE_GRADIENT: // quantum_keycodes.h L400 for definitions
231 if (record->event.pressed) { //This disables layer indication, as it's assumed that if you're changing this ... you want that disabled
232 if (userspace_config.rgb_layer_change) {
233 userspace_config.rgb_layer_change = false;
234 xprintf("rgblight layer change [EEPROM]: %u\n", userspace_config.rgb_layer_change);
235 eeconfig_update_user(userspace_config.raw);
239 #endif // RGBLIGHT_ENABLE
246 void matrix_init_rgb(void) {
248 // #ifdef RGBLIGHT_ENABLE
249 // if (userspace_config.rgb_layer_change) {
250 // rgblight_enable_noeeprom();
251 // switch (biton32(eeconfig_read_default_layer())) {
253 // rgblight_sethsv_noeeprom_magenta(); break;
255 // rgblight_sethsv_noeeprom_springgreen(); break;
257 // rgblight_sethsv_noeeprom_goldenrod(); break;
259 // rgblight_sethsv_noeeprom_cyan(); break;
261 // rgblight_mode(RGBLIGHT_MODE_STATIC_LIGHT);
266 void matrix_scan_rgb(void) {
267 #ifdef RGBLIGHT_TWINKLE
268 scan_rgblight_fadeout();
269 #endif // RGBLIGHT_ENABLE
271 #ifdef INDICATOR_LIGHTS
272 matrix_scan_indicator();
278 uint32_t layer_state_set_rgb(uint32_t state) {
279 #ifdef RGBLIGHT_ENABLE
280 if (userspace_config.rgb_layer_change) {
281 switch (biton32(state)) {
283 rgblight_sethsv_noeeprom_orange();
284 userspace_config.is_overwatch ? rgblight_effect_snake(RGBLIGHT_MODE_SNAKE + 2) : rgblight_effect_snake(RGBLIGHT_MODE_SNAKE + 3);
287 rgblight_sethsv_noeeprom_chartreuse();
288 rgblight_mode_noeeprom(RGBLIGHT_MODE_KNIGHT + 1);
291 rgblight_sethsv_noeeprom_orange();
292 rgblight_mode_noeeprom(RGBLIGHT_MODE_SNAKE + 2);
295 rgblight_sethsv_noeeprom_red();
296 rgblight_mode_noeeprom(RGBLIGHT_MODE_BREATHING + 3);
299 rgblight_sethsv_noeeprom_yellow();
300 rgblight_mode_noeeprom(RGBLIGHT_MODE_BREATHING + 3);
303 rgblight_sethsv_noeeprom_green();
304 rgblight_mode_noeeprom(RGBLIGHT_MODE_BREATHING + 3);
307 rgblight_sethsv_noeeprom_red();
308 rgblight_mode_noeeprom(RGBLIGHT_MODE_KNIGHT + 2);
310 default: // for any other layers, or the default layer
311 switch (biton32(default_layer_state)) {
313 rgblight_sethsv_noeeprom_magenta(); break;
315 rgblight_sethsv_noeeprom_springgreen(); break;
317 rgblight_sethsv_noeeprom_goldenrod(); break;
319 rgblight_sethsv_noeeprom_cyan(); break;
321 biton32(state) == _MODS ? rgblight_mode_noeeprom(RGBLIGHT_MODE_BREATHING) : rgblight_mode_noeeprom(RGBLIGHT_MODE_STATIC_LIGHT); // if _MODS layer is on, then breath to denote it
324 // layer_state_set_indicator(); // Runs every scan, so need to call this here .... since I can't get it working "right" anyhow
326 #endif // RGBLIGHT_ENABLE
331 uint32_t default_layer_state_set_rgb(uint32_t state) {
332 #ifdef RGBLIGHT_ENABLE
333 if (userspace_config.rgb_layer_change) {
334 rgblight_config_t temp_rgblight_config = rgblight_config;
335 switch (biton32(state)) {
337 temp_rgblight_config.hue = 300;
338 temp_rgblight_config.val = 255;
339 temp_rgblight_config.sat = 255;
340 temp_rgblight_config.mode = 1;
343 temp_rgblight_config.hue = 150;
344 temp_rgblight_config.val = 255;
345 temp_rgblight_config.sat = 255;
346 temp_rgblight_config.mode = 1;
348 temp_rgblight_config.hue = 43;
349 temp_rgblight_config.val = 218;
350 temp_rgblight_config.sat = 218;
351 temp_rgblight_config.mode = 1;
353 temp_rgblight_config.hue = 180;
354 temp_rgblight_config.val = 255;
355 temp_rgblight_config.sat = 255;
356 temp_rgblight_config.mode = 1;
358 if (temp_rgblight_config.raw != eeconfig_read_rgblight()) {
359 xprintf("rgblight set default layer hsv [EEPROM]: %u,%u,%u,%u\n", temp_rgblight_config.hue, temp_rgblight_config.sat, temp_rgblight_config.val, temp_rgblight_config.mode);
360 eeconfig_update_rgblight(temp_rgblight_config.raw);
363 #endif // RGBLIGHT_ENABLE