]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/visualizer/visualizer.c
a4b3ea7e491805434ac1218e35c8c82f73d6ce68
[qmk_firmware.git] / quantum / visualizer / 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 "config.h"
26 #include "visualizer.h"
27 #include <string.h>
28 #ifdef PROTOCOL_CHIBIOS
29 #include "ch.h"
30 #endif
31
32 #include "gfx.h"
33
34 #ifdef LCD_BACKLIGHT_ENABLE
35 #include "lcd_backlight.h"
36 #endif
37
38 //#define DEBUG_VISUALIZER
39
40 #ifdef DEBUG_VISUALIZER
41 #include "debug.h"
42 #else
43 #include "nodebug.h"
44 #endif
45
46 #ifdef SERIAL_LINK_ENABLE
47 #include "serial_link/protocol/transport.h"
48 #include "serial_link/system/serial_link.h"
49 #endif
50
51 #include "action_util.h"
52
53 // Define this in config.h
54 #ifndef VISUALIZER_THREAD_PRIORITY
55 #define "Visualizer thread priority not defined"
56 #endif
57
58 static visualizer_keyboard_status_t current_status = {
59     .layer = 0xFFFFFFFF,
60     .default_layer = 0xFFFFFFFF,
61     .leds = 0xFFFFFFFF,
62 #ifdef BACKLIGHT_ENABLE
63     .backlight_level = 0,
64 #endif
65     .mods = 0xFF,
66     .suspended = false,
67 #ifdef VISUALIZER_USER_DATA_SIZE
68     .user_data = {0}
69 #endif
70 };
71
72 static bool same_status(visualizer_keyboard_status_t* status1, visualizer_keyboard_status_t* status2) {
73     return status1->layer == status2->layer &&
74         status1->default_layer == status2->default_layer &&
75         status1->mods == status2->mods &&
76         status1->leds == status2->leds &&
77         status1->suspended == status2->suspended
78 #ifdef BACKLIGHT_ENABLE
79         && status1->backlight_level == status2->backlight_level
80 #endif
81 #ifdef VISUALIZER_USER_DATA_SIZE
82         && memcmp(status1->user_data, status2->user_data, VISUALIZER_USER_DATA_SIZE) == 0
83 #endif
84     ;
85 }
86
87 static bool visualizer_enabled = false;
88
89 #ifdef VISUALIZER_USER_DATA_SIZE
90 static uint8_t user_data[VISUALIZER_USER_DATA_SIZE];
91 #endif
92
93 #define MAX_SIMULTANEOUS_ANIMATIONS 4
94 static keyframe_animation_t* animations[MAX_SIMULTANEOUS_ANIMATIONS] = {};
95
96 #ifdef SERIAL_LINK_ENABLE
97 MASTER_TO_ALL_SLAVES_OBJECT(current_status, visualizer_keyboard_status_t);
98
99 static remote_object_t* remote_objects[] = {
100     REMOTE_OBJECT(current_status),
101 };
102
103 #endif
104
105 GDisplay* LCD_DISPLAY = 0;
106 GDisplay* LED_DISPLAY = 0;
107
108 __attribute__((weak))
109 GDisplay* get_lcd_display(void) {
110     return gdispGetDisplay(0);
111 }
112
113 __attribute__((weak))
114 GDisplay* get_led_display(void) {
115     return gdispGetDisplay(1);
116 }
117
118 void start_keyframe_animation(keyframe_animation_t* animation) {
119     animation->current_frame = -1;
120     animation->time_left_in_frame = 0;
121     animation->need_update = true;
122     int free_index = -1;
123     for (int i=0;i<MAX_SIMULTANEOUS_ANIMATIONS;i++) {
124         if (animations[i] == animation) {
125             return;
126         }
127         if (free_index == -1 && animations[i] == NULL) {
128            free_index=i;
129         }
130     }
131     if (free_index!=-1) {
132         animations[free_index] = animation;
133     }
134 }
135
136 void stop_keyframe_animation(keyframe_animation_t* animation) {
137     animation->current_frame = animation->num_frames;
138     animation->time_left_in_frame = 0;
139     animation->need_update = true;
140     animation->first_update_of_frame = false;
141     animation->last_update_of_frame = false;
142     for (int i=0;i<MAX_SIMULTANEOUS_ANIMATIONS;i++) {
143         if (animations[i] == animation) {
144             animations[i] = NULL;
145             return;
146         }
147     }
148 }
149
150 void stop_all_keyframe_animations(void) {
151     for (int i=0;i<MAX_SIMULTANEOUS_ANIMATIONS;i++) {
152         if (animations[i]) {
153             animations[i]->current_frame = animations[i]->num_frames;
154             animations[i]->time_left_in_frame = 0;
155             animations[i]->need_update = true;
156             animations[i]->first_update_of_frame = false;
157             animations[i]->last_update_of_frame = false;
158             animations[i] = NULL;
159         }
160     }
161 }
162
163 static uint8_t get_num_running_animations(void) {
164     uint8_t count = 0;
165     for (int i=0;i<MAX_SIMULTANEOUS_ANIMATIONS;i++) {
166         count += animations[i] ? 1 : 0;
167     }
168     return count;
169 }
170
171 static bool update_keyframe_animation(keyframe_animation_t* animation, visualizer_state_t* state, systemticks_t delta, systemticks_t* sleep_time) {
172     // TODO: Clean up this messy code
173     dprintf("Animation frame%d, left %d, delta %d\n", animation->current_frame,
174             animation->time_left_in_frame, delta);
175     if (animation->current_frame == animation->num_frames) {
176         animation->need_update = false;
177         return false;
178     }
179     if (animation->current_frame == -1) {
180        animation->current_frame = 0;
181        animation->time_left_in_frame = animation->frame_lengths[0];
182        animation->need_update = true;
183        animation->first_update_of_frame = true;
184     } else {
185         animation->time_left_in_frame -= delta;
186         while (animation->time_left_in_frame <= 0) {
187             int left = animation->time_left_in_frame;
188             if (animation->need_update) {
189                 animation->time_left_in_frame = 0;
190                 animation->last_update_of_frame = true;
191                 (*animation->frame_functions[animation->current_frame])(animation, state);
192                 animation->last_update_of_frame = false;
193             }
194             animation->current_frame++;
195             animation->need_update = true;
196             animation->first_update_of_frame = true;
197             if (animation->current_frame == animation->num_frames) {
198                 if (animation->loop) {
199                     animation->current_frame = 0;
200                 }
201                 else {
202                     stop_keyframe_animation(animation);
203                     return false;
204                 }
205             }
206             delta = -left;
207             animation->time_left_in_frame = animation->frame_lengths[animation->current_frame];
208             animation->time_left_in_frame -= delta;
209         }
210     }
211     if (animation->need_update) {
212         animation->need_update = (*animation->frame_functions[animation->current_frame])(animation, state);
213         animation->first_update_of_frame = false;
214     }
215
216     systemticks_t wanted_sleep = animation->need_update ? gfxMillisecondsToTicks(10) : (unsigned)animation->time_left_in_frame;
217     if (wanted_sleep < *sleep_time) {
218         *sleep_time = wanted_sleep;
219     }
220
221     return true;
222 }
223
224 void run_next_keyframe(keyframe_animation_t* animation, visualizer_state_t* state) {
225     int next_frame = animation->current_frame + 1;
226     if (next_frame == animation->num_frames) {
227         next_frame = 0;
228     }
229     keyframe_animation_t temp_animation = *animation;
230     temp_animation.current_frame = next_frame;
231     temp_animation.time_left_in_frame = animation->frame_lengths[next_frame];
232     temp_animation.first_update_of_frame = true;
233     temp_animation.last_update_of_frame = false;
234     temp_animation.need_update  = false;
235     visualizer_state_t temp_state = *state;
236     (*temp_animation.frame_functions[next_frame])(&temp_animation, &temp_state);
237 }
238
239 // TODO: Optimize the stack size, this is probably way too big
240 static DECLARE_THREAD_STACK(visualizerThreadStack, 1024);
241 static DECLARE_THREAD_FUNCTION(visualizerThread, arg) {
242     (void)arg;
243
244     GListener event_listener;
245     geventListenerInit(&event_listener);
246     geventAttachSource(&event_listener, (GSourceHandle)&current_status, 0);
247
248     visualizer_keyboard_status_t initial_status = {
249         .default_layer = 0xFFFFFFFF,
250         .layer = 0xFFFFFFFF,
251         .mods = 0xFF,
252         .leds = 0xFFFFFFFF,
253         .suspended = false,
254 #ifdef VISUALIZER_USER_DATA_SIZE
255         .user_data = {0},
256 #endif
257     };
258
259     visualizer_state_t state = {
260         .status = initial_status,
261         .current_lcd_color = 0,
262 #ifdef LCD_ENABLE
263         .font_fixed5x8 = gdispOpenFont("fixed_5x8"),
264         .font_dejavusansbold12 = gdispOpenFont("DejaVuSansBold12")
265 #endif
266     };
267     initialize_user_visualizer(&state);
268     state.prev_lcd_color = state.current_lcd_color;
269
270 #ifdef LCD_BACKLIGHT_ENABLE
271     lcd_backlight_color(
272             LCD_HUE(state.current_lcd_color),
273             LCD_SAT(state.current_lcd_color),
274             LCD_INT(state.current_lcd_color));
275 #endif
276
277     systemticks_t sleep_time = TIME_INFINITE;
278     systemticks_t current_time = gfxSystemTicks();
279     bool force_update = true;
280
281     while(true) {
282         systemticks_t new_time = gfxSystemTicks();
283         systemticks_t delta = new_time - current_time;
284         current_time = new_time;
285         bool enabled = visualizer_enabled;
286         if (force_update || !same_status(&state.status, &current_status)) {
287             force_update = false;
288     #if BACKLIGHT_ENABLE
289             if(current_status.backlight_level != state.status.backlight_level) {
290                 if (current_status.backlight_level != 0) {
291                     gdispGSetPowerMode(LED_DISPLAY, powerOn);
292                     uint16_t percent = (uint16_t)current_status.backlight_level * 100 / BACKLIGHT_LEVELS;
293                     gdispGSetBacklight(LED_DISPLAY, percent);
294                 }
295                 else {
296                     gdispGSetPowerMode(LED_DISPLAY, powerOff);
297                 }
298             }
299     #endif
300             if (visualizer_enabled) {
301                 if (current_status.suspended) {
302                     stop_all_keyframe_animations();
303                     visualizer_enabled = false;
304                     state.status = current_status;
305                     user_visualizer_suspend(&state);
306                 }
307                 else {
308                     visualizer_keyboard_status_t prev_status = state.status;
309                     state.status = current_status;
310                     update_user_visualizer_state(&state, &prev_status);
311                 }
312                 state.prev_lcd_color = state.current_lcd_color;
313             }
314         }
315         if (!enabled && state.status.suspended && current_status.suspended == false) {
316             // Setting the status to the initial status will force an update
317             // when the visualizer is enabled again
318             state.status = initial_status;
319             state.status.suspended = false;
320             stop_all_keyframe_animations();
321             user_visualizer_resume(&state);
322             state.prev_lcd_color = state.current_lcd_color;
323         }
324         sleep_time = TIME_INFINITE;
325         for (int i=0;i<MAX_SIMULTANEOUS_ANIMATIONS;i++) {
326             if (animations[i]) {
327                 update_keyframe_animation(animations[i], &state, delta, &sleep_time);
328             }
329         }
330 #ifdef BACKLIGHT_ENABLE
331         gdispGFlush(LED_DISPLAY);
332 #endif
333
334 #ifdef LCD_ENABLE
335         gdispGFlush(LCD_DISPLAY);
336 #endif
337
338 #ifdef EMULATOR
339         draw_emulator();
340 #endif
341         // Enable the visualizer when the startup or the suspend animation has finished
342         if (!visualizer_enabled && state.status.suspended == false && get_num_running_animations() == 0) {
343             visualizer_enabled = true;
344             force_update = true;
345             sleep_time = 0;
346         }
347
348         systemticks_t after_update = gfxSystemTicks();
349         unsigned update_delta = after_update - current_time;
350         if (sleep_time != TIME_INFINITE) {
351             if (sleep_time > update_delta) {
352                 sleep_time -= update_delta;
353             }
354             else {
355                 sleep_time = 0;
356             }
357         }
358         dprintf("Update took %d, last delta %d, sleep_time %d\n", update_delta, delta, sleep_time);
359 #ifdef PROTOCOL_CHIBIOS
360         // The gEventWait function really takes milliseconds, even if the documentation says ticks.
361         // Unfortunately there's no generic ugfx conversion from system time to milliseconds,
362         // so let's do it in a platform dependent way.
363
364         // On windows the system ticks is the same as milliseconds anyway
365         if (sleep_time != TIME_INFINITE) {
366             sleep_time = ST2MS(sleep_time);
367         }
368 #endif
369         geventEventWait(&event_listener, sleep_time);
370     }
371 #ifdef LCD_ENABLE
372     gdispCloseFont(state.font_fixed5x8);
373     gdispCloseFont(state.font_dejavusansbold12);
374 #endif
375
376     return 0;
377 }
378
379 void visualizer_init(void) {
380     gfxInit();
381
382 #ifdef LCD_BACKLIGHT_ENABLE
383     lcd_backlight_init();
384 #endif
385
386 #ifdef SERIAL_LINK_ENABLE
387     add_remote_objects(remote_objects, sizeof(remote_objects) / sizeof(remote_object_t*) );
388 #endif
389
390 #ifdef LCD_ENABLE
391     LCD_DISPLAY = get_lcd_display();
392 #endif
393 #ifdef BACKLIGHT_ENABLE
394     LED_DISPLAY = get_led_display();
395 #endif
396
397     // We are using a low priority thread, the idea is to have it run only
398     // when the main thread is sleeping during the matrix scanning
399     gfxThreadCreate(visualizerThreadStack, sizeof(visualizerThreadStack),
400                               VISUALIZER_THREAD_PRIORITY, visualizerThread, NULL);
401 }
402
403 void update_status(bool changed) {
404     if (changed) {
405         GSourceListener* listener = geventGetSourceListener((GSourceHandle)&current_status, NULL);
406         if (listener) {
407             geventSendEvent(listener);
408         }
409     }
410 #ifdef SERIAL_LINK_ENABLE
411     static systime_t last_update = 0;
412     systime_t current_update = chVTGetSystemTimeX();
413     systime_t delta = current_update - last_update;
414     if (changed || delta > MS2ST(10)) {
415         last_update = current_update;
416         visualizer_keyboard_status_t* r = begin_write_current_status();
417         *r = current_status;
418         end_write_current_status();
419     }
420 #endif
421 }
422
423 uint8_t visualizer_get_mods() {
424   uint8_t mods = get_mods();
425
426 #ifndef NO_ACTION_ONESHOT
427   if (!has_oneshot_mods_timed_out()) {
428     mods |= get_oneshot_mods();
429   }
430 #endif  
431   return mods;
432 }
433
434 #ifdef VISUALIZER_USER_DATA_SIZE
435 void visualizer_set_user_data(void* u) {
436     memcpy(user_data, u, VISUALIZER_USER_DATA_SIZE);
437 }
438 #endif
439
440 void visualizer_update(uint32_t default_state, uint32_t state, uint8_t mods, uint32_t leds) {
441     // Note that there's a small race condition here, the thread could read
442     // a state where one of these are set but not the other. But this should
443     // not really matter as it will be fixed during the next loop step.
444     // Alternatively a mutex could be used instead of the volatile variables
445
446     bool changed = false;
447 #ifdef SERIAL_LINK_ENABLE
448     if (is_serial_link_connected ()) {
449         visualizer_keyboard_status_t* new_status = read_current_status();
450         if (new_status) {
451             if (!same_status(&current_status, new_status)) {
452                 changed = true;
453                 current_status = *new_status;
454             }
455         }
456     }
457     else {
458 #else
459    {
460 #endif
461         visualizer_keyboard_status_t new_status = {
462             .layer = state,
463             .default_layer = default_state,
464             .mods = mods,
465             .leds = leds,
466 #ifdef BACKLIGHT_ENABLE
467             .backlight_level = current_status.backlight_level,
468 #endif
469             .suspended = current_status.suspended,
470         };
471 #ifdef VISUALIZER_USER_DATA_SIZE
472        memcpy(new_status.user_data, user_data, VISUALIZER_USER_DATA_SIZE);
473 #endif
474         if (!same_status(&current_status, &new_status)) {
475             changed = true;
476             current_status = new_status;
477         }
478     }
479     update_status(changed);
480 }
481
482 void visualizer_suspend(void) {
483     current_status.suspended = true;
484     update_status(true);
485 }
486
487 void visualizer_resume(void) {
488     current_status.suspended = false;
489     update_status(true);
490 }
491
492 #ifdef BACKLIGHT_ENABLE
493 void backlight_set(uint8_t level) {
494     current_status.backlight_level = level;
495     update_status(true);
496 }
497 #endif