2020 February 29 Breaking Changes Update (#8064)
[jackhill/qmk/firmware.git] / quantum / visualizer / visualizer.h
CommitLineData
9e58d022
FS
1/*
2The MIT License (MIT)
3
4Copyright (c) 2016 Fred Sundvik
5
6Permission is hereby granted, free of charge, to any person obtaining a copy
7of this software and associated documentation files (the "Software"), to deal
8in the Software without restriction, including without limitation the rights
9to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10copies of the Software, and to permit persons to whom the Software is
11furnished to do so, subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in all
14copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22SOFTWARE.
23*/
24
25#ifndef VISUALIZER_H
26#define VISUALIZER_H
27#include <stdlib.h>
28#include <stdint.h>
29#include <stdbool.h>
30
ff49259a 31#include "config.h"
9e58d022 32#include "gfx.h"
b62e160a 33#include "action_layer.h"
9e58d022
FS
34
35#ifdef LCD_BACKLIGHT_ENABLE
b624f32f 36# include "lcd_backlight.h"
9e58d022
FS
37#endif
38
effffa33 39#ifdef BACKLIGHT_ENABLE
b624f32f 40# include "backlight.h"
effffa33
FS
41#endif
42
b7041d06 43// use this function to merge both real_mods and oneshot_mods in a uint16_t
9eb8d052
S
44uint8_t visualizer_get_mods(void);
45
9e58d022
FS
46// This need to be called once at the start
47void visualizer_init(void);
b93d0719 48// This should be called at every matrix scan
b62e160a 49void visualizer_update(layer_state_t default_state, layer_state_t state, uint8_t mods, uint32_t leds);
9eb8d052 50
b93d0719
FS
51// This should be called when the keyboard goes to suspend state
52void visualizer_suspend(void);
53// This should be called when the keyboard wakes up from suspend state
54void visualizer_resume(void);
9e58d022 55
fa8feb21
FS
56// These functions are week, so they can be overridden by the keyboard
57// if needed
58GDisplay* get_lcd_display(void);
59GDisplay* get_led_display(void);
60
94519e38 61// For emulator builds, this function need to be implemented
9c955145 62#ifdef EMULATOR
94519e38
FS
63void draw_emulator(void);
64#endif
65
74baa489
FS
66// If you need support for more than 16 keyframes per animation, you can change this
67#define MAX_VISUALIZER_KEY_FRAMES 16
9e58d022
FS
68
69struct keyframe_animation_t;
70
71typedef struct {
b62e160a
DJ
72 layer_state_t layer;
73 layer_state_t default_layer;
b624f32f 74 uint32_t leds; // See led.h for available statuses
75 uint8_t mods;
76 bool suspended;
effffa33
FS
77#ifdef BACKLIGHT_ENABLE
78 uint8_t backlight_level;
79#endif
39385144
FS
80#ifdef VISUALIZER_USER_DATA_SIZE
81 uint8_t user_data[VISUALIZER_USER_DATA_SIZE];
82#endif
9e58d022
FS
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
88typedef struct visualizer_state_t {
89 // The user code should primarily be modifying these
b624f32f 90 uint32_t target_lcd_color;
9e58d022
FS
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
26eef35f
JY
100 gFont font_fixed5x8;
101 gFont font_dejavusansbold12;
9e58d022
FS
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
108typedef 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
112typedef struct keyframe_animation_t {
113 // These should be initialized
b624f32f 114 int num_frames;
115 bool loop;
116 int frame_lengths[MAX_VISUALIZER_KEY_FRAMES];
9e58d022
FS
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
b624f32f 121 int current_frame;
122 int time_left_in_frame;
444132ed
FS
123 bool first_update_of_frame;
124 bool last_update_of_frame;
9e58d022
FS
125 bool need_update;
126
127} keyframe_animation_t;
128
c95b17b5
FS
129extern GDisplay* LCD_DISPLAY;
130extern GDisplay* LED_DISPLAY;
131
9e58d022
FS
132void start_keyframe_animation(keyframe_animation_t* animation);
133void stop_keyframe_animation(keyframe_animation_t* animation);
891edbd5
FS
134// This runs the next keyframe, but does not update the animation state
135// Useful for crossfades for example
136void run_next_keyframe(keyframe_animation_t* animation, visualizer_state_t* state);
b93d0719 137
39385144
FS
138// The master can set userdata which will be transferred to the slave
139#ifdef VISUALIZER_USER_DATA_SIZE
140void visualizer_set_user_data(void* user_data);
141#endif
142
94519e38 143// These functions have to be implemented by the user
64d63ab4 144// Called regularly each time the state has changed (but not every scan loop)
5fbaf31d 145void update_user_visualizer_state(visualizer_state_t* state, visualizer_keyboard_status_t* prev_status);
64d63ab4 146// Called when the computer goes to suspend, will also stop calling update_user_visualizer_state
b93d0719 147void user_visualizer_suspend(visualizer_state_t* state);
64d63ab4
FS
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
152void initialize_user_visualizer(visualizer_state_t* state);
153// Called when the computer resumes from a suspend
b93d0719 154void user_visualizer_resume(visualizer_state_t* state);
9e58d022 155
9e58d022 156#endif /* VISUALIZER_H */