visualizer.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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 "lcd_keyframes.h"
  25. #include "lcd_backlight_keyframes.h"
  26. #include "system/serial_link.h"
  27. // To generate an image array like this
  28. // Ensure the image is 128 x 32 or smaller
  29. // Convert the bitmap to a C array using a program like http://www.riuson.com/lcd-image-converter/
  30. // Ensure the the conversion process produces a monochrome format array - 1 bit/pixel, left to right, top to bottom
  31. // Update array in the source code with the C array produced by the conversion program
  32. // The image below is generated from lcd_logo.png
  33. static const uint8_t image_data_lcd_logo[512] = {
  34. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  35. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  36. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  37. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  38. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  39. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  40. 0x00, 0xf8, 0xfe, 0x3e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  41. 0x00, 0x38, 0x38, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  42. 0x00, 0x38, 0x38, 0x38, 0x06, 0x29, 0x41, 0x24, 0x52, 0x24, 0x46, 0x00, 0x00, 0x00, 0x00, 0x00,
  43. 0x00, 0x38, 0x38, 0x38, 0x09, 0x55, 0x42, 0xaa, 0xaa, 0xaa, 0xa8, 0x00, 0x00, 0x00, 0x00, 0x00,
  44. 0x00, 0x38, 0x38, 0x38, 0x09, 0x55, 0x82, 0x28, 0xaa, 0xae, 0x8c, 0x00, 0x00, 0x00, 0x00, 0x00,
  45. 0x00, 0x38, 0x38, 0x38, 0x09, 0x55, 0x43, 0x28, 0xaa, 0xaa, 0x88, 0x00, 0x00, 0x00, 0x00, 0x00,
  46. 0x00, 0x38, 0x38, 0x38, 0x0a, 0x55, 0x42, 0x28, 0xaa, 0xaa, 0x88, 0x00, 0x00, 0x00, 0x00, 0x00,
  47. 0x00, 0x38, 0x38, 0x38, 0x05, 0x45, 0x42, 0x28, 0x89, 0x4a, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00,
  48. 0x00, 0x18, 0x38, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  49. 0x00, 0x1c, 0x38, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  50. 0x00, 0x0e, 0x38, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  51. 0x00, 0x03, 0xff, 0x80, 0x04, 0x45, 0x14, 0xa4, 0x92, 0x83, 0x52, 0x22, 0x22, 0x36, 0x00, 0x00,
  52. 0x00, 0x00, 0x38, 0x00, 0x0a, 0xaa, 0xaa, 0xaa, 0xba, 0x84, 0x55, 0x55, 0x57, 0x45, 0x00, 0x00,
  53. 0x00, 0x00, 0x38, 0x00, 0x08, 0xaa, 0xaa, 0xaa, 0x92, 0xb2, 0x55, 0x55, 0x42, 0x65, 0x00, 0x00,
  54. 0x00, 0x00, 0x38, 0x00, 0x08, 0xaa, 0xaa, 0xaa, 0x92, 0x81, 0x56, 0x65, 0x42, 0x45, 0x00, 0x00,
  55. 0x00, 0x00, 0x38, 0x00, 0x0a, 0xaa, 0xaa, 0xaa, 0x92, 0x81, 0x54, 0x45, 0x42, 0x45, 0x00, 0x00,
  56. 0x00, 0x00, 0x38, 0x00, 0x04, 0x48, 0xa2, 0x4a, 0x89, 0x06, 0x24, 0x42, 0x41, 0x36, 0x00, 0x00,
  57. 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  58. 0x00, 0x00, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  59. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  60. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  61. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  62. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  63. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  64. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  65. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  66. };
  67. static const uint32_t logo_background_color = LCD_COLOR(0x00, 0x00, 0xFF);
  68. static const uint32_t initial_color = LCD_COLOR(0, 0, 0);
  69. static const uint32_t led_emulation_colors[4] = {
  70. LCD_COLOR(0, 0, 0),
  71. LCD_COLOR(255, 255, 255),
  72. LCD_COLOR(84, 255, 255),
  73. LCD_COLOR(168, 255, 255),
  74. };
  75. static uint32_t next_led_target_color = 0;
  76. typedef enum {
  77. LCD_STATE_INITIAL,
  78. LCD_STATE_LAYER_BITMAP,
  79. LCD_STATE_BITMAP_AND_LEDS,
  80. } lcd_state_t;
  81. static lcd_state_t lcd_state = LCD_STATE_INITIAL;
  82. typedef struct {
  83. uint8_t led_on;
  84. uint8_t led1;
  85. uint8_t led2;
  86. uint8_t led3;
  87. } visualizer_user_data_t;
  88. // Don't access from visualization function, use the visualizer state instead
  89. static visualizer_user_data_t user_data_keyboard = {
  90. .led_on = 0,
  91. .led1 = LED_BRIGHTNESS_HI,
  92. .led2 = LED_BRIGHTNESS_HI,
  93. .led3 = LED_BRIGHTNESS_HI,
  94. };
  95. _Static_assert(sizeof(visualizer_user_data_t) <= VISUALIZER_USER_DATA_SIZE,
  96. "Please increase the VISUALIZER_USER_DATA_SIZE");
  97. bool display_logo(keyframe_animation_t* animation, visualizer_state_t* state) {
  98. (void)state;
  99. (void)animation;
  100. (void)state;
  101. // Read the uGFX documentation for information how to use the displays
  102. // http://wiki.ugfx.org/index.php/Main_Page
  103. gdispClear(White);
  104. // You can use static variables for things that can't be found in the animation
  105. // or state structs, here we use the image
  106. //gdispGBlitArea is a tricky function to use since it supports blitting part of the image
  107. // if you have full screen image, then just use 128 and 32 for both source and target dimensions
  108. gdispGBlitArea(GDISP, 0, 0, 128, 32, 0, 0, 128, (pixel_t*)image_data_lcd_logo);
  109. // Always remember to flush the display
  110. gdispFlush();
  111. return false;
  112. }
  113. // Feel free to modify the animations below, or even add new ones if needed
  114. // Don't worry, if the startup animation is long, you can use the keyboard like normal
  115. // during that time
  116. static keyframe_animation_t startup_animation = {
  117. .num_frames = 3,
  118. .loop = false,
  119. .frame_lengths = {0, gfxMillisecondsToTicks(10000), 0},
  120. .frame_functions = {
  121. display_logo,
  122. backlight_keyframe_animate_color,
  123. enable_visualization
  124. },
  125. };
  126. // The color animation animates the LCD color when you change layers
  127. static keyframe_animation_t one_led_color = {
  128. .num_frames = 1,
  129. .loop = false,
  130. .frame_lengths = {gfxMillisecondsToTicks(0)},
  131. .frame_functions = {backlight_keyframe_set_color},
  132. };
  133. bool swap_led_target_color(keyframe_animation_t* animation, visualizer_state_t* state) {
  134. uint32_t temp = next_led_target_color;
  135. next_led_target_color = state->target_lcd_color;
  136. state->target_lcd_color = temp;
  137. return false;
  138. }
  139. // The color animation animates the LCD color when you change layers
  140. static keyframe_animation_t two_led_colors = {
  141. .num_frames = 2,
  142. .loop = true,
  143. .frame_lengths = {gfxMillisecondsToTicks(1000), gfxMillisecondsToTicks(0)},
  144. .frame_functions = {backlight_keyframe_set_color, swap_led_target_color},
  145. };
  146. // The LCD animation alternates between the layer name display and a
  147. // bitmap that displays all active layers
  148. static keyframe_animation_t lcd_bitmap_animation = {
  149. .num_frames = 1,
  150. .loop = false,
  151. .frame_lengths = {gfxMillisecondsToTicks(0)},
  152. .frame_functions = {lcd_keyframe_display_layer_bitmap},
  153. };
  154. static keyframe_animation_t lcd_bitmap_leds_animation = {
  155. .num_frames = 2,
  156. .loop = true,
  157. .frame_lengths = {gfxMillisecondsToTicks(2000), gfxMillisecondsToTicks(2000)},
  158. .frame_functions = {lcd_keyframe_display_layer_bitmap, lcd_keyframe_display_led_states},
  159. };
  160. static keyframe_animation_t suspend_animation = {
  161. .num_frames = 4,
  162. .loop = false,
  163. .frame_lengths = {0, gfxMillisecondsToTicks(1000), 0, 0},
  164. .frame_functions = {
  165. lcd_keyframe_display_layer_text,
  166. backlight_keyframe_animate_color,
  167. lcd_keyframe_disable,
  168. lcd_keyframe_disable,
  169. },
  170. };
  171. static keyframe_animation_t resume_animation = {
  172. .num_frames = 5,
  173. .loop = false,
  174. .frame_lengths = {0, 0, 0, gfxMillisecondsToTicks(10000), 0},
  175. .frame_functions = {
  176. lcd_keyframe_enable,
  177. backlight_keyframe_enable,
  178. display_logo,
  179. backlight_keyframe_animate_color,
  180. enable_visualization,
  181. },
  182. };
  183. void initialize_user_visualizer(visualizer_state_t* state) {
  184. // The brightness will be dynamically adjustable in the future
  185. // But for now, change it here.
  186. lcd_backlight_brightness(130);
  187. state->current_lcd_color = initial_color;
  188. state->target_lcd_color = logo_background_color;
  189. lcd_state = LCD_STATE_INITIAL;
  190. start_keyframe_animation(&startup_animation);
  191. }
  192. static const uint32_t red;
  193. static const uint32_t green;
  194. static const uint32_t blue;
  195. inline bool is_led_on(visualizer_user_data_t* user_data, uint8_t num) {
  196. return user_data->led_on & (1u << num);
  197. }
  198. static uint8_t get_led_index_master(visualizer_user_data_t* user_data) {
  199. for (int i=0; i < 3; i++) {
  200. if (is_led_on(user_data, i)) {
  201. return i + 1;
  202. }
  203. }
  204. return 0;
  205. }
  206. static uint8_t get_led_index_slave(visualizer_user_data_t* user_data) {
  207. uint8_t master_index = get_led_index_master(user_data);
  208. if (master_index!=0) {
  209. for (int i=master_index; i < 3; i++) {
  210. if (is_led_on(user_data, i)) {
  211. return i + 1;
  212. }
  213. }
  214. }
  215. return 0;
  216. }
  217. static uint8_t get_secondary_led_index(visualizer_user_data_t* user_data) {
  218. if (is_led_on(user_data, 0) &&
  219. is_led_on(user_data, 1) &&
  220. is_led_on(user_data, 2)) {
  221. return 3;
  222. }
  223. return 0;
  224. }
  225. static uint8_t get_brightness(visualizer_user_data_t* user_data, uint8_t index) {
  226. switch (index) {
  227. case 1:
  228. return user_data->led1;
  229. case 2:
  230. return user_data->led2;
  231. case 3:
  232. return user_data->led3;
  233. }
  234. return 0;
  235. }
  236. static void update_emulated_leds(visualizer_state_t* state, visualizer_keyboard_status_t* prev_status) {
  237. visualizer_user_data_t* user_data_new = (visualizer_user_data_t*)state->status.user_data;
  238. visualizer_user_data_t* user_data_old = (visualizer_user_data_t*)prev_status->user_data;
  239. uint8_t new_index;
  240. uint8_t old_index;
  241. if (is_serial_link_master()) {
  242. new_index = get_led_index_master(user_data_new);
  243. old_index = get_led_index_master(user_data_old);
  244. }
  245. else {
  246. new_index = get_led_index_slave(user_data_new);
  247. old_index = get_led_index_slave(user_data_old);
  248. }
  249. uint8_t new_secondary_index = get_secondary_led_index(user_data_new);
  250. uint8_t old_secondary_index = get_secondary_led_index(user_data_old);
  251. uint8_t old_brightness = get_brightness(user_data_old, old_index);
  252. uint8_t new_brightness = get_brightness(user_data_new, new_index);
  253. uint8_t old_secondary_brightness = get_brightness(user_data_old, old_secondary_index);
  254. uint8_t new_secondary_brightness = get_brightness(user_data_new, new_secondary_index);
  255. if (lcd_state == LCD_STATE_INITIAL ||
  256. new_index != old_index ||
  257. new_secondary_index != old_secondary_index ||
  258. new_brightness != old_brightness ||
  259. new_secondary_brightness != old_secondary_brightness) {
  260. if (new_secondary_index != 0) {
  261. state->target_lcd_color = change_lcd_color_intensity(
  262. led_emulation_colors[new_index], new_brightness);
  263. next_led_target_color = change_lcd_color_intensity(
  264. led_emulation_colors[new_secondary_index], new_secondary_brightness);
  265. stop_keyframe_animation(&one_led_color);
  266. start_keyframe_animation(&two_led_colors);
  267. } else {
  268. state->target_lcd_color = change_lcd_color_intensity(
  269. led_emulation_colors[new_index], new_brightness);
  270. stop_keyframe_animation(&two_led_colors);
  271. start_keyframe_animation(&one_led_color);
  272. }
  273. }
  274. }
  275. static void update_lcd_text(visualizer_state_t* state, visualizer_keyboard_status_t* prev_status) {
  276. if (state->status.leds) {
  277. if (lcd_state != LCD_STATE_BITMAP_AND_LEDS ||
  278. state->status.leds != prev_status->leds ||
  279. state->status.layer != prev_status->layer ||
  280. state->status.default_layer != prev_status->default_layer) {
  281. // NOTE: that it doesn't matter if the animation isn't playing, stop will do nothing in that case
  282. stop_keyframe_animation(&lcd_bitmap_animation);
  283. lcd_state = LCD_STATE_BITMAP_AND_LEDS;
  284. // For information:
  285. // The logic in this function makes sure that this doesn't happen, but if you call start on an
  286. // animation that is already playing it will be restarted.
  287. start_keyframe_animation(&lcd_bitmap_leds_animation);
  288. }
  289. } else {
  290. if (lcd_state != LCD_STATE_LAYER_BITMAP ||
  291. state->status.layer != prev_status->layer ||
  292. state->status.default_layer != prev_status->default_layer) {
  293. stop_keyframe_animation(&lcd_bitmap_leds_animation);
  294. lcd_state = LCD_STATE_LAYER_BITMAP;
  295. start_keyframe_animation(&lcd_bitmap_animation);
  296. }
  297. }
  298. }
  299. void update_user_visualizer_state(visualizer_state_t* state, visualizer_keyboard_status_t* prev_status) {
  300. // Check the status here to start and stop animations
  301. // You might have to save some state, like the current animation here so that you can start the right
  302. // This function is called every time the status changes
  303. // NOTE that this is called from the visualizer thread, so don't access anything else outside the status
  304. // This is also important because the slave won't have access to the active layer for example outside the
  305. // status.
  306. update_emulated_leds(state, prev_status);
  307. update_lcd_text(state, prev_status);
  308. }
  309. void user_visualizer_suspend(visualizer_state_t* state) {
  310. state->layer_text = "Suspending...";
  311. uint8_t hue = LCD_HUE(state->current_lcd_color);
  312. uint8_t sat = LCD_SAT(state->current_lcd_color);
  313. state->target_lcd_color = LCD_COLOR(hue, sat, 0);
  314. start_keyframe_animation(&suspend_animation);
  315. }
  316. void user_visualizer_resume(visualizer_state_t* state) {
  317. state->current_lcd_color = initial_color;
  318. state->target_lcd_color = logo_background_color;
  319. lcd_state = LCD_STATE_INITIAL;
  320. start_keyframe_animation(&resume_animation);
  321. }
  322. void ergodox_board_led_on(void){
  323. // No board led support
  324. }
  325. void ergodox_right_led_1_on(void){
  326. user_data_keyboard.led_on |= (1u << 0);
  327. visualizer_set_user_data(&user_data_keyboard);
  328. }
  329. void ergodox_right_led_2_on(void){
  330. user_data_keyboard.led_on |= (1u << 1);
  331. visualizer_set_user_data(&user_data_keyboard);
  332. }
  333. void ergodox_right_led_3_on(void){
  334. user_data_keyboard.led_on |= (1u << 2);
  335. visualizer_set_user_data(&user_data_keyboard);
  336. }
  337. void ergodox_board_led_off(void){
  338. // No board led support
  339. }
  340. void ergodox_right_led_1_off(void){
  341. user_data_keyboard.led_on &= ~(1u << 0);
  342. visualizer_set_user_data(&user_data_keyboard);
  343. }
  344. void ergodox_right_led_2_off(void){
  345. user_data_keyboard.led_on &= ~(1u << 1);
  346. visualizer_set_user_data(&user_data_keyboard);
  347. }
  348. void ergodox_right_led_3_off(void){
  349. user_data_keyboard.led_on &= ~(1u << 2);
  350. visualizer_set_user_data(&user_data_keyboard);
  351. }
  352. void ergodox_right_led_1_set(uint8_t n) {
  353. user_data_keyboard.led1 = n;
  354. visualizer_set_user_data(&user_data_keyboard);
  355. }
  356. void ergodox_right_led_2_set(uint8_t n) {
  357. user_data_keyboard.led2 = n;
  358. visualizer_set_user_data(&user_data_keyboard);
  359. }
  360. void ergodox_right_led_3_set(uint8_t n) {
  361. user_data_keyboard.led3 = n;
  362. visualizer_set_user_data(&user_data_keyboard);
  363. }