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"
34 #include "default_animations.h"
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);
39 static const uint32_t led_emulation_colors[4] = {
41 LCD_COLOR(255, 255, 255),
42 LCD_COLOR(84, 255, 255),
43 LCD_COLOR(168, 255, 255),
46 static uint32_t next_led_target_color = 0;
50 LCD_STATE_LAYER_BITMAP,
51 LCD_STATE_BITMAP_AND_LEDS,
54 static lcd_state_t lcd_state = LCD_STATE_INITIAL;
61 } visualizer_user_data_t;
63 // Don't access from visualization function, use the visualizer state instead
64 static visualizer_user_data_t user_data_keyboard = {
66 .led1 = LED_BRIGHTNESS_HI,
67 .led2 = LED_BRIGHTNESS_HI,
68 .led3 = LED_BRIGHTNESS_HI,
71 _Static_assert(sizeof(visualizer_user_data_t) <= VISUALIZER_USER_DATA_SIZE,
72 "Please increase the VISUALIZER_USER_DATA_SIZE");
74 // Feel free to modify the animations below, or even add new ones if needed
77 // The color animation animates the LCD color when you change layers
78 static keyframe_animation_t one_led_color = {
81 .frame_lengths = {gfxMillisecondsToTicks(0)},
82 .frame_functions = {lcd_backlight_keyframe_set_color},
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;
92 // The color animation animates the LCD color when you change layers
93 static keyframe_animation_t two_led_colors = {
96 .frame_lengths = {gfxMillisecondsToTicks(1000), gfxMillisecondsToTicks(0)},
97 .frame_functions = {lcd_backlight_keyframe_set_color, swap_led_target_color},
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 = {
105 .frame_lengths = {gfxMillisecondsToTicks(0)},
106 .frame_functions = {lcd_keyframe_display_layer_bitmap},
109 static keyframe_animation_t lcd_bitmap_leds_animation = {
112 .frame_lengths = {gfxMillisecondsToTicks(2000), gfxMillisecondsToTicks(2000)},
113 .frame_functions = {lcd_keyframe_display_layer_bitmap, lcd_keyframe_display_led_states},
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);
126 static inline bool is_led_on(visualizer_user_data_t* user_data, uint8_t num) {
127 return user_data->led_on & (1u << num);
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)) {
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)) {
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)) {
161 static uint8_t get_brightness(visualizer_user_data_t* user_data, uint8_t index) {
164 return user_data->led1;
166 return user_data->led2;
168 return user_data->led3;
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;
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);
185 new_index = get_led_index_slave(user_data_new);
186 old_index = get_led_index_slave(user_data_old);
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);
191 uint8_t old_brightness = get_brightness(user_data_old, old_index);
192 uint8_t new_brightness = get_brightness(user_data_new, new_index);
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);
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) {
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);
209 stop_keyframe_animation(&one_led_color);
210 start_keyframe_animation(&two_led_colors);
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);
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) {
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);
230 lcd_state = LCD_STATE_BITMAP_AND_LEDS;
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);
237 if (lcd_state != LCD_STATE_LAYER_BITMAP ||
238 state->status.layer != prev_status->layer ||
239 state->status.default_layer != prev_status->default_layer) {
241 stop_keyframe_animation(&lcd_bitmap_leds_animation);
243 lcd_state = LCD_STATE_LAYER_BITMAP;
244 start_keyframe_animation(&lcd_bitmap_animation);
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
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
258 update_emulated_leds(state, prev_status);
259 update_lcd_text(state, prev_status);
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);
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);
278 void ergodox_board_led_on(void){
279 // No board led support
282 void ergodox_right_led_1_on(void){
283 user_data_keyboard.led_on |= (1u << 0);
284 visualizer_set_user_data(&user_data_keyboard);
287 void ergodox_right_led_2_on(void){
288 user_data_keyboard.led_on |= (1u << 1);
289 visualizer_set_user_data(&user_data_keyboard);
292 void ergodox_right_led_3_on(void){
293 user_data_keyboard.led_on |= (1u << 2);
294 visualizer_set_user_data(&user_data_keyboard);
297 void ergodox_board_led_off(void){
298 // No board led support
301 void ergodox_right_led_1_off(void){
302 user_data_keyboard.led_on &= ~(1u << 0);
303 visualizer_set_user_data(&user_data_keyboard);
306 void ergodox_right_led_2_off(void){
307 user_data_keyboard.led_on &= ~(1u << 1);
308 visualizer_set_user_data(&user_data_keyboard);
311 void ergodox_right_led_3_off(void){
312 user_data_keyboard.led_on &= ~(1u << 2);
313 visualizer_set_user_data(&user_data_keyboard);
316 void ergodox_right_led_1_set(uint8_t n) {
317 user_data_keyboard.led1 = n;
318 visualizer_set_user_data(&user_data_keyboard);
321 void ergodox_right_led_2_set(uint8_t n) {
322 user_data_keyboard.led2 = n;
323 visualizer_set_user_data(&user_data_keyboard);
326 void ergodox_right_led_3_set(uint8_t n) {
327 user_data_keyboard.led3 = n;
328 visualizer_set_user_data(&user_data_keyboard);