visualizer.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  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. bool keyframe_display_led_states(keyframe_animation_t* animation, visualizer_state_t* state)
  319. {
  320. char output[sizeof("NUM CAPS SCRL COMP KANA")];
  321. uint8_t pos = 0;
  322. if (state->status.leds & (1u << USB_LED_NUM_LOCK)) {
  323. memcpy(output + pos, "NUM ", 4);
  324. pos += 4;
  325. }
  326. if (state->status.leds & (1u << USB_LED_CAPS_LOCK)) {
  327. memcpy(output + pos, "CAPS ", 5);
  328. pos += 5;
  329. }
  330. if (state->status.leds & (1u << USB_LED_SCROLL_LOCK)) {
  331. memcpy(output + pos, "SCRL ", 5);
  332. pos += 5;
  333. }
  334. if (state->status.leds & (1u << USB_LED_COMPOSE)) {
  335. memcpy(output + pos, "COMP ", 5);
  336. pos += 5;
  337. }
  338. if (state->status.leds & (1u << USB_LED_KANA)) {
  339. memcpy(output + pos, "KANA ", 5);
  340. pos += 5;
  341. }
  342. output[pos] = 0;
  343. gdispClear(White);
  344. gdispDrawString(0, 10, output, state->font_dejavusansbold12, Black);
  345. gdispFlush();
  346. return false;
  347. }
  348. #endif // LCD_ENABLE
  349. bool keyframe_disable_lcd_and_backlight(keyframe_animation_t* animation, visualizer_state_t* state) {
  350. (void)animation;
  351. (void)state;
  352. #ifdef LCD_ENABLE
  353. gdispSetPowerMode(powerOff);
  354. #endif
  355. #ifdef LCD_BACKLIGHT_ENABLE
  356. lcd_backlight_hal_color(0, 0, 0);
  357. #endif
  358. return false;
  359. }
  360. bool keyframe_enable_lcd_and_backlight(keyframe_animation_t* animation, visualizer_state_t* state) {
  361. (void)animation;
  362. (void)state;
  363. #ifdef LCD_ENABLE
  364. gdispSetPowerMode(powerOn);
  365. #endif
  366. return false;
  367. }
  368. bool enable_visualization(keyframe_animation_t* animation, visualizer_state_t* state) {
  369. (void)animation;
  370. (void)state;
  371. dprint("User visualizer inited\n");
  372. visualizer_enabled = true;
  373. return false;
  374. }
  375. // TODO: Optimize the stack size, this is probably way too big
  376. static DECLARE_THREAD_STACK(visualizerThreadStack, 1024);
  377. static DECLARE_THREAD_FUNCTION(visualizerThread, arg) {
  378. (void)arg;
  379. GListener event_listener;
  380. geventListenerInit(&event_listener);
  381. geventAttachSource(&event_listener, (GSourceHandle)&current_status, 0);
  382. visualizer_keyboard_status_t initial_status = {
  383. .default_layer = 0xFFFFFFFF,
  384. .layer = 0xFFFFFFFF,
  385. .mods = 0xFF,
  386. .leds = 0xFFFFFFFF,
  387. .suspended = false,
  388. #ifdef VISUALIZER_USER_DATA_SIZE
  389. .user_data = {0},
  390. #endif
  391. };
  392. visualizer_state_t state = {
  393. .status = initial_status,
  394. .current_lcd_color = 0,
  395. #ifdef LCD_ENABLE
  396. .font_fixed5x8 = gdispOpenFont("fixed_5x8"),
  397. .font_dejavusansbold12 = gdispOpenFont("DejaVuSansBold12")
  398. #endif
  399. };
  400. initialize_user_visualizer(&state);
  401. state.prev_lcd_color = state.current_lcd_color;
  402. #ifdef LCD_BACKLIGHT_ENABLE
  403. lcd_backlight_color(
  404. LCD_HUE(state.current_lcd_color),
  405. LCD_SAT(state.current_lcd_color),
  406. LCD_INT(state.current_lcd_color));
  407. #endif
  408. systemticks_t sleep_time = TIME_INFINITE;
  409. systemticks_t current_time = gfxSystemTicks();
  410. while(true) {
  411. systemticks_t new_time = gfxSystemTicks();
  412. systemticks_t delta = new_time - current_time;
  413. current_time = new_time;
  414. bool enabled = visualizer_enabled;
  415. if (!same_status(&state.status, &current_status)) {
  416. if (visualizer_enabled) {
  417. if (current_status.suspended) {
  418. stop_all_keyframe_animations();
  419. visualizer_enabled = false;
  420. state.status = current_status;
  421. user_visualizer_suspend(&state);
  422. }
  423. else {
  424. visualizer_keyboard_status_t prev_status = state.status;
  425. state.status = current_status;
  426. update_user_visualizer_state(&state, prev_status);
  427. }
  428. state.prev_lcd_color = state.current_lcd_color;
  429. }
  430. }
  431. if (!enabled && state.status.suspended && current_status.suspended == false) {
  432. // Setting the status to the initial status will force an update
  433. // when the visualizer is enabled again
  434. state.status = initial_status;
  435. state.status.suspended = false;
  436. stop_all_keyframe_animations();
  437. user_visualizer_resume(&state);
  438. state.prev_lcd_color = state.current_lcd_color;
  439. }
  440. sleep_time = TIME_INFINITE;
  441. for (int i=0;i<MAX_SIMULTANEOUS_ANIMATIONS;i++) {
  442. if (animations[i]) {
  443. update_keyframe_animation(animations[i], &state, delta, &sleep_time);
  444. }
  445. }
  446. #ifdef LED_ENABLE
  447. gdispGFlush(LED_DISPLAY);
  448. #endif
  449. #ifdef EMULATOR
  450. draw_emulator();
  451. #endif
  452. // The animation can enable the visualizer
  453. // And we might need to update the state when that happens
  454. // so don't sleep
  455. if (enabled != visualizer_enabled) {
  456. sleep_time = 0;
  457. }
  458. systemticks_t after_update = gfxSystemTicks();
  459. unsigned update_delta = after_update - current_time;
  460. if (sleep_time != TIME_INFINITE) {
  461. if (sleep_time > update_delta) {
  462. sleep_time -= update_delta;
  463. }
  464. else {
  465. sleep_time = 0;
  466. }
  467. }
  468. dprintf("Update took %d, last delta %d, sleep_time %d\n", update_delta, delta, sleep_time);
  469. #ifdef PROTOCOL_CHIBIOS
  470. // The gEventWait function really takes milliseconds, even if the documentation says ticks.
  471. // Unfortunately there's no generic ugfx conversion from system time to milliseconds,
  472. // so let's do it in a platform dependent way.
  473. // On windows the system ticks is the same as milliseconds anyway
  474. if (sleep_time != TIME_INFINITE) {
  475. sleep_time = ST2MS(sleep_time);
  476. }
  477. #endif
  478. geventEventWait(&event_listener, sleep_time);
  479. }
  480. #ifdef LCD_ENABLE
  481. gdispCloseFont(state.font_fixed5x8);
  482. gdispCloseFont(state.font_dejavusansbold12);
  483. #endif
  484. return 0;
  485. }
  486. void visualizer_init(void) {
  487. gfxInit();
  488. #ifdef LCD_BACKLIGHT_ENABLE
  489. lcd_backlight_init();
  490. #endif
  491. #ifdef SERIAL_LINK_ENABLE
  492. add_remote_objects(remote_objects, sizeof(remote_objects) / sizeof(remote_object_t*) );
  493. #endif
  494. #ifdef LCD_ENABLE
  495. LCD_DISPLAY = get_lcd_display();
  496. #endif
  497. #ifdef LED_ENABLE
  498. LED_DISPLAY = get_led_display();
  499. #endif
  500. // We are using a low priority thread, the idea is to have it run only
  501. // when the main thread is sleeping during the matrix scanning
  502. gfxThreadCreate(visualizerThreadStack, sizeof(visualizerThreadStack),
  503. VISUALIZER_THREAD_PRIORITY, visualizerThread, NULL);
  504. }
  505. void update_status(bool changed) {
  506. if (changed) {
  507. GSourceListener* listener = geventGetSourceListener((GSourceHandle)&current_status, NULL);
  508. if (listener) {
  509. geventSendEvent(listener);
  510. }
  511. }
  512. #ifdef SERIAL_LINK_ENABLE
  513. static systime_t last_update = 0;
  514. systime_t current_update = chVTGetSystemTimeX();
  515. systime_t delta = current_update - last_update;
  516. if (changed || delta > MS2ST(10)) {
  517. last_update = current_update;
  518. visualizer_keyboard_status_t* r = begin_write_current_status();
  519. *r = current_status;
  520. end_write_current_status();
  521. }
  522. #endif
  523. }
  524. uint8_t visualizer_get_mods() {
  525. uint8_t mods = get_mods();
  526. #ifndef NO_ACTION_ONESHOT
  527. if (!has_oneshot_mods_timed_out()) {
  528. mods |= get_oneshot_mods();
  529. }
  530. #endif
  531. return mods;
  532. }
  533. #ifdef VISUALIZER_USER_DATA_SIZE
  534. void visualizer_set_user_data(void* u) {
  535. memcpy(user_data, u, VISUALIZER_USER_DATA_SIZE);
  536. }
  537. #endif
  538. void visualizer_update(uint32_t default_state, uint32_t state, uint8_t mods, uint32_t leds) {
  539. // Note that there's a small race condition here, the thread could read
  540. // a state where one of these are set but not the other. But this should
  541. // not really matter as it will be fixed during the next loop step.
  542. // Alternatively a mutex could be used instead of the volatile variables
  543. bool changed = false;
  544. #ifdef SERIAL_LINK_ENABLE
  545. if (is_serial_link_connected ()) {
  546. visualizer_keyboard_status_t* new_status = read_current_status();
  547. if (new_status) {
  548. if (!same_status(&current_status, new_status)) {
  549. changed = true;
  550. current_status = *new_status;
  551. }
  552. }
  553. }
  554. else {
  555. #else
  556. {
  557. #endif
  558. visualizer_keyboard_status_t new_status = {
  559. .layer = state,
  560. .default_layer = default_state,
  561. .mods = mods,
  562. .leds = leds,
  563. .suspended = current_status.suspended,
  564. };
  565. #ifdef VISUALIZER_USER_DATA_SIZE
  566. memcpy(new_status.user_data, user_data, VISUALIZER_USER_DATA_SIZE);
  567. #endif
  568. if (!same_status(&current_status, &new_status)) {
  569. changed = true;
  570. current_status = new_status;
  571. }
  572. }
  573. update_status(changed);
  574. }
  575. void visualizer_suspend(void) {
  576. current_status.suspended = true;
  577. update_status(true);
  578. }
  579. void visualizer_resume(void) {
  580. current_status.suspended = false;
  581. update_status(true);
  582. }