visualizer.c 13 KB

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