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