visualizer.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 "animations.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. static keyframe_animation_t lcd_layer_display = {
  40. .num_frames = 1,
  41. .loop = false,
  42. .frame_lengths = {gfxMillisecondsToTicks(0)},
  43. .frame_functions = {lcd_keyframe_display_layer_and_led_states}
  44. };
  45. // The color animation animates the LCD color when you change layers
  46. static keyframe_animation_t color_animation = {
  47. .num_frames = 2,
  48. .loop = false,
  49. // Note that there's a 200 ms no-operation frame,
  50. // this prevents the color from changing when activating the layer
  51. // momentarily
  52. .frame_lengths = {gfxMillisecondsToTicks(200), gfxMillisecondsToTicks(500)},
  53. .frame_functions = {keyframe_no_operation, backlight_keyframe_animate_color},
  54. };
  55. void initialize_user_visualizer(visualizer_state_t* state) {
  56. // The brightness will be dynamically adjustable in the future
  57. // But for now, change it here.
  58. lcd_backlight_brightness(130);
  59. state->current_lcd_color = initial_color;
  60. state->target_lcd_color = logo_background_color;
  61. lcd_state = LCD_STATE_INITIAL;
  62. start_keyframe_animation(&default_startup_animation);
  63. }
  64. void update_user_visualizer_state(visualizer_state_t* state, visualizer_keyboard_status_t* prev_status) {
  65. // Add more tests, change the colors and layer texts here
  66. // Usually you want to check the high bits (higher layers first)
  67. // because that's the order layers are processed for keypresses
  68. // You can for check for example:
  69. // state->status.layer
  70. // state->status.default_layer
  71. // state->status.leds (see led.h for available statuses)
  72. uint8_t saturation = 60;
  73. if (state->status.leds & (1u << USB_LED_CAPS_LOCK)) {
  74. saturation = 255;
  75. }
  76. if (state->status.layer & 0x4) {
  77. state->target_lcd_color = LCD_COLOR(0, saturation, 0xFF);
  78. state->layer_text = "Media & Mouse";
  79. }
  80. else if (state->status.layer & 0x2) {
  81. state->target_lcd_color = LCD_COLOR(168, saturation, 0xFF);
  82. state->layer_text = "Symbol";
  83. }
  84. else {
  85. state->target_lcd_color = LCD_COLOR(84, saturation, 0xFF);
  86. state->layer_text = "Default";
  87. }
  88. if (lcd_state == LCD_STATE_INITIAL ||
  89. state->status.layer != prev_status->layer ||
  90. state->status.default_layer != prev_status->default_layer ||
  91. state->status.leds != prev_status->leds) {
  92. start_keyframe_animation(&color_animation);
  93. start_keyframe_animation(&lcd_layer_display);
  94. }
  95. // You can also stop existing animations, and start your custom ones here
  96. // remember that you should normally have only one animation for the LCD
  97. // and one for the background. But you can also combine them if you want.
  98. }
  99. void user_visualizer_suspend(visualizer_state_t* state) {
  100. state->layer_text = "Suspending...";
  101. uint8_t hue = LCD_HUE(state->current_lcd_color);
  102. uint8_t sat = LCD_SAT(state->current_lcd_color);
  103. state->target_lcd_color = LCD_COLOR(hue, sat, 0);
  104. start_keyframe_animation(&default_suspend_animation);
  105. }
  106. void user_visualizer_resume(visualizer_state_t* state) {
  107. state->current_lcd_color = initial_color;
  108. state->target_lcd_color = logo_background_color;
  109. lcd_state = LCD_STATE_INITIAL;
  110. start_keyframe_animation(&default_startup_animation);
  111. }