visualizer.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. #include "action_util.h"
  41. // Define this in config.h
  42. #ifndef VISUALIZER_THREAD_PRIORITY
  43. #define "Visualizer thread priority not defined"
  44. #endif
  45. static visualizer_keyboard_status_t current_status = {
  46. .layer = 0xFFFFFFFF,
  47. .default_layer = 0xFFFFFFFF,
  48. .mods = 0xFF,
  49. .leds = 0xFFFFFFFF,
  50. .suspended = false,
  51. #ifdef VISUALIZER_USER_DATA_SIZE
  52. .user_data = {0}
  53. #endif
  54. };
  55. static bool same_status(visualizer_keyboard_status_t* status1, visualizer_keyboard_status_t* status2) {
  56. return status1->layer == status2->layer &&
  57. status1->default_layer == status2->default_layer &&
  58. status1->mods == status2->mods &&
  59. status1->leds == status2->leds &&
  60. status1->suspended == status2->suspended
  61. #ifdef VISUALIZER_USER_DATA_SIZE
  62. && memcmp(status1->user_data, status2->user_data, VISUALIZER_USER_DATA_SIZE) == 0
  63. #endif
  64. ;
  65. }
  66. static bool visualizer_enabled = false;
  67. #ifdef VISUALIZER_USER_DATA_SIZE
  68. static uint8_t user_data[VISUALIZER_USER_DATA_SIZE];
  69. #endif
  70. #define MAX_SIMULTANEOUS_ANIMATIONS 4
  71. static keyframe_animation_t* animations[MAX_SIMULTANEOUS_ANIMATIONS] = {};
  72. #ifdef SERIAL_LINK_ENABLE
  73. MASTER_TO_ALL_SLAVES_OBJECT(current_status, visualizer_keyboard_status_t);
  74. static remote_object_t* remote_objects[] = {
  75. REMOTE_OBJECT(current_status),
  76. };
  77. #endif
  78. GDisplay* LCD_DISPLAY = 0;
  79. GDisplay* LED_DISPLAY = 0;
  80. __attribute__((weak))
  81. GDisplay* get_lcd_display(void) {
  82. return gdispGetDisplay(0);
  83. }
  84. __attribute__((weak))
  85. GDisplay* get_led_display(void) {
  86. return gdispGetDisplay(1);
  87. }
  88. void start_keyframe_animation(keyframe_animation_t* animation) {
  89. animation->current_frame = -1;
  90. animation->time_left_in_frame = 0;
  91. animation->need_update = true;
  92. int free_index = -1;
  93. for (int i=0;i<MAX_SIMULTANEOUS_ANIMATIONS;i++) {
  94. if (animations[i] == animation) {
  95. return;
  96. }
  97. if (free_index == -1 && animations[i] == NULL) {
  98. free_index=i;
  99. }
  100. }
  101. if (free_index!=-1) {
  102. animations[free_index] = animation;
  103. }
  104. }
  105. void stop_keyframe_animation(keyframe_animation_t* animation) {
  106. animation->current_frame = animation->num_frames;
  107. animation->time_left_in_frame = 0;
  108. animation->need_update = true;
  109. animation->first_update_of_frame = false;
  110. animation->last_update_of_frame = false;
  111. for (int i=0;i<MAX_SIMULTANEOUS_ANIMATIONS;i++) {
  112. if (animations[i] == animation) {
  113. animations[i] = NULL;
  114. return;
  115. }
  116. }
  117. }
  118. void stop_all_keyframe_animations(void) {
  119. for (int i=0;i<MAX_SIMULTANEOUS_ANIMATIONS;i++) {
  120. if (animations[i]) {
  121. animations[i]->current_frame = animations[i]->num_frames;
  122. animations[i]->time_left_in_frame = 0;
  123. animations[i]->need_update = true;
  124. animations[i]->first_update_of_frame = false;
  125. animations[i]->last_update_of_frame = false;
  126. animations[i] = NULL;
  127. }
  128. }
  129. }
  130. static bool update_keyframe_animation(keyframe_animation_t* animation, visualizer_state_t* state, systemticks_t delta, systemticks_t* sleep_time) {
  131. // TODO: Clean up this messy code
  132. dprintf("Animation frame%d, left %d, delta %d\n", animation->current_frame,
  133. animation->time_left_in_frame, delta);
  134. if (animation->current_frame == animation->num_frames) {
  135. animation->need_update = false;
  136. return false;
  137. }
  138. if (animation->current_frame == -1) {
  139. animation->current_frame = 0;
  140. animation->time_left_in_frame = animation->frame_lengths[0];
  141. animation->need_update = true;
  142. animation->first_update_of_frame = true;
  143. } else {
  144. animation->time_left_in_frame -= delta;
  145. while (animation->time_left_in_frame <= 0) {
  146. int left = animation->time_left_in_frame;
  147. if (animation->need_update) {
  148. animation->time_left_in_frame = 0;
  149. animation->last_update_of_frame = true;
  150. (*animation->frame_functions[animation->current_frame])(animation, state);
  151. animation->last_update_of_frame = false;
  152. }
  153. animation->current_frame++;
  154. animation->need_update = true;
  155. animation->first_update_of_frame = true;
  156. if (animation->current_frame == animation->num_frames) {
  157. if (animation->loop) {
  158. animation->current_frame = 0;
  159. }
  160. else {
  161. stop_keyframe_animation(animation);
  162. return false;
  163. }
  164. }
  165. delta = -left;
  166. animation->time_left_in_frame = animation->frame_lengths[animation->current_frame];
  167. animation->time_left_in_frame -= delta;
  168. }
  169. }
  170. if (animation->need_update) {
  171. animation->need_update = (*animation->frame_functions[animation->current_frame])(animation, state);
  172. animation->first_update_of_frame = false;
  173. }
  174. systemticks_t wanted_sleep = animation->need_update ? gfxMillisecondsToTicks(10) : (unsigned)animation->time_left_in_frame;
  175. if (wanted_sleep < *sleep_time) {
  176. *sleep_time = wanted_sleep;
  177. }
  178. return true;
  179. }
  180. void run_next_keyframe(keyframe_animation_t* animation, visualizer_state_t* state) {
  181. int next_frame = animation->current_frame + 1;
  182. if (next_frame == animation->num_frames) {
  183. next_frame = 0;
  184. }
  185. keyframe_animation_t temp_animation = *animation;
  186. temp_animation.current_frame = next_frame;
  187. temp_animation.time_left_in_frame = animation->frame_lengths[next_frame];
  188. temp_animation.first_update_of_frame = true;
  189. temp_animation.last_update_of_frame = false;
  190. temp_animation.need_update = false;
  191. visualizer_state_t temp_state = *state;
  192. (*temp_animation.frame_functions[next_frame])(&temp_animation, &temp_state);
  193. }
  194. bool keyframe_no_operation(keyframe_animation_t* animation, visualizer_state_t* state) {
  195. (void)animation;
  196. (void)state;
  197. return false;
  198. }
  199. bool keyframe_disable_lcd_and_backlight(keyframe_animation_t* animation, visualizer_state_t* state) {
  200. (void)animation;
  201. (void)state;
  202. #ifdef LCD_ENABLE
  203. gdispSetPowerMode(powerOff);
  204. #endif
  205. #ifdef LCD_BACKLIGHT_ENABLE
  206. lcd_backlight_hal_color(0, 0, 0);
  207. #endif
  208. return false;
  209. }
  210. bool keyframe_enable_lcd_and_backlight(keyframe_animation_t* animation, visualizer_state_t* state) {
  211. (void)animation;
  212. (void)state;
  213. #ifdef LCD_ENABLE
  214. gdispSetPowerMode(powerOn);
  215. #endif
  216. return false;
  217. }
  218. bool enable_visualization(keyframe_animation_t* animation, visualizer_state_t* state) {
  219. (void)animation;
  220. (void)state;
  221. dprint("User visualizer inited\n");
  222. visualizer_enabled = true;
  223. return false;
  224. }
  225. // TODO: Optimize the stack size, this is probably way too big
  226. static DECLARE_THREAD_STACK(visualizerThreadStack, 1024);
  227. static DECLARE_THREAD_FUNCTION(visualizerThread, arg) {
  228. (void)arg;
  229. GListener event_listener;
  230. geventListenerInit(&event_listener);
  231. geventAttachSource(&event_listener, (GSourceHandle)&current_status, 0);
  232. visualizer_keyboard_status_t initial_status = {
  233. .default_layer = 0xFFFFFFFF,
  234. .layer = 0xFFFFFFFF,
  235. .mods = 0xFF,
  236. .leds = 0xFFFFFFFF,
  237. .suspended = false,
  238. #ifdef VISUALIZER_USER_DATA_SIZE
  239. .user_data = {0},
  240. #endif
  241. };
  242. visualizer_state_t state = {
  243. .status = initial_status,
  244. .current_lcd_color = 0,
  245. #ifdef LCD_ENABLE
  246. .font_fixed5x8 = gdispOpenFont("fixed_5x8"),
  247. .font_dejavusansbold12 = gdispOpenFont("DejaVuSansBold12")
  248. #endif
  249. };
  250. initialize_user_visualizer(&state);
  251. state.prev_lcd_color = state.current_lcd_color;
  252. #ifdef LCD_BACKLIGHT_ENABLE
  253. lcd_backlight_color(
  254. LCD_HUE(state.current_lcd_color),
  255. LCD_SAT(state.current_lcd_color),
  256. LCD_INT(state.current_lcd_color));
  257. #endif
  258. systemticks_t sleep_time = TIME_INFINITE;
  259. systemticks_t current_time = gfxSystemTicks();
  260. while(true) {
  261. systemticks_t new_time = gfxSystemTicks();
  262. systemticks_t delta = new_time - current_time;
  263. current_time = new_time;
  264. bool enabled = visualizer_enabled;
  265. if (!same_status(&state.status, &current_status)) {
  266. if (visualizer_enabled) {
  267. if (current_status.suspended) {
  268. stop_all_keyframe_animations();
  269. visualizer_enabled = false;
  270. state.status = current_status;
  271. user_visualizer_suspend(&state);
  272. }
  273. else {
  274. visualizer_keyboard_status_t prev_status = state.status;
  275. state.status = current_status;
  276. update_user_visualizer_state(&state, &prev_status);
  277. }
  278. state.prev_lcd_color = state.current_lcd_color;
  279. }
  280. }
  281. if (!enabled && state.status.suspended && current_status.suspended == false) {
  282. // Setting the status to the initial status will force an update
  283. // when the visualizer is enabled again
  284. state.status = initial_status;
  285. state.status.suspended = false;
  286. stop_all_keyframe_animations();
  287. user_visualizer_resume(&state);
  288. state.prev_lcd_color = state.current_lcd_color;
  289. }
  290. sleep_time = TIME_INFINITE;
  291. for (int i=0;i<MAX_SIMULTANEOUS_ANIMATIONS;i++) {
  292. if (animations[i]) {
  293. update_keyframe_animation(animations[i], &state, delta, &sleep_time);
  294. }
  295. }
  296. #ifdef LED_ENABLE
  297. gdispGFlush(LED_DISPLAY);
  298. #endif
  299. #ifdef EMULATOR
  300. draw_emulator();
  301. #endif
  302. // The animation can enable the visualizer
  303. // And we might need to update the state when that happens
  304. // so don't sleep
  305. if (enabled != visualizer_enabled) {
  306. sleep_time = 0;
  307. }
  308. systemticks_t after_update = gfxSystemTicks();
  309. unsigned update_delta = after_update - current_time;
  310. if (sleep_time != TIME_INFINITE) {
  311. if (sleep_time > update_delta) {
  312. sleep_time -= update_delta;
  313. }
  314. else {
  315. sleep_time = 0;
  316. }
  317. }
  318. dprintf("Update took %d, last delta %d, sleep_time %d\n", update_delta, delta, sleep_time);
  319. #ifdef PROTOCOL_CHIBIOS
  320. // The gEventWait function really takes milliseconds, even if the documentation says ticks.
  321. // Unfortunately there's no generic ugfx conversion from system time to milliseconds,
  322. // so let's do it in a platform dependent way.
  323. // On windows the system ticks is the same as milliseconds anyway
  324. if (sleep_time != TIME_INFINITE) {
  325. sleep_time = ST2MS(sleep_time);
  326. }
  327. #endif
  328. geventEventWait(&event_listener, sleep_time);
  329. }
  330. #ifdef LCD_ENABLE
  331. gdispCloseFont(state.font_fixed5x8);
  332. gdispCloseFont(state.font_dejavusansbold12);
  333. #endif
  334. return 0;
  335. }
  336. void visualizer_init(void) {
  337. gfxInit();
  338. #ifdef LCD_BACKLIGHT_ENABLE
  339. lcd_backlight_init();
  340. #endif
  341. #ifdef SERIAL_LINK_ENABLE
  342. add_remote_objects(remote_objects, sizeof(remote_objects) / sizeof(remote_object_t*) );
  343. #endif
  344. #ifdef LCD_ENABLE
  345. LCD_DISPLAY = get_lcd_display();
  346. #endif
  347. #ifdef LED_ENABLE
  348. LED_DISPLAY = get_led_display();
  349. #endif
  350. // We are using a low priority thread, the idea is to have it run only
  351. // when the main thread is sleeping during the matrix scanning
  352. gfxThreadCreate(visualizerThreadStack, sizeof(visualizerThreadStack),
  353. VISUALIZER_THREAD_PRIORITY, visualizerThread, NULL);
  354. }
  355. void update_status(bool changed) {
  356. if (changed) {
  357. GSourceListener* listener = geventGetSourceListener((GSourceHandle)&current_status, NULL);
  358. if (listener) {
  359. geventSendEvent(listener);
  360. }
  361. }
  362. #ifdef SERIAL_LINK_ENABLE
  363. static systime_t last_update = 0;
  364. systime_t current_update = chVTGetSystemTimeX();
  365. systime_t delta = current_update - last_update;
  366. if (changed || delta > MS2ST(10)) {
  367. last_update = current_update;
  368. visualizer_keyboard_status_t* r = begin_write_current_status();
  369. *r = current_status;
  370. end_write_current_status();
  371. }
  372. #endif
  373. }
  374. uint8_t visualizer_get_mods() {
  375. uint8_t mods = get_mods();
  376. #ifndef NO_ACTION_ONESHOT
  377. if (!has_oneshot_mods_timed_out()) {
  378. mods |= get_oneshot_mods();
  379. }
  380. #endif
  381. return mods;
  382. }
  383. #ifdef VISUALIZER_USER_DATA_SIZE
  384. void visualizer_set_user_data(void* u) {
  385. memcpy(user_data, u, VISUALIZER_USER_DATA_SIZE);
  386. }
  387. #endif
  388. void visualizer_update(uint32_t default_state, uint32_t state, uint8_t mods, uint32_t leds) {
  389. // Note that there's a small race condition here, the thread could read
  390. // a state where one of these are set but not the other. But this should
  391. // not really matter as it will be fixed during the next loop step.
  392. // Alternatively a mutex could be used instead of the volatile variables
  393. bool changed = false;
  394. #ifdef SERIAL_LINK_ENABLE
  395. if (is_serial_link_connected ()) {
  396. visualizer_keyboard_status_t* new_status = read_current_status();
  397. if (new_status) {
  398. if (!same_status(&current_status, new_status)) {
  399. changed = true;
  400. current_status = *new_status;
  401. }
  402. }
  403. }
  404. else {
  405. #else
  406. {
  407. #endif
  408. visualizer_keyboard_status_t new_status = {
  409. .layer = state,
  410. .default_layer = default_state,
  411. .mods = mods,
  412. .leds = leds,
  413. .suspended = current_status.suspended,
  414. };
  415. #ifdef VISUALIZER_USER_DATA_SIZE
  416. memcpy(new_status.user_data, user_data, VISUALIZER_USER_DATA_SIZE);
  417. #endif
  418. if (!same_status(&current_status, &new_status)) {
  419. changed = true;
  420. current_status = new_status;
  421. }
  422. }
  423. update_status(changed);
  424. }
  425. void visualizer_suspend(void) {
  426. current_status.suspended = true;
  427. update_status(true);
  428. }
  429. void visualizer_resume(void) {
  430. current_status.suspended = false;
  431. update_status(true);
  432. }