From 315edb48265e6baedd07f34c9e6e323d28814b4e Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sat, 12 Mar 2016 19:42:57 +0200 Subject: [PATCH] Add serial link support for visualizer --- visualizer.c | 63 +++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 55 insertions(+), 8 deletions(-) diff --git a/visualizer.c b/visualizer.c index 402bbd151..ca7bcb776 100644 --- a/visualizer.c +++ b/visualizer.c @@ -42,6 +42,11 @@ SOFTWARE. #include "nodebug.h" #endif +#ifdef USE_SERIAL_LINK +#include "serial_link/protocol/transport.h" +#include "serial_link/system/driver.h" +#endif + static visualizer_keyboard_status_t current_status = { .layer = 0xFFFFFFFF, @@ -59,6 +64,16 @@ static bool visualizer_enabled = false; #define MAX_SIMULTANEOUS_ANIMATIONS 4 static keyframe_animation_t* animations[MAX_SIMULTANEOUS_ANIMATIONS] = {}; +#ifdef USE_SERIAL_LINK +MASTER_TO_ALL_SLAVES_OBJECT(current_status, visualizer_keyboard_status_t); + +static remote_object_t* remote_objects[] = { + REMOTE_OBJECT(current_status), +}; + +#endif + + void start_keyframe_animation(keyframe_animation_t* animation) { animation->current_frame = -1; animation->time_left_in_frame = 0; @@ -328,6 +343,10 @@ void visualizer_init(void) { #ifdef LCD_BACKLIGHT_ENABLE lcd_backlight_init(); #endif + +#ifdef USE_SERIAL_LINK + add_remote_objects(remote_objects, sizeof(remote_objects) / sizeof(remote_object_t*) ); +#endif // We are using a low priority thread, the idea is to have it run only // when the main thread is sleeping during the matrix scanning chEvtObjectInit(&layer_changed_event); @@ -340,17 +359,45 @@ void visualizer_set_state(uint32_t default_state, uint32_t state, uint32_t leds) // a state where one of these are set but not the other. But this should // not really matter as it will be fixed during the next loop step. // Alternatively a mutex could be used instead of the volatile variables + bool changed = false; - visualizer_keyboard_status_t new_status = { - .layer = state, - .default_layer = default_state, - .leds = leds, - }; - if (!same_status(¤t_status, &new_status)) { - changed = true; +#ifdef USE_SERIAL_LINK + if (is_serial_link_connected ()) { + visualizer_keyboard_status_t* new_status = read_current_status(); + if (new_status) { + if (!same_status(¤t_status, new_status)) { + changed = true; + current_status = *new_status; + } + } + } + else { +#else + { +#endif + visualizer_keyboard_status_t new_status = { + .layer = state, + .default_layer = default_state, + .leds = leds, + }; + if (!same_status(¤t_status, &new_status)) { + changed = true; + current_status = new_status; + } } - current_status = new_status; if (changed) { chEvtBroadcast(&layer_changed_event); + } +#ifdef USE_SERIAL_LINK + static systime_t last_update = 0; + systime_t current_update = chVTGetSystemTimeX(); + systime_t delta = current_update - last_update; + if (changed || delta > MS2ST(10)) { + last_update = current_update; + visualizer_keyboard_status_t* r = begin_write_current_status(); + *r = current_status; + end_write_current_status(); + } +#endif }