]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/ergodox/keymaps/default/visualizer.c
Move LCD logo to visualizer resources
[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 bool display_logo(keyframe_animation_t* animation, visualizer_state_t* state) {
50     (void)state;
51     (void)animation;
52     (void)state;
53     // Read the uGFX documentation for information how to use the displays
54     // http://wiki.ugfx.org/index.php/Main_Page
55     gdispClear(White);
56
57     // You can use static variables for things that can't be found in the animation
58     // or state structs, here we use the image
59
60     //gdispGBlitArea is a tricky function to use since it supports blitting part of the image
61     // if you have full screen image, then just use 128 and 32 for both source and target dimensions
62     gdispGBlitArea(GDISP, 0, 0, 128, 32, 0, 0, 128, (pixel_t*)resource_lcd_logo);
63
64     return false;
65 }
66
67 // Feel free to modify the animations below, or even add new ones if needed
68
69 // Don't worry, if the startup animation is long, you can use the keyboard like normal
70 // during that time
71 static keyframe_animation_t startup_animation = {
72     .num_frames = 2,
73     .loop = false,
74     .frame_lengths = {0, gfxMillisecondsToTicks(10000), 0},
75     .frame_functions = {
76             display_logo,
77             backlight_keyframe_animate_color,
78     },
79 };
80
81 static keyframe_animation_t lcd_layer_display = {
82     .num_frames = 1,
83     .loop = false,
84     .frame_lengths = {gfxMillisecondsToTicks(0)},
85     .frame_functions = {lcd_keyframe_display_layer_and_led_states}
86 };
87
88 static keyframe_animation_t suspend_animation = {
89     .num_frames = 4,
90     .loop = false,
91     .frame_lengths = {0, gfxMillisecondsToTicks(1000), 0, 0},
92     .frame_functions = {
93             lcd_keyframe_display_layer_text,
94             backlight_keyframe_animate_color,
95             lcd_keyframe_disable,
96             lcd_keyframe_disable,
97     },
98 };
99
100 static keyframe_animation_t resume_animation = {
101     .num_frames = 4,
102     .loop = false,
103     .frame_lengths = {0, 0, 0, gfxMillisecondsToTicks(10000), 0},
104     .frame_functions = {
105             lcd_keyframe_enable,
106             backlight_keyframe_enable,
107             display_logo,
108             backlight_keyframe_animate_color,
109     },
110 };
111
112 // The color animation animates the LCD color when you change layers
113 static keyframe_animation_t color_animation = {
114     .num_frames = 2,
115     .loop = false,
116     // Note that there's a 200 ms no-operation frame,
117     // this prevents the color from changing when activating the layer
118     // momentarily
119     .frame_lengths = {gfxMillisecondsToTicks(200), gfxMillisecondsToTicks(500)},
120     .frame_functions = {keyframe_no_operation, backlight_keyframe_animate_color},
121 };
122
123 void initialize_user_visualizer(visualizer_state_t* state) {
124     // The brightness will be dynamically adjustable in the future
125     // But for now, change it here.
126     lcd_backlight_brightness(130);
127     state->current_lcd_color = initial_color;
128     state->target_lcd_color = logo_background_color;
129     lcd_state = LCD_STATE_INITIAL;
130     start_keyframe_animation(&startup_animation);
131 }
132
133 void update_user_visualizer_state(visualizer_state_t* state, visualizer_keyboard_status_t* prev_status) {
134     // Add more tests, change the colors and layer texts here
135     // Usually you want to check the high bits (higher layers first)
136     // because that's the order layers are processed for keypresses
137     // You can for check for example:
138     // state->status.layer
139     // state->status.default_layer
140     // state->status.leds (see led.h for available statuses)
141
142     uint8_t saturation = 60;
143     if (state->status.leds & (1u << USB_LED_CAPS_LOCK)) {
144         saturation = 255;
145     }
146     if (state->status.layer & 0x4) {
147         state->target_lcd_color = LCD_COLOR(0, saturation, 0xFF);
148         state->layer_text = "Media & Mouse";
149     }
150     else if (state->status.layer & 0x2) {
151         state->target_lcd_color = LCD_COLOR(168, saturation, 0xFF);
152         state->layer_text = "Symbol";
153     }
154     else {
155         state->target_lcd_color = LCD_COLOR(84, saturation, 0xFF);
156         state->layer_text = "Default";
157     }
158
159     if (lcd_state == LCD_STATE_INITIAL ||
160             state->status.layer != prev_status->layer ||
161             state->status.default_layer != prev_status->default_layer ||
162             state->status.leds != prev_status->leds) {
163         start_keyframe_animation(&color_animation);
164         start_keyframe_animation(&lcd_layer_display);
165     }
166
167     // You can also stop existing animations, and start your custom ones here
168     // remember that you should normally have only one animation for the LCD
169     // and one for the background. But you can also combine them if you want.
170 }
171
172 void user_visualizer_suspend(visualizer_state_t* state) {
173     state->layer_text = "Suspending...";
174     uint8_t hue = LCD_HUE(state->current_lcd_color);
175     uint8_t sat = LCD_SAT(state->current_lcd_color);
176     state->target_lcd_color = LCD_COLOR(hue, sat, 0);
177     start_keyframe_animation(&suspend_animation);
178 }
179
180 void user_visualizer_resume(visualizer_state_t* state) {
181     state->current_lcd_color = initial_color;
182     state->target_lcd_color = logo_background_color;
183     lcd_state = LCD_STATE_INITIAL;
184     start_keyframe_animation(&resume_animation);
185 }