visualizer.c 17 KB

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