visualizer.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. // Feel free to modify the animations below, or even add new ones if needed
  39. // Don't worry, if the startup animation is long, you can use the keyboard like normal
  40. // during that time
  41. static keyframe_animation_t startup_animation = {
  42. .num_frames = 2,
  43. .loop = false,
  44. .frame_lengths = {0, gfxMillisecondsToTicks(10000), 0},
  45. .frame_functions = {
  46. lcd_keyframe_draw_logo,
  47. backlight_keyframe_animate_color,
  48. },
  49. };
  50. static keyframe_animation_t lcd_layer_display = {
  51. .num_frames = 1,
  52. .loop = false,
  53. .frame_lengths = {gfxMillisecondsToTicks(0)},
  54. .frame_functions = {lcd_keyframe_display_layer_and_led_states}
  55. };
  56. static keyframe_animation_t suspend_animation = {
  57. .num_frames = 4,
  58. .loop = false,
  59. .frame_lengths = {0, gfxMillisecondsToTicks(1000), 0, 0},
  60. .frame_functions = {
  61. lcd_keyframe_display_layer_text,
  62. backlight_keyframe_animate_color,
  63. lcd_keyframe_disable,
  64. lcd_keyframe_disable,
  65. },
  66. };
  67. static keyframe_animation_t resume_animation = {
  68. .num_frames = 4,
  69. .loop = false,
  70. .frame_lengths = {0, 0, 0, gfxMillisecondsToTicks(10000), 0},
  71. .frame_functions = {
  72. lcd_keyframe_enable,
  73. backlight_keyframe_enable,
  74. lcd_keyframe_draw_logo,
  75. backlight_keyframe_animate_color,
  76. },
  77. };
  78. // The color animation animates the LCD color when you change layers
  79. static keyframe_animation_t color_animation = {
  80. .num_frames = 2,
  81. .loop = false,
  82. // Note that there's a 200 ms no-operation frame,
  83. // this prevents the color from changing when activating the layer
  84. // momentarily
  85. .frame_lengths = {gfxMillisecondsToTicks(200), gfxMillisecondsToTicks(500)},
  86. .frame_functions = {keyframe_no_operation, backlight_keyframe_animate_color},
  87. };
  88. void initialize_user_visualizer(visualizer_state_t* state) {
  89. // The brightness will be dynamically adjustable in the future
  90. // But for now, change it here.
  91. lcd_backlight_brightness(130);
  92. state->current_lcd_color = initial_color;
  93. state->target_lcd_color = logo_background_color;
  94. lcd_state = LCD_STATE_INITIAL;
  95. start_keyframe_animation(&startup_animation);
  96. }
  97. void update_user_visualizer_state(visualizer_state_t* state, visualizer_keyboard_status_t* prev_status) {
  98. // Add more tests, change the colors and layer texts here
  99. // Usually you want to check the high bits (higher layers first)
  100. // because that's the order layers are processed for keypresses
  101. // You can for check for example:
  102. // state->status.layer
  103. // state->status.default_layer
  104. // state->status.leds (see led.h for available statuses)
  105. uint8_t saturation = 60;
  106. if (state->status.leds & (1u << USB_LED_CAPS_LOCK)) {
  107. saturation = 255;
  108. }
  109. if (state->status.layer & 0x4) {
  110. state->target_lcd_color = LCD_COLOR(0, saturation, 0xFF);
  111. state->layer_text = "Media & Mouse";
  112. }
  113. else if (state->status.layer & 0x2) {
  114. state->target_lcd_color = LCD_COLOR(168, saturation, 0xFF);
  115. state->layer_text = "Symbol";
  116. }
  117. else {
  118. state->target_lcd_color = LCD_COLOR(84, saturation, 0xFF);
  119. state->layer_text = "Default";
  120. }
  121. if (lcd_state == LCD_STATE_INITIAL ||
  122. state->status.layer != prev_status->layer ||
  123. state->status.default_layer != prev_status->default_layer ||
  124. state->status.leds != prev_status->leds) {
  125. start_keyframe_animation(&color_animation);
  126. start_keyframe_animation(&lcd_layer_display);
  127. }
  128. // You can also stop existing animations, and start your custom ones here
  129. // remember that you should normally have only one animation for the LCD
  130. // and one for the background. But you can also combine them if you want.
  131. }
  132. void user_visualizer_suspend(visualizer_state_t* state) {
  133. state->layer_text = "Suspending...";
  134. uint8_t hue = LCD_HUE(state->current_lcd_color);
  135. uint8_t sat = LCD_SAT(state->current_lcd_color);
  136. state->target_lcd_color = LCD_COLOR(hue, sat, 0);
  137. start_keyframe_animation(&suspend_animation);
  138. }
  139. void user_visualizer_resume(visualizer_state_t* state) {
  140. state->current_lcd_color = initial_color;
  141. state->target_lcd_color = logo_background_color;
  142. lcd_state = LCD_STATE_INITIAL;
  143. start_keyframe_animation(&resume_animation);
  144. }