2 Copyright 2016 Fred Sundvik <fsundvik@gmail.com>
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.
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.
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/>.
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
21 #ifndef LCD_BACKLIGHT_ENABLE
22 #error This visualizer needs that LCD backlight is enabled
26 #error This visualizer needs that LCD is enabled
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"
35 #include "resources/resources.h"
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);
40 static const uint32_t led_emulation_colors[4] = {
42 LCD_COLOR(255, 255, 255),
43 LCD_COLOR(84, 255, 255),
44 LCD_COLOR(168, 255, 255),
47 static uint32_t next_led_target_color = 0;
51 LCD_STATE_LAYER_BITMAP,
52 LCD_STATE_BITMAP_AND_LEDS,
55 static lcd_state_t lcd_state = LCD_STATE_INITIAL;
62 } visualizer_user_data_t;
64 // Don't access from visualization function, use the visualizer state instead
65 static visualizer_user_data_t user_data_keyboard = {
67 .led1 = LED_BRIGHTNESS_HI,
68 .led2 = LED_BRIGHTNESS_HI,
69 .led3 = LED_BRIGHTNESS_HI,
72 _Static_assert(sizeof(visualizer_user_data_t) <= VISUALIZER_USER_DATA_SIZE,
73 "Please increase the VISUALIZER_USER_DATA_SIZE");
75 // Feel free to modify the animations below, or even add new ones if needed
77 // Don't worry, if the startup animation is long, you can use the keyboard like normal
79 static keyframe_animation_t startup_animation = {
82 .frame_lengths = {0, gfxMillisecondsToTicks(10000), 0},
84 lcd_keyframe_draw_logo,
85 backlight_keyframe_animate_color,
89 // The color animation animates the LCD color when you change layers
90 static keyframe_animation_t one_led_color = {
93 .frame_lengths = {gfxMillisecondsToTicks(0)},
94 .frame_functions = {backlight_keyframe_set_color},
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;
104 // The color animation animates the LCD color when you change layers
105 static keyframe_animation_t two_led_colors = {
108 .frame_lengths = {gfxMillisecondsToTicks(1000), gfxMillisecondsToTicks(0)},
109 .frame_functions = {backlight_keyframe_set_color, swap_led_target_color},
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 = {
117 .frame_lengths = {gfxMillisecondsToTicks(0)},
118 .frame_functions = {lcd_keyframe_display_layer_bitmap},
121 static keyframe_animation_t lcd_bitmap_leds_animation = {
124 .frame_lengths = {gfxMillisecondsToTicks(2000), gfxMillisecondsToTicks(2000)},
125 .frame_functions = {lcd_keyframe_display_layer_bitmap, lcd_keyframe_display_led_states},
128 static keyframe_animation_t suspend_animation = {
131 .frame_lengths = {0, gfxMillisecondsToTicks(1000), 0, 0},
133 lcd_keyframe_display_layer_text,
134 backlight_keyframe_animate_color,
135 lcd_keyframe_disable,
136 backlight_keyframe_disable,
140 static keyframe_animation_t resume_animation = {
143 .frame_lengths = {0, 0, 0, gfxMillisecondsToTicks(10000), 0},
146 backlight_keyframe_enable,
147 lcd_keyframe_draw_logo,
148 backlight_keyframe_animate_color,
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);
162 static const uint32_t red;
163 static const uint32_t green;
164 static const uint32_t blue;
166 inline bool is_led_on(visualizer_user_data_t* user_data, uint8_t num) {
167 return user_data->led_on & (1u << num);
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)) {
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)) {
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)) {
201 static uint8_t get_brightness(visualizer_user_data_t* user_data, uint8_t index) {
204 return user_data->led1;
206 return user_data->led2;
208 return user_data->led3;
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;
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);
225 new_index = get_led_index_slave(user_data_new);
226 old_index = get_led_index_slave(user_data_old);
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);
231 uint8_t old_brightness = get_brightness(user_data_old, old_index);
232 uint8_t new_brightness = get_brightness(user_data_new, new_index);
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);
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) {
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);
249 stop_keyframe_animation(&one_led_color);
250 start_keyframe_animation(&two_led_colors);
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);
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) {
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);
270 lcd_state = LCD_STATE_BITMAP_AND_LEDS;
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);
277 if (lcd_state != LCD_STATE_LAYER_BITMAP ||
278 state->status.layer != prev_status->layer ||
279 state->status.default_layer != prev_status->default_layer) {
281 stop_keyframe_animation(&lcd_bitmap_leds_animation);
283 lcd_state = LCD_STATE_LAYER_BITMAP;
284 start_keyframe_animation(&lcd_bitmap_animation);
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
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
298 update_emulated_leds(state, prev_status);
299 update_lcd_text(state, prev_status);
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);
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);
318 void ergodox_board_led_on(void){
319 // No board led support
322 void ergodox_right_led_1_on(void){
323 user_data_keyboard.led_on |= (1u << 0);
324 visualizer_set_user_data(&user_data_keyboard);
327 void ergodox_right_led_2_on(void){
328 user_data_keyboard.led_on |= (1u << 1);
329 visualizer_set_user_data(&user_data_keyboard);
332 void ergodox_right_led_3_on(void){
333 user_data_keyboard.led_on |= (1u << 2);
334 visualizer_set_user_data(&user_data_keyboard);
337 void ergodox_board_led_off(void){
338 // No board led support
341 void ergodox_right_led_1_off(void){
342 user_data_keyboard.led_on &= ~(1u << 0);
343 visualizer_set_user_data(&user_data_keyboard);
346 void ergodox_right_led_2_off(void){
347 user_data_keyboard.led_on &= ~(1u << 1);
348 visualizer_set_user_data(&user_data_keyboard);
351 void ergodox_right_led_3_off(void){
352 user_data_keyboard.led_on &= ~(1u << 2);
353 visualizer_set_user_data(&user_data_keyboard);
356 void ergodox_right_led_1_set(uint8_t n) {
357 user_data_keyboard.led1 = n;
358 visualizer_set_user_data(&user_data_keyboard);
361 void ergodox_right_led_2_set(uint8_t n) {
362 user_data_keyboard.led2 = n;
363 visualizer_set_user_data(&user_data_keyboard);
366 void ergodox_right_led_3_set(uint8_t n) {
367 user_data_keyboard.led3 = n;
368 visualizer_set_user_data(&user_data_keyboard);