You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
5.5 KiB

  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 "config.h"
  26. #include "gfx.h"
  27. #include "action_layer.h"
  28. #ifdef LCD_BACKLIGHT_ENABLE
  29. # include "lcd_backlight.h"
  30. #endif
  31. #ifdef BACKLIGHT_ENABLE
  32. # include "backlight.h"
  33. #endif
  34. // use this function to merge both real_mods and oneshot_mods in a uint16_t
  35. uint8_t visualizer_get_mods(void);
  36. // This need to be called once at the start
  37. void visualizer_init(void);
  38. // This should be called at every matrix scan
  39. void visualizer_update(layer_state_t default_state, layer_state_t state, uint8_t mods, uint32_t leds);
  40. // This should be called when the keyboard goes to suspend state
  41. void visualizer_suspend(void);
  42. // This should be called when the keyboard wakes up from suspend state
  43. void visualizer_resume(void);
  44. // These functions are week, so they can be overridden by the keyboard
  45. // if needed
  46. GDisplay* get_lcd_display(void);
  47. GDisplay* get_led_display(void);
  48. // For emulator builds, this function need to be implemented
  49. #ifdef EMULATOR
  50. void draw_emulator(void);
  51. #endif
  52. // If you need support for more than 16 keyframes per animation, you can change this
  53. #define MAX_VISUALIZER_KEY_FRAMES 16
  54. struct keyframe_animation_t;
  55. typedef struct {
  56. layer_state_t layer;
  57. layer_state_t default_layer;
  58. uint32_t leds; // See led.h for available statuses
  59. uint8_t mods;
  60. bool suspended;
  61. #ifdef BACKLIGHT_ENABLE
  62. uint8_t backlight_level;
  63. #endif
  64. #ifdef VISUALIZER_USER_DATA_SIZE
  65. uint8_t user_data[VISUALIZER_USER_DATA_SIZE];
  66. #endif
  67. } visualizer_keyboard_status_t;
  68. // The state struct is used by the various keyframe functions
  69. // It's also used for setting the LCD color and layer text
  70. // from the user customized code
  71. typedef struct visualizer_state_t {
  72. // The user code should primarily be modifying these
  73. uint32_t target_lcd_color;
  74. const char* layer_text;
  75. // The user visualizer(and animation functions) can read these
  76. visualizer_keyboard_status_t status;
  77. // These are used by the animation functions
  78. uint32_t current_lcd_color;
  79. uint32_t prev_lcd_color;
  80. #ifdef LCD_ENABLE
  81. font_t font_fixed5x8;
  82. font_t font_dejavusansbold12;
  83. #endif
  84. } visualizer_state_t;
  85. // Any custom keyframe function should have this signature
  86. // return true to get continuous updates, otherwise you will only get one
  87. // update per frame
  88. typedef bool (*frame_func)(struct keyframe_animation_t*, visualizer_state_t*);
  89. // Represents a keyframe animation, so fields are internal to the system
  90. // while others are meant to be initialized by the user code
  91. typedef struct keyframe_animation_t {
  92. // These should be initialized
  93. int num_frames;
  94. bool loop;
  95. int frame_lengths[MAX_VISUALIZER_KEY_FRAMES];
  96. frame_func frame_functions[MAX_VISUALIZER_KEY_FRAMES];
  97. // Used internally by the system, and can also be read by
  98. // keyframe update functions
  99. int current_frame;
  100. int time_left_in_frame;
  101. bool first_update_of_frame;
  102. bool last_update_of_frame;
  103. bool need_update;
  104. } keyframe_animation_t;
  105. extern GDisplay* LCD_DISPLAY;
  106. extern GDisplay* LED_DISPLAY;
  107. void start_keyframe_animation(keyframe_animation_t* animation);
  108. void stop_keyframe_animation(keyframe_animation_t* animation);
  109. // This runs the next keyframe, but does not update the animation state
  110. // Useful for crossfades for example
  111. void run_next_keyframe(keyframe_animation_t* animation, visualizer_state_t* state);
  112. // The master can set userdata which will be transferred to the slave
  113. #ifdef VISUALIZER_USER_DATA_SIZE
  114. void visualizer_set_user_data(void* user_data);
  115. #endif
  116. // These functions have to be implemented by the user
  117. // Called regularly each time the state has changed (but not every scan loop)
  118. void update_user_visualizer_state(visualizer_state_t* state, visualizer_keyboard_status_t* prev_status);
  119. // Called when the computer goes to suspend, will also stop calling update_user_visualizer_state
  120. void user_visualizer_suspend(visualizer_state_t* state);
  121. // You have to start at least one animation as a response to the following two functions
  122. // When the animation has finished the visualizer will resume normal operation and start calling the
  123. // update_user_visualizer_state again
  124. // Called when the keyboard boots up
  125. void initialize_user_visualizer(visualizer_state_t* state);
  126. // Called when the computer resumes from a suspend
  127. void user_visualizer_resume(visualizer_state_t* state);
  128. #endif /* VISUALIZER_H */