visualizer.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. /*
  2. The MIT License (MIT)
  3. Copyright (c) 2016 Fred Sundvik
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. */
  20. #include "visualizer.h"
  21. #include "config.h"
  22. #include <string.h>
  23. #ifdef PROTOCOL_CHIBIOS
  24. #include "ch.h"
  25. #endif
  26. #include "gfx.h"
  27. #ifdef LCD_BACKLIGHT_ENABLE
  28. #include "lcd_backlight.h"
  29. #endif
  30. //#define DEBUG_VISUALIZER
  31. #ifdef DEBUG_VISUALIZER
  32. #include "debug.h"
  33. #else
  34. #include "nodebug.h"
  35. #endif
  36. #ifdef SERIAL_LINK_ENABLE
  37. #include "serial_link/protocol/transport.h"
  38. #include "serial_link/system/serial_link.h"
  39. #endif
  40. // Define this in config.h
  41. #ifndef VISUALIZER_THREAD_PRIORITY
  42. #define "Visualizer thread priority not defined"
  43. #endif
  44. // mods status
  45. #include "action_util.h"
  46. #include "led.h"
  47. static visualizer_keyboard_status_t current_status = {
  48. .layer = 0xFFFFFFFF,
  49. .default_layer = 0xFFFFFFFF,
  50. .mods = 0xFF,
  51. .leds = 0xFFFFFFFF,
  52. .suspended = false,
  53. #ifdef VISUALIZER_USER_DATA_SIZE
  54. .user_data = {0}
  55. #endif
  56. };
  57. static bool same_status(visualizer_keyboard_status_t* status1, visualizer_keyboard_status_t* status2) {
  58. return status1->layer == status2->layer &&
  59. status1->default_layer == status2->default_layer &&
  60. status1->mods == status2->mods &&
  61. status1->leds == status2->leds &&
  62. status1->suspended == status2->suspended
  63. #ifdef VISUALIZER_USER_DATA_SIZE
  64. && memcmp(status1->user_data, status2->user_data, VISUALIZER_USER_DATA_SIZE) == 0
  65. #endif
  66. ;
  67. }
  68. static bool visualizer_enabled = false;
  69. #ifdef VISUALIZER_USER_DATA_SIZE
  70. static uint8_t user_data[VISUALIZER_USER_DATA_SIZE];
  71. #endif
  72. #define MAX_SIMULTANEOUS_ANIMATIONS 4
  73. static keyframe_animation_t* animations[MAX_SIMULTANEOUS_ANIMATIONS] = {};
  74. #ifdef SERIAL_LINK_ENABLE
  75. MASTER_TO_ALL_SLAVES_OBJECT(current_status, visualizer_keyboard_status_t);
  76. static remote_object_t* remote_objects[] = {
  77. REMOTE_OBJECT(current_status),
  78. };
  79. #endif
  80. GDisplay* LCD_DISPLAY = 0;
  81. GDisplay* LED_DISPLAY = 0;
  82. __attribute__((weak))
  83. GDisplay* get_lcd_display(void) {
  84. return gdispGetDisplay(0);
  85. }
  86. __attribute__((weak))
  87. GDisplay* get_led_display(void) {
  88. return gdispGetDisplay(1);
  89. }
  90. void start_keyframe_animation(keyframe_animation_t* animation) {
  91. animation->current_frame = -1;
  92. animation->time_left_in_frame = 0;
  93. animation->need_update = true;
  94. int free_index = -1;
  95. for (int i=0;i<MAX_SIMULTANEOUS_ANIMATIONS;i++) {
  96. if (animations[i] == animation) {
  97. return;
  98. }
  99. if (free_index == -1 && animations[i] == NULL) {
  100. free_index=i;
  101. }
  102. }
  103. if (free_index!=-1) {
  104. animations[free_index] = animation;
  105. }
  106. }
  107. void stop_keyframe_animation(keyframe_animation_t* animation) {
  108. animation->current_frame = animation->num_frames;
  109. animation->time_left_in_frame = 0;
  110. animation->need_update = true;
  111. animation->first_update_of_frame = false;
  112. animation->last_update_of_frame = false;
  113. for (int i=0;i<MAX_SIMULTANEOUS_ANIMATIONS;i++) {
  114. if (animations[i] == animation) {
  115. animations[i] = NULL;
  116. return;
  117. }
  118. }
  119. }
  120. void stop_all_keyframe_animations(void) {
  121. for (int i=0;i<MAX_SIMULTANEOUS_ANIMATIONS;i++) {
  122. if (animations[i]) {
  123. animations[i]->current_frame = animations[i]->num_frames;
  124. animations[i]->time_left_in_frame = 0;
  125. animations[i]->need_update = true;
  126. animations[i]->first_update_of_frame = false;
  127. animations[i]->last_update_of_frame = false;
  128. animations[i] = NULL;
  129. }
  130. }
  131. }
  132. static bool update_keyframe_animation(keyframe_animation_t* animation, visualizer_state_t* state, systemticks_t delta, systemticks_t* sleep_time) {
  133. // TODO: Clean up this messy code
  134. dprintf("Animation frame%d, left %d, delta %d\n", animation->current_frame,
  135. animation->time_left_in_frame, delta);
  136. if (animation->current_frame == animation->num_frames) {
  137. animation->need_update = false;
  138. return false;
  139. }
  140. if (animation->current_frame == -1) {
  141. animation->current_frame = 0;
  142. animation->time_left_in_frame = animation->frame_lengths[0];
  143. animation->need_update = true;
  144. animation->first_update_of_frame = true;
  145. } else {
  146. animation->time_left_in_frame -= delta;
  147. while (animation->time_left_in_frame <= 0) {
  148. int left = animation->time_left_in_frame;
  149. if (animation->need_update) {
  150. animation->time_left_in_frame = 0;
  151. animation->last_update_of_frame = true;
  152. (*animation->frame_functions[animation->current_frame])(animation, state);
  153. animation->last_update_of_frame = false;
  154. }
  155. animation->current_frame++;
  156. animation->need_update = true;
  157. animation->first_update_of_frame = true;
  158. if (animation->current_frame == animation->num_frames) {
  159. if (animation->loop) {
  160. animation->current_frame = 0;
  161. }
  162. else {
  163. stop_keyframe_animation(animation);
  164. return false;
  165. }
  166. }
  167. delta = -left;
  168. animation->time_left_in_frame = animation->frame_lengths[animation->current_frame];
  169. animation->time_left_in_frame -= delta;
  170. }
  171. }
  172. if (animation->need_update) {
  173. animation->need_update = (*animation->frame_functions[animation->current_frame])(animation, state);
  174. animation->first_update_of_frame = false;
  175. }
  176. systemticks_t wanted_sleep = animation->need_update ? gfxMillisecondsToTicks(10) : (unsigned)animation->time_left_in_frame;
  177. if (wanted_sleep < *sleep_time) {
  178. *sleep_time = wanted_sleep;
  179. }
  180. return true;
  181. }
  182. void run_next_keyframe(keyframe_animation_t* animation, visualizer_state_t* state) {
  183. int next_frame = animation->current_frame + 1;
  184. if (next_frame == animation->num_frames) {
  185. next_frame = 0;
  186. }
  187. keyframe_animation_t temp_animation = *animation;
  188. temp_animation.current_frame = next_frame;
  189. temp_animation.time_left_in_frame = animation->frame_lengths[next_frame];
  190. temp_animation.first_update_of_frame = true;
  191. temp_animation.last_update_of_frame = false;
  192. temp_animation.need_update = false;
  193. visualizer_state_t temp_state = *state;
  194. (*temp_animation.frame_functions[next_frame])(&temp_animation, &temp_state);
  195. }
  196. bool keyframe_no_operation(keyframe_animation_t* animation, visualizer_state_t* state) {
  197. (void)animation;
  198. (void)state;
  199. return false;
  200. }
  201. #ifdef LCD_BACKLIGHT_ENABLE
  202. bool keyframe_animate_backlight_color(keyframe_animation_t* animation, visualizer_state_t* state) {
  203. int frame_length = animation->frame_lengths[animation->current_frame];
  204. int current_pos = frame_length - animation->time_left_in_frame;
  205. uint8_t t_h = LCD_HUE(state->target_lcd_color);
  206. uint8_t t_s = LCD_SAT(state->target_lcd_color);
  207. uint8_t t_i = LCD_INT(state->target_lcd_color);
  208. uint8_t p_h = LCD_HUE(state->prev_lcd_color);
  209. uint8_t p_s = LCD_SAT(state->prev_lcd_color);
  210. uint8_t p_i = LCD_INT(state->prev_lcd_color);
  211. uint8_t d_h1 = t_h - p_h; //Modulo arithmetic since we want to wrap around
  212. int d_h2 = t_h - p_h;
  213. // Chose the shortest way around
  214. int d_h = abs(d_h2) < d_h1 ? d_h2 : d_h1;
  215. int d_s = t_s - p_s;
  216. int d_i = t_i - p_i;
  217. int hue = (d_h * current_pos) / frame_length;
  218. int sat = (d_s * current_pos) / frame_length;
  219. int intensity = (d_i * current_pos) / frame_length;
  220. //dprintf("%X -> %X = %X\n", p_h, t_h, hue);
  221. hue += p_h;
  222. sat += p_s;
  223. intensity += p_i;
  224. state->current_lcd_color = LCD_COLOR(hue, sat, intensity);
  225. lcd_backlight_color(
  226. LCD_HUE(state->current_lcd_color),
  227. LCD_SAT(state->current_lcd_color),
  228. LCD_INT(state->current_lcd_color));
  229. return true;
  230. }
  231. bool keyframe_set_backlight_color(keyframe_animation_t* animation, visualizer_state_t* state) {
  232. (void)animation;
  233. state->prev_lcd_color = state->target_lcd_color;
  234. state->current_lcd_color = state->target_lcd_color;
  235. lcd_backlight_color(
  236. LCD_HUE(state->current_lcd_color),
  237. LCD_SAT(state->current_lcd_color),
  238. LCD_INT(state->current_lcd_color));
  239. return false;
  240. }
  241. #endif // LCD_BACKLIGHT_ENABLE
  242. #ifdef LCD_ENABLE
  243. bool keyframe_display_layer_text(keyframe_animation_t* animation, visualizer_state_t* state) {
  244. (void)animation;
  245. gdispClear(White);
  246. gdispDrawString(0, 10, state->layer_text, state->font_dejavusansbold12, Black);
  247. gdispFlush();
  248. return false;
  249. }
  250. static void format_layer_bitmap_string(uint16_t default_layer, uint16_t layer, char* buffer) {
  251. for (int i=0; i<16;i++)
  252. {
  253. uint32_t mask = (1u << i);
  254. if (default_layer & mask) {
  255. if (layer & mask) {
  256. *buffer = 'B';
  257. } else {
  258. *buffer = 'D';
  259. }
  260. } else if (layer & mask) {
  261. *buffer = '1';
  262. } else {
  263. *buffer = '0';
  264. }
  265. ++buffer;
  266. if (i==3 || i==7 || i==11) {
  267. *buffer = ' ';
  268. ++buffer;
  269. }
  270. }
  271. *buffer = 0;
  272. }
  273. bool keyframe_display_layer_bitmap(keyframe_animation_t* animation, visualizer_state_t* state) {
  274. (void)animation;
  275. const char* layer_help = "1=On D=Default B=Both";
  276. char layer_buffer[16 + 4]; // 3 spaces and one null terminator
  277. gdispClear(White);
  278. gdispDrawString(0, 0, layer_help, state->font_fixed5x8, Black);
  279. format_layer_bitmap_string(state->status.default_layer, state->status.layer, layer_buffer);
  280. gdispDrawString(0, 10, layer_buffer, state->font_fixed5x8, Black);
  281. format_layer_bitmap_string(state->status.default_layer >> 16, state->status.layer >> 16, layer_buffer);
  282. gdispDrawString(0, 20, layer_buffer, state->font_fixed5x8, Black);
  283. gdispFlush();
  284. return false;
  285. }
  286. static void format_mods_bitmap_string(uint8_t mods, char* buffer) {
  287. *buffer = ' ';
  288. ++buffer;
  289. for (int i = 0; i<8; i++)
  290. {
  291. uint32_t mask = (1u << i);
  292. if (mods & mask) {
  293. *buffer = '1';
  294. } else {
  295. *buffer = '0';
  296. }
  297. ++buffer;
  298. if (i==3) {
  299. *buffer = ' ';
  300. ++buffer;
  301. }
  302. }
  303. *buffer = 0;
  304. }
  305. bool keyframe_display_mods_bitmap(keyframe_animation_t* animation, visualizer_state_t* state) {
  306. (void)animation;
  307. const char* title = "Modifier states";
  308. const char* mods_header = " CSAG CSAG ";
  309. char status_buffer[12];
  310. gdispClear(White);
  311. gdispDrawString(0, 0, title, state->font_fixed5x8, Black);
  312. gdispDrawString(0, 10, mods_header, state->font_fixed5x8, Black);
  313. format_mods_bitmap_string(state->status.mods, status_buffer);
  314. gdispDrawString(0, 20, status_buffer, state->font_fixed5x8, Black);
  315. gdispFlush();
  316. return false;
  317. }
  318. #define LED_STATE_STRING_SIZE sizeof("NUM CAPS SCRL COMP KANA")
  319. static void get_led_state_string(char* output, visualizer_state_t* state) {
  320. uint8_t pos = 0;
  321. if (state->status.leds & (1u << USB_LED_NUM_LOCK)) {
  322. memcpy(output + pos, "NUM ", 4);
  323. pos += 4;
  324. }
  325. if (state->status.leds & (1u << USB_LED_CAPS_LOCK)) {
  326. memcpy(output + pos, "CAPS ", 5);
  327. pos += 5;
  328. }
  329. if (state->status.leds & (1u << USB_LED_SCROLL_LOCK)) {
  330. memcpy(output + pos, "SCRL ", 5);
  331. pos += 5;
  332. }
  333. if (state->status.leds & (1u << USB_LED_COMPOSE)) {
  334. memcpy(output + pos, "COMP ", 5);
  335. pos += 5;
  336. }
  337. if (state->status.leds & (1u << USB_LED_KANA)) {
  338. memcpy(output + pos, "KANA ", 5);
  339. pos += 5;
  340. }
  341. output[pos] = 0;
  342. }
  343. bool keyframe_display_led_states(keyframe_animation_t* animation, visualizer_state_t* state)
  344. {
  345. (void)animation;
  346. char output[LED_STATE_STRING_SIZE];
  347. get_led_state_string(output, state);
  348. gdispClear(White);
  349. gdispDrawString(0, 10, output, state->font_dejavusansbold12, Black);
  350. gdispFlush();
  351. return false;
  352. }
  353. bool keyframe_display_layer_and_led_states(keyframe_animation_t* animation, visualizer_state_t* state) {
  354. (void)animation;
  355. gdispClear(White);
  356. uint8_t y = 10;
  357. if (state->status.leds) {
  358. char output[LED_STATE_STRING_SIZE];
  359. get_led_state_string(output, state);
  360. gdispDrawString(0, 1, output, state->font_dejavusansbold12, Black);
  361. y = 17;
  362. }
  363. gdispDrawString(0, y, state->layer_text, state->font_dejavusansbold12, Black);
  364. gdispFlush();
  365. return false;
  366. }
  367. #endif // LCD_ENABLE
  368. bool keyframe_disable_lcd_and_backlight(keyframe_animation_t* animation, visualizer_state_t* state) {
  369. (void)animation;
  370. (void)state;
  371. #ifdef LCD_ENABLE
  372. gdispSetPowerMode(powerOff);
  373. #endif
  374. #ifdef LCD_BACKLIGHT_ENABLE
  375. lcd_backlight_hal_color(0, 0, 0);
  376. #endif
  377. return false;
  378. }
  379. bool keyframe_enable_lcd_and_backlight(keyframe_animation_t* animation, visualizer_state_t* state) {
  380. (void)animation;
  381. (void)state;
  382. #ifdef LCD_ENABLE
  383. gdispSetPowerMode(powerOn);
  384. #endif
  385. return false;
  386. }
  387. bool enable_visualization(keyframe_animation_t* animation, visualizer_state_t* state) {
  388. (void)animation;
  389. (void)state;
  390. dprint("User visualizer inited\n");
  391. visualizer_enabled = true;
  392. return false;
  393. }
  394. // TODO: Optimize the stack size, this is probably way too big
  395. static DECLARE_THREAD_STACK(visualizerThreadStack, 1024);
  396. static DECLARE_THREAD_FUNCTION(visualizerThread, arg) {
  397. (void)arg;
  398. GListener event_listener;
  399. geventListenerInit(&event_listener);
  400. geventAttachSource(&event_listener, (GSourceHandle)&current_status, 0);
  401. visualizer_keyboard_status_t initial_status = {
  402. .default_layer = 0xFFFFFFFF,
  403. .layer = 0xFFFFFFFF,
  404. .mods = 0xFF,
  405. .leds = 0xFFFFFFFF,
  406. .suspended = false,
  407. #ifdef VISUALIZER_USER_DATA_SIZE
  408. .user_data = {0},
  409. #endif
  410. };
  411. visualizer_state_t state = {
  412. .status = initial_status,
  413. .current_lcd_color = 0,
  414. #ifdef LCD_ENABLE
  415. .font_fixed5x8 = gdispOpenFont("fixed_5x8"),
  416. .font_dejavusansbold12 = gdispOpenFont("DejaVuSansBold12")
  417. #endif
  418. };
  419. initialize_user_visualizer(&state);
  420. state.prev_lcd_color = state.current_lcd_color;
  421. #ifdef LCD_BACKLIGHT_ENABLE
  422. lcd_backlight_color(
  423. LCD_HUE(state.current_lcd_color),
  424. LCD_SAT(state.current_lcd_color),
  425. LCD_INT(state.current_lcd_color));
  426. #endif
  427. systemticks_t sleep_time = TIME_INFINITE;
  428. systemticks_t current_time = gfxSystemTicks();
  429. while(true) {
  430. systemticks_t new_time = gfxSystemTicks();
  431. systemticks_t delta = new_time - current_time;
  432. current_time = new_time;
  433. bool enabled = visualizer_enabled;
  434. if (!same_status(&state.status, &current_status)) {
  435. if (visualizer_enabled) {
  436. if (current_status.suspended) {
  437. stop_all_keyframe_animations();
  438. visualizer_enabled = false;
  439. state.status = current_status;
  440. user_visualizer_suspend(&state);
  441. }
  442. else {
  443. visualizer_keyboard_status_t prev_status = state.status;
  444. state.status = current_status;
  445. update_user_visualizer_state(&state, &prev_status);
  446. }
  447. state.prev_lcd_color = state.current_lcd_color;
  448. }
  449. }
  450. if (!enabled && state.status.suspended && current_status.suspended == false) {
  451. // Setting the status to the initial status will force an update
  452. // when the visualizer is enabled again
  453. state.status = initial_status;
  454. state.status.suspended = false;
  455. stop_all_keyframe_animations();
  456. user_visualizer_resume(&state);
  457. state.prev_lcd_color = state.current_lcd_color;
  458. }
  459. sleep_time = TIME_INFINITE;
  460. for (int i=0;i<MAX_SIMULTANEOUS_ANIMATIONS;i++) {
  461. if (animations[i]) {
  462. update_keyframe_animation(animations[i], &state, delta, &sleep_time);
  463. }
  464. }
  465. #ifdef LED_ENABLE
  466. gdispGFlush(LED_DISPLAY);
  467. #endif
  468. #ifdef EMULATOR
  469. draw_emulator();
  470. #endif
  471. // The animation can enable the visualizer
  472. // And we might need to update the state when that happens
  473. // so don't sleep
  474. if (enabled != visualizer_enabled) {
  475. sleep_time = 0;
  476. }
  477. systemticks_t after_update = gfxSystemTicks();
  478. unsigned update_delta = after_update - current_time;
  479. if (sleep_time != TIME_INFINITE) {
  480. if (sleep_time > update_delta) {
  481. sleep_time -= update_delta;
  482. }
  483. else {
  484. sleep_time = 0;
  485. }
  486. }
  487. dprintf("Update took %d, last delta %d, sleep_time %d\n", update_delta, delta, sleep_time);
  488. #ifdef PROTOCOL_CHIBIOS
  489. // The gEventWait function really takes milliseconds, even if the documentation says ticks.
  490. // Unfortunately there's no generic ugfx conversion from system time to milliseconds,
  491. // so let's do it in a platform dependent way.
  492. // On windows the system ticks is the same as milliseconds anyway
  493. if (sleep_time != TIME_INFINITE) {
  494. sleep_time = ST2MS(sleep_time);
  495. }
  496. #endif
  497. geventEventWait(&event_listener, sleep_time);
  498. }
  499. #ifdef LCD_ENABLE
  500. gdispCloseFont(state.font_fixed5x8);
  501. gdispCloseFont(state.font_dejavusansbold12);
  502. #endif
  503. return 0;
  504. }
  505. void visualizer_init(void) {
  506. gfxInit();
  507. #ifdef LCD_BACKLIGHT_ENABLE
  508. lcd_backlight_init();
  509. #endif
  510. #ifdef SERIAL_LINK_ENABLE
  511. add_remote_objects(remote_objects, sizeof(remote_objects) / sizeof(remote_object_t*) );
  512. #endif
  513. #ifdef LCD_ENABLE
  514. LCD_DISPLAY = get_lcd_display();
  515. #endif
  516. #ifdef LED_ENABLE
  517. LED_DISPLAY = get_led_display();
  518. #endif
  519. // We are using a low priority thread, the idea is to have it run only
  520. // when the main thread is sleeping during the matrix scanning
  521. gfxThreadCreate(visualizerThreadStack, sizeof(visualizerThreadStack),
  522. VISUALIZER_THREAD_PRIORITY, visualizerThread, NULL);
  523. }
  524. void update_status(bool changed) {
  525. if (changed) {
  526. GSourceListener* listener = geventGetSourceListener((GSourceHandle)&current_status, NULL);
  527. if (listener) {
  528. geventSendEvent(listener);
  529. }
  530. }
  531. #ifdef SERIAL_LINK_ENABLE
  532. static systime_t last_update = 0;
  533. systime_t current_update = chVTGetSystemTimeX();
  534. systime_t delta = current_update - last_update;
  535. if (changed || delta > MS2ST(10)) {
  536. last_update = current_update;
  537. visualizer_keyboard_status_t* r = begin_write_current_status();
  538. *r = current_status;
  539. end_write_current_status();
  540. }
  541. #endif
  542. }
  543. uint8_t visualizer_get_mods() {
  544. uint8_t mods = get_mods();
  545. #ifndef NO_ACTION_ONESHOT
  546. if (!has_oneshot_mods_timed_out()) {
  547. mods |= get_oneshot_mods();
  548. }
  549. #endif
  550. return mods;
  551. }
  552. #ifdef VISUALIZER_USER_DATA_SIZE
  553. void visualizer_set_user_data(void* u) {
  554. memcpy(user_data, u, VISUALIZER_USER_DATA_SIZE);
  555. }
  556. #endif
  557. void visualizer_update(uint32_t default_state, uint32_t state, uint8_t mods, uint32_t leds) {
  558. // Note that there's a small race condition here, the thread could read
  559. // a state where one of these are set but not the other. But this should
  560. // not really matter as it will be fixed during the next loop step.
  561. // Alternatively a mutex could be used instead of the volatile variables
  562. bool changed = false;
  563. #ifdef SERIAL_LINK_ENABLE
  564. if (is_serial_link_connected ()) {
  565. visualizer_keyboard_status_t* new_status = read_current_status();
  566. if (new_status) {
  567. if (!same_status(&current_status, new_status)) {
  568. changed = true;
  569. current_status = *new_status;
  570. }
  571. }
  572. }
  573. else {
  574. #else
  575. {
  576. #endif
  577. visualizer_keyboard_status_t new_status = {
  578. .layer = state,
  579. .default_layer = default_state,
  580. .mods = mods,
  581. .leds = leds,
  582. .suspended = current_status.suspended,
  583. };
  584. #ifdef VISUALIZER_USER_DATA_SIZE
  585. memcpy(new_status.user_data, user_data, VISUALIZER_USER_DATA_SIZE);
  586. #endif
  587. if (!same_status(&current_status, &new_status)) {
  588. changed = true;
  589. current_status = new_status;
  590. }
  591. }
  592. update_status(changed);
  593. }
  594. void visualizer_suspend(void) {
  595. current_status.suspended = true;
  596. update_status(true);
  597. }
  598. void visualizer_resume(void) {
  599. current_status.suspended = false;
  600. update_status(true);
  601. }