visualizer.c 16 KB

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