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.

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