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