visualizer.h 4.6 KB

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