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