1 /* Copyright 2015-2017 Christon DeWan
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.
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.
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/>.
17 #ifdef RGBLIGHT_ENABLE
23 #include "fancylighting.h"
26 __attribute__ ((weak))
27 void matrix_scan_keymap(void) {
28 // override me, if you want.
32 #define ABSDIFF(a,b) ((a)>(b)?(a)-(b):(b)-(a))
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)
41 uint16_t effect_start_timer = 0;
42 uint8_t user_rgb_mode = 0;
43 LED_TYPE shadowed_led[RGBLED_NUM] = {0};
45 void 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];
62 void 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;
69 progress = 1.0 - progress;
71 progress = fmax(0.0,progress);
72 progress *= progress; // squared!
74 float alpha = (time_progress + 0.1) * 7.0 - space_progress;
75 alpha = fmin(1.0, alpha*alpha);
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;
85 * It's actually a rainbow: a fire curve didn't really look right.
86 * it's still cool, though!
88 void 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();
93 // too soon. don't spam updates
94 if(this_timer - last_timer < ANIMATION_STEP_INTERVAL) return;
96 uint16_t elapsed = this_timer - effect_start_timer;
98 last_timer = this_timer;
99 if(elapsed >= BREATH_FIRE_TIME) {
101 user_rgb_mode = FADE_BACK;
102 effect_start_timer = this_timer;
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;
109 set_color_for_offsets(elapsed, space_offset, i);
115 void 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();
120 // too soon. don't spam updates
121 if(this_timer - last_timer < ANIMATION_STEP_INTERVAL) return;
123 uint16_t elapsed = this_timer - effect_start_timer;
125 last_timer = this_timer;
126 float progress = (float)elapsed / FADE_BACK_TIME;
127 progress = fmin(1.0,progress);
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;
136 if(elapsed >= FADE_BACK_TIME) user_rgb_mode = 0;
139 /** called when layer state or vstate has changed */
140 __attribute__ ((weak))
141 void set_state_leds(void) {
145 void 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;
152 switch (user_rgb_mode) {
154 rgb_mode_breath_fire();
157 rgb_mode_fade_back();
160 matrix_scan_keymap();
165 void start_firey_return(void) {}