]> git.donarmstrong.com Git - qmk_firmware.git/blob - users/drashna/rgb_stuff.c
Merge branch 'master' of https://github.com/qmk/qmk_firmware
[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   if ((keycode >= QK_MOD_TAP && keycode <= QK_MOD_TAP_MAX) || (keycode >= QK_LAYER_TAP && keycode <= QK_LAYER_TAP_MAX)) {
204     keycode = keycode & 0xFF;
205   }
206   switch (keycode) {
207 #ifdef RGBLIGHT_TWINKLE
208     case KC_A ... KC_SLASH:
209     case KC_F1 ... KC_F12:
210     case KC_INSERT ... KC_UP:
211     case KC_KP_SLASH ... KC_KP_DOT:
212     case KC_F13 ... KC_F24:
213     case KC_AUDIO_MUTE ... KC_MEDIA_REWIND:
214       if (record->event.pressed) { start_rgb_light(); }
215       return true; break;
216 #endif // RGBLIGHT_TWINKLE
217   case KC_RGB_T:  // This allows me to use underglow as layer indication, or as normal
218 #if defined(RGBLIGHT_ENABLE) || defined(RGB_MATRIX_ENABLE)
219     if (record->event.pressed) {
220       userspace_config.rgb_layer_change ^= 1;
221       xprintf("rgblight layer change [EEPROM]: %u\n", userspace_config.rgb_layer_change);
222       eeconfig_update_user(userspace_config.raw);
223       if (userspace_config.rgb_layer_change) {
224         layer_state_set(layer_state); // This is needed to immediately set the layer color (looks better)
225       }
226     }
227 #endif // RGBLIGHT_ENABLE
228     return false; break;
229 #ifdef RGBLIGHT_ENABLE
230   case RGB_MODE_FORWARD ... RGB_MODE_GRADIENT: // quantum_keycodes.h L400 for definitions
231     if (record->event.pressed) { //This disables layer indication, as it's assumed that if you're changing this ... you want that disabled
232       if (userspace_config.rgb_layer_change) {
233         userspace_config.rgb_layer_change = false;
234         xprintf("rgblight layer change [EEPROM]: %u\n", userspace_config.rgb_layer_change);
235         eeconfig_update_user(userspace_config.raw);
236       }
237     }
238     return true; break;
239 #endif // RGBLIGHT_ENABLE
240   }
241     return true;
242 }
243
244
245
246  void matrix_init_rgb(void) {
247
248 // #ifdef RGBLIGHT_ENABLE
249 //   if (userspace_config.rgb_layer_change) {
250 //     rgblight_enable_noeeprom();
251 //     switch (biton32(eeconfig_read_default_layer())) {
252 //       case _COLEMAK:
253 //         rgblight_sethsv_noeeprom_magenta(); break;
254 //       case _DVORAK:
255 //         rgblight_sethsv_noeeprom_springgreen(); break;
256 //       case _WORKMAN:
257 //         rgblight_sethsv_noeeprom_goldenrod(); break;
258 //       default:
259 //         rgblight_sethsv_noeeprom_cyan(); break;
260 //     }
261 //     rgblight_mode(RGBLIGHT_MODE_STATIC_LIGHT);
262 //   }
263 // #endif
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_effect_snake(RGBLIGHT_MODE_SNAKE + 2) : rgblight_effect_snake(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         default:
319           rgblight_sethsv_noeeprom_cyan(); break;
320       }
321       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
322       break;
323     }
324 //    layer_state_set_indicator(); // Runs every scan, so need to call this here .... since I can't get it working "right" anyhow
325   }
326 #endif // RGBLIGHT_ENABLE
327
328   return state;
329 }
330
331 uint32_t default_layer_state_set_rgb(uint32_t state) {
332 #ifdef RGBLIGHT_ENABLE
333   if (userspace_config.rgb_layer_change) {
334     rgblight_config_t temp_rgblight_config = rgblight_config;
335     switch (biton32(state)) {
336       case _COLEMAK:
337         temp_rgblight_config.hue = 300;
338         temp_rgblight_config.val = 255;
339         temp_rgblight_config.sat = 255;
340         temp_rgblight_config.mode = 1;
341         break;
342       case _DVORAK:
343         temp_rgblight_config.hue = 150;
344         temp_rgblight_config.val = 255;
345         temp_rgblight_config.sat = 255;
346         temp_rgblight_config.mode = 1;
347       case _WORKMAN:
348         temp_rgblight_config.hue = 43;
349         temp_rgblight_config.val = 218;
350         temp_rgblight_config.sat = 218;
351         temp_rgblight_config.mode = 1;
352       default:
353         temp_rgblight_config.hue = 180;
354         temp_rgblight_config.val = 255;
355         temp_rgblight_config.sat = 255;
356         temp_rgblight_config.mode = 1;
357     }
358     if (temp_rgblight_config.raw != eeconfig_read_rgblight()) {
359       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);
360       eeconfig_update_rgblight(temp_rgblight_config.raw);
361     }
362   }
363 #endif // RGBLIGHT_ENABLE
364   return state;
365 }