]> git.donarmstrong.com Git - qmk_firmware.git/blob - users/drashna/rgb_stuff.c
Update to drashna keymaps (#4365)
[qmk_firmware.git] / users / drashna / rgb_stuff.c
1 #include "drashna.h"
2 #include "rgb_stuff.h"
3 #include "eeprom.h"
4
5 extern rgblight_config_t rgblight_config;
6 extern userspace_config_t userspace_config;
7
8 #ifdef RGBLIGHT_ENABLE
9 void rgblight_sethsv_default_helper(uint8_t index) {
10   rgblight_sethsv_at(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, index);
11 }
12 #endif // RGBLIGHT_ENABLE
13
14 #ifdef INDICATOR_LIGHTS
15 void set_rgb_indicators(uint8_t this_mod, uint8_t this_led, uint8_t this_osm) {
16   if (userspace_config.rgb_layer_change && biton32(layer_state) == 0) {
17     if (this_mod & MODS_SHIFT_MASK || this_led & (1<<USB_LED_CAPS_LOCK) || this_osm & MODS_SHIFT_MASK) {
18       #ifdef SHFT_LED1
19         rgblight_sethsv_at(120, 255, 255, SHFT_LED1);
20       #endif // SHFT_LED1
21       #ifdef SHFT_LED2
22         rgblight_sethsv_at(120, 255, 255, SHFT_LED2);
23       #endif // SHFT_LED2
24     } else {
25       #ifdef SHFT_LED1
26         rgblight_sethsv_default_helper(SHFT_LED1);
27       #endif // SHFT_LED1
28       #ifdef SHFT_LED2
29         rgblight_sethsv_default_helper(SHFT_LED2);
30       #endif // SHFT_LED2
31     }
32     if (this_mod & MODS_CTRL_MASK || this_osm & MODS_CTRL_MASK) {
33       #ifdef CTRL_LED1
34         rgblight_sethsv_at(0, 255, 255, CTRL_LED1);
35       #endif // CTRL_LED1
36       #ifdef CTRL_LED2
37         rgblight_sethsv_at(0, 255, 255, CTRL_LED2);
38       #endif // CTRL_LED2
39     } else {
40       #ifdef CTRL_LED1
41         rgblight_sethsv_default_helper(CTRL_LED1);
42       #endif // CTRL_LED1
43       #ifdef CTRL_LED2
44         rgblight_sethsv_default_helper(CTRL_LED2);
45       #endif // CTRL_LED2
46     }
47     if (this_mod & MODS_GUI_MASK || this_osm & MODS_GUI_MASK) {
48       #ifdef GUI_LED1
49         rgblight_sethsv_at(51, 255, 255, GUI_LED1);
50       #endif // GUI_LED1
51       #ifdef GUI_LED2
52         rgblight_sethsv_at(51, 255, 255, GUI_LED2);
53       #endif // GUI_LED2
54     } else {
55       #ifdef GUI_LED1
56         rgblight_sethsv_default_helper(GUI_LED1);
57       #endif // GUI_LED1
58       #ifdef GUI_LED2
59         rgblight_sethsv_default_helper(GUI_LED2);
60       #endif // GUI_LED2
61     }
62     if (this_mod & MODS_ALT_MASK || this_osm & MODS_ALT_MASK) {
63       #ifdef ALT_LED1
64         rgblight_sethsv_at(240, 255, 255, ALT_LED1);
65       #endif // ALT_LED1
66       #ifdef GUI_LED2
67         rgblight_sethsv_at(240, 255, 255, ALT_LED2);
68       #endif // GUI_LED2
69     } else {
70       #ifdef GUI_LED1
71         rgblight_sethsv_default_helper(ALT_LED1);
72       #endif // GUI_LED1
73       #ifdef GUI_LED2
74         rgblight_sethsv_default_helper(ALT_LED2);
75       #endif // GUI_LED2
76     }
77   }
78 }
79
80 void matrix_scan_indicator(void) {
81   set_rgb_indicators(get_mods(), host_keyboard_leds(), get_oneshot_mods());
82 }
83 #endif //INDICATOR_LIGHTS
84
85 #ifdef RGBLIGHT_TWINKLE
86 static rgblight_fadeout lights[RGBLED_NUM];
87
88 __attribute__ ((weak))
89 bool rgblight_twinkle_is_led_used_keymap(uint8_t index) { return false; }
90
91 bool rgblight_twinkle_is_led_used(uint8_t index) {
92   switch (index) {
93 #ifdef INDICATOR_LIGHTS
94 #ifdef SHFT_LED1
95     case SHFT_LED1:
96       return true;
97 #endif //SHFT_LED1
98 #ifdef SHFT_LED2
99     case SHFT_LED2:
100       return true;
101 #endif //SHFT_LED2
102 #ifdef CTRL_LED1
103     case CTRL_LED1:
104       return true;
105 #endif //CTRL_LED1
106 #ifdef CTRL_LED2
107     case CTRL_LED2:
108       return true;
109 #endif //CTRL_LED2
110 #ifdef GUI_LED1
111     case GUI_LED1:
112       return true;
113 #endif //GUI_LED1
114 #ifdef GUI_LED2
115     case GUI_LED2:
116       return true;
117 #endif //GUI_LED2
118 #ifdef ALT_LED1
119     case ALT_LED1:
120       return true;
121 #endif //ALT_LED1
122 #ifdef ALT_LED2
123     case ALT_LED2:
124       return true;
125 #endif //ALT_LED2
126 #endif //INDICATOR_LIGHTS
127     default:
128     return rgblight_twinkle_is_led_used_keymap(index);
129   }
130 }
131
132 void scan_rgblight_fadeout(void) { // Don't effing change this function .... rgblight_sethsv is supppppper intensive
133   bool litup = false;
134   for (uint8_t light_index = 0 ; light_index < RGBLED_NUM ; ++light_index ) {
135     if (lights[light_index].enabled && timer_elapsed(lights[light_index].timer) > 10) {
136       rgblight_fadeout *light = &lights[light_index];
137       litup = true;
138
139       if (light->life) {
140         light->life -= 1;
141         if (biton32(layer_state) == 0) {
142           sethsv(light->hue + rand() % 0xF, 255, light->life, (LED_TYPE *)&led[light_index]);
143         }
144         light->timer = timer_read();
145       }
146       else {
147         if (light->enabled && biton32(layer_state) == 0) { rgblight_sethsv_default_helper(light_index); }
148         litup = light->enabled = false;
149       }
150     }
151   }
152   if (litup && biton32(layer_state) == 0) {
153     rgblight_set();
154   }
155 }
156
157 void start_rgb_light(void) {
158
159     uint8_t indices[RGBLED_NUM];
160     uint8_t indices_count = 0;
161     uint8_t min_life = 0xFF;
162     uint8_t min_life_index = -1;
163     for (uint8_t index = 0 ; index < RGBLED_NUM ; ++index ) {
164       if (rgblight_twinkle_is_led_used(index)) { continue; }
165       if (lights[index].enabled) {
166         if (min_life_index == -1 ||
167           lights[index].life < min_life)
168         {
169           min_life = lights[index].life;
170           min_life_index = index;
171         }
172         continue;
173       }
174
175       indices[indices_count] = index;
176       ++indices_count;
177     }
178
179     uint8_t light_index;
180     if (!indices_count) {
181         light_index = min_life_index;
182     }
183     else {
184       light_index = indices[rand() % indices_count];
185     }
186
187     rgblight_fadeout *light = &lights[light_index];
188     light->enabled = true;
189     light->timer = timer_read();
190     light->life = 0xC0 + rand() % 0x40;
191
192     light->hue = rgblight_config.hue + (rand() % 0xB4) - 0x54;
193
194     rgblight_sethsv_at(light->hue, 255, light->life, light_index);
195 }
196 #endif
197
198
199 bool process_record_user_rgb(uint16_t keycode, keyrecord_t *record) {
200     switch (keycode) {
201 #ifdef RGBLIGHT_TWINKLE
202     case KC_A ... KC_SLASH:
203     case KC_F1 ... KC_F12:
204     case KC_INSERT ... KC_UP:
205     case KC_KP_SLASH ... KC_KP_DOT:
206     case KC_F13 ... KC_F24:
207     case KC_AUDIO_MUTE ... KC_MEDIA_REWIND:
208       if (record->event.pressed) { start_rgb_light(); }
209       return true; break;
210 #endif // RGBLIGHT_TWINKLE
211   case KC_RGB_T:  // This allows me to use underglow as layer indication, or as normal
212 #ifdef RGBLIGHT_ENABLE
213     if (record->event.pressed) {
214       userspace_config.rgb_layer_change ^= 1;
215       xprintf("rgblight layer change [EEPROM]: %u\n", userspace_config.rgb_layer_change);
216       eeconfig_update_user(userspace_config.raw);
217       if (userspace_config.rgb_layer_change) {
218         layer_state_set(layer_state); // This is needed to immediately set the layer color (looks better)
219       }
220     }
221 #endif // RGBLIGHT_ENABLE
222     return false; break;
223 #ifdef RGBLIGHT_ENABLE
224   case RGB_MODE_FORWARD ... RGB_MODE_GRADIENT: // quantum_keycodes.h L400 for definitions
225     if (record->event.pressed) { //This disables layer indication, as it's assumed that if you're changing this ... you want that disabled
226       if (userspace_config.rgb_layer_change) {
227         userspace_config.rgb_layer_change = false;
228         xprintf("rgblight layer change [EEPROM]: %u\n", userspace_config.rgb_layer_change);
229         eeconfig_update_user(userspace_config.raw);
230       }
231     }
232     return true; break;
233 #endif // RGBLIGHT_ENABLE
234   }
235     return true;
236 }
237
238
239
240 void matrix_init_rgb(void) {
241
242   if (userspace_config.rgb_layer_change) {
243     rgblight_init();
244     rgblight_enable_noeeprom();
245     switch (biton32(eeconfig_read_default_layer())) {
246       case _COLEMAK:
247         rgblight_sethsv_noeeprom_magenta(); break;
248       case _DVORAK:
249         rgblight_sethsv_noeeprom_springgreen(); break;
250       case _WORKMAN:
251         rgblight_sethsv_noeeprom_goldenrod(); break;
252       default:
253         rgblight_sethsv_noeeprom_cyan(); break;
254     }
255     rgblight_mode(RGBLIGHT_MODE_STATIC_LIGHT);
256   }
257 }
258
259 void matrix_scan_rgb(void) {
260 #ifdef RGBLIGHT_TWINKLE
261   scan_rgblight_fadeout();
262 #endif // RGBLIGHT_ENABLE
263
264 #ifdef INDICATOR_LIGHTS
265   matrix_scan_indicator();
266 #endif
267
268 }
269
270
271 uint32_t layer_state_set_rgb(uint32_t state) {
272 #ifdef RGBLIGHT_ENABLE
273   static bool has_ran;
274   if (userspace_config.rgb_layer_change) {
275     switch (biton32(state)) {
276     case _MACROS:
277       rgblight_sethsv_noeeprom_orange();
278       userspace_config.is_overwatch ? rgblight_effect_snake(RGBLIGHT_MODE_SNAKE + 2) : rgblight_effect_snake(RGBLIGHT_MODE_SNAKE + 3);
279       break;
280     case _MEDIA:
281       rgblight_sethsv_noeeprom_chartreuse();
282       rgblight_mode_noeeprom(RGBLIGHT_MODE_KNIGHT + 1);
283       break;
284     case _GAMEPAD:
285       rgblight_sethsv_noeeprom_orange();
286       rgblight_mode_noeeprom(RGBLIGHT_MODE_SNAKE + 2);
287       break;
288     case _DIABLO:
289       rgblight_sethsv_noeeprom_red();
290       rgblight_mode_noeeprom(RGBLIGHT_MODE_BREATHING + 3);
291       break;
292     case _RAISE:
293       rgblight_sethsv_noeeprom_yellow();
294       rgblight_mode_noeeprom(RGBLIGHT_MODE_BREATHING + 3);
295       break;
296     case _LOWER:
297       rgblight_sethsv_noeeprom_green();
298       rgblight_mode_noeeprom(RGBLIGHT_MODE_BREATHING + 3);
299       break;
300     case _ADJUST:
301       rgblight_sethsv_noeeprom_red();
302       rgblight_mode_noeeprom(RGBLIGHT_MODE_KNIGHT + 2);
303       break;
304     default: //  for any other layers, or the default layer
305       switch (biton32(default_layer_state)) {
306         case _COLEMAK:
307           rgblight_sethsv_noeeprom_magenta(); break;
308         case _DVORAK:
309           rgblight_sethsv_noeeprom_springgreen(); break;
310         case _WORKMAN:
311           rgblight_sethsv_noeeprom_goldenrod(); break;
312         default:
313           rgblight_sethsv_noeeprom_cyan(); break;
314       }
315       if (has_ran) {
316         biton32(state) == _MODS ? rgblight_mode(RGBLIGHT_MODE_BREATHING) : rgblight_mode(RGBLIGHT_MODE_STATIC_LIGHT); // if _MODS layer is on, then breath to denote it
317       } else {
318         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
319         has_ran = true;
320       }
321       break;
322     }
323 //    layer_state_set_indicator(); // Runs every scan, so need to call this here .... since I can't get it working "right" anyhow
324   }
325 #endif // RGBLIGHT_ENABLE
326
327   return state;
328 }