4 Copyright (c) 2016 Fred Sundvik
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:
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
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
25 #include "visualizer.h"
28 #ifdef PROTOCOL_CHIBIOS
34 #ifdef LCD_BACKLIGHT_ENABLE
35 #include "lcd_backlight.h"
38 //#define DEBUG_VISUALIZER
40 #ifdef DEBUG_VISUALIZER
46 #ifdef SERIAL_LINK_ENABLE
47 #include "serial_link/protocol/transport.h"
48 #include "serial_link/system/serial_link.h"
51 // Define this in config.h
52 #ifndef VISUALIZER_THREAD_PRIORITY
53 #define "Visualizer thread priority not defined"
57 #include "action_util.h"
61 static visualizer_keyboard_status_t current_status = {
63 .default_layer = 0xFFFFFFFF,
67 #ifdef VISUALIZER_USER_DATA_SIZE
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 VISUALIZER_USER_DATA_SIZE
79 && memcmp(status1->user_data, status2->user_data, VISUALIZER_USER_DATA_SIZE) == 0
84 static bool visualizer_enabled = false;
86 #ifdef VISUALIZER_USER_DATA_SIZE
87 static uint8_t user_data[VISUALIZER_USER_DATA_SIZE];
90 #define MAX_SIMULTANEOUS_ANIMATIONS 4
91 static keyframe_animation_t* animations[MAX_SIMULTANEOUS_ANIMATIONS] = {};
93 #ifdef SERIAL_LINK_ENABLE
94 MASTER_TO_ALL_SLAVES_OBJECT(current_status, visualizer_keyboard_status_t);
96 static remote_object_t* remote_objects[] = {
97 REMOTE_OBJECT(current_status),
102 GDisplay* LCD_DISPLAY = 0;
103 GDisplay* LED_DISPLAY = 0;
105 __attribute__((weak))
106 GDisplay* get_lcd_display(void) {
107 return gdispGetDisplay(0);
110 __attribute__((weak))
111 GDisplay* get_led_display(void) {
112 return gdispGetDisplay(1);
115 void start_keyframe_animation(keyframe_animation_t* animation) {
116 animation->current_frame = -1;
117 animation->time_left_in_frame = 0;
118 animation->need_update = true;
120 for (int i=0;i<MAX_SIMULTANEOUS_ANIMATIONS;i++) {
121 if (animations[i] == animation) {
124 if (free_index == -1 && animations[i] == NULL) {
128 if (free_index!=-1) {
129 animations[free_index] = animation;
133 void stop_keyframe_animation(keyframe_animation_t* animation) {
134 animation->current_frame = animation->num_frames;
135 animation->time_left_in_frame = 0;
136 animation->need_update = true;
137 animation->first_update_of_frame = false;
138 animation->last_update_of_frame = false;
139 for (int i=0;i<MAX_SIMULTANEOUS_ANIMATIONS;i++) {
140 if (animations[i] == animation) {
141 animations[i] = NULL;
147 void stop_all_keyframe_animations(void) {
148 for (int i=0;i<MAX_SIMULTANEOUS_ANIMATIONS;i++) {
150 animations[i]->current_frame = animations[i]->num_frames;
151 animations[i]->time_left_in_frame = 0;
152 animations[i]->need_update = true;
153 animations[i]->first_update_of_frame = false;
154 animations[i]->last_update_of_frame = false;
155 animations[i] = NULL;
160 static bool update_keyframe_animation(keyframe_animation_t* animation, visualizer_state_t* state, systemticks_t delta, systemticks_t* sleep_time) {
161 // TODO: Clean up this messy code
162 dprintf("Animation frame%d, left %d, delta %d\n", animation->current_frame,
163 animation->time_left_in_frame, delta);
164 if (animation->current_frame == animation->num_frames) {
165 animation->need_update = false;
168 if (animation->current_frame == -1) {
169 animation->current_frame = 0;
170 animation->time_left_in_frame = animation->frame_lengths[0];
171 animation->need_update = true;
172 animation->first_update_of_frame = true;
174 animation->time_left_in_frame -= delta;
175 while (animation->time_left_in_frame <= 0) {
176 int left = animation->time_left_in_frame;
177 if (animation->need_update) {
178 animation->time_left_in_frame = 0;
179 animation->last_update_of_frame = true;
180 (*animation->frame_functions[animation->current_frame])(animation, state);
181 animation->last_update_of_frame = false;
183 animation->current_frame++;
184 animation->need_update = true;
185 animation->first_update_of_frame = true;
186 if (animation->current_frame == animation->num_frames) {
187 if (animation->loop) {
188 animation->current_frame = 0;
191 stop_keyframe_animation(animation);
196 animation->time_left_in_frame = animation->frame_lengths[animation->current_frame];
197 animation->time_left_in_frame -= delta;
200 if (animation->need_update) {
201 animation->need_update = (*animation->frame_functions[animation->current_frame])(animation, state);
202 animation->first_update_of_frame = false;
205 systemticks_t wanted_sleep = animation->need_update ? gfxMillisecondsToTicks(10) : (unsigned)animation->time_left_in_frame;
206 if (wanted_sleep < *sleep_time) {
207 *sleep_time = wanted_sleep;
213 void run_next_keyframe(keyframe_animation_t* animation, visualizer_state_t* state) {
214 int next_frame = animation->current_frame + 1;
215 if (next_frame == animation->num_frames) {
218 keyframe_animation_t temp_animation = *animation;
219 temp_animation.current_frame = next_frame;
220 temp_animation.time_left_in_frame = animation->frame_lengths[next_frame];
221 temp_animation.first_update_of_frame = true;
222 temp_animation.last_update_of_frame = false;
223 temp_animation.need_update = false;
224 visualizer_state_t temp_state = *state;
225 (*temp_animation.frame_functions[next_frame])(&temp_animation, &temp_state);
228 bool keyframe_no_operation(keyframe_animation_t* animation, visualizer_state_t* state) {
234 #ifdef LCD_BACKLIGHT_ENABLE
235 bool keyframe_animate_backlight_color(keyframe_animation_t* animation, visualizer_state_t* state) {
236 int frame_length = animation->frame_lengths[animation->current_frame];
237 int current_pos = frame_length - animation->time_left_in_frame;
238 uint8_t t_h = LCD_HUE(state->target_lcd_color);
239 uint8_t t_s = LCD_SAT(state->target_lcd_color);
240 uint8_t t_i = LCD_INT(state->target_lcd_color);
241 uint8_t p_h = LCD_HUE(state->prev_lcd_color);
242 uint8_t p_s = LCD_SAT(state->prev_lcd_color);
243 uint8_t p_i = LCD_INT(state->prev_lcd_color);
245 uint8_t d_h1 = t_h - p_h; //Modulo arithmetic since we want to wrap around
246 int d_h2 = t_h - p_h;
247 // Chose the shortest way around
248 int d_h = abs(d_h2) < d_h1 ? d_h2 : d_h1;
252 int hue = (d_h * current_pos) / frame_length;
253 int sat = (d_s * current_pos) / frame_length;
254 int intensity = (d_i * current_pos) / frame_length;
255 //dprintf("%X -> %X = %X\n", p_h, t_h, hue);
259 state->current_lcd_color = LCD_COLOR(hue, sat, intensity);
261 LCD_HUE(state->current_lcd_color),
262 LCD_SAT(state->current_lcd_color),
263 LCD_INT(state->current_lcd_color));
268 bool keyframe_set_backlight_color(keyframe_animation_t* animation, visualizer_state_t* state) {
270 state->prev_lcd_color = state->target_lcd_color;
271 state->current_lcd_color = state->target_lcd_color;
273 LCD_HUE(state->current_lcd_color),
274 LCD_SAT(state->current_lcd_color),
275 LCD_INT(state->current_lcd_color));
278 #endif // LCD_BACKLIGHT_ENABLE
281 bool keyframe_display_layer_text(keyframe_animation_t* animation, visualizer_state_t* state) {
284 gdispDrawString(0, 10, state->layer_text, state->font_dejavusansbold12, Black);
289 static void format_layer_bitmap_string(uint16_t default_layer, uint16_t layer, char* buffer) {
290 for (int i=0; i<16;i++)
292 uint32_t mask = (1u << i);
293 if (default_layer & mask) {
299 } else if (layer & mask) {
306 if (i==3 || i==7 || i==11) {
314 bool keyframe_display_layer_bitmap(keyframe_animation_t* animation, visualizer_state_t* state) {
316 const char* layer_help = "1=On D=Default B=Both";
317 char layer_buffer[16 + 4]; // 3 spaces and one null terminator
319 gdispDrawString(0, 0, layer_help, state->font_fixed5x8, Black);
320 format_layer_bitmap_string(state->status.default_layer, state->status.layer, layer_buffer);
321 gdispDrawString(0, 10, layer_buffer, state->font_fixed5x8, Black);
322 format_layer_bitmap_string(state->status.default_layer >> 16, state->status.layer >> 16, layer_buffer);
323 gdispDrawString(0, 20, layer_buffer, state->font_fixed5x8, Black);
328 static void format_mods_bitmap_string(uint8_t mods, char* buffer) {
332 for (int i = 0; i<8; i++)
334 uint32_t mask = (1u << i);
350 bool keyframe_display_mods_bitmap(keyframe_animation_t* animation, visualizer_state_t* state) {
353 const char* title = "Modifier states";
354 const char* mods_header = " CSAG CSAG ";
355 char status_buffer[12];
358 gdispDrawString(0, 0, title, state->font_fixed5x8, Black);
359 gdispDrawString(0, 10, mods_header, state->font_fixed5x8, Black);
360 format_mods_bitmap_string(state->status.mods, status_buffer);
361 gdispDrawString(0, 20, status_buffer, state->font_fixed5x8, Black);
367 bool keyframe_display_led_states(keyframe_animation_t* animation, visualizer_state_t* state)
369 char output[sizeof("NUM CAPS SCRL COMP KANA")];
372 if (state->status.leds & (1u << USB_LED_NUM_LOCK)) {
373 memcpy(output + pos, "NUM ", 4);
376 if (state->status.leds & (1u << USB_LED_CAPS_LOCK)) {
377 memcpy(output + pos, "CAPS ", 5);
380 if (state->status.leds & (1u << USB_LED_SCROLL_LOCK)) {
381 memcpy(output + pos, "SCRL ", 5);
384 if (state->status.leds & (1u << USB_LED_COMPOSE)) {
385 memcpy(output + pos, "COMP ", 5);
388 if (state->status.leds & (1u << USB_LED_KANA)) {
389 memcpy(output + pos, "KANA ", 5);
394 gdispDrawString(0, 10, output, state->font_dejavusansbold12, Black);
401 bool keyframe_disable_lcd_and_backlight(keyframe_animation_t* animation, visualizer_state_t* state) {
405 gdispSetPowerMode(powerOff);
407 #ifdef LCD_BACKLIGHT_ENABLE
408 lcd_backlight_hal_color(0, 0, 0);
413 bool keyframe_enable_lcd_and_backlight(keyframe_animation_t* animation, visualizer_state_t* state) {
417 gdispSetPowerMode(powerOn);
422 bool enable_visualization(keyframe_animation_t* animation, visualizer_state_t* state) {
425 dprint("User visualizer inited\n");
426 visualizer_enabled = true;
430 // TODO: Optimize the stack size, this is probably way too big
431 static DECLARE_THREAD_STACK(visualizerThreadStack, 1024);
432 static DECLARE_THREAD_FUNCTION(visualizerThread, arg) {
435 GListener event_listener;
436 geventListenerInit(&event_listener);
437 geventAttachSource(&event_listener, (GSourceHandle)¤t_status, 0);
439 visualizer_keyboard_status_t initial_status = {
440 .default_layer = 0xFFFFFFFF,
445 #ifdef VISUALIZER_USER_DATA_SIZE
450 visualizer_state_t state = {
451 .status = initial_status,
452 .current_lcd_color = 0,
454 .font_fixed5x8 = gdispOpenFont("fixed_5x8"),
455 .font_dejavusansbold12 = gdispOpenFont("DejaVuSansBold12")
458 initialize_user_visualizer(&state);
459 state.prev_lcd_color = state.current_lcd_color;
461 #ifdef LCD_BACKLIGHT_ENABLE
463 LCD_HUE(state.current_lcd_color),
464 LCD_SAT(state.current_lcd_color),
465 LCD_INT(state.current_lcd_color));
468 systemticks_t sleep_time = TIME_INFINITE;
469 systemticks_t current_time = gfxSystemTicks();
472 systemticks_t new_time = gfxSystemTicks();
473 systemticks_t delta = new_time - current_time;
474 current_time = new_time;
475 bool enabled = visualizer_enabled;
476 if (!same_status(&state.status, ¤t_status)) {
477 if (visualizer_enabled) {
478 if (current_status.suspended) {
479 stop_all_keyframe_animations();
480 visualizer_enabled = false;
481 state.status = current_status;
482 user_visualizer_suspend(&state);
485 visualizer_keyboard_status_t prev_status = state.status;
486 state.status = current_status;
487 update_user_visualizer_state(&state, &prev_status);
489 state.prev_lcd_color = state.current_lcd_color;
492 if (!enabled && state.status.suspended && current_status.suspended == false) {
493 // Setting the status to the initial status will force an update
494 // when the visualizer is enabled again
495 state.status = initial_status;
496 state.status.suspended = false;
497 stop_all_keyframe_animations();
498 user_visualizer_resume(&state);
499 state.prev_lcd_color = state.current_lcd_color;
501 sleep_time = TIME_INFINITE;
502 for (int i=0;i<MAX_SIMULTANEOUS_ANIMATIONS;i++) {
504 update_keyframe_animation(animations[i], &state, delta, &sleep_time);
508 gdispGFlush(LED_DISPLAY);
514 // The animation can enable the visualizer
515 // And we might need to update the state when that happens
517 if (enabled != visualizer_enabled) {
521 systemticks_t after_update = gfxSystemTicks();
522 unsigned update_delta = after_update - current_time;
523 if (sleep_time != TIME_INFINITE) {
524 if (sleep_time > update_delta) {
525 sleep_time -= update_delta;
531 dprintf("Update took %d, last delta %d, sleep_time %d\n", update_delta, delta, sleep_time);
532 #ifdef PROTOCOL_CHIBIOS
533 // The gEventWait function really takes milliseconds, even if the documentation says ticks.
534 // Unfortunately there's no generic ugfx conversion from system time to milliseconds,
535 // so let's do it in a platform dependent way.
537 // On windows the system ticks is the same as milliseconds anyway
538 if (sleep_time != TIME_INFINITE) {
539 sleep_time = ST2MS(sleep_time);
542 geventEventWait(&event_listener, sleep_time);
545 gdispCloseFont(state.font_fixed5x8);
546 gdispCloseFont(state.font_dejavusansbold12);
552 void visualizer_init(void) {
555 #ifdef LCD_BACKLIGHT_ENABLE
556 lcd_backlight_init();
559 #ifdef SERIAL_LINK_ENABLE
560 add_remote_objects(remote_objects, sizeof(remote_objects) / sizeof(remote_object_t*) );
564 LCD_DISPLAY = get_lcd_display();
567 LED_DISPLAY = get_led_display();
570 // We are using a low priority thread, the idea is to have it run only
571 // when the main thread is sleeping during the matrix scanning
572 gfxThreadCreate(visualizerThreadStack, sizeof(visualizerThreadStack),
573 VISUALIZER_THREAD_PRIORITY, visualizerThread, NULL);
576 void update_status(bool changed) {
578 GSourceListener* listener = geventGetSourceListener((GSourceHandle)¤t_status, NULL);
580 geventSendEvent(listener);
583 #ifdef SERIAL_LINK_ENABLE
584 static systime_t last_update = 0;
585 systime_t current_update = chVTGetSystemTimeX();
586 systime_t delta = current_update - last_update;
587 if (changed || delta > MS2ST(10)) {
588 last_update = current_update;
589 visualizer_keyboard_status_t* r = begin_write_current_status();
591 end_write_current_status();
596 uint8_t visualizer_get_mods() {
597 uint8_t mods = get_mods();
599 #ifndef NO_ACTION_ONESHOT
600 if (!has_oneshot_mods_timed_out()) {
601 mods |= get_oneshot_mods();
607 #ifdef VISUALIZER_USER_DATA_SIZE
608 void visualizer_set_user_data(void* u) {
609 memcpy(user_data, u, VISUALIZER_USER_DATA_SIZE);
613 void visualizer_update(uint32_t default_state, uint32_t state, uint8_t mods, uint32_t leds) {
614 // Note that there's a small race condition here, the thread could read
615 // a state where one of these are set but not the other. But this should
616 // not really matter as it will be fixed during the next loop step.
617 // Alternatively a mutex could be used instead of the volatile variables
619 bool changed = false;
620 #ifdef SERIAL_LINK_ENABLE
621 if (is_serial_link_connected ()) {
622 visualizer_keyboard_status_t* new_status = read_current_status();
624 if (!same_status(¤t_status, new_status)) {
626 current_status = *new_status;
634 visualizer_keyboard_status_t new_status = {
636 .default_layer = default_state,
639 .suspended = current_status.suspended,
641 #ifdef VISUALIZER_USER_DATA_SIZE
642 memcpy(new_status.user_data, user_data, VISUALIZER_USER_DATA_SIZE);
644 if (!same_status(¤t_status, &new_status)) {
646 current_status = new_status;
649 update_status(changed);
652 void visualizer_suspend(void) {
653 current_status.suspended = true;
657 void visualizer_resume(void) {
658 current_status.suspended = false;