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