]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/ergodox/infinity/visualizer.c
Move LCD logo to visualizer resources
[qmk_firmware.git] / keyboards / ergodox / infinity / visualizer.c
1 /*
2 Copyright 2016 Fred Sundvik <fsundvik@gmail.com>
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
35 #include "resources/resources.h"
36
37 static const uint32_t logo_background_color = LCD_COLOR(0x00, 0x00, 0xFF);
38 static const uint32_t initial_color = LCD_COLOR(0, 0, 0);
39
40 static const uint32_t led_emulation_colors[4] = {
41     LCD_COLOR(0, 0, 0),
42     LCD_COLOR(255, 255, 255),
43     LCD_COLOR(84, 255, 255),
44     LCD_COLOR(168, 255, 255),
45 };
46
47 static uint32_t next_led_target_color = 0;
48
49 typedef enum {
50     LCD_STATE_INITIAL,
51     LCD_STATE_LAYER_BITMAP,
52     LCD_STATE_BITMAP_AND_LEDS,
53 } lcd_state_t;
54
55 static lcd_state_t lcd_state = LCD_STATE_INITIAL;
56
57 typedef struct {
58     uint8_t led_on;
59     uint8_t led1;
60     uint8_t led2;
61     uint8_t led3;
62 } visualizer_user_data_t;
63
64 // Don't access from visualization function, use the visualizer state instead
65 static visualizer_user_data_t user_data_keyboard = {
66     .led_on = 0,
67     .led1 = LED_BRIGHTNESS_HI,
68     .led2 = LED_BRIGHTNESS_HI,
69     .led3 = LED_BRIGHTNESS_HI,
70 };
71
72 _Static_assert(sizeof(visualizer_user_data_t) <= VISUALIZER_USER_DATA_SIZE,
73     "Please increase the VISUALIZER_USER_DATA_SIZE");
74
75 bool display_logo(keyframe_animation_t* animation, visualizer_state_t* state) {
76     (void)state;
77     (void)animation;
78     (void)state;
79     // Read the uGFX documentation for information how to use the displays
80     // http://wiki.ugfx.org/index.php/Main_Page
81     gdispClear(White);
82
83     // You can use static variables for things that can't be found in the animation
84     // or state structs, here we use the image
85
86     //gdispGBlitArea is a tricky function to use since it supports blitting part of the image
87     // if you have full screen image, then just use 128 and 32 for both source and target dimensions
88     gdispGBlitArea(GDISP, 0, 0, 128, 32, 0, 0, 128, (pixel_t*)resource_lcd_logo);
89
90     return false;
91 }
92
93 // Feel free to modify the animations below, or even add new ones if needed
94
95 // Don't worry, if the startup animation is long, you can use the keyboard like normal
96 // during that time
97 static keyframe_animation_t startup_animation = {
98     .num_frames = 2,
99     .loop = false,
100     .frame_lengths = {0, gfxMillisecondsToTicks(10000), 0},
101     .frame_functions = {
102             display_logo,
103             backlight_keyframe_animate_color,
104     },
105 };
106
107 // The color animation animates the LCD color when you change layers
108 static keyframe_animation_t one_led_color = {
109     .num_frames = 1,
110     .loop = false,
111     .frame_lengths = {gfxMillisecondsToTicks(0)},
112     .frame_functions = {backlight_keyframe_set_color},
113 };
114
115 bool swap_led_target_color(keyframe_animation_t* animation, visualizer_state_t* state) {
116     uint32_t temp = next_led_target_color;
117     next_led_target_color = state->target_lcd_color;
118     state->target_lcd_color = temp;
119     return false;
120 }
121
122 // The color animation animates the LCD color when you change layers
123 static keyframe_animation_t two_led_colors = {
124     .num_frames = 2,
125     .loop = true,
126     .frame_lengths = {gfxMillisecondsToTicks(1000), gfxMillisecondsToTicks(0)},
127     .frame_functions = {backlight_keyframe_set_color, swap_led_target_color},
128 };
129
130 // The LCD animation alternates between the layer name display and a
131 // bitmap that displays all active layers
132 static keyframe_animation_t lcd_bitmap_animation = {
133     .num_frames = 1,
134     .loop = false,
135     .frame_lengths = {gfxMillisecondsToTicks(0)},
136     .frame_functions = {lcd_keyframe_display_layer_bitmap},
137 };
138
139 static keyframe_animation_t lcd_bitmap_leds_animation = {
140     .num_frames = 2,
141     .loop = true,
142     .frame_lengths = {gfxMillisecondsToTicks(2000), gfxMillisecondsToTicks(2000)},
143     .frame_functions = {lcd_keyframe_display_layer_bitmap, lcd_keyframe_display_led_states},
144 };
145
146 static keyframe_animation_t suspend_animation = {
147     .num_frames = 4,
148     .loop = false,
149     .frame_lengths = {0, gfxMillisecondsToTicks(1000), 0, 0},
150     .frame_functions = {
151             lcd_keyframe_display_layer_text,
152             backlight_keyframe_animate_color,
153             lcd_keyframe_disable,
154             lcd_keyframe_disable,
155     },
156 };
157
158 static keyframe_animation_t resume_animation = {
159     .num_frames = 4,
160     .loop = false,
161     .frame_lengths = {0, 0, 0, gfxMillisecondsToTicks(10000), 0},
162     .frame_functions = {
163             lcd_keyframe_enable,
164             backlight_keyframe_enable,
165             display_logo,
166             backlight_keyframe_animate_color,
167     },
168 };
169
170 void initialize_user_visualizer(visualizer_state_t* state) {
171     // The brightness will be dynamically adjustable in the future
172     // But for now, change it here.
173     lcd_backlight_brightness(130);
174     state->current_lcd_color = initial_color;
175     state->target_lcd_color = logo_background_color;
176     lcd_state = LCD_STATE_INITIAL;
177     start_keyframe_animation(&startup_animation);
178 }
179
180 static const uint32_t red;
181 static const uint32_t green;
182 static const uint32_t blue;
183
184 inline bool is_led_on(visualizer_user_data_t* user_data, uint8_t num) {
185     return user_data->led_on & (1u << num);
186 }
187
188 static uint8_t get_led_index_master(visualizer_user_data_t* user_data) {
189     for (int i=0; i < 3; i++) {
190         if (is_led_on(user_data, i)) {
191             return i + 1;
192         }
193     }
194     return 0;
195 }
196
197 static uint8_t get_led_index_slave(visualizer_user_data_t* user_data) {
198     uint8_t master_index = get_led_index_master(user_data);
199     if (master_index!=0) {
200         for (int i=master_index; i < 3; i++) {
201             if (is_led_on(user_data, i)) {
202                 return i + 1;
203             }
204         }
205     }
206
207     return 0;
208 }
209
210 static uint8_t get_secondary_led_index(visualizer_user_data_t* user_data) {
211     if (is_led_on(user_data, 0) &&
212             is_led_on(user_data, 1) &&
213             is_led_on(user_data, 2)) {
214         return 3;
215     }
216     return 0;
217 }
218
219 static uint8_t get_brightness(visualizer_user_data_t* user_data, uint8_t index) {
220     switch (index) {
221     case 1:
222         return user_data->led1;
223     case 2:
224         return user_data->led2;
225     case 3:
226         return user_data->led3;
227     }
228     return 0;
229 }
230
231 static void update_emulated_leds(visualizer_state_t* state, visualizer_keyboard_status_t* prev_status) {
232     visualizer_user_data_t* user_data_new = (visualizer_user_data_t*)state->status.user_data;
233     visualizer_user_data_t* user_data_old = (visualizer_user_data_t*)prev_status->user_data;
234
235     uint8_t new_index;
236     uint8_t old_index;
237
238     if (is_serial_link_master()) {
239         new_index = get_led_index_master(user_data_new);
240         old_index = get_led_index_master(user_data_old);
241     }
242     else {
243         new_index = get_led_index_slave(user_data_new);
244         old_index = get_led_index_slave(user_data_old);
245     }
246     uint8_t new_secondary_index = get_secondary_led_index(user_data_new);
247     uint8_t old_secondary_index = get_secondary_led_index(user_data_old);
248
249     uint8_t old_brightness = get_brightness(user_data_old, old_index);
250     uint8_t new_brightness = get_brightness(user_data_new, new_index);
251
252     uint8_t old_secondary_brightness = get_brightness(user_data_old, old_secondary_index);
253     uint8_t new_secondary_brightness = get_brightness(user_data_new, new_secondary_index);
254
255     if (lcd_state == LCD_STATE_INITIAL ||
256             new_index != old_index ||
257             new_secondary_index != old_secondary_index ||
258             new_brightness != old_brightness ||
259             new_secondary_brightness != old_secondary_brightness) {
260
261         if (new_secondary_index != 0) {
262             state->target_lcd_color = change_lcd_color_intensity(
263                 led_emulation_colors[new_index], new_brightness);
264             next_led_target_color = change_lcd_color_intensity(
265                 led_emulation_colors[new_secondary_index], new_secondary_brightness);
266
267             stop_keyframe_animation(&one_led_color);
268             start_keyframe_animation(&two_led_colors);
269         } else {
270             state->target_lcd_color = change_lcd_color_intensity(
271                 led_emulation_colors[new_index], new_brightness);
272             stop_keyframe_animation(&two_led_colors);
273             start_keyframe_animation(&one_led_color);
274         }
275     }
276 }
277
278 static void update_lcd_text(visualizer_state_t* state, visualizer_keyboard_status_t* prev_status) {
279     if (state->status.leds) {
280         if (lcd_state != LCD_STATE_BITMAP_AND_LEDS ||
281                 state->status.leds != prev_status->leds ||
282                 state->status.layer != prev_status->layer ||
283                 state->status.default_layer != prev_status->default_layer) {
284
285             // NOTE: that it doesn't matter if the animation isn't playing, stop will do nothing in that case
286             stop_keyframe_animation(&lcd_bitmap_animation);
287
288             lcd_state = LCD_STATE_BITMAP_AND_LEDS;
289             // For information:
290             // The logic in this function makes sure that this doesn't happen, but if you call start on an
291             // animation that is already playing it will be restarted.
292             start_keyframe_animation(&lcd_bitmap_leds_animation);
293         }
294     } else {
295         if (lcd_state != LCD_STATE_LAYER_BITMAP ||
296                 state->status.layer != prev_status->layer ||
297                 state->status.default_layer != prev_status->default_layer) {
298
299             stop_keyframe_animation(&lcd_bitmap_leds_animation);
300
301             lcd_state = LCD_STATE_LAYER_BITMAP;
302             start_keyframe_animation(&lcd_bitmap_animation);
303         }
304     }
305 }
306
307 void update_user_visualizer_state(visualizer_state_t* state, visualizer_keyboard_status_t* prev_status) {
308     // Check the status here to start and stop animations
309     // You might have to save some state, like the current animation here so that you can start the right
310     // This function is called every time the status changes
311
312     // NOTE that this is called from the visualizer thread, so don't access anything else outside the status
313     // This is also important because the slave won't have access to the active layer for example outside the
314     // status.
315
316     update_emulated_leds(state, prev_status);
317     update_lcd_text(state, prev_status);
318
319 }
320
321 void user_visualizer_suspend(visualizer_state_t* state) {
322     state->layer_text = "Suspending...";
323     uint8_t hue = LCD_HUE(state->current_lcd_color);
324     uint8_t sat = LCD_SAT(state->current_lcd_color);
325     state->target_lcd_color = LCD_COLOR(hue, sat, 0);
326     start_keyframe_animation(&suspend_animation);
327 }
328
329 void user_visualizer_resume(visualizer_state_t* state) {
330     state->current_lcd_color = initial_color;
331     state->target_lcd_color = logo_background_color;
332     lcd_state = LCD_STATE_INITIAL;
333     start_keyframe_animation(&resume_animation);
334 }
335
336 void ergodox_board_led_on(void){
337     // No board led support
338 }
339
340 void ergodox_right_led_1_on(void){
341     user_data_keyboard.led_on |= (1u << 0);
342     visualizer_set_user_data(&user_data_keyboard);
343 }
344
345 void ergodox_right_led_2_on(void){
346     user_data_keyboard.led_on |= (1u << 1);
347     visualizer_set_user_data(&user_data_keyboard);
348 }
349
350 void ergodox_right_led_3_on(void){
351     user_data_keyboard.led_on |= (1u << 2);
352     visualizer_set_user_data(&user_data_keyboard);
353 }
354
355 void ergodox_board_led_off(void){
356     // No board led support
357 }
358
359 void ergodox_right_led_1_off(void){
360     user_data_keyboard.led_on &= ~(1u << 0);
361     visualizer_set_user_data(&user_data_keyboard);
362 }
363
364 void ergodox_right_led_2_off(void){
365     user_data_keyboard.led_on &= ~(1u << 1);
366     visualizer_set_user_data(&user_data_keyboard);
367 }
368
369 void ergodox_right_led_3_off(void){
370     user_data_keyboard.led_on &= ~(1u << 2);
371     visualizer_set_user_data(&user_data_keyboard);
372 }
373
374 void ergodox_right_led_1_set(uint8_t n) {
375     user_data_keyboard.led1 = n;
376     visualizer_set_user_data(&user_data_keyboard);
377 }
378
379 void ergodox_right_led_2_set(uint8_t n) {
380     user_data_keyboard.led2 = n;
381     visualizer_set_user_data(&user_data_keyboard);
382 }
383
384 void ergodox_right_led_3_set(uint8_t n) {
385     user_data_keyboard.led3 = n;
386     visualizer_set_user_data(&user_data_keyboard);
387 }