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