visualizer.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 "visualizer_keyframes.h"
  25. #include "lcd_keyframes.h"
  26. #include "lcd_backlight_keyframes.h"
  27. #include "system/serial_link.h"
  28. #include "led.h"
  29. #include "resources/resources.h"
  30. static const uint32_t logo_background_color = LCD_COLOR(0x00, 0x00, 0xFF);
  31. static const uint32_t initial_color = LCD_COLOR(0, 0, 0);
  32. typedef enum {
  33. LCD_STATE_INITIAL,
  34. LCD_STATE_LAYER_BITMAP,
  35. LCD_STATE_BITMAP_AND_LEDS,
  36. } lcd_state_t;
  37. static lcd_state_t lcd_state = LCD_STATE_INITIAL;
  38. bool display_logo(keyframe_animation_t* animation, visualizer_state_t* state) {
  39. (void)state;
  40. (void)animation;
  41. (void)state;
  42. // Read the uGFX documentation for information how to use the displays
  43. // http://wiki.ugfx.org/index.php/Main_Page
  44. gdispClear(White);
  45. // You can use static variables for things that can't be found in the animation
  46. // or state structs, here we use the image
  47. //gdispGBlitArea is a tricky function to use since it supports blitting part of the image
  48. // if you have full screen image, then just use 128 and 32 for both source and target dimensions
  49. gdispGBlitArea(GDISP, 0, 0, 128, 32, 0, 0, 128, (pixel_t*)resource_lcd_logo);
  50. return false;
  51. }
  52. // Feel free to modify the animations below, or even add new ones if needed
  53. // Don't worry, if the startup animation is long, you can use the keyboard like normal
  54. // during that time
  55. static keyframe_animation_t startup_animation = {
  56. .num_frames = 2,
  57. .loop = false,
  58. .frame_lengths = {0, gfxMillisecondsToTicks(10000), 0},
  59. .frame_functions = {
  60. display_logo,
  61. backlight_keyframe_animate_color,
  62. },
  63. };
  64. static keyframe_animation_t lcd_layer_display = {
  65. .num_frames = 1,
  66. .loop = false,
  67. .frame_lengths = {gfxMillisecondsToTicks(0)},
  68. .frame_functions = {lcd_keyframe_display_layer_and_led_states}
  69. };
  70. static keyframe_animation_t suspend_animation = {
  71. .num_frames = 4,
  72. .loop = false,
  73. .frame_lengths = {0, gfxMillisecondsToTicks(1000), 0, 0},
  74. .frame_functions = {
  75. lcd_keyframe_display_layer_text,
  76. backlight_keyframe_animate_color,
  77. lcd_keyframe_disable,
  78. lcd_keyframe_disable,
  79. },
  80. };
  81. static keyframe_animation_t resume_animation = {
  82. .num_frames = 4,
  83. .loop = false,
  84. .frame_lengths = {0, 0, 0, gfxMillisecondsToTicks(10000), 0},
  85. .frame_functions = {
  86. lcd_keyframe_enable,
  87. backlight_keyframe_enable,
  88. display_logo,
  89. backlight_keyframe_animate_color,
  90. },
  91. };
  92. // The color animation animates the LCD color when you change layers
  93. static keyframe_animation_t color_animation = {
  94. .num_frames = 2,
  95. .loop = false,
  96. // Note that there's a 200 ms no-operation frame,
  97. // this prevents the color from changing when activating the layer
  98. // momentarily
  99. .frame_lengths = {gfxMillisecondsToTicks(200), gfxMillisecondsToTicks(500)},
  100. .frame_functions = {keyframe_no_operation, backlight_keyframe_animate_color},
  101. };
  102. void initialize_user_visualizer(visualizer_state_t* state) {
  103. // The brightness will be dynamically adjustable in the future
  104. // But for now, change it here.
  105. lcd_backlight_brightness(130);
  106. state->current_lcd_color = initial_color;
  107. state->target_lcd_color = logo_background_color;
  108. lcd_state = LCD_STATE_INITIAL;
  109. start_keyframe_animation(&startup_animation);
  110. }
  111. void update_user_visualizer_state(visualizer_state_t* state, visualizer_keyboard_status_t* prev_status) {
  112. // Add more tests, change the colors and layer texts here
  113. // Usually you want to check the high bits (higher layers first)
  114. // because that's the order layers are processed for keypresses
  115. // You can for check for example:
  116. // state->status.layer
  117. // state->status.default_layer
  118. // state->status.leds (see led.h for available statuses)
  119. uint8_t saturation = 60;
  120. if (state->status.leds & (1u << USB_LED_CAPS_LOCK)) {
  121. saturation = 255;
  122. }
  123. if (state->status.layer & 0x4) {
  124. state->target_lcd_color = LCD_COLOR(0, saturation, 0xFF);
  125. state->layer_text = "Media & Mouse";
  126. }
  127. else if (state->status.layer & 0x2) {
  128. state->target_lcd_color = LCD_COLOR(168, saturation, 0xFF);
  129. state->layer_text = "Symbol";
  130. }
  131. else {
  132. state->target_lcd_color = LCD_COLOR(84, saturation, 0xFF);
  133. state->layer_text = "Default";
  134. }
  135. if (lcd_state == LCD_STATE_INITIAL ||
  136. state->status.layer != prev_status->layer ||
  137. state->status.default_layer != prev_status->default_layer ||
  138. state->status.leds != prev_status->leds) {
  139. start_keyframe_animation(&color_animation);
  140. start_keyframe_animation(&lcd_layer_display);
  141. }
  142. // You can also stop existing animations, and start your custom ones here
  143. // remember that you should normally have only one animation for the LCD
  144. // and one for the background. But you can also combine them if you want.
  145. }
  146. void user_visualizer_suspend(visualizer_state_t* state) {
  147. state->layer_text = "Suspending...";
  148. uint8_t hue = LCD_HUE(state->current_lcd_color);
  149. uint8_t sat = LCD_SAT(state->current_lcd_color);
  150. state->target_lcd_color = LCD_COLOR(hue, sat, 0);
  151. start_keyframe_animation(&suspend_animation);
  152. }
  153. void user_visualizer_resume(visualizer_state_t* state) {
  154. state->current_lcd_color = initial_color;
  155. state->target_lcd_color = logo_background_color;
  156. lcd_state = LCD_STATE_INITIAL;
  157. start_keyframe_animation(&resume_animation);
  158. }