visualizer.c 16 KB

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