aboutsummaryrefslogtreecommitdiff
path: root/users/curry/rgb_lighting_user.c
diff options
context:
space:
mode:
authorAkaash Suresh <casa.akaash@gmail.com>2020-01-09 13:57:54 -0600
committerDrashna Jaelre <drashna@live.com>2020-01-09 11:57:54 -0800
commitcaa70df816033c30dbbbf4c5a90d803c7bb1dfde (patch)
tree4246ca4b2808cdd1b8ed681392258f195e579014 /users/curry/rgb_lighting_user.c
parent71de09d7510213d707ca1056c6e0eca840678d37 (diff)
downloadqmk_firmware-caa70df816033c30dbbbf4c5a90d803c7bb1dfde.tar.gz
qmk_firmware-caa70df816033c30dbbbf4c5a90d803c7bb1dfde.zip
[Keymap] Userspace refactor, adding leader key functionality (#7790)
* Userspace refactor * Fixed missed ifdef * tapcode16, adjust layout * glcdfont changes from #7745 * Modify Keymaps, add workman * RGB & OLED update
Diffstat (limited to 'users/curry/rgb_lighting_user.c')
-rw-r--r--users/curry/rgb_lighting_user.c325
1 files changed, 325 insertions, 0 deletions
diff --git a/users/curry/rgb_lighting_user.c b/users/curry/rgb_lighting_user.c
new file mode 100644
index 000000000..25e1ce010
--- /dev/null
+++ b/users/curry/rgb_lighting_user.c
@@ -0,0 +1,325 @@
1#include "curry.h"
2#include "rgb_lighting_user.h"
3
4extern rgblight_config_t rgblight_config;
5bool has_initialized;
6
7void rgblight_sethsv_default_helper(uint8_t index) { rgblight_sethsv_at(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, index); }
8
9/* Custom indicators for modifiers.
10 * This allows for certain lights to be lit up, based on what mods are active, giving some visual feedback.
11 * This is especially useful for One Shot Mods, since it's not always obvious if they're still lit up.
12 */
13#if defined(INDICATOR_LIGHTS)
14void set_rgb_indicators(uint8_t this_mod, uint8_t this_led, uint8_t this_osm) {
15 if (userspace_config.rgb_layer_change && get_highest_layer(layer_state) == 0) {
16 if ((this_mod | this_osm) & MOD_MASK_SHIFT || this_led & (1 << USB_LED_CAPS_LOCK)) {
17# ifdef SHFT_LED1
18 rgblight_sethsv_at(120, 255, 255, SHFT_LED1);
19# endif // SHFT_LED1
20# ifdef SHFT_LED2
21 rgblight_sethsv_at(120, 255, 255, SHFT_LED2);
22# endif // SHFT_LED2
23 } else {
24# ifdef SHFT_LED1
25 rgblight_sethsv_default_helper(SHFT_LED1);
26# endif // SHFT_LED1
27# ifdef SHFT_LED2
28 rgblight_sethsv_default_helper(SHFT_LED2);
29# endif // SHFT_LED2
30 }
31 if ((this_mod | this_osm) & MOD_MASK_CTRL) {
32# ifdef CTRL_LED1
33 rgblight_sethsv_at(0, 255, 255, CTRL_LED1);
34# endif // CTRL_LED1
35# ifdef CTRL_LED2
36 rgblight_sethsv_at(0, 255, 255, CTRL_LED2);
37# endif // CTRL_LED2
38 } else {
39# ifdef CTRL_LED1
40 rgblight_sethsv_default_helper(CTRL_LED1);
41# endif // CTRL_LED1
42# ifdef CTRL_LED2
43 rgblight_sethsv_default_helper(CTRL_LED2);
44# endif // CTRL_LED2
45 }
46 if ((this_mod | this_osm) & MOD_MASK_GUI) {
47# ifdef GUI_LED1
48 rgblight_sethsv_at(51, 255, 255, GUI_LED1);
49# endif // GUI_LED1
50# ifdef GUI_LED2
51 rgblight_sethsv_at(51, 255, 255, GUI_LED2);
52# endif // GUI_LED2
53 } else {
54# ifdef GUI_LED1
55 rgblight_sethsv_default_helper(GUI_LED1);
56# endif // GUI_LED1
57# ifdef GUI_LED2
58 rgblight_sethsv_default_helper(GUI_LED2);
59# endif // GUI_LED2
60 }
61 if ((this_mod | this_osm) & MOD_MASK_ALT) {
62# ifdef ALT_LED1
63 rgblight_sethsv_at(240, 255, 255, ALT_LED1);
64# endif // ALT_LED1
65# ifdef GUI_LED2
66 rgblight_sethsv_at(240, 255, 255, ALT_LED2);
67# endif // GUI_LED2
68 } else {
69# ifdef GUI_LED1
70 rgblight_sethsv_default_helper(ALT_LED1);
71# endif // GUI_LED1
72# ifdef GUI_LED2
73 rgblight_sethsv_default_helper(ALT_LED2);
74# endif // GUI_LED2
75 }
76 }
77}
78
79/* Function for the indicators */
80void matrix_scan_indicator(void) {
81 if (has_initialized) {
82 set_rgb_indicators(get_mods(), host_keyboard_leds(), get_oneshot_mods());
83 }
84}
85#endif // INDICATOR_LIGHTS
86
87#if defined(RGBLIGHT_TWINKLE)
88static rgblight_fadeout lights[RGBLED_NUM];
89
90__attribute__((weak)) bool rgblight_twinkle_is_led_used_keymap(uint8_t index) { return false; }
91
92/* This function checks for used LEDs. This way, collisions don't occur and cause weird rendering */
93bool rgblight_twinkle_is_led_used(uint8_t index) {
94 switch (index) {
95# ifdef INDICATOR_LIGHTS
96# ifdef SHFT_LED1
97 case SHFT_LED1:
98 return true;
99# endif // SHFT_LED1
100# ifdef SHFT_LED2
101 case SHFT_LED2:
102 return true;
103# endif // SHFT_LED2
104# ifdef CTRL_LED1
105 case CTRL_LED1:
106 return true;
107# endif // CTRL_LED1
108# ifdef CTRL_LED2
109 case CTRL_LED2:
110 return true;
111# endif // CTRL_LED2
112# ifdef GUI_LED1
113 case GUI_LED1:
114 return true;
115# endif // GUI_LED1
116# ifdef GUI_LED2
117 case GUI_LED2:
118 return true;
119# endif // GUI_LED2
120# ifdef ALT_LED1
121 case ALT_LED1:
122 return true;
123# endif // ALT_LED1
124# ifdef ALT_LED2
125 case ALT_LED2:
126 return true;
127# endif // ALT_LED2
128# endif // INDICATOR_LIGHTS
129 default:
130 return rgblight_twinkle_is_led_used_keymap(index);
131 }
132}
133
134/* Handler for fading/twinkling effect */
135void 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 (get_highest_layer(layer_state) == 0) {
145 sethsv(light->hue + rand() % 0xF, 255, light->life, (LED_TYPE *)&led[light_index]);
146 }
147 light->timer = timer_read();
148 } else {
149 if (light->enabled && get_highest_layer(layer_state) == 0) {
150 rgblight_sethsv_default_helper(light_index);
151 }
152 litup = light->enabled = false;
153 }
154 }
155 }
156 if (litup && get_highest_layer(layer_state) == 0) {
157 rgblight_set();
158 }
159}
160
161/* Triggers a LED to fade/twinkle.
162 * This function handles the selection of the LED and prepres for it to be used.
163 */
164void start_rgb_light(void) {
165 uint8_t indices[RGBLED_NUM];
166 uint8_t indices_count = 0;
167 uint8_t min_life = 0xFF;
168 uint8_t min_life_index = -1;
169 for (uint8_t index = 0; index < RGBLED_NUM; ++index) {
170 if (rgblight_twinkle_is_led_used(index)) {
171 continue;
172 }
173 if (lights[index].enabled) {
174 if (min_life_index == -1 || lights[index].life < min_life) {
175 min_life = lights[index].life;
176 min_life_index = index;
177 }
178 continue;
179 }
180
181 indices[indices_count] = index;
182 ++indices_count;
183 }
184
185 uint8_t light_index;
186 if (!indices_count) {
187 light_index = min_life_index;
188 } else {
189 light_index = indices[rand() % indices_count];
190 }
191
192 rgblight_fadeout *light = &lights[light_index];
193 light->enabled = true;
194 light->timer = timer_read();
195 light->life = 0xC0 + rand() % 0x40;
196
197 light->hue = rgblight_config.hue + (rand() % 0xB4) - 0x54;
198
199 rgblight_sethsv_at(light->hue, 255, light->life, light_index);
200}
201#endif
202
203bool process_record_user_rgb(uint16_t keycode, keyrecord_t *record) {
204 uint16_t temp_keycode = keycode;
205 // Filter out the actual keycode from MT and LT keys.
206 if ((keycode >= QK_MOD_TAP && keycode <= QK_MOD_TAP_MAX) || (keycode >= QK_LAYER_TAP && keycode <= QK_LAYER_TAP_MAX)) {
207 temp_keycode &= 0xFF;
208 }
209
210 switch (temp_keycode) {
211#if defined(RGBLIGHT_TWINKLE)
212 case KC_A ... KC_SLASH:
213 case KC_F1 ... KC_F12:
214 case KC_INSERT ... KC_UP:
215 case KC_KP_SLASH ... KC_KP_DOT:
216 case KC_F13 ... KC_F24:
217 case KC_AUDIO_MUTE ... KC_MEDIA_REWIND:
218 if (record->event.pressed) {
219 start_rgb_light();
220 }
221 break;
222#endif // RGBLIGHT_TWINKLE
223 case KC_RGB_T: // This allows me to use underglow as layer indication, or as normal
224 if (record->event.pressed) {
225 userspace_config.rgb_layer_change ^= 1;
226 dprintf("rgblight layer change [EEPROM]: %u\n", userspace_config.rgb_layer_change);
227 eeconfig_update_user(userspace_config.raw);
228 if (userspace_config.rgb_layer_change) {
229 layer_state_set(layer_state); // This is needed to immediately set the layer color (looks better)
230 }
231 }
232 break;
233 case RGB_MODE_FORWARD ... RGB_MODE_GRADIENT: // quantum_keycodes.h L400 for definitions
234 if (record->event.pressed) {
235 bool is_eeprom_updated = false;
236 // This disables layer indication, as it's assumed that if you're changing this ... you want that disabled
237 if (userspace_config.rgb_layer_change) {
238 userspace_config.rgb_layer_change = false;
239 dprintf("rgblight layer change [EEPROM]: %u\n", userspace_config.rgb_layer_change);
240 is_eeprom_updated = true;
241 }
242 if (is_eeprom_updated) {
243 eeconfig_update_user(userspace_config.raw);
244 }
245 }
246 break;
247 }
248 return true;
249}
250
251void keyboard_post_init_rgb(void) {
252#if defined(RGBLIGHT_STARTUP_ANIMATION)
253 bool is_enabled = rgblight_config.enable;
254 if (userspace_config.rgb_layer_change) {
255 rgblight_enable_noeeprom();
256 }
257 if (rgblight_config.enable) {
258 layer_state_set_user(layer_state);
259 uint16_t old_hue = rgblight_config.hue;
260 rgblight_mode_noeeprom(RGBLIGHT_MODE_STATIC_LIGHT);
261 for (uint16_t i = 255; i > 0; i--) {
262 rgblight_sethsv_noeeprom((i + old_hue) % 255, 255, 255);
263 matrix_scan();
264 wait_ms(10);
265 }
266 }
267 if (!is_enabled) {
268 rgblight_disable_noeeprom();
269 }
270
271#endif
272 layer_state_set_user(layer_state);
273}
274
275void matrix_scan_rgb(void) {
276#if defined(RGBLIGHT_TWINKLE)
277 scan_rgblight_fadeout();
278#endif // RGBLIGHT_ENABLE
279
280#if defined(INDICATOR_LIGHTS)
281 matrix_scan_indicator();
282#endif
283}
284
285void rgblight_set_hsv_and_mode(uint8_t hue, uint8_t sat, uint8_t val, uint8_t mode) {
286 rgblight_sethsv_noeeprom(hue, sat, val);
287 wait_us(175); // Add a slight delay between color and mode to ensure it's processed correctly
288 rgblight_mode_noeeprom(mode);
289}
290
291layer_state_t layer_state_set_rgb(layer_state_t state) {
292 if (userspace_config.rgb_layer_change) {
293 switch (get_highest_layer(state)) {
294 case _RAISE:
295 rgblight_set_hsv_and_mode(HSV_YELLOW, RGBLIGHT_MODE_BREATHING + 3);
296 break;
297 case _LOWER:
298 rgblight_set_hsv_and_mode(HSV_GREEN, RGBLIGHT_MODE_BREATHING + 3);
299 break;
300 case _ADJUST:
301 rgblight_set_hsv_and_mode(HSV_RED, RGBLIGHT_MODE_KNIGHT + 2);
302 break;
303 default: // for any other layers, or the default layer
304 {
305 uint8_t mode = get_highest_layer(state) == _MODS ? RGBLIGHT_MODE_BREATHING : RGBLIGHT_MODE_STATIC_LIGHT;
306 switch (get_highest_layer(default_layer_state)) {
307 case _COLEMAK:
308 rgblight_set_hsv_and_mode(HSV_MAGENTA, mode);
309 break;
310 case _DVORAK:
311 rgblight_set_hsv_and_mode(HSV_SPRINGGREEN, mode);
312 break;
313 case _WORKMAN:
314 rgblight_set_hsv_and_mode(HSV_GOLDENROD, mode);
315 break;
316 default:
317 rgblight_set_hsv_and_mode(HSV_CYAN, mode);
318 break;
319 }
320 break;
321 }
322 }
323 }
324 return state;
325}