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