aboutsummaryrefslogtreecommitdiff
path: root/users/drashna/rgb_stuff.c
diff options
context:
space:
mode:
Diffstat (limited to 'users/drashna/rgb_stuff.c')
-rw-r--r--users/drashna/rgb_stuff.c135
1 files changed, 20 insertions, 115 deletions
diff --git a/users/drashna/rgb_stuff.c b/users/drashna/rgb_stuff.c
index 8a0866539..15108bde0 100644
--- a/users/drashna/rgb_stuff.c
+++ b/users/drashna/rgb_stuff.c
@@ -23,106 +23,19 @@
23bool has_initialized; 23bool has_initialized;
24 24
25void rgblight_sethsv_default_helper(uint8_t index) { rgblight_sethsv_at(rgblight_get_hue(), rgblight_get_sat(), rgblight_get_val(), index); } 25void rgblight_sethsv_default_helper(uint8_t index) { rgblight_sethsv_at(rgblight_get_hue(), rgblight_get_sat(), rgblight_get_val(), index); }
26 26void rgblight_set_hsv_and_mode(uint8_t hue, uint8_t sat, uint8_t val, uint8_t mode) {
27# ifdef RGBLIGHT_TWINKLE 27 rgblight_sethsv_noeeprom(hue, sat, val);
28static rgblight_fadeout lights[RGBLED_NUM]; 28 // wait_us(175); // Add a slight delay between color and mode to ensure it's processed correctly
29 29 rgblight_mode_noeeprom(mode);
30/* Handler for fading/twinkling effect */
31void scan_rgblight_fadeout(void) { // Don't effing change this function .... rgblight_sethsv is supppppper intensive
32 bool litup = false;
33
34 for (uint8_t light_index = 0; light_index < RGBLED_NUM; ++light_index) {
35 if (lights[light_index].enabled && sync_timer_elapsed(lights[light_index].timer) > 10) {
36 rgblight_fadeout *light = &lights[light_index];
37 litup = true;
38
39 if (light->life) {
40 light->life -= 1;
41 if (get_highest_layer(layer_state) == 0) {
42 sethsv(light->hue + rand() % 0xF, 255, light->life, (LED_TYPE *)&led[light_index]);
43 }
44 light->timer = sync_timer_read();
45 } else {
46 if (light->enabled && get_highest_layer(layer_state) == 0) {
47 rgblight_sethsv_default_helper(light_index);
48 }
49 litup = light->enabled = false;
50 }
51 }
52 }
53 if (litup && get_highest_layer(layer_state) == 0) {
54 rgblight_set();
55 }
56}
57
58/* Triggers a LED to fade/twinkle.
59 * This function handles the selection of the LED and prepres for it to be used.
60 */
61void start_rgb_light(void) {
62 uint8_t indices[RGBLED_NUM];
63 uint8_t indices_count = 0;
64 uint8_t min_life = 0xFF;
65 uint8_t min_life_index = -1;
66
67 for (uint8_t index = 0; index < RGBLED_NUM; ++index) {
68 if (lights[index].enabled) {
69 if (min_life_index == -1 || lights[index].life < min_life) {
70 min_life = lights[index].life;
71 min_life_index = index;
72 }
73 continue;
74 }
75
76 indices[indices_count] = index;
77 ++indices_count;
78 }
79
80 uint8_t light_index;
81 if (!indices_count) {
82 light_index = min_life_index;
83 } else {
84 light_index = indices[rand() % indices_count];
85 }
86
87 rgblight_fadeout *light = &lights[light_index];
88 light->enabled = true;
89 light->timer = sync_timer_read();
90 light->life = 0xC0 + rand() % 0x40;
91
92 light->hue = rgblight_get_hue() + (rand() % 0xB4) - 0x54;
93
94 rgblight_sethsv_at(light->hue, 255, light->life, light_index);
95} 30}
96# endif
97
98bool process_record_user_rgb_light(uint16_t keycode, keyrecord_t *record) {
99 uint16_t temp_keycode = keycode;
100 // Filter out the actual keycode from MT and LT keys.
101 if ((keycode >= QK_MOD_TAP && keycode <= QK_MOD_TAP_MAX) || (keycode >= QK_LAYER_TAP && keycode <= QK_LAYER_TAP_MAX)) {
102 temp_keycode &= 0xFF;
103 }
104 31
105 switch (temp_keycode) { 32bool process_record_user_rgb_light(uint16_t keycode, keyrecord_t *record) { return true; }
106# ifdef RGBLIGHT_TWINKLE
107 case KC_A ... KC_SLASH:
108 case KC_F1 ... KC_F12:
109 case KC_INSERT ... KC_UP:
110 case KC_KP_SLASH ... KC_KP_DOT:
111 case KC_F13 ... KC_F24:
112 case KC_AUDIO_MUTE ... KC_MEDIA_REWIND:
113 if (record->event.pressed) {
114 start_rgb_light();
115 }
116 break;
117# endif // RGBLIGHT_TWINKLE
118 }
119 return true;
120}
121 33
122# if defined(RGBLIGHT_STARTUP_ANIMATION) 34# if defined(RGBLIGHT_STARTUP_ANIMATION)
123static bool is_enabled; 35static bool is_enabled;
124static bool is_rgblight_startup; 36static bool is_rgblight_startup;
125static uint8_t old_hue; 37static HSV old_hsv;
38static uint8_t old_mode;
126static uint16_t rgblight_startup_loop_timer; 39static uint16_t rgblight_startup_loop_timer;
127# endif 40# endif
128 41
@@ -130,37 +43,35 @@ void keyboard_post_init_rgb_light(void) {
130# if defined(RGBLIGHT_STARTUP_ANIMATION) 43# if defined(RGBLIGHT_STARTUP_ANIMATION)
131 is_enabled = rgblight_is_enabled(); 44 is_enabled = rgblight_is_enabled();
132 if (userspace_config.rgb_layer_change) { 45 if (userspace_config.rgb_layer_change) {
133 rgblight_enable_noeeprom();
134 }
135 if (rgblight_is_enabled()) {
136 layer_state_set_rgb_light(layer_state); 46 layer_state_set_rgb_light(layer_state);
137 old_hue = rgblight_get_hue();
138 rgblight_mode_noeeprom(RGBLIGHT_MODE_STATIC_LIGHT);
139 is_rgblight_startup = true;
140 } 47 }
48 old_hsv = rgblight_get_hsv();
49 old_mode = rgblight_get_mode();
50 rgblight_mode_noeeprom(RGBLIGHT_MODE_STATIC_LIGHT);
51 is_rgblight_startup = true;
141# endif 52# endif
142 layer_state_set_rgb_light(layer_state); 53 if (userspace_config.rgb_layer_change) {
54 layer_state_set_rgb_light(layer_state);
55 }
143} 56}
144 57
145void matrix_scan_rgb_light(void) { 58void matrix_scan_rgb_light(void) {
146# ifdef RGBLIGHT_TWINKLE
147 scan_rgblight_fadeout();
148# endif // RGBLIGHT_ENABLE
149
150# if defined(RGBLIGHT_STARTUP_ANIMATION) 59# if defined(RGBLIGHT_STARTUP_ANIMATION)
151 if (is_rgblight_startup && is_keyboard_master()) { 60 if (is_rgblight_startup && is_keyboard_master()) {
152 if (sync_timer_elapsed(rgblight_startup_loop_timer) > 10) { 61 if (sync_timer_elapsed(rgblight_startup_loop_timer) > 10) {
153 static uint8_t counter; 62 static uint8_t counter;
154 counter++; 63 counter++;
155 rgblight_sethsv_noeeprom((counter + old_hue) % 255, 255, 255); 64 rgblight_sethsv_noeeprom((counter + old_hsv.h) % 255, 255, 255);
156 rgblight_startup_loop_timer = sync_timer_read(); 65 rgblight_startup_loop_timer = sync_timer_read();
157 if (counter == 255) { 66 if (counter == 255) {
158 is_rgblight_startup = false; 67 is_rgblight_startup = false;
159 if (!is_enabled) {
160 rgblight_disable_noeeprom();
161 }
162 if (userspace_config.rgb_layer_change) { 68 if (userspace_config.rgb_layer_change) {
163 layer_state_set_rgb_light(layer_state); 69 layer_state_set_rgb_light(layer_state);
70 } else {
71 rgblight_set_hsv_and_mode(old_hsv.h, old_hsv.s, old_hsv.v, old_mode);
72 }
73 if (!is_enabled) {
74 rgblight_disable_noeeprom();
164 } 75 }
165 } 76 }
166 } 77 }
@@ -168,12 +79,6 @@ void matrix_scan_rgb_light(void) {
168# endif 79# endif
169} 80}
170 81
171void rgblight_set_hsv_and_mode(uint8_t hue, uint8_t sat, uint8_t val, uint8_t mode) {
172 rgblight_sethsv_noeeprom(hue, sat, val);
173 // wait_us(175); // Add a slight delay between color and mode to ensure it's processed correctly
174 rgblight_mode_noeeprom(mode);
175}
176
177layer_state_t layer_state_set_rgb_light(layer_state_t state) { 82layer_state_t layer_state_set_rgb_light(layer_state_t state) {
178# ifdef RGBLIGHT_ENABLE 83# ifdef RGBLIGHT_ENABLE
179 if (userspace_config.rgb_layer_change) { 84 if (userspace_config.rgb_layer_change) {