2020 February 29 Breaking Changes Update (#8064)
[jackhill/qmk/firmware.git] / quantum / visualizer / visualizer.h
1 /*
2 The MIT License (MIT)
3
4 Copyright (c) 2016 Fred Sundvik
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23 */
24
25 #ifndef VISUALIZER_H
26 #define VISUALIZER_H
27 #include <stdlib.h>
28 #include <stdint.h>
29 #include <stdbool.h>
30
31 #include "config.h"
32 #include "gfx.h"
33 #include "action_layer.h"
34
35 #ifdef LCD_BACKLIGHT_ENABLE
36 # include "lcd_backlight.h"
37 #endif
38
39 #ifdef BACKLIGHT_ENABLE
40 # include "backlight.h"
41 #endif
42
43 // use this function to merge both real_mods and oneshot_mods in a uint16_t
44 uint8_t visualizer_get_mods(void);
45
46 // This need to be called once at the start
47 void visualizer_init(void);
48 // This should be called at every matrix scan
49 void visualizer_update(layer_state_t default_state, layer_state_t state, uint8_t mods, uint32_t leds);
50
51 // This should be called when the keyboard goes to suspend state
52 void visualizer_suspend(void);
53 // This should be called when the keyboard wakes up from suspend state
54 void visualizer_resume(void);
55
56 // These functions are week, so they can be overridden by the keyboard
57 // if needed
58 GDisplay* get_lcd_display(void);
59 GDisplay* get_led_display(void);
60
61 // For emulator builds, this function need to be implemented
62 #ifdef EMULATOR
63 void draw_emulator(void);
64 #endif
65
66 // If you need support for more than 16 keyframes per animation, you can change this
67 #define MAX_VISUALIZER_KEY_FRAMES 16
68
69 struct keyframe_animation_t;
70
71 typedef struct {
72 layer_state_t layer;
73 layer_state_t default_layer;
74 uint32_t leds; // See led.h for available statuses
75 uint8_t mods;
76 bool suspended;
77 #ifdef BACKLIGHT_ENABLE
78 uint8_t backlight_level;
79 #endif
80 #ifdef VISUALIZER_USER_DATA_SIZE
81 uint8_t user_data[VISUALIZER_USER_DATA_SIZE];
82 #endif
83 } visualizer_keyboard_status_t;
84
85 // The state struct is used by the various keyframe functions
86 // It's also used for setting the LCD color and layer text
87 // from the user customized code
88 typedef struct visualizer_state_t {
89 // The user code should primarily be modifying these
90 uint32_t target_lcd_color;
91 const char* layer_text;
92
93 // The user visualizer(and animation functions) can read these
94 visualizer_keyboard_status_t status;
95
96 // These are used by the animation functions
97 uint32_t current_lcd_color;
98 uint32_t prev_lcd_color;
99 #ifdef LCD_ENABLE
100 gFont font_fixed5x8;
101 gFont font_dejavusansbold12;
102 #endif
103 } visualizer_state_t;
104
105 // Any custom keyframe function should have this signature
106 // return true to get continuous updates, otherwise you will only get one
107 // update per frame
108 typedef bool (*frame_func)(struct keyframe_animation_t*, visualizer_state_t*);
109
110 // Represents a keyframe animation, so fields are internal to the system
111 // while others are meant to be initialized by the user code
112 typedef struct keyframe_animation_t {
113 // These should be initialized
114 int num_frames;
115 bool loop;
116 int frame_lengths[MAX_VISUALIZER_KEY_FRAMES];
117 frame_func frame_functions[MAX_VISUALIZER_KEY_FRAMES];
118
119 // Used internally by the system, and can also be read by
120 // keyframe update functions
121 int current_frame;
122 int time_left_in_frame;
123 bool first_update_of_frame;
124 bool last_update_of_frame;
125 bool need_update;
126
127 } keyframe_animation_t;
128
129 extern GDisplay* LCD_DISPLAY;
130 extern GDisplay* LED_DISPLAY;
131
132 void start_keyframe_animation(keyframe_animation_t* animation);
133 void stop_keyframe_animation(keyframe_animation_t* animation);
134 // This runs the next keyframe, but does not update the animation state
135 // Useful for crossfades for example
136 void run_next_keyframe(keyframe_animation_t* animation, visualizer_state_t* state);
137
138 // The master can set userdata which will be transferred to the slave
139 #ifdef VISUALIZER_USER_DATA_SIZE
140 void visualizer_set_user_data(void* user_data);
141 #endif
142
143 // These functions have to be implemented by the user
144 // Called regularly each time the state has changed (but not every scan loop)
145 void update_user_visualizer_state(visualizer_state_t* state, visualizer_keyboard_status_t* prev_status);
146 // Called when the computer goes to suspend, will also stop calling update_user_visualizer_state
147 void user_visualizer_suspend(visualizer_state_t* state);
148 // You have to start at least one animation as a response to the following two functions
149 // When the animation has finished the visualizer will resume normal operation and start calling the
150 // update_user_visualizer_state again
151 // Called when the keyboard boots up
152 void initialize_user_visualizer(visualizer_state_t* state);
153 // Called when the computer resumes from a suspend
154 void user_visualizer_resume(visualizer_state_t* state);
155
156 #endif /* VISUALIZER_H */