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 #define LED_STATE_STRING_SIZE sizeof("NUM CAPS SCRL COMP KANA")
369 static void get_led_state_string(char* output, visualizer_state_t* state) {
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);
395 bool keyframe_display_led_states(keyframe_animation_t* animation, visualizer_state_t* state)
398 char output[LED_STATE_STRING_SIZE];
399 get_led_state_string(output, state);
401 gdispDrawString(0, 10, output, state->font_dejavusansbold12, Black);
406 bool keyframe_display_layer_and_led_states(keyframe_animation_t* animation, visualizer_state_t* state) {
410 if (state->status.leds) {
411 char output[LED_STATE_STRING_SIZE];
412 get_led_state_string(output, state);
413 gdispDrawString(0, 1, output, state->font_dejavusansbold12, Black);
416 gdispDrawString(0, y, state->layer_text, state->font_dejavusansbold12, Black);
423 bool keyframe_disable_lcd_and_backlight(keyframe_animation_t* animation, visualizer_state_t* state) {
427 gdispSetPowerMode(powerOff);
429 #ifdef LCD_BACKLIGHT_ENABLE
430 lcd_backlight_hal_color(0, 0, 0);
435 bool keyframe_enable_lcd_and_backlight(keyframe_animation_t* animation, visualizer_state_t* state) {
439 gdispSetPowerMode(powerOn);
444 bool enable_visualization(keyframe_animation_t* animation, visualizer_state_t* state) {
447 dprint("User visualizer inited\n");
448 visualizer_enabled = true;
452 // TODO: Optimize the stack size, this is probably way too big
453 static DECLARE_THREAD_STACK(visualizerThreadStack, 1024);
454 static DECLARE_THREAD_FUNCTION(visualizerThread, arg) {
457 GListener event_listener;
458 geventListenerInit(&event_listener);
459 geventAttachSource(&event_listener, (GSourceHandle)¤t_status, 0);
461 visualizer_keyboard_status_t initial_status = {
462 .default_layer = 0xFFFFFFFF,
467 #ifdef VISUALIZER_USER_DATA_SIZE
472 visualizer_state_t state = {
473 .status = initial_status,
474 .current_lcd_color = 0,
476 .font_fixed5x8 = gdispOpenFont("fixed_5x8"),
477 .font_dejavusansbold12 = gdispOpenFont("DejaVuSansBold12")
480 initialize_user_visualizer(&state);
481 state.prev_lcd_color = state.current_lcd_color;
483 #ifdef LCD_BACKLIGHT_ENABLE
485 LCD_HUE(state.current_lcd_color),
486 LCD_SAT(state.current_lcd_color),
487 LCD_INT(state.current_lcd_color));
490 systemticks_t sleep_time = TIME_INFINITE;
491 systemticks_t current_time = gfxSystemTicks();
494 systemticks_t new_time = gfxSystemTicks();
495 systemticks_t delta = new_time - current_time;
496 current_time = new_time;
497 bool enabled = visualizer_enabled;
498 if (!same_status(&state.status, ¤t_status)) {
499 if (visualizer_enabled) {
500 if (current_status.suspended) {
501 stop_all_keyframe_animations();
502 visualizer_enabled = false;
503 state.status = current_status;
504 user_visualizer_suspend(&state);
507 visualizer_keyboard_status_t prev_status = state.status;
508 state.status = current_status;
509 update_user_visualizer_state(&state, &prev_status);
511 state.prev_lcd_color = state.current_lcd_color;
514 if (!enabled && state.status.suspended && current_status.suspended == false) {
515 // Setting the status to the initial status will force an update
516 // when the visualizer is enabled again
517 state.status = initial_status;
518 state.status.suspended = false;
519 stop_all_keyframe_animations();
520 user_visualizer_resume(&state);
521 state.prev_lcd_color = state.current_lcd_color;
523 sleep_time = TIME_INFINITE;
524 for (int i=0;i<MAX_SIMULTANEOUS_ANIMATIONS;i++) {
526 update_keyframe_animation(animations[i], &state, delta, &sleep_time);
530 gdispGFlush(LED_DISPLAY);
536 // The animation can enable the visualizer
537 // And we might need to update the state when that happens
539 if (enabled != visualizer_enabled) {
543 systemticks_t after_update = gfxSystemTicks();
544 unsigned update_delta = after_update - current_time;
545 if (sleep_time != TIME_INFINITE) {
546 if (sleep_time > update_delta) {
547 sleep_time -= update_delta;
553 dprintf("Update took %d, last delta %d, sleep_time %d\n", update_delta, delta, sleep_time);
554 #ifdef PROTOCOL_CHIBIOS
555 // The gEventWait function really takes milliseconds, even if the documentation says ticks.
556 // Unfortunately there's no generic ugfx conversion from system time to milliseconds,
557 // so let's do it in a platform dependent way.
559 // On windows the system ticks is the same as milliseconds anyway
560 if (sleep_time != TIME_INFINITE) {
561 sleep_time = ST2MS(sleep_time);
564 geventEventWait(&event_listener, sleep_time);
567 gdispCloseFont(state.font_fixed5x8);
568 gdispCloseFont(state.font_dejavusansbold12);
574 void visualizer_init(void) {
577 #ifdef LCD_BACKLIGHT_ENABLE
578 lcd_backlight_init();
581 #ifdef SERIAL_LINK_ENABLE
582 add_remote_objects(remote_objects, sizeof(remote_objects) / sizeof(remote_object_t*) );
586 LCD_DISPLAY = get_lcd_display();
589 LED_DISPLAY = get_led_display();
592 // We are using a low priority thread, the idea is to have it run only
593 // when the main thread is sleeping during the matrix scanning
594 gfxThreadCreate(visualizerThreadStack, sizeof(visualizerThreadStack),
595 VISUALIZER_THREAD_PRIORITY, visualizerThread, NULL);
598 void update_status(bool changed) {
600 GSourceListener* listener = geventGetSourceListener((GSourceHandle)¤t_status, NULL);
602 geventSendEvent(listener);
605 #ifdef SERIAL_LINK_ENABLE
606 static systime_t last_update = 0;
607 systime_t current_update = chVTGetSystemTimeX();
608 systime_t delta = current_update - last_update;
609 if (changed || delta > MS2ST(10)) {
610 last_update = current_update;
611 visualizer_keyboard_status_t* r = begin_write_current_status();
613 end_write_current_status();
618 uint8_t visualizer_get_mods() {
619 uint8_t mods = get_mods();
621 #ifndef NO_ACTION_ONESHOT
622 if (!has_oneshot_mods_timed_out()) {
623 mods |= get_oneshot_mods();
629 #ifdef VISUALIZER_USER_DATA_SIZE
630 void visualizer_set_user_data(void* u) {
631 memcpy(user_data, u, VISUALIZER_USER_DATA_SIZE);
635 void visualizer_update(uint32_t default_state, uint32_t state, uint8_t mods, uint32_t leds) {
636 // Note that there's a small race condition here, the thread could read
637 // a state where one of these are set but not the other. But this should
638 // not really matter as it will be fixed during the next loop step.
639 // Alternatively a mutex could be used instead of the volatile variables
641 bool changed = false;
642 #ifdef SERIAL_LINK_ENABLE
643 if (is_serial_link_connected ()) {
644 visualizer_keyboard_status_t* new_status = read_current_status();
646 if (!same_status(¤t_status, new_status)) {
648 current_status = *new_status;
656 visualizer_keyboard_status_t new_status = {
658 .default_layer = default_state,
661 .suspended = current_status.suspended,
663 #ifdef VISUALIZER_USER_DATA_SIZE
664 memcpy(new_status.user_data, user_data, VISUALIZER_USER_DATA_SIZE);
666 if (!same_status(¤t_status, &new_status)) {
668 current_status = new_status;
671 update_status(changed);
674 void visualizer_suspend(void) {
675 current_status.suspended = true;
679 void visualizer_resume(void) {
680 current_status.suspended = false;