You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

483 lines
17 KiB

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