visualizer.c 15 KB

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