]> git.donarmstrong.com Git - qmk_firmware.git/blob - visualizer.c
Add function for running the next keyframe
[qmk_firmware.git] / visualizer.c
1 /*
2 The MIT License (MIT)
3
4 Copyright (c) 2016 Fred Sundvik
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23 */
24
25 #include "visualizer.h"
26 #include "ch.h"
27 #include <string.h>
28
29 #ifdef LCD_ENABLE
30 #include "gfx.h"
31 #endif
32
33 #ifdef LCD_BACKLIGHT_ENABLE
34 #include "lcd_backlight.h"
35 #endif
36
37 //#define DEBUG_VISUALIZER
38
39 #ifdef DEBUG_VISUALIZER
40 #include "debug.h"
41 #else
42 #include "nodebug.h"
43 #endif
44
45 #ifdef USE_SERIAL_LINK
46 #include "serial_link/protocol/transport.h"
47 #include "serial_link/system/driver.h"
48 #endif
49
50
51 static visualizer_keyboard_status_t current_status = {
52     .layer = 0xFFFFFFFF,
53     .default_layer = 0xFFFFFFFF,
54     .leds = 0xFFFFFFFF,
55     .suspended = false,
56 };
57
58 static bool same_status(visualizer_keyboard_status_t* status1, visualizer_keyboard_status_t* status2) {
59     return status1->layer == status2->layer &&
60         status1->default_layer == status2->default_layer &&
61         status1->leds == status2->leds &&
62         status1->suspended == status2->suspended;
63 }
64
65 static event_source_t layer_changed_event;
66 static bool visualizer_enabled = false;
67
68 #define MAX_SIMULTANEOUS_ANIMATIONS 4
69 static keyframe_animation_t* animations[MAX_SIMULTANEOUS_ANIMATIONS] = {};
70
71 #ifdef USE_SERIAL_LINK
72 MASTER_TO_ALL_SLAVES_OBJECT(current_status, visualizer_keyboard_status_t);
73
74 static remote_object_t* remote_objects[] = {
75     REMOTE_OBJECT(current_status),
76 };
77
78 #endif
79
80 GDisplay* LCDDisplay;
81 GDisplay* LEDDisplay;
82
83
84 void start_keyframe_animation(keyframe_animation_t* animation) {
85     animation->current_frame = -1;
86     animation->time_left_in_frame = 0;
87     animation->need_update = true;
88     int free_index = -1;
89     for (int i=0;i<MAX_SIMULTANEOUS_ANIMATIONS;i++) {
90         if (animations[i] == animation) {
91             return;
92         }
93         if (free_index == -1 && animations[i] == NULL) {
94            free_index=i;
95         }
96     }
97     if (free_index!=-1) {
98         animations[free_index] = animation;
99     }
100 }
101
102 void stop_keyframe_animation(keyframe_animation_t* animation) {
103     animation->current_frame = animation->num_frames;
104     animation->time_left_in_frame = 0;
105     animation->need_update = true;
106     animation->first_update_of_frame = false;
107     animation->last_update_of_frame = false;
108     for (int i=0;i<MAX_SIMULTANEOUS_ANIMATIONS;i++) {
109         if (animations[i] == animation) {
110             animations[i] = NULL;
111             return;
112         }
113     }
114 }
115
116 void stop_all_keyframe_animations(void) {
117     for (int i=0;i<MAX_SIMULTANEOUS_ANIMATIONS;i++) {
118         if (animations[i]) {
119             animations[i]->current_frame = animations[i]->num_frames;
120             animations[i]->time_left_in_frame = 0;
121             animations[i]->need_update = true;
122             animations[i]->first_update_of_frame = false;
123             animations[i]->last_update_of_frame = false;
124             animations[i] = NULL;
125         }
126     }
127 }
128
129 static bool update_keyframe_animation(keyframe_animation_t* animation, visualizer_state_t* state, systime_t delta, systime_t* sleep_time) {
130     dprintf("Animation frame%d, left %d, delta %d\n", animation->current_frame,
131             animation->time_left_in_frame, delta);
132     if (animation->current_frame == animation->num_frames) {
133         animation->need_update = false;
134         return false;
135     }
136     if (animation->current_frame == -1) {
137        animation->current_frame = 0;
138        animation->time_left_in_frame = animation->frame_lengths[0];
139        animation->need_update = true;
140        animation->first_update_of_frame = true;
141     } else {
142         animation->time_left_in_frame -= delta;
143         while (animation->time_left_in_frame <= 0) {
144             int left = animation->time_left_in_frame;
145             if (animation->need_update) {
146                 animation->time_left_in_frame = 0;
147                 animation->last_update_of_frame = true;
148                 (*animation->frame_functions[animation->current_frame])(animation, state);
149                 animation->last_update_of_frame = false;
150             }
151             animation->current_frame++;
152             animation->need_update = true;
153             animation->first_update_of_frame = true;
154             if (animation->current_frame == animation->num_frames) {
155                 if (animation->loop) {
156                     animation->current_frame = 0;
157                 }
158                 else {
159                     stop_keyframe_animation(animation);
160                     return false;
161                 }
162             }
163             delta = -left;
164             animation->time_left_in_frame = animation->frame_lengths[animation->current_frame];
165             animation->time_left_in_frame -= delta;
166         }
167     }
168     if (animation->need_update) {
169         animation->need_update = (*animation->frame_functions[animation->current_frame])(animation, state);
170         animation->first_update_of_frame = false;
171     }
172
173     int wanted_sleep = animation->need_update ? 10 : animation->time_left_in_frame;
174     if ((unsigned)wanted_sleep < *sleep_time) {
175         *sleep_time = wanted_sleep;
176     }
177
178     return true;
179 }
180
181 void run_next_keyframe(keyframe_animation_t* animation, visualizer_state_t* state) {
182     int next_frame = animation->current_frame + 1;
183     if (next_frame == animation->num_frames) {
184         next_frame = 0;
185     }
186     keyframe_animation_t temp_animation = *animation;
187     temp_animation.current_frame = next_frame;
188     temp_animation.time_left_in_frame = animation->frame_lengths[next_frame];
189     temp_animation.first_update_of_frame = true;
190     temp_animation.last_update_of_frame = false;
191     temp_animation.need_update  = false;
192     visualizer_state_t temp_state = *state;
193     (*temp_animation.frame_functions[next_frame])(&temp_animation, &temp_state);
194 }
195
196 bool keyframe_no_operation(keyframe_animation_t* animation, visualizer_state_t* state) {
197     (void)animation;
198     (void)state;
199     return false;
200 }
201
202 #ifdef LCD_BACKLIGHT_ENABLE
203 bool keyframe_animate_backlight_color(keyframe_animation_t* animation, visualizer_state_t* state) {
204     int frame_length = animation->frame_lengths[animation->current_frame];
205     int current_pos = frame_length - animation->time_left_in_frame;
206     uint8_t t_h = LCD_HUE(state->target_lcd_color);
207     uint8_t t_s = LCD_SAT(state->target_lcd_color);
208     uint8_t t_i = LCD_INT(state->target_lcd_color);
209     uint8_t p_h = LCD_HUE(state->prev_lcd_color);
210     uint8_t p_s = LCD_SAT(state->prev_lcd_color);
211     uint8_t p_i = LCD_INT(state->prev_lcd_color);
212
213     uint8_t d_h1 = t_h - p_h; //Modulo arithmetic since we want to wrap around
214     int d_h2 = t_h - p_h;
215     // Chose the shortest way around
216     int d_h = abs(d_h2) < d_h1 ? d_h2 : d_h1;
217     int d_s = t_s - p_s;
218     int d_i = t_i - p_i;
219
220     int hue = (d_h * current_pos) / frame_length;
221     int sat = (d_s * current_pos) / frame_length;
222     int intensity = (d_i * current_pos) / frame_length;
223     //dprintf("%X -> %X = %X\n", p_h, t_h, hue);
224     hue += p_h;
225     sat += p_s;
226     intensity += p_i;
227     state->current_lcd_color = LCD_COLOR(hue, sat, intensity);
228     lcd_backlight_color(
229             LCD_HUE(state->current_lcd_color),
230             LCD_SAT(state->current_lcd_color),
231             LCD_INT(state->current_lcd_color));
232
233     return true;
234 }
235
236 bool keyframe_set_backlight_color(keyframe_animation_t* animation, visualizer_state_t* state) {
237     (void)animation;
238     state->prev_lcd_color = state->target_lcd_color;
239     state->current_lcd_color = state->target_lcd_color;
240     lcd_backlight_color(
241             LCD_HUE(state->current_lcd_color),
242             LCD_SAT(state->current_lcd_color),
243             LCD_INT(state->current_lcd_color));
244     return false;
245 }
246 #endif // LCD_BACKLIGHT_ENABLE
247
248 #ifdef LCD_ENABLE
249 bool keyframe_display_layer_text(keyframe_animation_t* animation, visualizer_state_t* state) {
250     (void)animation;
251     gdispClear(White);
252     gdispDrawString(0, 10, state->layer_text, state->font_dejavusansbold12, Black);
253     gdispFlush();
254     return false;
255 }
256
257 static void format_layer_bitmap_string(uint16_t default_layer, uint16_t layer, char* buffer) {
258     for (int i=0; i<16;i++)
259     {
260         uint32_t mask = (1u << i);
261         if (default_layer & mask) {
262             if (layer & mask) {
263                 *buffer = 'B';
264             } else {
265                 *buffer = 'D';
266             }
267         } else if (layer & mask) {
268             *buffer = '1';
269         } else {
270             *buffer = '0';
271         }
272         ++buffer;
273
274         if (i==3 || i==7 || i==11) {
275             *buffer = ' ';
276             ++buffer;
277         }
278     }
279     *buffer = 0;
280 }
281
282 bool keyframe_display_layer_bitmap(keyframe_animation_t* animation, visualizer_state_t* state) {
283     (void)animation;
284     const char* layer_help = "1=On D=Default B=Both";
285     char layer_buffer[16 + 4]; // 3 spaces and one null terminator
286     gdispClear(White);
287     gdispDrawString(0, 0, layer_help, state->font_fixed5x8, Black);
288     format_layer_bitmap_string(state->status.default_layer, state->status.layer, layer_buffer);
289     gdispDrawString(0, 10, layer_buffer, state->font_fixed5x8, Black);
290     format_layer_bitmap_string(state->status.default_layer >> 16, state->status.layer >> 16, layer_buffer);
291     gdispDrawString(0, 20, layer_buffer, state->font_fixed5x8, Black);
292     gdispFlush();
293     return false;
294 }
295 #endif // LCD_ENABLE
296
297 bool keyframe_disable_lcd_and_backlight(keyframe_animation_t* animation, visualizer_state_t* state) {
298     (void)animation;
299     (void)state;
300 #ifdef LCD_ENABLE
301     gdispSetPowerMode(powerOff);
302 #endif
303 #ifdef LCD_BACKLIGHT_ENABLE
304     lcd_backlight_hal_color(0, 0, 0);
305 #endif
306     return false;
307 }
308
309 bool keyframe_enable_lcd_and_backlight(keyframe_animation_t* animation, visualizer_state_t* state) {
310     (void)animation;
311     (void)state;
312 #ifdef LCD_ENABLE
313     gdispSetPowerMode(powerOn);
314 #endif
315     return false;
316 }
317
318 bool enable_visualization(keyframe_animation_t* animation, visualizer_state_t* state) {
319     (void)animation;
320     (void)state;
321     dprint("User visualizer inited\n");
322     visualizer_enabled = true;
323     return false;
324 }
325
326 // TODO: Optimize the stack size, this is probably way too big
327 static THD_WORKING_AREA(visualizerThreadStack, 1024);
328 static THD_FUNCTION(visualizerThread, arg) {
329     (void)arg;
330
331     event_listener_t event_listener;
332     chEvtRegister(&layer_changed_event, &event_listener, 0);
333
334     visualizer_keyboard_status_t initial_status = {
335         .default_layer = 0xFFFFFFFF,
336         .layer = 0xFFFFFFFF,
337         .leds = 0xFFFFFFFF,
338         .suspended = false,
339     };
340
341     visualizer_state_t state = {
342         .status = initial_status,
343         .current_lcd_color = 0,
344 #ifdef LCD_ENABLE
345         .font_fixed5x8 = gdispOpenFont("fixed_5x8"),
346         .font_dejavusansbold12 = gdispOpenFont("DejaVuSansBold12")
347 #endif
348     };
349     initialize_user_visualizer(&state);
350     state.prev_lcd_color = state.current_lcd_color;
351
352 #ifdef LCD_BACKLIGHT_ENABLE
353     lcd_backlight_color(
354             LCD_HUE(state.current_lcd_color),
355             LCD_SAT(state.current_lcd_color),
356             LCD_INT(state.current_lcd_color));
357 #endif
358
359     systime_t sleep_time = TIME_INFINITE;
360     systime_t current_time = chVTGetSystemTimeX();
361
362     while(true) {
363         systime_t new_time = chVTGetSystemTimeX();
364         systime_t delta = new_time - current_time;
365         current_time = new_time;
366         bool enabled = visualizer_enabled;
367         if (!same_status(&state.status, &current_status)) {
368             if (visualizer_enabled) {
369                 if (current_status.suspended) {
370                     stop_all_keyframe_animations();
371                     visualizer_enabled = false;
372                     state.status = current_status;
373                     user_visualizer_suspend(&state);
374                 }
375                 else {
376                     state.status = current_status;
377                     update_user_visualizer_state(&state);
378                 }
379                 state.prev_lcd_color = state.current_lcd_color;
380             }
381         }
382         if (!enabled && state.status.suspended && current_status.suspended == false) {
383             // Setting the status to the initial status will force an update
384             // when the visualizer is enabled again
385             state.status = initial_status;
386             state.status.suspended = false;
387             stop_all_keyframe_animations();
388             user_visualizer_resume(&state);
389             state.prev_lcd_color = state.current_lcd_color;
390         }
391         sleep_time = TIME_INFINITE;
392         for (int i=0;i<MAX_SIMULTANEOUS_ANIMATIONS;i++) {
393             if (animations[i]) {
394                 update_keyframe_animation(animations[i], &state, delta, &sleep_time);
395             }
396         }
397         // The animation can enable the visualizer
398         // And we might need to update the state when that happens
399         // so don't sleep
400         if (enabled != visualizer_enabled) {
401             sleep_time = 0;
402         }
403
404         systime_t after_update = chVTGetSystemTimeX();
405         unsigned update_delta = after_update - current_time;
406         if (sleep_time != TIME_INFINITE) {
407             if (sleep_time > update_delta) {
408                 sleep_time -= update_delta;
409             }
410             else {
411                 sleep_time = 0;
412             }
413         }
414         dprintf("Update took %d, last delta %d, sleep_time %d\n", update_delta, delta, sleep_time);
415         chEvtWaitOneTimeout(EVENT_MASK(0), sleep_time);
416     }
417 #ifdef LCD_ENABLE
418     gdispCloseFont(state.font_fixed5x8);
419     gdispCloseFont(state.font_dejavusansbold12);
420 #endif
421 }
422
423 void visualizer_init(void) {
424 #ifdef LCD_ENABLE
425     gfxInit();
426 #endif
427
428 #ifdef LCD_BACKLIGHT_ENABLE
429     lcd_backlight_init();
430 #endif
431
432 #ifdef USE_SERIAL_LINK
433     add_remote_objects(remote_objects, sizeof(remote_objects) / sizeof(remote_object_t*) );
434 #endif
435     // TODO: Make sure these works when either of these are disabled
436     LCDDisplay = gdispGetDisplay(0);
437     LEDDisplay = gdispGetDisplay(1);
438
439
440     // We are using a low priority thread, the idea is to have it run only
441     // when the main thread is sleeping during the matrix scanning
442     chEvtObjectInit(&layer_changed_event);
443     (void)chThdCreateStatic(visualizerThreadStack, sizeof(visualizerThreadStack),
444                               LOWPRIO, visualizerThread, NULL);
445 }
446
447 void update_status(bool changed) {
448     if (changed) {
449         chEvtBroadcast(&layer_changed_event);
450     }
451 #ifdef USE_SERIAL_LINK
452     static systime_t last_update = 0;
453     systime_t current_update = chVTGetSystemTimeX();
454     systime_t delta = current_update - last_update;
455     if (changed || delta > MS2ST(10)) {
456         last_update = current_update;
457         visualizer_keyboard_status_t* r = begin_write_current_status();
458         *r = current_status;
459         end_write_current_status();
460     }
461 #endif
462 }
463
464 void visualizer_update(uint32_t default_state, uint32_t state, uint32_t leds) {
465     // Note that there's a small race condition here, the thread could read
466     // a state where one of these are set but not the other. But this should
467     // not really matter as it will be fixed during the next loop step.
468     // Alternatively a mutex could be used instead of the volatile variables
469
470     bool changed = false;
471 #ifdef USE_SERIAL_LINK
472     if (is_serial_link_connected ()) {
473         visualizer_keyboard_status_t* new_status = read_current_status();
474         if (new_status) {
475             if (!same_status(&current_status, new_status)) {
476                 changed = true;
477                 current_status = *new_status;
478             }
479         }
480     }
481     else {
482 #else
483    {
484 #endif
485         visualizer_keyboard_status_t new_status = {
486             .layer = state,
487             .default_layer = default_state,
488             .leds = leds,
489             .suspended = current_status.suspended,
490         };
491         if (!same_status(&current_status, &new_status)) {
492             changed = true;
493             current_status = new_status;
494         }
495     }
496     update_status(changed);
497 }
498
499 void visualizer_suspend(void) {
500     current_status.suspended = true;
501     update_status(true);
502 }
503
504 void visualizer_resume(void) {
505     current_status.suspended = false;
506     update_status(true);
507 }