visualizer.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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. static visualizer_keyboard_status_t current_status = {
  47. .layer = 0xFFFFFFFF,
  48. .default_layer = 0xFFFFFFFF,
  49. .mods = 0xFF,
  50. .leds = 0xFFFFFFFF,
  51. .suspended = false,
  52. };
  53. static bool same_status(visualizer_keyboard_status_t* status1, visualizer_keyboard_status_t* status2) {
  54. return status1->layer == status2->layer &&
  55. status1->default_layer == status2->default_layer &&
  56. status1->mods == status2->mods &&
  57. status1->leds == status2->leds &&
  58. status1->suspended == status2->suspended;
  59. }
  60. static bool visualizer_enabled = false;
  61. #define MAX_SIMULTANEOUS_ANIMATIONS 4
  62. static keyframe_animation_t* animations[MAX_SIMULTANEOUS_ANIMATIONS] = {};
  63. #ifdef SERIAL_LINK_ENABLE
  64. MASTER_TO_ALL_SLAVES_OBJECT(current_status, visualizer_keyboard_status_t);
  65. static remote_object_t* remote_objects[] = {
  66. REMOTE_OBJECT(current_status),
  67. };
  68. #endif
  69. GDisplay* LCD_DISPLAY = 0;
  70. GDisplay* LED_DISPLAY = 0;
  71. __attribute__((weak))
  72. GDisplay* get_lcd_display(void) {
  73. return gdispGetDisplay(0);
  74. }
  75. __attribute__((weak))
  76. GDisplay* get_led_display(void) {
  77. return gdispGetDisplay(1);
  78. }
  79. void start_keyframe_animation(keyframe_animation_t* animation) {
  80. animation->current_frame = -1;
  81. animation->time_left_in_frame = 0;
  82. animation->need_update = true;
  83. int free_index = -1;
  84. for (int i=0;i<MAX_SIMULTANEOUS_ANIMATIONS;i++) {
  85. if (animations[i] == animation) {
  86. return;
  87. }
  88. if (free_index == -1 && animations[i] == NULL) {
  89. free_index=i;
  90. }
  91. }
  92. if (free_index!=-1) {
  93. animations[free_index] = animation;
  94. }
  95. }
  96. void stop_keyframe_animation(keyframe_animation_t* animation) {
  97. animation->current_frame = animation->num_frames;
  98. animation->time_left_in_frame = 0;
  99. animation->need_update = true;
  100. animation->first_update_of_frame = false;
  101. animation->last_update_of_frame = false;
  102. for (int i=0;i<MAX_SIMULTANEOUS_ANIMATIONS;i++) {
  103. if (animations[i] == animation) {
  104. animations[i] = NULL;
  105. return;
  106. }
  107. }
  108. }
  109. void stop_all_keyframe_animations(void) {
  110. for (int i=0;i<MAX_SIMULTANEOUS_ANIMATIONS;i++) {
  111. if (animations[i]) {
  112. animations[i]->current_frame = animations[i]->num_frames;
  113. animations[i]->time_left_in_frame = 0;
  114. animations[i]->need_update = true;
  115. animations[i]->first_update_of_frame = false;
  116. animations[i]->last_update_of_frame = false;
  117. animations[i] = NULL;
  118. }
  119. }
  120. }
  121. static bool update_keyframe_animation(keyframe_animation_t* animation, visualizer_state_t* state, systemticks_t delta, systemticks_t* sleep_time) {
  122. // TODO: Clean up this messy code
  123. dprintf("Animation frame%d, left %d, delta %d\n", animation->current_frame,
  124. animation->time_left_in_frame, delta);
  125. if (animation->current_frame == animation->num_frames) {
  126. animation->need_update = false;
  127. return false;
  128. }
  129. if (animation->current_frame == -1) {
  130. animation->current_frame = 0;
  131. animation->time_left_in_frame = animation->frame_lengths[0];
  132. animation->need_update = true;
  133. animation->first_update_of_frame = true;
  134. } else {
  135. animation->time_left_in_frame -= delta;
  136. while (animation->time_left_in_frame <= 0) {
  137. int left = animation->time_left_in_frame;
  138. if (animation->need_update) {
  139. animation->time_left_in_frame = 0;
  140. animation->last_update_of_frame = true;
  141. (*animation->frame_functions[animation->current_frame])(animation, state);
  142. animation->last_update_of_frame = false;
  143. }
  144. animation->current_frame++;
  145. animation->need_update = true;
  146. animation->first_update_of_frame = true;
  147. if (animation->current_frame == animation->num_frames) {
  148. if (animation->loop) {
  149. animation->current_frame = 0;
  150. }
  151. else {
  152. stop_keyframe_animation(animation);
  153. return false;
  154. }
  155. }
  156. delta = -left;
  157. animation->time_left_in_frame = animation->frame_lengths[animation->current_frame];
  158. animation->time_left_in_frame -= delta;
  159. }
  160. }
  161. if (animation->need_update) {
  162. animation->need_update = (*animation->frame_functions[animation->current_frame])(animation, state);
  163. animation->first_update_of_frame = false;
  164. }
  165. systemticks_t wanted_sleep = animation->need_update ? gfxMillisecondsToTicks(10) : (unsigned)animation->time_left_in_frame;
  166. if (wanted_sleep < *sleep_time) {
  167. *sleep_time = wanted_sleep;
  168. }
  169. return true;
  170. }
  171. void run_next_keyframe(keyframe_animation_t* animation, visualizer_state_t* state) {
  172. int next_frame = animation->current_frame + 1;
  173. if (next_frame == animation->num_frames) {
  174. next_frame = 0;
  175. }
  176. keyframe_animation_t temp_animation = *animation;
  177. temp_animation.current_frame = next_frame;
  178. temp_animation.time_left_in_frame = animation->frame_lengths[next_frame];
  179. temp_animation.first_update_of_frame = true;
  180. temp_animation.last_update_of_frame = false;
  181. temp_animation.need_update = false;
  182. visualizer_state_t temp_state = *state;
  183. (*temp_animation.frame_functions[next_frame])(&temp_animation, &temp_state);
  184. }
  185. bool keyframe_no_operation(keyframe_animation_t* animation, visualizer_state_t* state) {
  186. (void)animation;
  187. (void)state;
  188. return false;
  189. }
  190. #ifdef LCD_BACKLIGHT_ENABLE
  191. bool keyframe_animate_backlight_color(keyframe_animation_t* animation, visualizer_state_t* state) {
  192. int frame_length = animation->frame_lengths[animation->current_frame];
  193. int current_pos = frame_length - animation->time_left_in_frame;
  194. uint8_t t_h = LCD_HUE(state->target_lcd_color);
  195. uint8_t t_s = LCD_SAT(state->target_lcd_color);
  196. uint8_t t_i = LCD_INT(state->target_lcd_color);
  197. uint8_t p_h = LCD_HUE(state->prev_lcd_color);
  198. uint8_t p_s = LCD_SAT(state->prev_lcd_color);
  199. uint8_t p_i = LCD_INT(state->prev_lcd_color);
  200. uint8_t d_h1 = t_h - p_h; //Modulo arithmetic since we want to wrap around
  201. int d_h2 = t_h - p_h;
  202. // Chose the shortest way around
  203. int d_h = abs(d_h2) < d_h1 ? d_h2 : d_h1;
  204. int d_s = t_s - p_s;
  205. int d_i = t_i - p_i;
  206. int hue = (d_h * current_pos) / frame_length;
  207. int sat = (d_s * current_pos) / frame_length;
  208. int intensity = (d_i * current_pos) / frame_length;
  209. //dprintf("%X -> %X = %X\n", p_h, t_h, hue);
  210. hue += p_h;
  211. sat += p_s;
  212. intensity += p_i;
  213. state->current_lcd_color = LCD_COLOR(hue, sat, intensity);
  214. lcd_backlight_color(
  215. LCD_HUE(state->current_lcd_color),
  216. LCD_SAT(state->current_lcd_color),
  217. LCD_INT(state->current_lcd_color));
  218. return true;
  219. }
  220. bool keyframe_set_backlight_color(keyframe_animation_t* animation, visualizer_state_t* state) {
  221. (void)animation;
  222. state->prev_lcd_color = state->target_lcd_color;
  223. state->current_lcd_color = state->target_lcd_color;
  224. lcd_backlight_color(
  225. LCD_HUE(state->current_lcd_color),
  226. LCD_SAT(state->current_lcd_color),
  227. LCD_INT(state->current_lcd_color));
  228. return false;
  229. }
  230. #endif // LCD_BACKLIGHT_ENABLE
  231. #ifdef LCD_ENABLE
  232. bool keyframe_display_layer_text(keyframe_animation_t* animation, visualizer_state_t* state) {
  233. (void)animation;
  234. gdispClear(White);
  235. gdispDrawString(0, 10, state->layer_text, state->font_dejavusansbold12, Black);
  236. gdispFlush();
  237. return false;
  238. }
  239. static void format_layer_bitmap_string(uint16_t default_layer, uint16_t layer, char* buffer) {
  240. for (int i=0; i<16;i++)
  241. {
  242. uint32_t mask = (1u << i);
  243. if (default_layer & mask) {
  244. if (layer & mask) {
  245. *buffer = 'B';
  246. } else {
  247. *buffer = 'D';
  248. }
  249. } else if (layer & mask) {
  250. *buffer = '1';
  251. } else {
  252. *buffer = '0';
  253. }
  254. ++buffer;
  255. if (i==3 || i==7 || i==11) {
  256. *buffer = ' ';
  257. ++buffer;
  258. }
  259. }
  260. *buffer = 0;
  261. }
  262. bool keyframe_display_layer_bitmap(keyframe_animation_t* animation, visualizer_state_t* state) {
  263. (void)animation;
  264. const char* layer_help = "1=On D=Default B=Both";
  265. char layer_buffer[16 + 4]; // 3 spaces and one null terminator
  266. gdispClear(White);
  267. gdispDrawString(0, 0, layer_help, state->font_fixed5x8, Black);
  268. format_layer_bitmap_string(state->status.default_layer, state->status.layer, layer_buffer);
  269. gdispDrawString(0, 10, layer_buffer, state->font_fixed5x8, Black);
  270. format_layer_bitmap_string(state->status.default_layer >> 16, state->status.layer >> 16, layer_buffer);
  271. gdispDrawString(0, 20, layer_buffer, state->font_fixed5x8, Black);
  272. gdispFlush();
  273. return false;
  274. }
  275. static void format_mods_bitmap_string(uint8_t mods, char* buffer) {
  276. *buffer = ' ';
  277. ++buffer;
  278. for (int i = 0; i<8; i++)
  279. {
  280. uint32_t mask = (1u << i);
  281. if (mods & mask) {
  282. *buffer = '1';
  283. } else {
  284. *buffer = '0';
  285. }
  286. ++buffer;
  287. if (i==3) {
  288. *buffer = ' ';
  289. ++buffer;
  290. }
  291. }
  292. *buffer = 0;
  293. }
  294. bool keyframe_display_mods_bitmap(keyframe_animation_t* animation, visualizer_state_t* state) {
  295. (void)animation;
  296. const char* title = "Modifier states";
  297. const char* mods_header = " CSAG CSAG ";
  298. char status_buffer[12];
  299. gdispClear(White);
  300. gdispDrawString(0, 0, title, state->font_fixed5x8, Black);
  301. gdispDrawString(0, 10, mods_header, state->font_fixed5x8, Black);
  302. format_mods_bitmap_string(state->status.mods, status_buffer);
  303. gdispDrawString(0, 20, status_buffer, state->font_fixed5x8, Black);
  304. gdispFlush();
  305. return false;
  306. }
  307. #endif // LCD_ENABLE
  308. bool keyframe_disable_lcd_and_backlight(keyframe_animation_t* animation, visualizer_state_t* state) {
  309. (void)animation;
  310. (void)state;
  311. #ifdef LCD_ENABLE
  312. gdispSetPowerMode(powerOff);
  313. #endif
  314. #ifdef LCD_BACKLIGHT_ENABLE
  315. lcd_backlight_hal_color(0, 0, 0);
  316. #endif
  317. return false;
  318. }
  319. bool keyframe_enable_lcd_and_backlight(keyframe_animation_t* animation, visualizer_state_t* state) {
  320. (void)animation;
  321. (void)state;
  322. #ifdef LCD_ENABLE
  323. gdispSetPowerMode(powerOn);
  324. #endif
  325. return false;
  326. }
  327. bool enable_visualization(keyframe_animation_t* animation, visualizer_state_t* state) {
  328. (void)animation;
  329. (void)state;
  330. dprint("User visualizer inited\n");
  331. visualizer_enabled = true;
  332. return false;
  333. }
  334. // TODO: Optimize the stack size, this is probably way too big
  335. static DECLARE_THREAD_STACK(visualizerThreadStack, 1024);
  336. static DECLARE_THREAD_FUNCTION(visualizerThread, arg) {
  337. (void)arg;
  338. GListener event_listener;
  339. geventListenerInit(&event_listener);
  340. geventAttachSource(&event_listener, (GSourceHandle)&current_status, 0);
  341. visualizer_keyboard_status_t initial_status = {
  342. .default_layer = 0xFFFFFFFF,
  343. .layer = 0xFFFFFFFF,
  344. .mods = 0xFF,
  345. .leds = 0xFFFFFFFF,
  346. .suspended = false,
  347. };
  348. visualizer_state_t state = {
  349. .status = initial_status,
  350. .current_lcd_color = 0,
  351. #ifdef LCD_ENABLE
  352. .font_fixed5x8 = gdispOpenFont("fixed_5x8"),
  353. .font_dejavusansbold12 = gdispOpenFont("DejaVuSansBold12")
  354. #endif
  355. };
  356. initialize_user_visualizer(&state);
  357. state.prev_lcd_color = state.current_lcd_color;
  358. #ifdef LCD_BACKLIGHT_ENABLE
  359. lcd_backlight_color(
  360. LCD_HUE(state.current_lcd_color),
  361. LCD_SAT(state.current_lcd_color),
  362. LCD_INT(state.current_lcd_color));
  363. #endif
  364. systemticks_t sleep_time = TIME_INFINITE;
  365. systemticks_t current_time = gfxSystemTicks();
  366. while(true) {
  367. systemticks_t new_time = gfxSystemTicks();
  368. systemticks_t delta = new_time - current_time;
  369. current_time = new_time;
  370. bool enabled = visualizer_enabled;
  371. if (!same_status(&state.status, &current_status)) {
  372. if (visualizer_enabled) {
  373. if (current_status.suspended) {
  374. stop_all_keyframe_animations();
  375. visualizer_enabled = false;
  376. state.status = current_status;
  377. user_visualizer_suspend(&state);
  378. }
  379. else {
  380. state.status = current_status;
  381. update_user_visualizer_state(&state);
  382. }
  383. state.prev_lcd_color = state.current_lcd_color;
  384. }
  385. }
  386. if (!enabled && state.status.suspended && current_status.suspended == false) {
  387. // Setting the status to the initial status will force an update
  388. // when the visualizer is enabled again
  389. state.status = initial_status;
  390. state.status.suspended = false;
  391. stop_all_keyframe_animations();
  392. user_visualizer_resume(&state);
  393. state.prev_lcd_color = state.current_lcd_color;
  394. }
  395. sleep_time = TIME_INFINITE;
  396. for (int i=0;i<MAX_SIMULTANEOUS_ANIMATIONS;i++) {
  397. if (animations[i]) {
  398. update_keyframe_animation(animations[i], &state, delta, &sleep_time);
  399. }
  400. }
  401. #ifdef LED_ENABLE
  402. gdispGFlush(LED_DISPLAY);
  403. #endif
  404. #ifdef EMULATOR
  405. draw_emulator();
  406. #endif
  407. // The animation can enable the visualizer
  408. // And we might need to update the state when that happens
  409. // so don't sleep
  410. if (enabled != visualizer_enabled) {
  411. sleep_time = 0;
  412. }
  413. systemticks_t after_update = gfxSystemTicks();
  414. unsigned update_delta = after_update - current_time;
  415. if (sleep_time != TIME_INFINITE) {
  416. if (sleep_time > update_delta) {
  417. sleep_time -= update_delta;
  418. }
  419. else {
  420. sleep_time = 0;
  421. }
  422. }
  423. dprintf("Update took %d, last delta %d, sleep_time %d\n", update_delta, delta, sleep_time);
  424. #ifdef PROTOCOL_CHIBIOS
  425. // The gEventWait function really takes milliseconds, even if the documentation says ticks.
  426. // Unfortunately there's no generic ugfx conversion from system time to milliseconds,
  427. // so let's do it in a platform dependent way.
  428. // On windows the system ticks is the same as milliseconds anyway
  429. if (sleep_time != TIME_INFINITE) {
  430. sleep_time = ST2MS(sleep_time);
  431. }
  432. #endif
  433. geventEventWait(&event_listener, sleep_time);
  434. }
  435. #ifdef LCD_ENABLE
  436. gdispCloseFont(state.font_fixed5x8);
  437. gdispCloseFont(state.font_dejavusansbold12);
  438. #endif
  439. return 0;
  440. }
  441. void visualizer_init(void) {
  442. gfxInit();
  443. #ifdef LCD_BACKLIGHT_ENABLE
  444. lcd_backlight_init();
  445. #endif
  446. #ifdef SERIAL_LINK_ENABLE
  447. add_remote_objects(remote_objects, sizeof(remote_objects) / sizeof(remote_object_t*) );
  448. #endif
  449. #ifdef LCD_ENABLE
  450. LCD_DISPLAY = get_lcd_display();
  451. #endif
  452. #ifdef LED_ENABLE
  453. LED_DISPLAY = get_led_display();
  454. #endif
  455. // We are using a low priority thread, the idea is to have it run only
  456. // when the main thread is sleeping during the matrix scanning
  457. gfxThreadCreate(visualizerThreadStack, sizeof(visualizerThreadStack),
  458. VISUALIZER_THREAD_PRIORITY, visualizerThread, NULL);
  459. }
  460. void update_status(bool changed) {
  461. if (changed) {
  462. GSourceListener* listener = geventGetSourceListener((GSourceHandle)&current_status, NULL);
  463. if (listener) {
  464. geventSendEvent(listener);
  465. }
  466. }
  467. #ifdef SERIAL_LINK_ENABLE
  468. static systime_t last_update = 0;
  469. systime_t current_update = chVTGetSystemTimeX();
  470. systime_t delta = current_update - last_update;
  471. if (changed || delta > MS2ST(10)) {
  472. last_update = current_update;
  473. visualizer_keyboard_status_t* r = begin_write_current_status();
  474. *r = current_status;
  475. end_write_current_status();
  476. }
  477. #endif
  478. }
  479. uint8_t visualizer_get_mods() {
  480. uint8_t mods = get_mods();
  481. #ifndef NO_ACTION_ONESHOT
  482. if (!has_oneshot_mods_timed_out()) {
  483. mods |= get_oneshot_mods();
  484. }
  485. #endif
  486. return mods;
  487. }
  488. void visualizer_update(uint32_t default_state, uint32_t state, uint8_t mods, uint32_t leds) {
  489. // Note that there's a small race condition here, the thread could read
  490. // a state where one of these are set but not the other. But this should
  491. // not really matter as it will be fixed during the next loop step.
  492. // Alternatively a mutex could be used instead of the volatile variables
  493. bool changed = false;
  494. #ifdef SERIAL_LINK_ENABLE
  495. if (is_serial_link_connected ()) {
  496. visualizer_keyboard_status_t* new_status = read_current_status();
  497. if (new_status) {
  498. if (!same_status(&current_status, new_status)) {
  499. changed = true;
  500. current_status = *new_status;
  501. }
  502. }
  503. }
  504. else {
  505. #else
  506. {
  507. #endif
  508. visualizer_keyboard_status_t new_status = {
  509. .layer = state,
  510. .default_layer = default_state,
  511. .mods = mods,
  512. .leds = leds,
  513. .suspended = current_status.suspended,
  514. };
  515. if (!same_status(&current_status, &new_status)) {
  516. changed = true;
  517. current_status = new_status;
  518. }
  519. }
  520. update_status(changed);
  521. }
  522. void visualizer_suspend(void) {
  523. current_status.suspended = true;
  524. update_status(true);
  525. }
  526. void visualizer_resume(void) {
  527. current_status.suspended = false;
  528. update_status(true);
  529. }