aboutsummaryrefslogtreecommitdiff
path: root/users/xtonhasvim/fancylighting.c
diff options
context:
space:
mode:
Diffstat (limited to 'users/xtonhasvim/fancylighting.c')
-rw-r--r--users/xtonhasvim/fancylighting.c167
1 files changed, 167 insertions, 0 deletions
diff --git a/users/xtonhasvim/fancylighting.c b/users/xtonhasvim/fancylighting.c
new file mode 100644
index 000000000..f4af5ec55
--- /dev/null
+++ b/users/xtonhasvim/fancylighting.c
@@ -0,0 +1,167 @@
1 /* Copyright 2015-2017 Christon DeWan
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17#ifdef RGBLIGHT_ENABLE
18
19#include <math.h>
20
21#include "rgblight.h"
22#include "color.h"
23#include "fancylighting.h"
24
25
26__attribute__ ((weak))
27void matrix_scan_keymap(void) {
28 // override me, if you want.
29 return;
30}
31
32#define ABSDIFF(a,b) ((a)>(b)?(a)-(b):(b)-(a))
33
34#define FADE_BACK_TIME 500
35#define BREATH_FIRE_TIME 1000
36#define ANIMATION_STEP_INTERVAL 20
37#define POWER_KEY_OFFSET (RGBLED_NUM / 2)
38#define SPACE_OFFSET_MAX (RGBLED_NUM / 2)
39
40
41uint16_t effect_start_timer = 0;
42uint8_t user_rgb_mode = 0;
43LED_TYPE shadowed_led[RGBLED_NUM] = {0};
44
45void start_firey_return(void) {
46 user_rgb_mode = BREATH_FIRE;
47 effect_start_timer = timer_read();
48 for(uint8_t i = 0; i < RGBLED_NUM; i++) {
49 shadowed_led[i] = led[i];
50 }
51}
52
53/** 0---max
54 * [___]
55 * [__/]
56 * [_/\]
57 * [/\_]
58 * [\__]
59 * [___]
60 **/
61
62void set_color_for_offsets(uint16_t time_offset, uint16_t space_offset, uint8_t idx) {
63 float time_progress = (float)time_offset / BREATH_FIRE_TIME;
64 float space_progress = (float)space_offset / SPACE_OFFSET_MAX;
65 float progress = time_progress * 5.0 - space_progress;
66 if(progress > 1.0) {
67 progress -= 1.0;
68 progress /= 4.0;
69 progress = 1.0 - progress;
70 }
71 progress = fmax(0.0,progress);
72 progress *= progress; // squared!
73
74 float alpha = (time_progress + 0.1) * 7.0 - space_progress;
75 alpha = fmin(1.0, alpha*alpha);
76
77 LED_TYPE px[1] = {0};
78 sethsv((uint16_t)(fmod(time_progress * 1.5 + space_progress,1.0)*360), 255, (uint8_t)(progress*255),&px[0]);
79 led[idx].r = alpha * px[0].r + ( 1.0 - alpha) * shadowed_led[idx].r;
80 led[idx].g = alpha * px[0].g + ( 1.0 - alpha) * shadowed_led[idx].g;
81 led[idx].b = alpha * px[0].b + ( 1.0 - alpha) * shadowed_led[idx].b;
82}
83
84/**
85 * It's actually a rainbow: a fire curve didn't really look right.
86 * it's still cool, though!
87 */
88void rgb_mode_breath_fire(void) {
89 static uint16_t last_timer = 0;
90 if(!last_timer) last_timer = timer_read();
91 uint16_t this_timer = timer_read();
92
93 // too soon. don't spam updates
94 if(this_timer - last_timer < ANIMATION_STEP_INTERVAL) return;
95
96 uint16_t elapsed = this_timer - effect_start_timer;
97
98 last_timer = this_timer;
99 if(elapsed >= BREATH_FIRE_TIME) {
100 // complete
101 user_rgb_mode = FADE_BACK;
102 effect_start_timer = this_timer;
103 } else {
104 // linear fade
105 for(uint16_t i = 0; i < RGBLED_NUM; i++) {
106 uint16_t space_offset = ABSDIFF(i,POWER_KEY_OFFSET);
107 if(space_offset > SPACE_OFFSET_MAX) space_offset = RGBLED_NUM - space_offset;
108
109 set_color_for_offsets(elapsed, space_offset, i);
110 }
111 rgblight_set();
112 }
113}
114
115void rgb_mode_fade_back(void) {
116 static uint16_t last_timer = 0;
117 if(!last_timer) last_timer = timer_read();
118 uint16_t this_timer = timer_read();
119
120 // too soon. don't spam updates
121 if(this_timer - last_timer < ANIMATION_STEP_INTERVAL) return;
122
123 uint16_t elapsed = this_timer - effect_start_timer;
124
125 last_timer = this_timer;
126 float progress = (float)elapsed / FADE_BACK_TIME;
127 progress = fmin(1.0,progress);
128
129 for(uint8_t i = 0; i < RGBLED_NUM; i++) {
130 led[i].r = shadowed_led[i].r * progress;
131 led[i].g = shadowed_led[i].g * progress;
132 led[i].b = shadowed_led[i].b * progress;
133 }
134 rgblight_set();
135
136 if(elapsed >= FADE_BACK_TIME) user_rgb_mode = 0;
137}
138
139/** called when layer state or vstate has changed */
140__attribute__ ((weak))
141void set_state_leds(void) {
142 return;
143}
144
145void matrix_scan_user(void) {
146 static uint32_t last_layer = 0;
147 static uint32_t last_vstate = 0;
148 if(last_layer != layer_state || last_vstate != vstate) set_state_leds();
149 last_layer = layer_state;
150 last_vstate = vstate;
151
152 switch (user_rgb_mode) {
153 case BREATH_FIRE:
154 rgb_mode_breath_fire();
155 break;
156 case FADE_BACK:
157 rgb_mode_fade_back();
158 break;
159 }
160 matrix_scan_keymap();
161}
162
163#else
164
165void start_firey_return(void) {}
166
167#endif