aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--led_test.c170
-rw-r--r--led_test.h41
-rw-r--r--visualizer.c36
-rw-r--r--visualizer.h12
-rw-r--r--visualizer.mk18
5 files changed, 272 insertions, 5 deletions
diff --git a/led_test.c b/led_test.c
new file mode 100644
index 000000000..d358ef81e
--- /dev/null
+++ b/led_test.c
@@ -0,0 +1,170 @@
1/*
2The MIT License (MIT)
3
4Copyright (c) 2016 Fred Sundvik
5
6Permission is hereby granted, free of charge, to any person obtaining a copy
7of this software and associated documentation files (the "Software"), to deal
8in the Software without restriction, including without limitation the rights
9to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10copies of the Software, and to permit persons to whom the Software is
11furnished to do so, subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in all
14copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22SOFTWARE.
23*/
24#include "led_test.h"
25#include "gfx.h"
26#include "math.h"
27
28keyframe_animation_t led_test_animation = {
29 .num_frames = 14,
30 .loop = true,
31 .frame_lengths = {
32 MS2ST(1000), // fade in
33 MS2ST(1000), // no op (leds on)
34 MS2ST(1000), // fade out
35 MS2ST(1000), // crossfade
36 MS2ST(3000), // left to rigt (outside in)
37 MS2ST(1000), // crossfade
38 MS2ST(3000), // top_to_bottom
39 0, // mirror leds
40 MS2ST(1000), // crossfade
41 MS2ST(3000), // left_to_right (mirrored, so inside out)
42 MS2ST(1000), // crossfade
43 MS2ST(3000), // top_to_bottom
44 0, // normal leds
45 MS2ST(1000), // crossfade
46
47 },
48 .frame_functions = {
49 keyframe_fade_in_all_leds,
50 keyframe_no_operation,
51 keyframe_fade_out_all_leds,
52 keyframe_led_crossfade,
53 keyframe_led_left_to_right_gradient,
54 keyframe_led_crossfade,
55 keyframe_led_top_to_bottom_gradient,
56 keyframe_mirror_led_orientation,
57 keyframe_led_crossfade,
58 keyframe_led_left_to_right_gradient,
59 keyframe_led_crossfade,
60 keyframe_led_top_to_bottom_gradient,
61 keyframe_normal_led_orientation,
62 keyframe_led_crossfade,
63 },
64};
65
66static uint8_t fade_led_color(keyframe_animation_t* animation, int from, int to) {
67 int frame_length = animation->frame_lengths[animation->current_frame];
68 int current_pos = frame_length - animation->time_left_in_frame;
69 int delta = to - from;
70 int luma = (delta * current_pos) / frame_length;
71 luma += from;
72 return luma;
73}
74
75static void keyframe_fade_all_leds_from_to(keyframe_animation_t* animation, uint8_t from, uint8_t to) {
76 uint8_t luma = fade_led_color(animation, from, to);
77 color_t color = LUMA2COLOR(luma);
78 gdispGClear(LED_DISPLAY, color);
79}
80
81// TODO: Should be customizable per keyboard
82#define NUM_ROWS 7
83#define NUM_COLS 7
84
85static uint8_t crossfade_start_frame[NUM_ROWS][NUM_COLS];
86static uint8_t crossfade_end_frame[NUM_ROWS][NUM_COLS];
87
88static uint8_t compute_gradient_color(float t, float index, float num) {
89 const float target = t * (num - 1.0f);
90 const float half_num = num / 2.0f;
91 float d = fabs(index - target);
92 if (d > half_num) {
93 d = num - d;
94 }
95 d = 1.0f - (d / half_num);
96 return (uint8_t)(255.0f * d);
97}
98
99bool keyframe_fade_in_all_leds(keyframe_animation_t* animation, visualizer_state_t* state) {
100 (void)state;
101 keyframe_fade_all_leds_from_to(animation, 0, 255);
102 return true;
103}
104
105bool keyframe_fade_out_all_leds(keyframe_animation_t* animation, visualizer_state_t* state) {
106 (void)state;
107 keyframe_fade_all_leds_from_to(animation, 255, 0);
108 return true;
109}
110
111bool keyframe_led_left_to_right_gradient(keyframe_animation_t* animation, visualizer_state_t* state) {
112 (void)state;
113 float frame_length = animation->frame_lengths[animation->current_frame];
114 float current_pos = frame_length - animation->time_left_in_frame;
115 float t = current_pos / frame_length;
116 for (int i=0; i< NUM_COLS; i++) {
117 uint8_t color = compute_gradient_color(t, i, NUM_COLS);
118 gdispGDrawLine(LED_DISPLAY, i, 0, i, NUM_ROWS - 1, LUMA2COLOR(color));
119 }
120 return true;
121}
122
123bool keyframe_led_top_to_bottom_gradient(keyframe_animation_t* animation, visualizer_state_t* state) {
124 (void)state;
125 float frame_length = animation->frame_lengths[animation->current_frame];
126 float current_pos = frame_length - animation->time_left_in_frame;
127 float t = current_pos / frame_length;
128 for (int i=0; i< NUM_ROWS; i++) {
129 uint8_t color = compute_gradient_color(t, i, NUM_ROWS);
130 gdispGDrawLine(LED_DISPLAY, 0, i, NUM_COLS - 1, i, LUMA2COLOR(color));
131 }
132 return true;
133}
134
135static void copy_current_led_state(uint8_t* dest) {
136 for (int i=0;i<NUM_ROWS;i++) {
137 for (int j=0;j<NUM_COLS;j++) {
138 dest[i*NUM_COLS + j] = gdispGGetPixelColor(LED_DISPLAY, j, i);
139 }
140 }
141}
142bool keyframe_led_crossfade(keyframe_animation_t* animation, visualizer_state_t* state) {
143 (void)state;
144 if (animation->first_update_of_frame) {
145 copy_current_led_state(&crossfade_start_frame[0][0]);
146 run_next_keyframe(animation, state);
147 copy_current_led_state(&crossfade_end_frame[0][0]);
148 }
149 for (int i=0;i<NUM_ROWS;i++) {
150 for (int j=0;j<NUM_COLS;j++) {
151 color_t color = LUMA2COLOR(fade_led_color(animation, crossfade_start_frame[i][j], crossfade_end_frame[i][j]));
152 gdispGDrawPixel(LED_DISPLAY, j, i, color);
153 }
154 }
155 return true;
156}
157
158bool keyframe_mirror_led_orientation(keyframe_animation_t* animation, visualizer_state_t* state) {
159 (void)state;
160 (void)animation;
161 gdispGSetOrientation(LED_DISPLAY, GDISP_ROTATE_180);
162 return false;
163}
164
165bool keyframe_normal_led_orientation(keyframe_animation_t* animation, visualizer_state_t* state) {
166 (void)state;
167 (void)animation;
168 gdispGSetOrientation(LED_DISPLAY, GDISP_ROTATE_0);
169 return false;
170}
diff --git a/led_test.h b/led_test.h
new file mode 100644
index 000000000..5e2325753
--- /dev/null
+++ b/led_test.h
@@ -0,0 +1,41 @@
1/*
2The MIT License (MIT)
3
4Copyright (c) 2016 Fred Sundvik
5
6Permission is hereby granted, free of charge, to any person obtaining a copy
7of this software and associated documentation files (the "Software"), to deal
8in the Software without restriction, including without limitation the rights
9to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10copies of the Software, and to permit persons to whom the Software is
11furnished to do so, subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in all
14copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22SOFTWARE.
23*/
24
25#ifndef TMK_VISUALIZER_LED_TEST_H_
26#define TMK_VISUALIZER_LED_TEST_H_
27
28#include "visualizer.h"
29
30bool keyframe_fade_in_all_leds(keyframe_animation_t* animation, visualizer_state_t* state);
31bool keyframe_fade_out_all_leds(keyframe_animation_t* animation, visualizer_state_t* state);
32bool keyframe_led_left_to_right_gradient(keyframe_animation_t* animation, visualizer_state_t* state);
33bool keyframe_led_top_to_bottom_gradient(keyframe_animation_t* animation, visualizer_state_t* state);
34bool keyframe_led_crossfade(keyframe_animation_t* animation, visualizer_state_t* state);
35bool keyframe_mirror_led_orientation(keyframe_animation_t* animation, visualizer_state_t* state);
36bool keyframe_normal_led_orientation(keyframe_animation_t* animation, visualizer_state_t* state);
37
38extern keyframe_animation_t led_test_animation;
39
40
41#endif /* TMK_VISUALIZER_LED_TEST_H_ */
diff --git a/visualizer.c b/visualizer.c
index 605be3059..579837edc 100644
--- a/visualizer.c
+++ b/visualizer.c
@@ -83,6 +83,9 @@ static remote_object_t* remote_objects[] = {
83 83
84#endif 84#endif
85 85
86GDisplay* LCD_DISPLAY = 0;
87GDisplay* LED_DISPLAY = 0;
88
86 89
87void start_keyframe_animation(keyframe_animation_t* animation) { 90void start_keyframe_animation(keyframe_animation_t* animation) {
88 animation->current_frame = -1; 91 animation->current_frame = -1;
@@ -106,6 +109,8 @@ void stop_keyframe_animation(keyframe_animation_t* animation) {
106 animation->current_frame = animation->num_frames; 109 animation->current_frame = animation->num_frames;
107 animation->time_left_in_frame = 0; 110 animation->time_left_in_frame = 0;
108 animation->need_update = true; 111 animation->need_update = true;
112 animation->first_update_of_frame = false;
113 animation->last_update_of_frame = false;
109 for (int i=0;i<MAX_SIMULTANEOUS_ANIMATIONS;i++) { 114 for (int i=0;i<MAX_SIMULTANEOUS_ANIMATIONS;i++) {
110 if (animations[i] == animation) { 115 if (animations[i] == animation) {
111 animations[i] = NULL; 116 animations[i] = NULL;
@@ -120,12 +125,15 @@ void stop_all_keyframe_animations(void) {
120 animations[i]->current_frame = animations[i]->num_frames; 125 animations[i]->current_frame = animations[i]->num_frames;
121 animations[i]->time_left_in_frame = 0; 126 animations[i]->time_left_in_frame = 0;
122 animations[i]->need_update = true; 127 animations[i]->need_update = true;
128 animations[i]->first_update_of_frame = false;
129 animations[i]->last_update_of_frame = false;
123 animations[i] = NULL; 130 animations[i] = NULL;
124 } 131 }
125 } 132 }
126} 133}
127 134
128static bool update_keyframe_animation(keyframe_animation_t* animation, visualizer_state_t* state, systime_t delta, systime_t* sleep_time) { 135static bool update_keyframe_animation(keyframe_animation_t* animation, visualizer_state_t* state, systime_t delta, systime_t* sleep_time) {
136 // TODO: Clean up this messy code
129 dprintf("Animation frame%d, left %d, delta %d\n", animation->current_frame, 137 dprintf("Animation frame%d, left %d, delta %d\n", animation->current_frame,
130 animation->time_left_in_frame, delta); 138 animation->time_left_in_frame, delta);
131 if (animation->current_frame == animation->num_frames) { 139 if (animation->current_frame == animation->num_frames) {
@@ -136,16 +144,20 @@ static bool update_keyframe_animation(keyframe_animation_t* animation, visualize
136 animation->current_frame = 0; 144 animation->current_frame = 0;
137 animation->time_left_in_frame = animation->frame_lengths[0]; 145 animation->time_left_in_frame = animation->frame_lengths[0];
138 animation->need_update = true; 146 animation->need_update = true;
147 animation->first_update_of_frame = true;
139 } else { 148 } else {
140 animation->time_left_in_frame -= delta; 149 animation->time_left_in_frame -= delta;
141 while (animation->time_left_in_frame <= 0) { 150 while (animation->time_left_in_frame <= 0) {
142 int left = animation->time_left_in_frame; 151 int left = animation->time_left_in_frame;
143 if (animation->need_update) { 152 if (animation->need_update) {
144 animation->time_left_in_frame = 0; 153 animation->time_left_in_frame = 0;
154 animation->last_update_of_frame = true;
145 (*animation->frame_functions[animation->current_frame])(animation, state); 155 (*animation->frame_functions[animation->current_frame])(animation, state);
156 animation->last_update_of_frame = false;
146 } 157 }
147 animation->current_frame++; 158 animation->current_frame++;
148 animation->need_update = true; 159 animation->need_update = true;
160 animation->first_update_of_frame = true;
149 if (animation->current_frame == animation->num_frames) { 161 if (animation->current_frame == animation->num_frames) {
150 if (animation->loop) { 162 if (animation->loop) {
151 animation->current_frame = 0; 163 animation->current_frame = 0;
@@ -162,6 +174,7 @@ static bool update_keyframe_animation(keyframe_animation_t* animation, visualize
162 } 174 }
163 if (animation->need_update) { 175 if (animation->need_update) {
164 animation->need_update = (*animation->frame_functions[animation->current_frame])(animation, state); 176 animation->need_update = (*animation->frame_functions[animation->current_frame])(animation, state);
177 animation->first_update_of_frame = false;
165 } 178 }
166 179
167 int wanted_sleep = animation->need_update ? 10 : animation->time_left_in_frame; 180 int wanted_sleep = animation->need_update ? 10 : animation->time_left_in_frame;
@@ -172,6 +185,21 @@ static bool update_keyframe_animation(keyframe_animation_t* animation, visualize
172 return true; 185 return true;
173} 186}
174 187
188void run_next_keyframe(keyframe_animation_t* animation, visualizer_state_t* state) {
189 int next_frame = animation->current_frame + 1;
190 if (next_frame == animation->num_frames) {
191 next_frame = 0;
192 }
193 keyframe_animation_t temp_animation = *animation;
194 temp_animation.current_frame = next_frame;
195 temp_animation.time_left_in_frame = animation->frame_lengths[next_frame];
196 temp_animation.first_update_of_frame = true;
197 temp_animation.last_update_of_frame = false;
198 temp_animation.need_update = false;
199 visualizer_state_t temp_state = *state;
200 (*temp_animation.frame_functions[next_frame])(&temp_animation, &temp_state);
201}
202
175bool keyframe_no_operation(keyframe_animation_t* animation, visualizer_state_t* state) { 203bool keyframe_no_operation(keyframe_animation_t* animation, visualizer_state_t* state) {
176 (void)animation; 204 (void)animation;
177 (void)state; 205 (void)state;
@@ -373,6 +401,9 @@ static THD_FUNCTION(visualizerThread, arg) {
373 update_keyframe_animation(animations[i], &state, delta, &sleep_time); 401 update_keyframe_animation(animations[i], &state, delta, &sleep_time);
374 } 402 }
375 } 403 }
404#ifdef LED_ENABLE
405 gdispGFlush(LED_DISPLAY);
406#endif
376 // The animation can enable the visualizer 407 // The animation can enable the visualizer
377 // And we might need to update the state when that happens 408 // And we might need to update the state when that happens
378 // so don't sleep 409 // so don't sleep
@@ -411,6 +442,11 @@ void visualizer_init(void) {
411#ifdef USE_SERIAL_LINK 442#ifdef USE_SERIAL_LINK
412 add_remote_objects(remote_objects, sizeof(remote_objects) / sizeof(remote_object_t*) ); 443 add_remote_objects(remote_objects, sizeof(remote_objects) / sizeof(remote_object_t*) );
413#endif 444#endif
445 // TODO: Make sure these works when either of these are disabled
446 LCD_DISPLAY = gdispGetDisplay(0);
447 LED_DISPLAY = gdispGetDisplay(1);
448
449
414 // We are using a low priority thread, the idea is to have it run only 450 // We are using a low priority thread, the idea is to have it run only
415 // when the main thread is sleeping during the matrix scanning 451 // when the main thread is sleeping during the matrix scanning
416 chEvtObjectInit(&layer_changed_event); 452 chEvtObjectInit(&layer_changed_event);
diff --git a/visualizer.h b/visualizer.h
index 22798cda6..8a2772c6d 100644
--- a/visualizer.h
+++ b/visualizer.h
@@ -45,8 +45,8 @@ void visualizer_suspend(void);
45// This should be called when the keyboard wakes up from suspend state 45// This should be called when the keyboard wakes up from suspend state
46void visualizer_resume(void); 46void visualizer_resume(void);
47 47
48// If you need support for more than 8 keyframes per animation, you can change this 48// If you need support for more than 16 keyframes per animation, you can change this
49#define MAX_VISUALIZER_KEY_FRAMES 8 49#define MAX_VISUALIZER_KEY_FRAMES 16
50 50
51struct keyframe_animation_t; 51struct keyframe_animation_t;
52 52
@@ -95,12 +95,20 @@ typedef struct keyframe_animation_t {
95 // keyframe update functions 95 // keyframe update functions
96 int current_frame; 96 int current_frame;
97 int time_left_in_frame; 97 int time_left_in_frame;
98 bool first_update_of_frame;
99 bool last_update_of_frame;
98 bool need_update; 100 bool need_update;
99 101
100} keyframe_animation_t; 102} keyframe_animation_t;
101 103
104extern GDisplay* LCD_DISPLAY;
105extern GDisplay* LED_DISPLAY;
106
102void start_keyframe_animation(keyframe_animation_t* animation); 107void start_keyframe_animation(keyframe_animation_t* animation);
103void stop_keyframe_animation(keyframe_animation_t* animation); 108void stop_keyframe_animation(keyframe_animation_t* animation);
109// This runs the next keyframe, but does not update the animation state
110// Useful for crossfades for example
111void run_next_keyframe(keyframe_animation_t* animation, visualizer_state_t* state);
104 112
105// Some predefined keyframe functions that can be used by the user code 113// Some predefined keyframe functions that can be used by the user code
106// Does nothing, useful for adding delays 114// Does nothing, useful for adding delays
diff --git a/visualizer.mk b/visualizer.mk
index 13c5d3158..5cc199cf4 100644
--- a/visualizer.mk
+++ b/visualizer.mk
@@ -21,13 +21,14 @@
21# SOFTWARE. 21# SOFTWARE.
22 22
23GFXLIB = $(VISUALIZER_DIR)/ugfx 23GFXLIB = $(VISUALIZER_DIR)/ugfx
24SRC += $(VISUALIZER_DIR)/visualizer.c
25UINCDIR += $(GFXINC) $(VISUALIZER_DIR)
26
24ifdef LCD_ENABLE 27ifdef LCD_ENABLE
25include $(GFXLIB)/gfx.mk
26UDEFS += -DLCD_ENABLE 28UDEFS += -DLCD_ENABLE
27ULIBS += -lm 29ULIBS += -lm
30USE_UGFX = yes
28endif 31endif
29SRC += $(GFXSRC) $(VISUALIZER_DIR)/visualizer.c
30UINCDIR += $(GFXINC) $(VISUALIZER_DIR)
31 32
32ifdef LCD_BACKLIGHT_ENABLE 33ifdef LCD_BACKLIGHT_ENABLE
33SRC += $(VISUALIZER_DIR)/lcd_backlight.c 34SRC += $(VISUALIZER_DIR)/lcd_backlight.c
@@ -35,6 +36,17 @@ SRC += lcd_backlight_hal.c
35UDEFS += -DLCD_BACKLIGHT_ENABLE 36UDEFS += -DLCD_BACKLIGHT_ENABLE
36endif 37endif
37 38
39ifdef LED_ENABLE
40SRC += $(VISUALIZER_DIR)/led_test.c
41UDEFS += -DLED_ENABLE
42USE_UGFX = yes
43endif
44
45ifdef USE_UGFX
46include $(GFXLIB)/gfx.mk
47SRC += $(GFXSRC)
48endif
49
38ifndef VISUALIZER_USER 50ifndef VISUALIZER_USER
39VISUALIZER_USER = visualizer_user.c 51VISUALIZER_USER = visualizer_user.c
40endif 52endif