visualizer.c 12 KB

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