]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/visualizer/visualizer.h
Add RGB support for Let’s Split v1
[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 // use this function to merget both real_mods and oneshot_mods in a uint16_t
38 uint8_t visualizer_get_mods(void);
39
40 // This need to be called once at the start
41 void visualizer_init(void);
42 // This should be called at every matrix scan
43 void visualizer_update(uint32_t default_state, uint32_t state, uint8_t mods, uint32_t leds);
44
45 // This should be called when the keyboard goes to suspend state
46 void visualizer_suspend(void);
47 // This should be called when the keyboard wakes up from suspend state
48 void visualizer_resume(void);
49
50 // These functions are week, so they can be overridden by the keyboard
51 // if needed
52 GDisplay* get_lcd_display(void);
53 GDisplay* get_led_display(void);
54
55 // For emulator builds, this function need to be implemented
56 #ifdef EMULATOR
57 void draw_emulator(void);
58 #endif
59
60 // If you need support for more than 16 keyframes per animation, you can change this
61 #define MAX_VISUALIZER_KEY_FRAMES 16
62
63 struct keyframe_animation_t;
64
65 typedef struct {
66     uint32_t layer;
67     uint32_t default_layer;
68     uint8_t mods;
69     uint32_t leds; // See led.h for available statuses
70     bool suspended;
71 } visualizer_keyboard_status_t;
72
73 // The state struct is used by the various keyframe functions
74 // It's also used for setting the LCD color and layer text
75 // from the user customized code
76 typedef struct visualizer_state_t {
77     // The user code should primarily be modifying these
78     uint32_t target_lcd_color;
79     const char* layer_text;
80
81     // The user visualizer(and animation functions) can read these
82     visualizer_keyboard_status_t status;
83
84     // These are used by the animation functions
85     uint32_t current_lcd_color;
86     uint32_t prev_lcd_color;
87 #ifdef LCD_ENABLE
88     font_t font_fixed5x8;
89     font_t font_dejavusansbold12;
90 #endif
91 } visualizer_state_t;
92
93 // Any custom keyframe function should have this signature
94 // return true to get continuous updates, otherwise you will only get one
95 // update per frame
96 typedef bool (*frame_func)(struct keyframe_animation_t*, visualizer_state_t*);
97
98 // Represents a keyframe animation, so fields are internal to the system
99 // while others are meant to be initialized by the user code
100 typedef struct keyframe_animation_t {
101     // These should be initialized
102     int num_frames;
103     bool loop;
104     int frame_lengths[MAX_VISUALIZER_KEY_FRAMES];
105     frame_func frame_functions[MAX_VISUALIZER_KEY_FRAMES];
106
107     // Used internally by the system, and can also be read by
108     // keyframe update functions
109     int current_frame;
110     int time_left_in_frame;
111     bool first_update_of_frame;
112     bool last_update_of_frame;
113     bool need_update;
114
115 } keyframe_animation_t;
116
117 extern GDisplay* LCD_DISPLAY;
118 extern GDisplay* LED_DISPLAY;
119
120 void start_keyframe_animation(keyframe_animation_t* animation);
121 void stop_keyframe_animation(keyframe_animation_t* animation);
122 // This runs the next keyframe, but does not update the animation state
123 // Useful for crossfades for example
124 void run_next_keyframe(keyframe_animation_t* animation, visualizer_state_t* state);
125
126 // Some predefined keyframe functions that can be used by the user code
127 // Does nothing, useful for adding delays
128 bool keyframe_no_operation(keyframe_animation_t* animation, visualizer_state_t* state);
129 // Animates the LCD backlight color between the current color and the target color (of the state)
130 bool keyframe_animate_backlight_color(keyframe_animation_t* animation, visualizer_state_t* state);
131 // Sets the backlight color to the target color
132 bool keyframe_set_backlight_color(keyframe_animation_t* animation, visualizer_state_t* state);
133 // Displays the layer text centered vertically on the screen
134 bool keyframe_display_layer_text(keyframe_animation_t* animation, visualizer_state_t* state);
135 // Displays a bitmap (0/1) of all the currently active layers
136 bool keyframe_display_layer_bitmap(keyframe_animation_t* animation, visualizer_state_t* state);
137 // Displays a bitmap (0/1) of all the currently active mods
138 bool keyframe_display_mods_bitmap(keyframe_animation_t* animation, visualizer_state_t* state);
139
140 bool keyframe_disable_lcd_and_backlight(keyframe_animation_t* animation, visualizer_state_t* state);
141 bool keyframe_enable_lcd_and_backlight(keyframe_animation_t* animation, visualizer_state_t* state);
142
143 // Call this once, when the initial animation has finished, alternatively you can call it
144 // directly from the initalize_user_visualizer function (the animation can be null)
145 bool enable_visualization(keyframe_animation_t* animation, visualizer_state_t* state);
146
147 // These functions have to be implemented by the user
148 void initialize_user_visualizer(visualizer_state_t* state);
149 void update_user_visualizer_state(visualizer_state_t* state);
150 void user_visualizer_suspend(visualizer_state_t* state);
151 void user_visualizer_resume(visualizer_state_t* state);
152
153
154 #endif /* VISUALIZER_H */