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