visualizer.c 16 KB

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