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/>.
15 #include "visualizer.h"
18 #include "default_animations.h"
19 #include "led_backlight_keyframes.h"
21 #define NUM_ROWS LED_HEIGHT
22 #define NUM_COLS LED_WIDTH
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)
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 = {
43 led_backlight_keyframe_fade_in_all,
46 keyframe_animation_t decreasing_gradient = {
50 gfxMillisecondsToTicks(GRADIENT_TIME), // left to rigt (outside in)
52 gfxMillisecondsToTicks(GRADIENT_TIME), // left_to_right (mirrored, so inside out)
54 gfxMillisecondsToTicks(GRADIENT_TIME), // left to rigt (outside in)
56 gfxMillisecondsToTicks(GRADIENT_TIME), // left_to_right (mirrored, so inside out)
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,
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))
80 float v = 0.5 * (cosf(x) + 1.0f);
81 return (uint8_t)(255.0f * v);
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;
93 if((1*M_PI) < x && x < (2*M_PI))
95 v = 0.5 * (cosf(x) + 1.0f);
97 else if(x >= (2*M_PI))
105 return (uint8_t)(255.0f * v);
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;
112 if((2*M_PI) < x && x < (3*M_PI))
114 v = 0.5 * (cosf(x) + 1.0f);
117 else if(x >= (3*M_PI))
125 return (uint8_t)(255.0f * v);
128 bool led_backlight_keyframe_one_period_sweep(keyframe_animation_t* animation, visualizer_state_t* 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));
140 bool led_backlight_keyframe_half_period_sweep_to_on(keyframe_animation_t* animation, visualizer_state_t* 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));
151 bool led_backlight_keyframe_half_period_sweep_to_off(keyframe_animation_t* animation, visualizer_state_t* 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));
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 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
170 | 5 | 6 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 2 |
171 | 4 | 5 | 6 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 3 |
172 | 3 | 4 | 5 | 6 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 4 |
173 | 2 | 3 | 4 | 5 | 6 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 5 |
174 | 1 | 2 | 3 | 4 | 5 | 6 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 6 |
175 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 7 |
176 | 0 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 0 | 0 | 0 | 0 | 0 | 0 | 8 |
177 | 0 | 0 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 0 | 0 | 0 | 0 | 0 | 9 |
178 | 0 | 0 | 0 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 0 | 0 | 0 | 0 | 10 |
179 | 0 | 0 | 0 | 0 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 0 | 0 | 0 | 11 |
180 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 0 | 0 | 12 |
181 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 0 | 13 |
182 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 14 |
183 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 2 | 3 | 4 | 5 | 15 |
184 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 2 | 3 | 4 | 16 |
185 | 0 | 0 | 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 +---+---+---+---+---+---+---+---+---+---+---+---+---+---+-------+
192 #ifdef MASTER_IS_ON_RIGHT /*right side*/
193 keyframe_animation_t sweep_on_sweep_off_left_and_right = {
199 gfxMillisecondsToTicks(GRADIENT_TIME), // left on
200 gfxMillisecondsToTicks(GRADIENT_TIME), // right on
201 gfxMillisecondsToTicks(GRADIENT_TIME), // left off
202 gfxMillisecondsToTicks(GRADIENT_TIME), // right off
204 gfxMillisecondsToTicks(GRADIENT_TIME), // right on
205 gfxMillisecondsToTicks(GRADIENT_TIME), // left on
206 gfxMillisecondsToTicks(GRADIENT_TIME), // right off
207 gfxMillisecondsToTicks(GRADIENT_TIME), // left off
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,
226 keyframe_animation_t both_sides_fade_across = {
232 gfxMillisecondsToTicks(GRADIENT_TIME), // left to rigt (outside in)
234 gfxMillisecondsToTicks(GRADIENT_TIME), // left_to_right (mirrored, so inside out)
236 gfxMillisecondsToTicks(GRADIENT_TIME), // left to rigt (outside in)
238 gfxMillisecondsToTicks(GRADIENT_TIME), // left_to_right (mirrored, so inside out)
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,
256 keyframe_animation_t sweep_on_sweep_off_left_and_right = {
260 gfxMillisecondsToTicks(GRADIENT_TIME), // left on
261 gfxMillisecondsToTicks(GRADIENT_TIME), // right on
262 gfxMillisecondsToTicks(GRADIENT_TIME), // left off
263 gfxMillisecondsToTicks(GRADIENT_TIME), // right off
265 gfxMillisecondsToTicks(GRADIENT_TIME), // right on
266 gfxMillisecondsToTicks(GRADIENT_TIME), // left on
267 gfxMillisecondsToTicks(GRADIENT_TIME), // right off
268 gfxMillisecondsToTicks(GRADIENT_TIME), // left off
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,
285 keyframe_animation_t both_sides_fade_across = {
289 gfxMillisecondsToTicks(GRADIENT_TIME), // left to rigt (outside in)
291 gfxMillisecondsToTicks(GRADIENT_TIME), // left_to_right (mirrored, so inside out)
293 gfxMillisecondsToTicks(GRADIENT_TIME), // left to rigt (outside in)
295 gfxMillisecondsToTicks(GRADIENT_TIME), // left_to_right (mirrored, so inside out)
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,
317 #define SPRING_GREEN 64
319 #define TURQUOISE 107
325 #define RASPBERRY 234
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)) {
338 if (state->status.layer & 0x400) {
339 state->target_lcd_color = LCD_COLOR(OCEAN, saturation, 0xFF);
340 state->layer_text = "STENOGRAPHY";
342 else if (state->status.layer & 0x200) {
343 state->target_lcd_color = LCD_COLOR(GREEN, saturation, 0xFF);
344 state->layer_text = "FUNCTION";
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);
352 else if (state->status.layer & 0x80) {
353 state->target_lcd_color = LCD_COLOR(VIOLET, saturation, 0xFF);
354 state->layer_text = "Plover";
356 else if (state->status.layer & 0x40) {
357 state->target_lcd_color = LCD_COLOR(RASPBERRY, saturation, 0xFF);
358 state->layer_text = "Mirrored Symbols";
360 else if (state->status.layer & 0x20) {
361 state->target_lcd_color = LCD_COLOR(RED, saturation, 0xFF);
362 state->layer_text = "Symbols";
364 else if (state->status.layer & 0x8) {
365 state->target_lcd_color = LCD_COLOR(OCEAN, saturation, 0xFF);
366 state->layer_text = "Mirrored Dvorak";
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);
374 else if (state->status.layer & 0x2) {
375 state->target_lcd_color = LCD_COLOR(ORANGE, saturation, 0xFF);
376 state->layer_text = "Mirrored Qwerty";
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);