visualizer.c 15 KB

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