visualizer.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /*
  2. Copyright 2016 Fred Sundvik <fsundvik@gmail.com>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. // Currently we are assuming that both the backlight and LCD are enabled
  15. // But it's entirely possible to write a custom visualizer that use only
  16. // one of them
  17. #ifndef LCD_BACKLIGHT_ENABLE
  18. #error This visualizer needs that LCD backlight is enabled
  19. #endif
  20. #ifndef LCD_ENABLE
  21. #error This visualizer needs that LCD is enabled
  22. #endif
  23. #include "visualizer.h"
  24. #include "visualizer_keyframes.h"
  25. #include "lcd_keyframes.h"
  26. #include "lcd_backlight_keyframes.h"
  27. #include "system/serial_link.h"
  28. #include "resources/resources.h"
  29. static const uint32_t logo_background_color = LCD_COLOR(0x00, 0x00, 0xFF);
  30. static const uint32_t initial_color = LCD_COLOR(0, 0, 0);
  31. static const uint32_t led_emulation_colors[4] = {
  32. LCD_COLOR(0, 0, 0),
  33. LCD_COLOR(255, 255, 255),
  34. LCD_COLOR(84, 255, 255),
  35. LCD_COLOR(168, 255, 255),
  36. };
  37. static uint32_t next_led_target_color = 0;
  38. typedef enum {
  39. LCD_STATE_INITIAL,
  40. LCD_STATE_LAYER_BITMAP,
  41. LCD_STATE_BITMAP_AND_LEDS,
  42. } lcd_state_t;
  43. static lcd_state_t lcd_state = LCD_STATE_INITIAL;
  44. typedef struct {
  45. uint8_t led_on;
  46. uint8_t led1;
  47. uint8_t led2;
  48. uint8_t led3;
  49. } visualizer_user_data_t;
  50. // Don't access from visualization function, use the visualizer state instead
  51. static visualizer_user_data_t user_data_keyboard = {
  52. .led_on = 0,
  53. .led1 = LED_BRIGHTNESS_HI,
  54. .led2 = LED_BRIGHTNESS_HI,
  55. .led3 = LED_BRIGHTNESS_HI,
  56. };
  57. _Static_assert(sizeof(visualizer_user_data_t) <= VISUALIZER_USER_DATA_SIZE,
  58. "Please increase the VISUALIZER_USER_DATA_SIZE");
  59. bool display_logo(keyframe_animation_t* animation, visualizer_state_t* state) {
  60. (void)state;
  61. (void)animation;
  62. (void)state;
  63. // Read the uGFX documentation for information how to use the displays
  64. // http://wiki.ugfx.org/index.php/Main_Page
  65. gdispClear(White);
  66. // You can use static variables for things that can't be found in the animation
  67. // or state structs, here we use the image
  68. //gdispGBlitArea is a tricky function to use since it supports blitting part of the image
  69. // if you have full screen image, then just use 128 and 32 for both source and target dimensions
  70. gdispGBlitArea(GDISP, 0, 0, 128, 32, 0, 0, 128, (pixel_t*)resource_lcd_logo);
  71. return false;
  72. }
  73. // Feel free to modify the animations below, or even add new ones if needed
  74. // Don't worry, if the startup animation is long, you can use the keyboard like normal
  75. // during that time
  76. static keyframe_animation_t startup_animation = {
  77. .num_frames = 2,
  78. .loop = false,
  79. .frame_lengths = {0, gfxMillisecondsToTicks(10000), 0},
  80. .frame_functions = {
  81. display_logo,
  82. backlight_keyframe_animate_color,
  83. },
  84. };
  85. // The color animation animates the LCD color when you change layers
  86. static keyframe_animation_t one_led_color = {
  87. .num_frames = 1,
  88. .loop = false,
  89. .frame_lengths = {gfxMillisecondsToTicks(0)},
  90. .frame_functions = {backlight_keyframe_set_color},
  91. };
  92. bool swap_led_target_color(keyframe_animation_t* animation, visualizer_state_t* state) {
  93. uint32_t temp = next_led_target_color;
  94. next_led_target_color = state->target_lcd_color;
  95. state->target_lcd_color = temp;
  96. return false;
  97. }
  98. // The color animation animates the LCD color when you change layers
  99. static keyframe_animation_t two_led_colors = {
  100. .num_frames = 2,
  101. .loop = true,
  102. .frame_lengths = {gfxMillisecondsToTicks(1000), gfxMillisecondsToTicks(0)},
  103. .frame_functions = {backlight_keyframe_set_color, swap_led_target_color},
  104. };
  105. // The LCD animation alternates between the layer name display and a
  106. // bitmap that displays all active layers
  107. static keyframe_animation_t lcd_bitmap_animation = {
  108. .num_frames = 1,
  109. .loop = false,
  110. .frame_lengths = {gfxMillisecondsToTicks(0)},
  111. .frame_functions = {lcd_keyframe_display_layer_bitmap},
  112. };
  113. static keyframe_animation_t lcd_bitmap_leds_animation = {
  114. .num_frames = 2,
  115. .loop = true,
  116. .frame_lengths = {gfxMillisecondsToTicks(2000), gfxMillisecondsToTicks(2000)},
  117. .frame_functions = {lcd_keyframe_display_layer_bitmap, lcd_keyframe_display_led_states},
  118. };
  119. static keyframe_animation_t suspend_animation = {
  120. .num_frames = 4,
  121. .loop = false,
  122. .frame_lengths = {0, gfxMillisecondsToTicks(1000), 0, 0},
  123. .frame_functions = {
  124. lcd_keyframe_display_layer_text,
  125. backlight_keyframe_animate_color,
  126. lcd_keyframe_disable,
  127. lcd_keyframe_disable,
  128. },
  129. };
  130. static keyframe_animation_t resume_animation = {
  131. .num_frames = 4,
  132. .loop = false,
  133. .frame_lengths = {0, 0, 0, gfxMillisecondsToTicks(10000), 0},
  134. .frame_functions = {
  135. lcd_keyframe_enable,
  136. backlight_keyframe_enable,
  137. display_logo,
  138. backlight_keyframe_animate_color,
  139. },
  140. };
  141. void initialize_user_visualizer(visualizer_state_t* state) {
  142. // The brightness will be dynamically adjustable in the future
  143. // But for now, change it here.
  144. lcd_backlight_brightness(130);
  145. state->current_lcd_color = initial_color;
  146. state->target_lcd_color = logo_background_color;
  147. lcd_state = LCD_STATE_INITIAL;
  148. start_keyframe_animation(&startup_animation);
  149. }
  150. static const uint32_t red;
  151. static const uint32_t green;
  152. static const uint32_t blue;
  153. inline bool is_led_on(visualizer_user_data_t* user_data, uint8_t num) {
  154. return user_data->led_on & (1u << num);
  155. }
  156. static uint8_t get_led_index_master(visualizer_user_data_t* user_data) {
  157. for (int i=0; i < 3; i++) {
  158. if (is_led_on(user_data, i)) {
  159. return i + 1;
  160. }
  161. }
  162. return 0;
  163. }
  164. static uint8_t get_led_index_slave(visualizer_user_data_t* user_data) {
  165. uint8_t master_index = get_led_index_master(user_data);
  166. if (master_index!=0) {
  167. for (int i=master_index; i < 3; i++) {
  168. if (is_led_on(user_data, i)) {
  169. return i + 1;
  170. }
  171. }
  172. }
  173. return 0;
  174. }
  175. static uint8_t get_secondary_led_index(visualizer_user_data_t* user_data) {
  176. if (is_led_on(user_data, 0) &&
  177. is_led_on(user_data, 1) &&
  178. is_led_on(user_data, 2)) {
  179. return 3;
  180. }
  181. return 0;
  182. }
  183. static uint8_t get_brightness(visualizer_user_data_t* user_data, uint8_t index) {
  184. switch (index) {
  185. case 1:
  186. return user_data->led1;
  187. case 2:
  188. return user_data->led2;
  189. case 3:
  190. return user_data->led3;
  191. }
  192. return 0;
  193. }
  194. static void update_emulated_leds(visualizer_state_t* state, visualizer_keyboard_status_t* prev_status) {
  195. visualizer_user_data_t* user_data_new = (visualizer_user_data_t*)state->status.user_data;
  196. visualizer_user_data_t* user_data_old = (visualizer_user_data_t*)prev_status->user_data;
  197. uint8_t new_index;
  198. uint8_t old_index;
  199. if (is_serial_link_master()) {
  200. new_index = get_led_index_master(user_data_new);
  201. old_index = get_led_index_master(user_data_old);
  202. }
  203. else {
  204. new_index = get_led_index_slave(user_data_new);
  205. old_index = get_led_index_slave(user_data_old);
  206. }
  207. uint8_t new_secondary_index = get_secondary_led_index(user_data_new);
  208. uint8_t old_secondary_index = get_secondary_led_index(user_data_old);
  209. uint8_t old_brightness = get_brightness(user_data_old, old_index);
  210. uint8_t new_brightness = get_brightness(user_data_new, new_index);
  211. uint8_t old_secondary_brightness = get_brightness(user_data_old, old_secondary_index);
  212. uint8_t new_secondary_brightness = get_brightness(user_data_new, new_secondary_index);
  213. if (lcd_state == LCD_STATE_INITIAL ||
  214. new_index != old_index ||
  215. new_secondary_index != old_secondary_index ||
  216. new_brightness != old_brightness ||
  217. new_secondary_brightness != old_secondary_brightness) {
  218. if (new_secondary_index != 0) {
  219. state->target_lcd_color = change_lcd_color_intensity(
  220. led_emulation_colors[new_index], new_brightness);
  221. next_led_target_color = change_lcd_color_intensity(
  222. led_emulation_colors[new_secondary_index], new_secondary_brightness);
  223. stop_keyframe_animation(&one_led_color);
  224. start_keyframe_animation(&two_led_colors);
  225. } else {
  226. state->target_lcd_color = change_lcd_color_intensity(
  227. led_emulation_colors[new_index], new_brightness);
  228. stop_keyframe_animation(&two_led_colors);
  229. start_keyframe_animation(&one_led_color);
  230. }
  231. }
  232. }
  233. static void update_lcd_text(visualizer_state_t* state, visualizer_keyboard_status_t* prev_status) {
  234. if (state->status.leds) {
  235. if (lcd_state != LCD_STATE_BITMAP_AND_LEDS ||
  236. state->status.leds != prev_status->leds ||
  237. state->status.layer != prev_status->layer ||
  238. state->status.default_layer != prev_status->default_layer) {
  239. // NOTE: that it doesn't matter if the animation isn't playing, stop will do nothing in that case
  240. stop_keyframe_animation(&lcd_bitmap_animation);
  241. lcd_state = LCD_STATE_BITMAP_AND_LEDS;
  242. // For information:
  243. // The logic in this function makes sure that this doesn't happen, but if you call start on an
  244. // animation that is already playing it will be restarted.
  245. start_keyframe_animation(&lcd_bitmap_leds_animation);
  246. }
  247. } else {
  248. if (lcd_state != LCD_STATE_LAYER_BITMAP ||
  249. state->status.layer != prev_status->layer ||
  250. state->status.default_layer != prev_status->default_layer) {
  251. stop_keyframe_animation(&lcd_bitmap_leds_animation);
  252. lcd_state = LCD_STATE_LAYER_BITMAP;
  253. start_keyframe_animation(&lcd_bitmap_animation);
  254. }
  255. }
  256. }
  257. void update_user_visualizer_state(visualizer_state_t* state, visualizer_keyboard_status_t* prev_status) {
  258. // Check the status here to start and stop animations
  259. // You might have to save some state, like the current animation here so that you can start the right
  260. // This function is called every time the status changes
  261. // NOTE that this is called from the visualizer thread, so don't access anything else outside the status
  262. // This is also important because the slave won't have access to the active layer for example outside the
  263. // status.
  264. update_emulated_leds(state, prev_status);
  265. update_lcd_text(state, prev_status);
  266. }
  267. void user_visualizer_suspend(visualizer_state_t* state) {
  268. state->layer_text = "Suspending...";
  269. uint8_t hue = LCD_HUE(state->current_lcd_color);
  270. uint8_t sat = LCD_SAT(state->current_lcd_color);
  271. state->target_lcd_color = LCD_COLOR(hue, sat, 0);
  272. start_keyframe_animation(&suspend_animation);
  273. }
  274. void user_visualizer_resume(visualizer_state_t* state) {
  275. state->current_lcd_color = initial_color;
  276. state->target_lcd_color = logo_background_color;
  277. lcd_state = LCD_STATE_INITIAL;
  278. start_keyframe_animation(&resume_animation);
  279. }
  280. void ergodox_board_led_on(void){
  281. // No board led support
  282. }
  283. void ergodox_right_led_1_on(void){
  284. user_data_keyboard.led_on |= (1u << 0);
  285. visualizer_set_user_data(&user_data_keyboard);
  286. }
  287. void ergodox_right_led_2_on(void){
  288. user_data_keyboard.led_on |= (1u << 1);
  289. visualizer_set_user_data(&user_data_keyboard);
  290. }
  291. void ergodox_right_led_3_on(void){
  292. user_data_keyboard.led_on |= (1u << 2);
  293. visualizer_set_user_data(&user_data_keyboard);
  294. }
  295. void ergodox_board_led_off(void){
  296. // No board led support
  297. }
  298. void ergodox_right_led_1_off(void){
  299. user_data_keyboard.led_on &= ~(1u << 0);
  300. visualizer_set_user_data(&user_data_keyboard);
  301. }
  302. void ergodox_right_led_2_off(void){
  303. user_data_keyboard.led_on &= ~(1u << 1);
  304. visualizer_set_user_data(&user_data_keyboard);
  305. }
  306. void ergodox_right_led_3_off(void){
  307. user_data_keyboard.led_on &= ~(1u << 2);
  308. visualizer_set_user_data(&user_data_keyboard);
  309. }
  310. void ergodox_right_led_1_set(uint8_t n) {
  311. user_data_keyboard.led1 = n;
  312. visualizer_set_user_data(&user_data_keyboard);
  313. }
  314. void ergodox_right_led_2_set(uint8_t n) {
  315. user_data_keyboard.led2 = n;
  316. visualizer_set_user_data(&user_data_keyboard);
  317. }
  318. void ergodox_right_led_3_set(uint8_t n) {
  319. user_data_keyboard.led3 = n;
  320. visualizer_set_user_data(&user_data_keyboard);
  321. }