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