visualizer.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. The MIT License (MIT)
  3. Copyright (c) 2016 Fred Sundvik
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. */
  20. #ifndef VISUALIZER_H
  21. #define VISUALIZER_H
  22. #include <stdlib.h>
  23. #include <stdint.h>
  24. #include <stdbool.h>
  25. #include "gfx.h"
  26. #ifdef LCD_BACKLIGHT_ENABLE
  27. #include "lcd_backlight.h"
  28. #endif
  29. // This need to be called once at the start
  30. void visualizer_init(void);
  31. // This should be called at every matrix scan
  32. void visualizer_update(uint32_t default_state, uint32_t state, uint32_t leds);
  33. // This should be called when the keyboard goes to suspend state
  34. void visualizer_suspend(void);
  35. // This should be called when the keyboard wakes up from suspend state
  36. void visualizer_resume(void);
  37. // These functions are week, so they can be overridden by the keyboard
  38. // if needed
  39. GDisplay* get_lcd_display(void);
  40. GDisplay* get_led_display(void);
  41. // For emulator builds, this function need to be implemented
  42. #ifdef EMULATOR
  43. void draw_emulator(void);
  44. #endif
  45. // If you need support for more than 16 keyframes per animation, you can change this
  46. #define MAX_VISUALIZER_KEY_FRAMES 16
  47. struct keyframe_animation_t;
  48. typedef struct {
  49. uint32_t layer;
  50. uint32_t default_layer;
  51. uint32_t leds; // See led.h for available statuses
  52. bool suspended;
  53. } visualizer_keyboard_status_t;
  54. // The state struct is used by the various keyframe functions
  55. // It's also used for setting the LCD color and layer text
  56. // from the user customized code
  57. typedef struct visualizer_state_t {
  58. // The user code should primarily be modifying these
  59. uint32_t target_lcd_color;
  60. const char* layer_text;
  61. // The user visualizer(and animation functions) can read these
  62. visualizer_keyboard_status_t status;
  63. // These are used by the animation functions
  64. uint32_t current_lcd_color;
  65. uint32_t prev_lcd_color;
  66. #ifdef LCD_ENABLE
  67. font_t font_fixed5x8;
  68. font_t font_dejavusansbold12;
  69. #endif
  70. } visualizer_state_t;
  71. // Any custom keyframe function should have this signature
  72. // return true to get continuous updates, otherwise you will only get one
  73. // update per frame
  74. typedef bool (*frame_func)(struct keyframe_animation_t*, visualizer_state_t*);
  75. // Represents a keyframe animation, so fields are internal to the system
  76. // while others are meant to be initialized by the user code
  77. typedef struct keyframe_animation_t {
  78. // These should be initialized
  79. int num_frames;
  80. bool loop;
  81. int frame_lengths[MAX_VISUALIZER_KEY_FRAMES];
  82. frame_func frame_functions[MAX_VISUALIZER_KEY_FRAMES];
  83. // Used internally by the system, and can also be read by
  84. // keyframe update functions
  85. int current_frame;
  86. int time_left_in_frame;
  87. bool first_update_of_frame;
  88. bool last_update_of_frame;
  89. bool need_update;
  90. } keyframe_animation_t;
  91. extern GDisplay* LCD_DISPLAY;
  92. extern GDisplay* LED_DISPLAY;
  93. void start_keyframe_animation(keyframe_animation_t* animation);
  94. void stop_keyframe_animation(keyframe_animation_t* animation);
  95. // This runs the next keyframe, but does not update the animation state
  96. // Useful for crossfades for example
  97. void run_next_keyframe(keyframe_animation_t* animation, visualizer_state_t* state);
  98. // Some predefined keyframe functions that can be used by the user code
  99. // Does nothing, useful for adding delays
  100. bool keyframe_no_operation(keyframe_animation_t* animation, visualizer_state_t* state);
  101. // Animates the LCD backlight color between the current color and the target color (of the state)
  102. bool keyframe_animate_backlight_color(keyframe_animation_t* animation, visualizer_state_t* state);
  103. // Sets the backlight color to the target color
  104. bool keyframe_set_backlight_color(keyframe_animation_t* animation, visualizer_state_t* state);
  105. // Displays the layer text centered vertically on the screen
  106. bool keyframe_display_layer_text(keyframe_animation_t* animation, visualizer_state_t* state);
  107. // Displays a bitmap (0/1) of all the currently active layers
  108. bool keyframe_display_layer_bitmap(keyframe_animation_t* animation, visualizer_state_t* state);
  109. bool keyframe_disable_lcd_and_backlight(keyframe_animation_t* animation, visualizer_state_t* state);
  110. bool keyframe_enable_lcd_and_backlight(keyframe_animation_t* animation, visualizer_state_t* state);
  111. // Call this once, when the initial animation has finished, alternatively you can call it
  112. // directly from the initalize_user_visualizer function (the animation can be null)
  113. bool enable_visualization(keyframe_animation_t* animation, visualizer_state_t* state);
  114. // These functions have to be implemented by the user
  115. void initialize_user_visualizer(visualizer_state_t* state);
  116. void update_user_visualizer_state(visualizer_state_t* state);
  117. void user_visualizer_suspend(visualizer_state_t* state);
  118. void user_visualizer_resume(visualizer_state_t* state);
  119. #endif /* VISUALIZER_H */