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