]> git.donarmstrong.com Git - qmk_firmware.git/blob - users/drashna/rgb_stuff.c
Cleanup/rgb matrix (#5811)
[qmk_firmware.git] / users / drashna / rgb_stuff.c
1 #include "drashna.h"
2 #include "rgb_stuff.h"
3 #include "eeprom.h"
4
5 #if defined(RGBLIGHT_ENABLE)
6 extern rgblight_config_t rgblight_config;
7 bool has_initialized;
8 #endif
9
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);
13 }
14 #endif // RGBLIGHT_ENABLE
15
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) ) {
20         #ifdef SHFT_LED1
21             rgblight_sethsv_at(120, 255, 255, SHFT_LED1);
22         #endif // SHFT_LED1
23         #ifdef SHFT_LED2
24             rgblight_sethsv_at(120, 255, 255, SHFT_LED2);
25         #endif // SHFT_LED2
26         } else {
27         #ifdef SHFT_LED1
28             rgblight_sethsv_default_helper(SHFT_LED1);
29         #endif // SHFT_LED1
30         #ifdef SHFT_LED2
31             rgblight_sethsv_default_helper(SHFT_LED2);
32         #endif // SHFT_LED2
33         }
34         if ( (this_mod | this_osm) & MOD_MASK_CTRL) {
35         #ifdef CTRL_LED1
36             rgblight_sethsv_at(0, 255, 255, CTRL_LED1);
37         #endif // CTRL_LED1
38         #ifdef CTRL_LED2
39             rgblight_sethsv_at(0, 255, 255, CTRL_LED2);
40         #endif // CTRL_LED2
41         } else {
42         #ifdef CTRL_LED1
43             rgblight_sethsv_default_helper(CTRL_LED1);
44         #endif // CTRL_LED1
45         #ifdef CTRL_LED2
46             rgblight_sethsv_default_helper(CTRL_LED2);
47         #endif // CTRL_LED2
48         }
49         if ( (this_mod | this_osm) & MOD_MASK_GUI) {
50         #ifdef GUI_LED1
51             rgblight_sethsv_at(51, 255, 255, GUI_LED1);
52         #endif // GUI_LED1
53         #ifdef GUI_LED2
54             rgblight_sethsv_at(51, 255, 255, GUI_LED2);
55         #endif // GUI_LED2
56         } else {
57         #ifdef GUI_LED1
58             rgblight_sethsv_default_helper(GUI_LED1);
59         #endif // GUI_LED1
60         #ifdef GUI_LED2
61             rgblight_sethsv_default_helper(GUI_LED2);
62         #endif // GUI_LED2
63         }
64         if ( (this_mod | this_osm) & MOD_MASK_ALT) {
65         #ifdef ALT_LED1
66             rgblight_sethsv_at(240, 255, 255, ALT_LED1);
67         #endif // ALT_LED1
68         #ifdef GUI_LED2
69             rgblight_sethsv_at(240, 255, 255, ALT_LED2);
70         #endif // GUI_LED2
71         } else {
72         #ifdef GUI_LED1
73             rgblight_sethsv_default_helper(ALT_LED1);
74         #endif // GUI_LED1
75         #ifdef GUI_LED2
76             rgblight_sethsv_default_helper(ALT_LED2);
77         #endif // GUI_LED2
78         }
79     }
80 }
81
82 void matrix_scan_indicator(void) {
83     if (has_initialized) {
84         set_rgb_indicators(get_mods(), host_keyboard_leds(), get_oneshot_mods());
85     }
86 }
87 #endif //INDICATOR_LIGHTS
88
89 #ifdef RGBLIGHT_TWINKLE
90 static rgblight_fadeout lights[RGBLED_NUM];
91
92 __attribute__ ((weak))
93 bool rgblight_twinkle_is_led_used_keymap(uint8_t index) { return false; }
94
95 bool rgblight_twinkle_is_led_used(uint8_t index) {
96     switch (index) {
97 #ifdef INDICATOR_LIGHTS
98 #ifdef SHFT_LED1
99         case SHFT_LED1:
100             return true;
101 #endif //SHFT_LED1
102 #ifdef SHFT_LED2
103         case SHFT_LED2:
104             return true;
105 #endif //SHFT_LED2
106 #ifdef CTRL_LED1
107         case CTRL_LED1:
108             return true;
109 #endif //CTRL_LED1
110 #ifdef CTRL_LED2
111         case CTRL_LED2:
112             return true;
113 #endif //CTRL_LED2
114 #ifdef GUI_LED1
115         case GUI_LED1:
116             return true;
117 #endif //GUI_LED1
118 #ifdef GUI_LED2
119         case GUI_LED2:
120             return true;
121 #endif //GUI_LED2
122 #ifdef ALT_LED1
123         case ALT_LED1:
124             return true;
125 #endif //ALT_LED1
126 #ifdef ALT_LED2
127         case ALT_LED2:
128             return true;
129 #endif //ALT_LED2
130 #endif //INDICATOR_LIGHTS
131         default:
132             return rgblight_twinkle_is_led_used_keymap(index);
133   }
134 }
135
136 void scan_rgblight_fadeout(void) { // Don't effing change this function .... rgblight_sethsv is supppppper intensive
137     bool litup = false;
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];
141         litup = true;
142
143         if (light->life) {
144             light->life -= 1;
145             if (biton32(layer_state) == 0) {
146                 sethsv(light->hue + rand() % 0xF, 255, light->life, (LED_TYPE *)&led[light_index]);
147             }
148             light->timer = timer_read();
149         }
150         else {
151             if (light->enabled && biton32(layer_state) == 0) {
152                 rgblight_sethsv_default_helper(light_index);
153             }
154             litup = light->enabled = false;
155         }
156         }
157     }
158     if (litup && biton32(layer_state) == 0) {
159         rgblight_set();
160     }
161 }
162
163 void start_rgb_light(void) {
164
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)
174         {
175           min_life = lights[index].life;
176           min_life_index = index;
177         }
178         continue;
179       }
180
181       indices[indices_count] = index;
182       ++indices_count;
183     }
184
185     uint8_t light_index;
186     if (!indices_count) {
187         light_index = min_life_index;
188     }
189     else {
190       light_index = indices[rand() % indices_count];
191     }
192
193     rgblight_fadeout *light = &lights[light_index];
194     light->enabled = true;
195     light->timer = timer_read();
196     light->life = 0xC0 + rand() % 0x40;
197
198     light->hue = rgblight_config.hue + (rand() % 0xB4) - 0x54;
199
200     rgblight_sethsv_at(light->hue, 255, light->life, light_index);
201 }
202 #endif
203
204
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;
208     }
209     switch (keycode) {
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(); }
218             return true; break;
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)
228                 }
229             }
230 #endif // RGBLIGHT_ENABLE
231             return false; break;
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);
239                 }
240             }
241             return true; break;
242 #endif // RGBLIGHT_ENABLE
243   }
244     return true;
245 }
246
247
248
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);
258             matrix_scan();
259             wait_ms(10);
260         }
261     }
262 #endif
263     layer_state_set_user(layer_state);
264 }
265
266 void matrix_scan_rgb(void) {
267 #ifdef RGBLIGHT_TWINKLE
268     scan_rgblight_fadeout();
269 #endif // RGBLIGHT_ENABLE
270
271 #ifdef INDICATOR_LIGHTS
272     matrix_scan_indicator();
273 #endif
274
275 }
276
277
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)) {
282             case _MACROS:
283                 rgblight_sethsv_noeeprom_orange();
284                 userspace_config.is_overwatch ? rgblight_mode_noeeprom(RGBLIGHT_MODE_SNAKE + 2) : rgblight_mode_noeeprom(RGBLIGHT_MODE_SNAKE + 3);
285                 break;
286             case _MEDIA:
287                 rgblight_sethsv_noeeprom_chartreuse();
288                 rgblight_mode_noeeprom(RGBLIGHT_MODE_KNIGHT + 1);
289                 break;
290             case _GAMEPAD:
291                 rgblight_sethsv_noeeprom_orange();
292                 rgblight_mode_noeeprom(RGBLIGHT_MODE_SNAKE + 2);
293                 break;
294             case _DIABLO:
295                 rgblight_sethsv_noeeprom_red();
296                 rgblight_mode_noeeprom(RGBLIGHT_MODE_BREATHING + 3);
297                 break;
298             case _RAISE:
299                 rgblight_sethsv_noeeprom_yellow();
300                 rgblight_mode_noeeprom(RGBLIGHT_MODE_BREATHING + 3);
301                 break;
302             case _LOWER:
303                 rgblight_sethsv_noeeprom_green();
304                 rgblight_mode_noeeprom(RGBLIGHT_MODE_BREATHING + 3);
305                 break;
306             case _ADJUST:
307                 rgblight_sethsv_noeeprom_red();
308                 rgblight_mode_noeeprom(RGBLIGHT_MODE_KNIGHT + 2);
309                 break;
310             default: //  for any other layers, or the default layer
311                 switch (biton32(default_layer_state)) {
312                     case _COLEMAK:
313                         rgblight_sethsv_noeeprom_magenta(); break;
314                     case _DVORAK:
315                         rgblight_sethsv_noeeprom_springgreen(); break;
316                     case _WORKMAN:
317                         rgblight_sethsv_noeeprom_goldenrod(); break;
318                     case _NORMAN:
319                         rgblight_sethsv_noeeprom_coral(); break;
320                     case _MALTRON:
321                         rgblight_sethsv_noeeprom_yellow(); break;
322                     case _EUCALYN:
323                         rgblight_sethsv_noeeprom_pink(); break;
324                     case _CARPLAX:
325                         rgblight_sethsv_noeeprom_blue(); break;
326                     default:
327                         rgblight_sethsv_noeeprom_cyan(); break;
328                 }
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
330                 break;
331         }
332     }
333 #endif // RGBLIGHT_ENABLE
334
335     return state;
336 }