visualizer.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. Copyright 2017 Fred Sundvik
  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 "system/serial_link.h"
  25. // To generate an image array like this
  26. // Ensure the image is 128 x 32 or smaller
  27. // Convert the bitmap to a C array using a program like http://www.riuson.com/lcd-image-converter/
  28. // Ensure the the conversion process produces a monochrome format array - 1 bit/pixel, left to right, top to bottom
  29. // Update array in the source code with the C array produced by the conversion program
  30. // The image below is generated from lcd_logo.png
  31. static const uint8_t image_data_lcd_logo[512] = {
  32. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  33. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  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, 0xf8, 0xfe, 0x3e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  39. 0x00, 0x38, 0x38, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  40. 0x00, 0x38, 0x38, 0x38, 0x06, 0x29, 0x41, 0x24, 0x52, 0x24, 0x46, 0x00, 0x00, 0x00, 0x00, 0x00,
  41. 0x00, 0x38, 0x38, 0x38, 0x09, 0x55, 0x42, 0xaa, 0xaa, 0xaa, 0xa8, 0x00, 0x00, 0x00, 0x00, 0x00,
  42. 0x00, 0x38, 0x38, 0x38, 0x09, 0x55, 0x82, 0x28, 0xaa, 0xae, 0x8c, 0x00, 0x00, 0x00, 0x00, 0x00,
  43. 0x00, 0x38, 0x38, 0x38, 0x09, 0x55, 0x43, 0x28, 0xaa, 0xaa, 0x88, 0x00, 0x00, 0x00, 0x00, 0x00,
  44. 0x00, 0x38, 0x38, 0x38, 0x0a, 0x55, 0x42, 0x28, 0xaa, 0xaa, 0x88, 0x00, 0x00, 0x00, 0x00, 0x00,
  45. 0x00, 0x38, 0x38, 0x38, 0x05, 0x45, 0x42, 0x28, 0x89, 0x4a, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00,
  46. 0x00, 0x18, 0x38, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  47. 0x00, 0x1c, 0x38, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  48. 0x00, 0x0e, 0x38, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  49. 0x00, 0x03, 0xff, 0x80, 0x04, 0x45, 0x14, 0xa4, 0x92, 0x83, 0x52, 0x22, 0x22, 0x36, 0x00, 0x00,
  50. 0x00, 0x00, 0x38, 0x00, 0x0a, 0xaa, 0xaa, 0xaa, 0xba, 0x84, 0x55, 0x55, 0x57, 0x45, 0x00, 0x00,
  51. 0x00, 0x00, 0x38, 0x00, 0x08, 0xaa, 0xaa, 0xaa, 0x92, 0xb2, 0x55, 0x55, 0x42, 0x65, 0x00, 0x00,
  52. 0x00, 0x00, 0x38, 0x00, 0x08, 0xaa, 0xaa, 0xaa, 0x92, 0x81, 0x56, 0x65, 0x42, 0x45, 0x00, 0x00,
  53. 0x00, 0x00, 0x38, 0x00, 0x0a, 0xaa, 0xaa, 0xaa, 0x92, 0x81, 0x54, 0x45, 0x42, 0x45, 0x00, 0x00,
  54. 0x00, 0x00, 0x38, 0x00, 0x04, 0x48, 0xa2, 0x4a, 0x89, 0x06, 0x24, 0x42, 0x41, 0x36, 0x00, 0x00,
  55. 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  56. 0x00, 0x00, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  57. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  58. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 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. };
  65. static const uint32_t logo_background_color = LCD_COLOR(0x00, 0x00, 0xFF);
  66. static const uint32_t initial_color = LCD_COLOR(0, 0, 0);
  67. typedef enum {
  68. LCD_STATE_INITIAL,
  69. LCD_STATE_LAYER_BITMAP,
  70. LCD_STATE_BITMAP_AND_LEDS,
  71. } lcd_state_t;
  72. static lcd_state_t lcd_state = LCD_STATE_INITIAL;
  73. bool display_logo(keyframe_animation_t* animation, visualizer_state_t* state) {
  74. (void)state;
  75. (void)animation;
  76. (void)state;
  77. // Read the uGFX documentation for information how to use the displays
  78. // http://wiki.ugfx.org/index.php/Main_Page
  79. gdispClear(White);
  80. // You can use static variables for things that can't be found in the animation
  81. // or state structs, here we use the image
  82. //gdispGBlitArea is a tricky function to use since it supports blitting part of the image
  83. // if you have full screen image, then just use 128 and 32 for both source and target dimensions
  84. gdispGBlitArea(GDISP, 0, 0, 128, 32, 0, 0, 128, (pixel_t*)image_data_lcd_logo);
  85. // Always remember to flush the display
  86. gdispFlush();
  87. return false;
  88. }
  89. // Feel free to modify the animations below, or even add new ones if needed
  90. // Don't worry, if the startup animation is long, you can use the keyboard like normal
  91. // during that time
  92. static keyframe_animation_t startup_animation = {
  93. .num_frames = 3,
  94. .loop = false,
  95. .frame_lengths = {0, gfxMillisecondsToTicks(10000), 0},
  96. .frame_functions = {
  97. display_logo,
  98. keyframe_animate_backlight_color,
  99. enable_visualization
  100. },
  101. };
  102. // The LCD animation alternates between the layer name display and a
  103. // bitmap that displays all active layers
  104. static keyframe_animation_t lcd_layer_bitmap_animation = {
  105. .num_frames = 2,
  106. .loop = true,
  107. .frame_lengths = {
  108. gfxMillisecondsToTicks(2000),
  109. gfxMillisecondsToTicks(2000)
  110. },
  111. .frame_functions = {
  112. keyframe_display_layer_text,
  113. keyframe_display_layer_bitmap
  114. },
  115. };
  116. static keyframe_animation_t lcd_layer_bitmap_leds_animation = {
  117. .num_frames = 3,
  118. .loop = true,
  119. .frame_lengths = {
  120. gfxMillisecondsToTicks(2000),
  121. gfxMillisecondsToTicks(2000),
  122. gfxMillisecondsToTicks(2000)
  123. },
  124. .frame_functions = {
  125. keyframe_display_layer_text,
  126. keyframe_display_led_states,
  127. keyframe_display_layer_bitmap,
  128. },
  129. };
  130. static keyframe_animation_t suspend_animation = {
  131. .num_frames = 3,
  132. .loop = false,
  133. .frame_lengths = {0, gfxMillisecondsToTicks(1000), 0},
  134. .frame_functions = {
  135. keyframe_display_layer_text,
  136. keyframe_animate_backlight_color,
  137. keyframe_disable_lcd_and_backlight,
  138. },
  139. };
  140. static keyframe_animation_t resume_animation = {
  141. .num_frames = 4,
  142. .loop = false,
  143. .frame_lengths = {0, 0, gfxMillisecondsToTicks(10000), 0},
  144. .frame_functions = {
  145. keyframe_enable_lcd_and_backlight,
  146. display_logo,
  147. keyframe_animate_backlight_color,
  148. enable_visualization,
  149. },
  150. };
  151. // The color animation animates the LCD color when you change layers
  152. static keyframe_animation_t color_animation = {
  153. .num_frames = 2,
  154. .loop = false,
  155. // Note that there's a 200 ms no-operation frame,
  156. // this prevents the color from changing when activating the layer
  157. // momentarily
  158. .frame_lengths = {gfxMillisecondsToTicks(200), gfxMillisecondsToTicks(500)},
  159. .frame_functions = {keyframe_no_operation, keyframe_animate_backlight_color},
  160. };
  161. void initialize_user_visualizer(visualizer_state_t* state) {
  162. // The brightness will be dynamically adjustable in the future
  163. // But for now, change it here.
  164. lcd_backlight_brightness(130);
  165. state->current_lcd_color = initial_color;
  166. state->target_lcd_color = logo_background_color;
  167. lcd_state = LCD_STATE_INITIAL;
  168. start_keyframe_animation(&startup_animation);
  169. }
  170. void update_user_visualizer_state(visualizer_state_t* state, visualizer_keyboard_status_t* prev_status) {
  171. // Add more tests, change the colors and layer texts here
  172. // Usually you want to check the high bits (higher layers first)
  173. // because that's the order layers are processed for keypresses
  174. // You can for check for example:
  175. // state->status.layer
  176. // state->status.default_layer
  177. // state->status.leds (see led.h for available statuses)
  178. if (state->status.layer & 0x4) {
  179. state->target_lcd_color = LCD_COLOR(0, 0xB0, 0xFF);
  180. state->layer_text = "Media & Mouse";
  181. }
  182. else if (state->status.layer & 0x2) {
  183. state->target_lcd_color = LCD_COLOR(0x80, 0xB0, 0xFF);
  184. state->layer_text = "Symbol";
  185. }
  186. else {
  187. state->target_lcd_color = LCD_COLOR(0x40, 0xB0, 0xFF);
  188. state->layer_text = "Default";
  189. }
  190. if (lcd_state == LCD_STATE_INITIAL || state->status.layer != prev_status->layer) {
  191. start_keyframe_animation(&color_animation);
  192. }
  193. if (state->status.leds) {
  194. if (lcd_state != LCD_STATE_BITMAP_AND_LEDS ||
  195. state->status.leds != prev_status->leds ||
  196. state->status.layer != prev_status->layer ||
  197. state->status.default_layer != prev_status->default_layer) {
  198. // NOTE: that it doesn't matter if the animation isn't playing, stop will do nothing in that case
  199. stop_keyframe_animation(&lcd_layer_bitmap_animation);
  200. lcd_state = LCD_STATE_BITMAP_AND_LEDS;
  201. // For information:
  202. // The logic in this function makes sure that this doesn't happen, but if you call start on an
  203. // animation that is already playing it will be restarted.
  204. start_keyframe_animation(&lcd_layer_bitmap_leds_animation);
  205. }
  206. } else {
  207. if (lcd_state != LCD_STATE_LAYER_BITMAP ||
  208. state->status.layer != prev_status->layer ||
  209. state->status.default_layer != prev_status->default_layer) {
  210. stop_keyframe_animation(&lcd_layer_bitmap_leds_animation);
  211. lcd_state = LCD_STATE_LAYER_BITMAP;
  212. start_keyframe_animation(&lcd_layer_bitmap_animation);
  213. }
  214. }
  215. // You can also stop existing animations, and start your custom ones here
  216. // remember that you should normally have only one animation for the LCD
  217. // and one for the background. But you can also combine them if you want.
  218. }
  219. void user_visualizer_suspend(visualizer_state_t* state) {
  220. state->layer_text = "Suspending...";
  221. uint8_t hue = LCD_HUE(state->current_lcd_color);
  222. uint8_t sat = LCD_SAT(state->current_lcd_color);
  223. state->target_lcd_color = LCD_COLOR(hue, sat, 0);
  224. start_keyframe_animation(&suspend_animation);
  225. }
  226. void user_visualizer_resume(visualizer_state_t* state) {
  227. state->current_lcd_color = initial_color;
  228. state->target_lcd_color = logo_background_color;
  229. lcd_state = LCD_STATE_INITIAL;
  230. start_keyframe_animation(&resume_animation);
  231. }