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