visualizer.c 18 KB

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