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