visualizer.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* Copyright 2017 Fred Sundvik
  2. *
  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. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #ifndef VISUALIZER_H_
  17. #define VISUALIZER_H_
  18. // Currently we are assuming that both the backlight and LCD are enabled
  19. // But it's entirely possible to write a custom visualizer that use only
  20. // one of them
  21. #ifndef LCD_BACKLIGHT_ENABLE
  22. #error This visualizer needs that LCD backlight is enabled
  23. #endif
  24. #ifndef LCD_ENABLE
  25. #error This visualizer needs that LCD is enabled
  26. #endif
  27. #include "visualizer.h"
  28. #include "visualizer_keyframes.h"
  29. #include "lcd_keyframes.h"
  30. #include "lcd_backlight_keyframes.h"
  31. #include "system/serial_link.h"
  32. #include "led.h"
  33. #include "default_animations.h"
  34. static const uint32_t logo_background_color = LCD_COLOR(0x00, 0x00, 0xFF);
  35. static const uint32_t initial_color = LCD_COLOR(0, 0, 0);
  36. static bool initial_update = true;
  37. // Feel free to modify the animations below, or even add new ones if needed
  38. extern keyframe_animation_t KITT_Scanner_animation;
  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, lcd_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. initial_update = true;
  62. start_keyframe_animation(&default_startup_animation);
  63. }
  64. // This function should be implemented by the keymap visualizer
  65. // Don't change anything else than state->target_lcd_color and state->layer_text as that's the only thing
  66. // that the simple_visualizer assumes that you are updating
  67. // Also make sure that the buffer passed to state->layer_text remains valid until the previous animation is
  68. // stopped. This can be done by either double buffering it or by using constant strings
  69. static void get_visualizer_layer_and_color(visualizer_state_t* state);
  70. void update_user_visualizer_state(visualizer_state_t* state, visualizer_keyboard_status_t* prev_status) {
  71. // Add more tests, change the colors and layer texts here
  72. // Usually you want to check the high bits (higher layers first)
  73. // because that's the order layers are processed for keypresses
  74. // You can for check for example:
  75. // state->status.layer
  76. // state->status.default_layer
  77. // state->status.leds (see led.h for available statuses)
  78. uint32_t prev_color = state->target_lcd_color;
  79. const char* prev_layer_text = state->layer_text;
  80. get_visualizer_layer_and_color(state);
  81. if (initial_update || prev_color != state->target_lcd_color) {
  82. start_keyframe_animation(&color_animation);
  83. }
  84. if (initial_update || prev_layer_text != state->layer_text) {
  85. start_keyframe_animation(&lcd_layer_display);
  86. }
  87. // You can also stop existing animations, and start your custom ones here
  88. // remember that you should normally have only one animation for the LCD
  89. // and one for the background. But you can also combine them if you want.
  90. }
  91. void user_visualizer_suspend(visualizer_state_t* state) {
  92. state->layer_text = "Suspending...";
  93. uint8_t hue = LCD_HUE(state->current_lcd_color);
  94. uint8_t sat = LCD_SAT(state->current_lcd_color);
  95. state->target_lcd_color = LCD_COLOR(hue, sat, 0);
  96. start_keyframe_animation(&default_suspend_animation);
  97. }
  98. void user_visualizer_resume(visualizer_state_t* state) {
  99. state->current_lcd_color = initial_color;
  100. state->target_lcd_color = logo_background_color;
  101. initial_update = true;
  102. start_keyframe_animation(&default_startup_animation);
  103. }
  104. #endif /* VISUALIZER_H_ */