]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/ergodox/keymaps/default/visualizer.c
afa6f1bdd772c281f781b646308b559f8de92c16
[qmk_firmware.git] / keyboards / ergodox / keymaps / default / visualizer.c
1 /*
2 Copyright 2017 Fred Sundvik
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 // Currently we are assuming that both the backlight and LCD are enabled
19 // But it's entirely possible to write a custom visualizer that use only
20 // one of them
21 #ifndef LCD_BACKLIGHT_ENABLE
22 #error This visualizer needs that LCD backlight is enabled
23 #endif
24
25 #ifndef LCD_ENABLE
26 #error This visualizer needs that LCD is enabled
27 #endif
28
29 #include "visualizer.h"
30 #include "visualizer_keyframes.h"
31 #include "lcd_keyframes.h"
32 #include "lcd_backlight_keyframes.h"
33 #include "system/serial_link.h"
34 #include "led.h"
35
36 #include "resources/resources.h"
37
38 static const uint32_t logo_background_color = LCD_COLOR(0x00, 0x00, 0xFF);
39 static const uint32_t initial_color = LCD_COLOR(0, 0, 0);
40
41 typedef enum {
42     LCD_STATE_INITIAL,
43     LCD_STATE_LAYER_BITMAP,
44     LCD_STATE_BITMAP_AND_LEDS,
45 } lcd_state_t;
46
47 static lcd_state_t lcd_state = LCD_STATE_INITIAL;
48
49 // Feel free to modify the animations below, or even add new ones if needed
50
51 // Don't worry, if the startup animation is long, you can use the keyboard like normal
52 // during that time
53 static keyframe_animation_t startup_animation = {
54     .num_frames = 2,
55     .loop = false,
56     .frame_lengths = {0, gfxMillisecondsToTicks(10000), 0},
57     .frame_functions = {
58             lcd_keyframe_draw_logo,
59             backlight_keyframe_animate_color,
60     },
61 };
62
63 static keyframe_animation_t lcd_layer_display = {
64     .num_frames = 1,
65     .loop = false,
66     .frame_lengths = {gfxMillisecondsToTicks(0)},
67     .frame_functions = {lcd_keyframe_display_layer_and_led_states}
68 };
69
70 static keyframe_animation_t suspend_animation = {
71     .num_frames = 4,
72     .loop = false,
73     .frame_lengths = {0, gfxMillisecondsToTicks(1000), 0, 0},
74     .frame_functions = {
75             lcd_keyframe_display_layer_text,
76             backlight_keyframe_animate_color,
77             lcd_keyframe_disable,
78             lcd_keyframe_disable,
79     },
80 };
81
82 static keyframe_animation_t resume_animation = {
83     .num_frames = 4,
84     .loop = false,
85     .frame_lengths = {0, 0, 0, gfxMillisecondsToTicks(10000), 0},
86     .frame_functions = {
87             lcd_keyframe_enable,
88             backlight_keyframe_enable,
89             lcd_keyframe_draw_logo,
90             backlight_keyframe_animate_color,
91     },
92 };
93
94 // The color animation animates the LCD color when you change layers
95 static keyframe_animation_t color_animation = {
96     .num_frames = 2,
97     .loop = false,
98     // Note that there's a 200 ms no-operation frame,
99     // this prevents the color from changing when activating the layer
100     // momentarily
101     .frame_lengths = {gfxMillisecondsToTicks(200), gfxMillisecondsToTicks(500)},
102     .frame_functions = {keyframe_no_operation, backlight_keyframe_animate_color},
103 };
104
105 void initialize_user_visualizer(visualizer_state_t* state) {
106     // The brightness will be dynamically adjustable in the future
107     // But for now, change it here.
108     lcd_backlight_brightness(130);
109     state->current_lcd_color = initial_color;
110     state->target_lcd_color = logo_background_color;
111     lcd_state = LCD_STATE_INITIAL;
112     start_keyframe_animation(&startup_animation);
113 }
114
115 void update_user_visualizer_state(visualizer_state_t* state, visualizer_keyboard_status_t* prev_status) {
116     // Add more tests, change the colors and layer texts here
117     // Usually you want to check the high bits (higher layers first)
118     // because that's the order layers are processed for keypresses
119     // You can for check for example:
120     // state->status.layer
121     // state->status.default_layer
122     // state->status.leds (see led.h for available statuses)
123
124     uint8_t saturation = 60;
125     if (state->status.leds & (1u << USB_LED_CAPS_LOCK)) {
126         saturation = 255;
127     }
128     if (state->status.layer & 0x4) {
129         state->target_lcd_color = LCD_COLOR(0, saturation, 0xFF);
130         state->layer_text = "Media & Mouse";
131     }
132     else if (state->status.layer & 0x2) {
133         state->target_lcd_color = LCD_COLOR(168, saturation, 0xFF);
134         state->layer_text = "Symbol";
135     }
136     else {
137         state->target_lcd_color = LCD_COLOR(84, saturation, 0xFF);
138         state->layer_text = "Default";
139     }
140
141     if (lcd_state == LCD_STATE_INITIAL ||
142             state->status.layer != prev_status->layer ||
143             state->status.default_layer != prev_status->default_layer ||
144             state->status.leds != prev_status->leds) {
145         start_keyframe_animation(&color_animation);
146         start_keyframe_animation(&lcd_layer_display);
147     }
148
149     // You can also stop existing animations, and start your custom ones here
150     // remember that you should normally have only one animation for the LCD
151     // and one for the background. But you can also combine them if you want.
152 }
153
154 void user_visualizer_suspend(visualizer_state_t* state) {
155     state->layer_text = "Suspending...";
156     uint8_t hue = LCD_HUE(state->current_lcd_color);
157     uint8_t sat = LCD_SAT(state->current_lcd_color);
158     state->target_lcd_color = LCD_COLOR(hue, sat, 0);
159     start_keyframe_animation(&suspend_animation);
160 }
161
162 void user_visualizer_resume(visualizer_state_t* state) {
163     state->current_lcd_color = initial_color;
164     state->target_lcd_color = logo_background_color;
165     lcd_state = LCD_STATE_INITIAL;
166     start_keyframe_animation(&resume_animation);
167 }