5 #if defined(RGBLIGHT_ENABLE)
6 extern rgblight_config_t rgblight_config;
10 #ifdef RGBLIGHT_ENABLE
11 void rgblight_sethsv_default_helper(uint8_t index) {
12 rgblight_sethsv_at(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, index);
14 #endif // RGBLIGHT_ENABLE
16 #ifdef INDICATOR_LIGHTS
17 void set_rgb_indicators(uint8_t this_mod, uint8_t this_led, uint8_t this_osm) {
18 if (userspace_config.rgb_layer_change && biton32(layer_state) == 0) {
19 if ( (this_mod | this_osm) & MOD_MASK_SHIFT || this_led & (1<<USB_LED_CAPS_LOCK) ) {
21 rgblight_sethsv_at(120, 255, 255, SHFT_LED1);
24 rgblight_sethsv_at(120, 255, 255, SHFT_LED2);
28 rgblight_sethsv_default_helper(SHFT_LED1);
31 rgblight_sethsv_default_helper(SHFT_LED2);
34 if ( (this_mod | this_osm) & MOD_MASK_CTRL) {
36 rgblight_sethsv_at(0, 255, 255, CTRL_LED1);
39 rgblight_sethsv_at(0, 255, 255, CTRL_LED2);
43 rgblight_sethsv_default_helper(CTRL_LED1);
46 rgblight_sethsv_default_helper(CTRL_LED2);
49 if ( (this_mod | this_osm) & MOD_MASK_GUI) {
51 rgblight_sethsv_at(51, 255, 255, GUI_LED1);
54 rgblight_sethsv_at(51, 255, 255, GUI_LED2);
58 rgblight_sethsv_default_helper(GUI_LED1);
61 rgblight_sethsv_default_helper(GUI_LED2);
64 if ( (this_mod | this_osm) & MOD_MASK_ALT) {
66 rgblight_sethsv_at(240, 255, 255, ALT_LED1);
69 rgblight_sethsv_at(240, 255, 255, ALT_LED2);
73 rgblight_sethsv_default_helper(ALT_LED1);
76 rgblight_sethsv_default_helper(ALT_LED2);
82 void matrix_scan_indicator(void) {
83 if (has_initialized) {
84 set_rgb_indicators(get_mods(), host_keyboard_leds(), get_oneshot_mods());
87 #endif //INDICATOR_LIGHTS
89 #ifdef RGBLIGHT_TWINKLE
90 static rgblight_fadeout lights[RGBLED_NUM];
92 __attribute__ ((weak))
93 bool rgblight_twinkle_is_led_used_keymap(uint8_t index) { return false; }
95 bool rgblight_twinkle_is_led_used(uint8_t index) {
97 #ifdef INDICATOR_LIGHTS
130 #endif //INDICATOR_LIGHTS
132 return rgblight_twinkle_is_led_used_keymap(index);
136 void scan_rgblight_fadeout(void) { // Don't effing change this function .... rgblight_sethsv is supppppper intensive
138 for (uint8_t light_index = 0 ; light_index < RGBLED_NUM ; ++light_index ) {
139 if (lights[light_index].enabled && timer_elapsed(lights[light_index].timer) > 10) {
140 rgblight_fadeout *light = &lights[light_index];
145 if (biton32(layer_state) == 0) {
146 sethsv(light->hue + rand() % 0xF, 255, light->life, (LED_TYPE *)&led[light_index]);
148 light->timer = timer_read();
151 if (light->enabled && biton32(layer_state) == 0) {
152 rgblight_sethsv_default_helper(light_index);
154 litup = light->enabled = false;
158 if (litup && biton32(layer_state) == 0) {
163 void start_rgb_light(void) {
165 uint8_t indices[RGBLED_NUM];
166 uint8_t indices_count = 0;
167 uint8_t min_life = 0xFF;
168 uint8_t min_life_index = -1;
169 for (uint8_t index = 0 ; index < RGBLED_NUM ; ++index ) {
170 if (rgblight_twinkle_is_led_used(index)) { continue; }
171 if (lights[index].enabled) {
172 if (min_life_index == -1 ||
173 lights[index].life < min_life)
175 min_life = lights[index].life;
176 min_life_index = index;
181 indices[indices_count] = index;
186 if (!indices_count) {
187 light_index = min_life_index;
190 light_index = indices[rand() % indices_count];
193 rgblight_fadeout *light = &lights[light_index];
194 light->enabled = true;
195 light->timer = timer_read();
196 light->life = 0xC0 + rand() % 0x40;
198 light->hue = rgblight_config.hue + (rand() % 0xB4) - 0x54;
200 rgblight_sethsv_at(light->hue, 255, light->life, light_index);
205 bool process_record_user_rgb(uint16_t keycode, keyrecord_t *record) {
206 if ((keycode >= QK_MOD_TAP && keycode <= QK_MOD_TAP_MAX) || (keycode >= QK_LAYER_TAP && keycode <= QK_LAYER_TAP_MAX)) {
207 keycode = keycode & 0xFF;
210 #ifdef RGBLIGHT_TWINKLE
211 case KC_A ... KC_SLASH:
212 case KC_F1 ... KC_F12:
213 case KC_INSERT ... KC_UP:
214 case KC_KP_SLASH ... KC_KP_DOT:
215 case KC_F13 ... KC_F24:
216 case KC_AUDIO_MUTE ... KC_MEDIA_REWIND:
217 if (record->event.pressed) { start_rgb_light(); }
219 #endif // RGBLIGHT_TWINKLE
220 case KC_RGB_T: // This allows me to use underglow as layer indication, or as normal
221 #if defined(RGBLIGHT_ENABLE) || defined(RGB_MATRIX_ENABLE)
222 if (record->event.pressed) {
223 userspace_config.rgb_layer_change ^= 1;
224 xprintf("rgblight layer change [EEPROM]: %u\n", userspace_config.rgb_layer_change);
225 eeconfig_update_user(userspace_config.raw);
226 if (userspace_config.rgb_layer_change) {
227 layer_state_set(layer_state); // This is needed to immediately set the layer color (looks better)
230 #endif // RGBLIGHT_ENABLE
232 #ifdef RGBLIGHT_ENABLE
233 case RGB_MODE_FORWARD ... RGB_MODE_GRADIENT: // quantum_keycodes.h L400 for definitions
234 if (record->event.pressed) { //This disables layer indication, as it's assumed that if you're changing this ... you want that disabled
235 if (userspace_config.rgb_layer_change) {
236 userspace_config.rgb_layer_change = false;
237 xprintf("rgblight layer change [EEPROM]: %u\n", userspace_config.rgb_layer_change);
238 eeconfig_update_user(userspace_config.raw);
242 #endif // RGBLIGHT_ENABLE
249 void keyboard_post_init_rgb(void) {
250 #if defined(RGBLIGHT_ENABLE) && defined(RGBLIGHT_STARTUP_ANIMATION)
251 if (userspace_config.rgb_layer_change) { rgblight_enable_noeeprom(); }
252 if (rgblight_config.enable) {
253 layer_state_set_user(layer_state);
254 uint16_t old_hue = rgblight_config.hue;
255 rgblight_mode_noeeprom(RGBLIGHT_MODE_STATIC_LIGHT);
256 for (uint16_t i = 255; i > 0; i--) {
257 rgblight_sethsv_noeeprom( ( i + old_hue) % 255, 255, 255);
263 layer_state_set_user(layer_state);
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 layer_state_t layer_state_set_rgb(layer_state_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_mode_noeeprom(RGBLIGHT_MODE_SNAKE + 2) : rgblight_mode_noeeprom(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_coral(); break;
321 rgblight_sethsv_noeeprom_yellow(); break;
323 rgblight_sethsv_noeeprom_pink(); break;
325 rgblight_sethsv_noeeprom_blue(); break;
327 rgblight_sethsv_noeeprom_cyan(); break;
329 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
333 #endif // RGBLIGHT_ENABLE
338 #ifdef RGB_MATRIX_ENABLE
339 extern led_config_t g_led_config;
340 void rgb_matrix_layer_helper (uint8_t red, uint8_t green, uint8_t blue, uint8_t led_type) {
341 for (int i = 0; i < DRIVER_LED_TOTAL; i++) {
342 if (HAS_FLAGS(g_led_config.flags[i], led_type)) {
343 rgb_matrix_set_color( i, red, green, blue );