visualizer.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. #ifdef LCD_ENABLE
  26. #include "gfx.h"
  27. #endif
  28. #ifdef LCD_BACKLIGHT_ENABLE
  29. #include "lcd_backlight.h"
  30. #endif
  31. // This need to be called once at the start
  32. void visualizer_init(void);
  33. // This should be called at every matrix scan
  34. void visualizer_update(uint32_t default_state, uint32_t state, uint32_t leds);
  35. // This should be called when the keyboard goes to suspend state
  36. void visualizer_suspend(void);
  37. // This should be called when the keyboard wakes up from suspend state
  38. void visualizer_resume(void);
  39. // If you need support for more than 8 keyframes per animation, you can change this
  40. #define MAX_VISUALIZER_KEY_FRAMES 8
  41. struct keyframe_animation_t;
  42. typedef struct {
  43. uint32_t layer;
  44. uint32_t default_layer;
  45. uint32_t leds; // See led.h for available statuses
  46. bool suspended;
  47. } visualizer_keyboard_status_t;
  48. // The state struct is used by the various keyframe functions
  49. // It's also used for setting the LCD color and layer text
  50. // from the user customized code
  51. typedef struct visualizer_state_t {
  52. // The user code should primarily be modifying these
  53. uint32_t target_lcd_color;
  54. const char* layer_text;
  55. // The user visualizer(and animation functions) can read these
  56. visualizer_keyboard_status_t status;
  57. // These are used by the animation functions
  58. uint32_t current_lcd_color;
  59. uint32_t prev_lcd_color;
  60. #ifdef LCD_ENABLE
  61. font_t font_fixed5x8;
  62. font_t font_dejavusansbold12;
  63. #endif
  64. } visualizer_state_t;
  65. // Any custom keyframe function should have this signature
  66. // return true to get continuous updates, otherwise you will only get one
  67. // update per frame
  68. typedef bool (*frame_func)(struct keyframe_animation_t*, visualizer_state_t*);
  69. // Represents a keyframe animation, so fields are internal to the system
  70. // while others are meant to be initialized by the user code
  71. typedef struct keyframe_animation_t {
  72. // These should be initialized
  73. int num_frames;
  74. bool loop;
  75. int frame_lengths[MAX_VISUALIZER_KEY_FRAMES];
  76. frame_func frame_functions[MAX_VISUALIZER_KEY_FRAMES];
  77. // Used internally by the system, and can also be read by
  78. // keyframe update functions
  79. int current_frame;
  80. int time_left_in_frame;
  81. bool need_update;
  82. } keyframe_animation_t;
  83. void start_keyframe_animation(keyframe_animation_t* animation);
  84. void stop_keyframe_animation(keyframe_animation_t* animation);
  85. // Some predefined keyframe functions that can be used by the user code
  86. // Does nothing, useful for adding delays
  87. bool keyframe_no_operation(keyframe_animation_t* animation, visualizer_state_t* state);
  88. // Animates the LCD backlight color between the current color and the target color (of the state)
  89. bool keyframe_animate_backlight_color(keyframe_animation_t* animation, visualizer_state_t* state);
  90. // Sets the backlight color to the target color
  91. bool keyframe_set_backlight_color(keyframe_animation_t* animation, visualizer_state_t* state);
  92. // Displays the layer text centered vertically on the screen
  93. bool keyframe_display_layer_text(keyframe_animation_t* animation, visualizer_state_t* state);
  94. // Displays a bitmap (0/1) of all the currently active layers
  95. bool keyframe_display_layer_bitmap(keyframe_animation_t* animation, visualizer_state_t* state);
  96. bool keyframe_disable_lcd_and_backlight(keyframe_animation_t* animation, visualizer_state_t* state);
  97. bool keyframe_enable_lcd_and_backlight(keyframe_animation_t* animation, visualizer_state_t* state);
  98. // Call this once, when the initial animation has finished, alternatively you can call it
  99. // directly from the initalize_user_visualizer function (the animation can be null)
  100. bool enable_visualization(keyframe_animation_t* animation, visualizer_state_t* state);
  101. // These two functions have to be implemented by the user
  102. void initialize_user_visualizer(visualizer_state_t* state);
  103. void update_user_visualizer_state(visualizer_state_t* state);
  104. void user_visualizer_suspend(visualizer_state_t* state);
  105. void user_visualizer_resume(visualizer_state_t* state);
  106. #endif /* VISUALIZER_H */