]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/ergodox_infinity/keymaps/halfkeyboard/visualizer.c
Merge branch 'master' of github.com:qmk/qmk_firmware into hf/shinydox
[qmk_firmware.git] / keyboards / ergodox_infinity / keymaps / halfkeyboard / visualizer.c
1 /*
2 Copyright 2017 Fred Sundvik
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 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 GNU General Public License for more details.
11 You should have received a copy of the GNU General Public License
12 along with this program.  If not, see <http://www.gnu.org/licenses/>.
13 */
14
15 #include "visualizer.h"
16 #include "gfx.h"
17 #include "math.h"
18 #include "default_animations.h"
19 #include "led_backlight_keyframes.h"
20
21 #define NUM_ROWS LED_HEIGHT
22 #define NUM_COLS LED_WIDTH
23
24 #define ONESIDESCAN 10
25 #define BOTHSIDESCAN 20
26 #define FULL_ON LUMA2COLOR(255)
27 #define THREE_QUARTER LUMA2COLOR(200)
28 #define HALF_ON LUMA2COLOR(150)
29 #define ONE_QUARTER LUMA2COLOR(50)
30
31 #define CROSSFADE_TIME 500
32 #define GRADIENT_TIME 3000
33 bool led_backlight_keyframe_one_period_sweep(keyframe_animation_t* animation, visualizer_state_t* state);
34 bool led_backlight_keyframe_half_period_sweep_to_on(keyframe_animation_t* animation, visualizer_state_t* state);
35 bool led_backlight_keyframe_half_period_sweep_to_off(keyframe_animation_t* animation, visualizer_state_t* state);
36 keyframe_animation_t Fade_in_all_leds = {
37     .num_frames = 1,
38     .loop = false,
39     .frame_lengths = {
40         CROSSFADE_TIME,
41     },
42     .frame_functions = {
43         led_backlight_keyframe_fade_in_all,
44     },
45 };
46 keyframe_animation_t decreasing_gradient = {
47     .num_frames = 8,
48     .loop = true,
49     .frame_lengths = {
50         gfxMillisecondsToTicks(GRADIENT_TIME), // left to rigt (outside in)
51         0,           // mirror leds
52         gfxMillisecondsToTicks(GRADIENT_TIME), // left_to_right (mirrored, so inside out)
53         0,           // normal leds
54         gfxMillisecondsToTicks(GRADIENT_TIME), // left to rigt (outside in)
55         0,           // mirror leds
56         gfxMillisecondsToTicks(GRADIENT_TIME), // left_to_right (mirrored, so inside out)
57         0,           // normal leds
58     },
59     .frame_functions = {
60         led_backlight_keyframe_one_period_sweep,
61         led_backlight_keyframe_mirror_orientation,
62         keyframe_no_operation,
63         keyframe_no_operation,
64         keyframe_no_operation,
65         keyframe_no_operation,
66         led_backlight_keyframe_one_period_sweep,
67         led_backlight_keyframe_normal_orientation,
68
69     },
70 };
71
72
73
74 static uint8_t off_on_off_gradient(float t, float index, float num) {
75     const float two_pi = M_PI * 2.0f;
76     float normalized_index = (1.0f - index / (num - 1.0f)) * two_pi;
77     float x = t * two_pi + normalized_index-M_PI;
78     if((1*M_PI) < x && x < (3*M_PI))
79     {
80       float v = 0.5 * (cosf(x) + 1.0f);
81       return (uint8_t)(255.0f * v);
82     }
83     else
84     {
85       return 0;
86     }
87 }
88 static uint8_t off_on_gradient(float t, float index, float num) {
89     const float two_pi = M_PI * 2.0f;
90     float normalized_index = (1.0f - index / (num - 1.0f)) * two_pi;
91     float x = t * two_pi + normalized_index-M_PI;
92     float v;
93     if((1*M_PI) < x && x < (2*M_PI))
94     {
95       v = 0.5 * (cosf(x) + 1.0f);
96     }
97     else if(x >= (2*M_PI))
98     {
99       v = 1;
100     }
101     else
102     {
103       v = 0;
104     }
105     return (uint8_t)(255.0f * v);
106 }
107 static uint8_t on_off_gradient(float t, float index, float num) {
108     const float two_pi = M_PI * 2.0f;
109     float normalized_index = (1.0f - index / (num - 1.0f)) * two_pi;
110     float x = t * two_pi + normalized_index-M_PI;
111     float v;
112     if((2*M_PI) < x && x < (3*M_PI))
113     {
114       v = 0.5 * (cosf(x) + 1.0f);
115
116     }
117     else if(x >= (3*M_PI))
118     {
119       v = 0;
120     }
121     else
122     {
123       v = 1;
124     }
125     return (uint8_t)(255.0f * v);
126 }
127
128 bool led_backlight_keyframe_one_period_sweep(keyframe_animation_t* animation, visualizer_state_t* state) {
129     (void)state;
130     float frame_length = animation->frame_lengths[animation->current_frame];
131     float current_pos = frame_length - animation->time_left_in_frame;
132     float t = current_pos / frame_length;
133     for (int i=0; i< NUM_COLS; i++) {
134         uint8_t color = off_on_off_gradient(t*2, i, NUM_COLS);
135         gdispGDrawLine(LED_DISPLAY, i, 0, i, NUM_ROWS - 1, LUMA2COLOR(color));
136     }
137     return true;
138 }
139
140 bool led_backlight_keyframe_half_period_sweep_to_on(keyframe_animation_t* animation, visualizer_state_t* state) {
141     (void)state;
142     float frame_length = animation->frame_lengths[animation->current_frame];
143     float current_pos = frame_length - animation->time_left_in_frame;
144     float t = current_pos / frame_length;
145     for (int i=0; i< NUM_COLS; i++) {
146         uint8_t color = off_on_gradient(t*2, i, NUM_COLS);
147         gdispGDrawLine(LED_DISPLAY, i, 0, i, NUM_ROWS - 1, LUMA2COLOR(color));
148     }
149     return true;
150 }
151 bool led_backlight_keyframe_half_period_sweep_to_off(keyframe_animation_t* animation, visualizer_state_t* state) {
152     (void)state;
153     float frame_length = animation->frame_lengths[animation->current_frame];
154     float current_pos = frame_length - animation->time_left_in_frame;
155     float t = current_pos / frame_length;
156     for (int i=0; i< NUM_COLS; i++) {
157         uint8_t color = on_off_gradient(t*2, i, NUM_COLS);
158         gdispGDrawLine(LED_DISPLAY, i, 0, i, NUM_ROWS - 1, LUMA2COLOR(color));
159     }
160     return true;
161 }
162
163
164 /*
165  +---+---+---+---+---+---+---+---+---+---+---+---+---+---+-------+
166 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | phase |
167 +---+---+---+---+---+---+---+---+---+---+---+---+---+---+-------+
168 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |     0 |
169 | 6 | 0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 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 2 | 3 |    17 |
186 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 2 |    18 |
187 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |    19 |
188 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |    20 |
189 +---+---+---+---+---+---+---+---+---+---+---+---+---+---+-------+
190 */
191
192 #ifdef MASTER_IS_ON_RIGHT /*right side*/
193 keyframe_animation_t sweep_on_sweep_off_left_and_right = {
194     .num_frames = 12,
195     .loop = true,
196     .frame_lengths = {
197         0,
198         1,
199         gfxMillisecondsToTicks(GRADIENT_TIME), // left  on
200         gfxMillisecondsToTicks(GRADIENT_TIME), // right on
201         gfxMillisecondsToTicks(GRADIENT_TIME), // left off
202         gfxMillisecondsToTicks(GRADIENT_TIME), // right off
203         0,           // mirror leds
204         gfxMillisecondsToTicks(GRADIENT_TIME), // right on
205         gfxMillisecondsToTicks(GRADIENT_TIME), // left on
206         gfxMillisecondsToTicks(GRADIENT_TIME), // right off
207         gfxMillisecondsToTicks(GRADIENT_TIME), // left off
208         0,           // normal leds
209     },
210     .frame_functions = {
211         led_backlight_keyframe_mirror_orientation,
212         led_backlight_keyframe_fade_out_all,
213         keyframe_no_operation,
214         led_backlight_keyframe_half_period_sweep_to_on,
215         keyframe_no_operation,
216         led_backlight_keyframe_half_period_sweep_to_off,
217         led_backlight_keyframe_normal_orientation,
218         led_backlight_keyframe_half_period_sweep_to_on,
219         keyframe_no_operation,
220         led_backlight_keyframe_half_period_sweep_to_off,
221         keyframe_no_operation,
222         led_backlight_keyframe_mirror_orientation,
223
224     },
225 };
226 keyframe_animation_t both_sides_fade_across = {
227     .num_frames = 10,
228     .loop = true,
229     .frame_lengths = {
230         0,
231         1,
232         gfxMillisecondsToTicks(GRADIENT_TIME), // left to rigt (outside in)
233         0,           // mirror leds
234         gfxMillisecondsToTicks(GRADIENT_TIME), // left_to_right (mirrored, so inside out)
235         0,           // normal leds
236         gfxMillisecondsToTicks(GRADIENT_TIME), // left to rigt (outside in)
237         0,           // mirror leds
238         gfxMillisecondsToTicks(GRADIENT_TIME), // left_to_right (mirrored, so inside out)
239         0,           // normal leds
240     },
241     .frame_functions = {
242         led_backlight_keyframe_mirror_orientation,
243         led_backlight_keyframe_fade_out_all,
244         keyframe_no_operation,
245         keyframe_no_operation,
246         led_backlight_keyframe_one_period_sweep,
247         led_backlight_keyframe_normal_orientation,
248         led_backlight_keyframe_one_period_sweep,
249         led_backlight_keyframe_mirror_orientation,
250         keyframe_no_operation,
251         keyframe_no_operation,
252     },
253 };
254
255 #else /*left side*/
256 keyframe_animation_t sweep_on_sweep_off_left_and_right = {
257     .num_frames = 10,
258     .loop = true,
259     .frame_lengths = {
260         gfxMillisecondsToTicks(GRADIENT_TIME), // left  on
261         gfxMillisecondsToTicks(GRADIENT_TIME), // right on
262         gfxMillisecondsToTicks(GRADIENT_TIME), // left off
263         gfxMillisecondsToTicks(GRADIENT_TIME), // right off
264         0,           // mirror leds
265         gfxMillisecondsToTicks(GRADIENT_TIME), // right on
266         gfxMillisecondsToTicks(GRADIENT_TIME), // left on
267         gfxMillisecondsToTicks(GRADIENT_TIME), // right off
268         gfxMillisecondsToTicks(GRADIENT_TIME), // left off
269         0,           // normal leds
270     },
271     .frame_functions = {
272         led_backlight_keyframe_half_period_sweep_to_on,
273         keyframe_no_operation,
274         led_backlight_keyframe_half_period_sweep_to_off,
275         keyframe_no_operation,
276         led_backlight_keyframe_mirror_orientation,
277         keyframe_no_operation,
278         led_backlight_keyframe_half_period_sweep_to_on,
279         keyframe_no_operation,
280         led_backlight_keyframe_half_period_sweep_to_off,
281         led_backlight_keyframe_normal_orientation,
282
283     },
284 };
285 keyframe_animation_t both_sides_fade_across = {
286     .num_frames = 8,
287     .loop = true,
288     .frame_lengths = {
289         gfxMillisecondsToTicks(GRADIENT_TIME), // left to rigt (outside in)
290         0,           // mirror leds
291         gfxMillisecondsToTicks(GRADIENT_TIME), // left_to_right (mirrored, so inside out)
292         0,           // normal leds
293         gfxMillisecondsToTicks(GRADIENT_TIME), // left to rigt (outside in)
294         0,           // mirror leds
295         gfxMillisecondsToTicks(GRADIENT_TIME), // left_to_right (mirrored, so inside out)
296         0,           // normal leds
297     },
298     .frame_functions = {
299         led_backlight_keyframe_one_period_sweep,
300         led_backlight_keyframe_mirror_orientation,
301         keyframe_no_operation,
302         keyframe_no_operation,
303         keyframe_no_operation,
304         keyframe_no_operation,
305         led_backlight_keyframe_one_period_sweep,
306         led_backlight_keyframe_normal_orientation,
307
308     },
309 };
310
311
312 #endif
313
314 #define RED 0
315 #define ORANGE 21
316 #define YELLOW 42
317 #define SPRING_GREEN 64
318 #define GREEN 85
319 #define TURQUOISE 107
320 #define CYAN 127
321 #define OCEAN 149
322 #define BLUE 170
323 #define VIOLET 192
324 #define MAGENTA 212
325 #define RASPBERRY 234
326
327 // This function should be implemented by the keymap visualizer
328 // Don't change anything else than state->target_lcd_color and state->layer_text as that's the only thing
329 // that the simple_visualizer assumes that you are updating
330 // Also make sure that the buffer passed to state->layer_text remains valid until the previous animation is
331 // stopped. This can be done by either double buffering it or by using constant strings
332 static void get_visualizer_layer_and_color(visualizer_state_t* state) {
333     uint8_t saturation = 255;
334     /* if (state->status.leds & (1u << USB_LED_CAPS_LOCK)) {
335         saturation = 255;
336     } */
337
338     if (state->status.layer & 0x400) {
339         state->target_lcd_color = LCD_COLOR(OCEAN, saturation, 0xFF);
340         state->layer_text = "STENOGRAPHY";
341     }
342     else if (state->status.layer & 0x200) {
343         state->target_lcd_color = LCD_COLOR(GREEN, saturation, 0xFF);
344         state->layer_text = "FUNCTION";
345     }
346     else if (state->status.layer & 0x100) {
347         state->target_lcd_color = LCD_COLOR(MAGENTA, saturation, 0xFF);
348         state->layer_text = "Shortcuts Layer";
349         stop_keyframe_animation(&sweep_on_sweep_off_left_and_right);
350         start_keyframe_animation(&led_test_animation);
351     }
352     else    if (state->status.layer & 0x80) {
353         state->target_lcd_color = LCD_COLOR(VIOLET, saturation, 0xFF);
354         state->layer_text = "Plover";
355     }
356     else if (state->status.layer & 0x40) {
357         state->target_lcd_color = LCD_COLOR(RASPBERRY, saturation, 0xFF);
358         state->layer_text = "Mirrored Symbols";
359     }
360     else if (state->status.layer & 0x20) {
361         state->target_lcd_color = LCD_COLOR(RED, saturation, 0xFF);
362         state->layer_text = "Symbols";
363     }
364     else if (state->status.layer & 0x8) {
365         state->target_lcd_color = LCD_COLOR(OCEAN, saturation, 0xFF);
366         state->layer_text = "Mirrored Dvorak";
367     }
368     else if (state->status.layer & 0x4) {
369         state->target_lcd_color = LCD_COLOR(BLUE, saturation, 0xFF);
370         state->layer_text = "Dvorak";
371         stop_keyframe_animation(&led_test_animation);
372         start_keyframe_animation(&sweep_on_sweep_off_left_and_right);
373     }
374     else if (state->status.layer & 0x2) {
375         state->target_lcd_color = LCD_COLOR(ORANGE, saturation, 0xFF);
376         state->layer_text = "Mirrored Qwerty";
377     }
378     else {
379         state->target_lcd_color = LCD_COLOR(YELLOW, saturation, 0xFF);
380         state->layer_text = "Qwerty";
381         stop_keyframe_animation(&led_test_animation);
382         start_keyframe_animation(&Fade_in_all_leds);
383     }
384 }