2 Copyright 2017 Fred Sundvik
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.
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.
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/>.
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
21 #ifndef LCD_BACKLIGHT_ENABLE
22 #error This visualizer needs that LCD backlight is enabled
26 #error This visualizer needs that LCD is enabled
29 #include "visualizer.h"
30 #include "system/serial_link.h"
32 // To generate an image array like this
33 // Ensure the image is 128 x 32 or smaller
34 // Convert the bitmap to a C array using a program like http://www.riuson.com/lcd-image-converter/
35 // Ensure the the conversion process produces a monochrome format array - 1 bit/pixel, left to right, top to bottom
36 // Update array in the source code with the C array produced by the conversion program
38 // The image below is generated from lcd_logo.png
39 static const uint8_t image_data_lcd_logo[512] = {
40 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
41 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
42 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
43 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
44 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
45 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
46 0x00, 0xf8, 0xfe, 0x3e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
47 0x00, 0x38, 0x38, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
48 0x00, 0x38, 0x38, 0x38, 0x06, 0x29, 0x41, 0x24, 0x52, 0x24, 0x46, 0x00, 0x00, 0x00, 0x00, 0x00,
49 0x00, 0x38, 0x38, 0x38, 0x09, 0x55, 0x42, 0xaa, 0xaa, 0xaa, 0xa8, 0x00, 0x00, 0x00, 0x00, 0x00,
50 0x00, 0x38, 0x38, 0x38, 0x09, 0x55, 0x82, 0x28, 0xaa, 0xae, 0x8c, 0x00, 0x00, 0x00, 0x00, 0x00,
51 0x00, 0x38, 0x38, 0x38, 0x09, 0x55, 0x43, 0x28, 0xaa, 0xaa, 0x88, 0x00, 0x00, 0x00, 0x00, 0x00,
52 0x00, 0x38, 0x38, 0x38, 0x0a, 0x55, 0x42, 0x28, 0xaa, 0xaa, 0x88, 0x00, 0x00, 0x00, 0x00, 0x00,
53 0x00, 0x38, 0x38, 0x38, 0x05, 0x45, 0x42, 0x28, 0x89, 0x4a, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00,
54 0x00, 0x18, 0x38, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
55 0x00, 0x1c, 0x38, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
56 0x00, 0x0e, 0x38, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
57 0x00, 0x03, 0xff, 0x80, 0x04, 0x45, 0x14, 0xa4, 0x92, 0x83, 0x52, 0x22, 0x22, 0x36, 0x00, 0x00,
58 0x00, 0x00, 0x38, 0x00, 0x0a, 0xaa, 0xaa, 0xaa, 0xba, 0x84, 0x55, 0x55, 0x57, 0x45, 0x00, 0x00,
59 0x00, 0x00, 0x38, 0x00, 0x08, 0xaa, 0xaa, 0xaa, 0x92, 0xb2, 0x55, 0x55, 0x42, 0x65, 0x00, 0x00,
60 0x00, 0x00, 0x38, 0x00, 0x08, 0xaa, 0xaa, 0xaa, 0x92, 0x81, 0x56, 0x65, 0x42, 0x45, 0x00, 0x00,
61 0x00, 0x00, 0x38, 0x00, 0x0a, 0xaa, 0xaa, 0xaa, 0x92, 0x81, 0x54, 0x45, 0x42, 0x45, 0x00, 0x00,
62 0x00, 0x00, 0x38, 0x00, 0x04, 0x48, 0xa2, 0x4a, 0x89, 0x06, 0x24, 0x42, 0x41, 0x36, 0x00, 0x00,
63 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
64 0x00, 0x00, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
65 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
66 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
67 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
68 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
69 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
70 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
71 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
74 static const uint32_t logo_background_color = LCD_COLOR(0x00, 0x00, 0xFF);
75 static const uint32_t initial_color = LCD_COLOR(0, 0, 0);
79 LCD_STATE_LAYER_BITMAP,
80 LCD_STATE_BITMAP_AND_LEDS,
83 static lcd_state_t lcd_state = LCD_STATE_INITIAL;
85 bool display_logo(keyframe_animation_t* animation, visualizer_state_t* state) {
89 // Read the uGFX documentation for information how to use the displays
90 // http://wiki.ugfx.org/index.php/Main_Page
93 // You can use static variables for things that can't be found in the animation
94 // or state structs, here we use the image
96 //gdispGBlitArea is a tricky function to use since it supports blitting part of the image
97 // if you have full screen image, then just use 128 and 32 for both source and target dimensions
98 gdispGBlitArea(GDISP, 0, 0, 128, 32, 0, 0, 128, (pixel_t*)image_data_lcd_logo);
100 // Always remember to flush the display
105 // Feel free to modify the animations below, or even add new ones if needed
107 // Don't worry, if the startup animation is long, you can use the keyboard like normal
109 static keyframe_animation_t startup_animation = {
112 .frame_lengths = {0, gfxMillisecondsToTicks(10000), 0},
115 keyframe_animate_backlight_color,
120 // The LCD animation alternates between the layer name display and a
121 // bitmap that displays all active layers
122 static keyframe_animation_t lcd_layer_bitmap_animation = {
126 gfxMillisecondsToTicks(2000),
127 gfxMillisecondsToTicks(2000)
130 keyframe_display_layer_text,
131 keyframe_display_layer_bitmap
135 static keyframe_animation_t lcd_layer_bitmap_leds_animation = {
139 gfxMillisecondsToTicks(2000),
140 gfxMillisecondsToTicks(2000),
141 gfxMillisecondsToTicks(2000)
144 keyframe_display_layer_text,
145 keyframe_display_led_states,
146 keyframe_display_layer_bitmap,
150 static keyframe_animation_t suspend_animation = {
153 .frame_lengths = {0, gfxMillisecondsToTicks(1000), 0},
155 keyframe_display_layer_text,
156 keyframe_animate_backlight_color,
157 keyframe_disable_lcd_and_backlight,
161 static keyframe_animation_t resume_animation = {
164 .frame_lengths = {0, 0, gfxMillisecondsToTicks(10000), 0},
166 keyframe_enable_lcd_and_backlight,
168 keyframe_animate_backlight_color,
169 enable_visualization,
173 // The color animation animates the LCD color when you change layers
174 static keyframe_animation_t color_animation = {
177 // Note that there's a 200 ms no-operation frame,
178 // this prevents the color from changing when activating the layer
180 .frame_lengths = {gfxMillisecondsToTicks(200), gfxMillisecondsToTicks(500)},
181 .frame_functions = {keyframe_no_operation, keyframe_animate_backlight_color},
184 void initialize_user_visualizer(visualizer_state_t* state) {
185 // The brightness will be dynamically adjustable in the future
186 // But for now, change it here.
187 lcd_backlight_brightness(130);
188 state->current_lcd_color = initial_color;
189 state->target_lcd_color = logo_background_color;
190 lcd_state = LCD_STATE_INITIAL;
191 start_keyframe_animation(&startup_animation);
194 void update_user_visualizer_state(visualizer_state_t* state, visualizer_keyboard_status_t* prev_status) {
195 // Add more tests, change the colors and layer texts here
196 // Usually you want to check the high bits (higher layers first)
197 // because that's the order layers are processed for keypresses
198 // You can for check for example:
199 // state->status.layer
200 // state->status.default_layer
201 // state->status.leds (see led.h for available statuses)
203 if (state->status.layer & 0x4) {
204 state->target_lcd_color = LCD_COLOR(0, 0xB0, 0xFF);
205 state->layer_text = "Media & Mouse";
207 else if (state->status.layer & 0x2) {
208 state->target_lcd_color = LCD_COLOR(0x80, 0xB0, 0xFF);
209 state->layer_text = "Symbol";
212 state->target_lcd_color = LCD_COLOR(0x40, 0xB0, 0xFF);
213 state->layer_text = "Default";
216 if (lcd_state == LCD_STATE_INITIAL || state->status.layer != prev_status->layer) {
217 start_keyframe_animation(&color_animation);
220 if (state->status.leds) {
221 if (lcd_state != LCD_STATE_BITMAP_AND_LEDS ||
222 state->status.leds != prev_status->leds ||
223 state->status.layer != prev_status->layer ||
224 state->status.default_layer != prev_status->default_layer) {
226 // NOTE: that it doesn't matter if the animation isn't playing, stop will do nothing in that case
227 stop_keyframe_animation(&lcd_layer_bitmap_animation);
229 lcd_state = LCD_STATE_BITMAP_AND_LEDS;
231 // The logic in this function makes sure that this doesn't happen, but if you call start on an
232 // animation that is already playing it will be restarted.
233 start_keyframe_animation(&lcd_layer_bitmap_leds_animation);
236 if (lcd_state != LCD_STATE_LAYER_BITMAP ||
237 state->status.layer != prev_status->layer ||
238 state->status.default_layer != prev_status->default_layer) {
240 stop_keyframe_animation(&lcd_layer_bitmap_leds_animation);
242 lcd_state = LCD_STATE_LAYER_BITMAP;
243 start_keyframe_animation(&lcd_layer_bitmap_animation);
246 // You can also stop existing animations, and start your custom ones here
247 // remember that you should normally have only one animation for the LCD
248 // and one for the background. But you can also combine them if you want.
251 void user_visualizer_suspend(visualizer_state_t* state) {
252 state->layer_text = "Suspending...";
253 uint8_t hue = LCD_HUE(state->current_lcd_color);
254 uint8_t sat = LCD_SAT(state->current_lcd_color);
255 state->target_lcd_color = LCD_COLOR(hue, sat, 0);
256 start_keyframe_animation(&suspend_animation);
259 void user_visualizer_resume(visualizer_state_t* state) {
260 state->current_lcd_color = initial_color;
261 state->target_lcd_color = logo_background_color;
262 lcd_state = LCD_STATE_INITIAL;
263 start_keyframe_animation(&resume_animation);