diff options
| author | Fred Sundvik <fsundvik@gmail.com> | 2016-07-06 20:30:58 +0300 |
|---|---|---|
| committer | Fred Sundvik <fsundvik@gmail.com> | 2016-07-06 20:30:58 +0300 |
| commit | 6c296557909501b71fe344ce379e74094cf77c8e (patch) | |
| tree | e02fdb7bb85a16de027c6c1946817e96d5304ab3 | |
| parent | f727801bc69b3db28f84b7b8986756193bbfd21e (diff) | |
| parent | 73d890a2c9c34b905cd5e74e7146fdd4578dcb96 (diff) | |
| download | qmk_firmware-6c296557909501b71fe344ce379e74094cf77c8e.tar.gz qmk_firmware-6c296557909501b71fe344ce379e74094cf77c8e.zip | |
Merge commit '73d890a2c9c34b905cd5e74e7146fdd4578dcb96' into add_visualizer
| -rw-r--r-- | quantum/visualizer/led_test.c | 170 | ||||
| -rw-r--r-- | quantum/visualizer/led_test.h | 41 | ||||
| -rw-r--r-- | quantum/visualizer/visualizer.c | 104 | ||||
| -rw-r--r-- | quantum/visualizer/visualizer.h | 24 | ||||
| -rw-r--r-- | quantum/visualizer/visualizer.mk | 28 |
5 files changed, 342 insertions, 25 deletions
diff --git a/quantum/visualizer/led_test.c b/quantum/visualizer/led_test.c new file mode 100644 index 000000000..c2ea30b55 --- /dev/null +++ b/quantum/visualizer/led_test.c | |||
| @@ -0,0 +1,170 @@ | |||
| 1 | /* | ||
| 2 | The MIT License (MIT) | ||
| 3 | |||
| 4 | Copyright (c) 2016 Fred Sundvik | ||
| 5 | |||
| 6 | Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| 7 | of this software and associated documentation files (the "Software"), to deal | ||
| 8 | in the Software without restriction, including without limitation the rights | ||
| 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| 10 | copies of the Software, and to permit persons to whom the Software is | ||
| 11 | furnished to do so, subject to the following conditions: | ||
| 12 | |||
| 13 | The above copyright notice and this permission notice shall be included in all | ||
| 14 | copies or substantial portions of the Software. | ||
| 15 | |||
| 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| 22 | SOFTWARE. | ||
| 23 | */ | ||
| 24 | #include "led_test.h" | ||
| 25 | #include "gfx.h" | ||
| 26 | #include "math.h" | ||
| 27 | |||
| 28 | #define CROSSFADE_TIME 1000 | ||
| 29 | #define GRADIENT_TIME 3000 | ||
| 30 | |||
| 31 | keyframe_animation_t led_test_animation = { | ||
| 32 | .num_frames = 14, | ||
| 33 | .loop = true, | ||
| 34 | .frame_lengths = { | ||
| 35 | gfxMillisecondsToTicks(1000), // fade in | ||
| 36 | gfxMillisecondsToTicks(1000), // no op (leds on) | ||
| 37 | gfxMillisecondsToTicks(1000), // fade out | ||
| 38 | gfxMillisecondsToTicks(CROSSFADE_TIME), // crossfade | ||
| 39 | gfxMillisecondsToTicks(GRADIENT_TIME), // left to rigt (outside in) | ||
| 40 | gfxMillisecondsToTicks(CROSSFADE_TIME), // crossfade | ||
| 41 | gfxMillisecondsToTicks(GRADIENT_TIME), // top_to_bottom | ||
| 42 | 0, // mirror leds | ||
| 43 | gfxMillisecondsToTicks(CROSSFADE_TIME), // crossfade | ||
| 44 | gfxMillisecondsToTicks(GRADIENT_TIME), // left_to_right (mirrored, so inside out) | ||
| 45 | gfxMillisecondsToTicks(CROSSFADE_TIME), // crossfade | ||
| 46 | gfxMillisecondsToTicks(GRADIENT_TIME), // top_to_bottom | ||
| 47 | 0, // normal leds | ||
| 48 | gfxMillisecondsToTicks(CROSSFADE_TIME), // crossfade | ||
| 49 | |||
| 50 | }, | ||
| 51 | .frame_functions = { | ||
| 52 | keyframe_fade_in_all_leds, | ||
| 53 | keyframe_no_operation, | ||
| 54 | keyframe_fade_out_all_leds, | ||
| 55 | keyframe_led_crossfade, | ||
| 56 | keyframe_led_left_to_right_gradient, | ||
| 57 | keyframe_led_crossfade, | ||
| 58 | keyframe_led_top_to_bottom_gradient, | ||
| 59 | keyframe_mirror_led_orientation, | ||
| 60 | keyframe_led_crossfade, | ||
| 61 | keyframe_led_left_to_right_gradient, | ||
| 62 | keyframe_led_crossfade, | ||
| 63 | keyframe_led_top_to_bottom_gradient, | ||
| 64 | keyframe_normal_led_orientation, | ||
| 65 | keyframe_led_crossfade, | ||
| 66 | }, | ||
| 67 | }; | ||
| 68 | |||
| 69 | static uint8_t fade_led_color(keyframe_animation_t* animation, int from, int to) { | ||
| 70 | int frame_length = animation->frame_lengths[animation->current_frame]; | ||
| 71 | int current_pos = frame_length - animation->time_left_in_frame; | ||
| 72 | int delta = to - from; | ||
| 73 | int luma = (delta * current_pos) / frame_length; | ||
| 74 | luma += from; | ||
| 75 | return luma; | ||
| 76 | } | ||
| 77 | |||
| 78 | static void keyframe_fade_all_leds_from_to(keyframe_animation_t* animation, uint8_t from, uint8_t to) { | ||
| 79 | uint8_t luma = fade_led_color(animation, from, to); | ||
| 80 | color_t color = LUMA2COLOR(luma); | ||
| 81 | gdispGClear(LED_DISPLAY, color); | ||
| 82 | } | ||
| 83 | |||
| 84 | // TODO: Should be customizable per keyboard | ||
| 85 | #define NUM_ROWS 7 | ||
| 86 | #define NUM_COLS 7 | ||
| 87 | |||
| 88 | static uint8_t crossfade_start_frame[NUM_ROWS][NUM_COLS]; | ||
| 89 | static uint8_t crossfade_end_frame[NUM_ROWS][NUM_COLS]; | ||
| 90 | |||
| 91 | static uint8_t compute_gradient_color(float t, float index, float num) { | ||
| 92 | const float two_pi = 2.0f * PI; | ||
| 93 | float normalized_index = (1.0f - index / (num - 1)) * two_pi; | ||
| 94 | float x = t * two_pi + normalized_index; | ||
| 95 | float v = 0.5 * (cosf(x) + 1.0f); | ||
| 96 | return (uint8_t)(255.0f * v); | ||
| 97 | } | ||
| 98 | |||
| 99 | bool 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 | |||
| 105 | bool 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 | |||
| 111 | bool 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 | |||
| 123 | bool 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 | |||
| 135 | static 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 | } | ||
| 142 | bool 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 | |||
| 158 | bool 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 | |||
| 165 | bool 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/quantum/visualizer/led_test.h b/quantum/visualizer/led_test.h new file mode 100644 index 000000000..5e2325753 --- /dev/null +++ b/quantum/visualizer/led_test.h | |||
| @@ -0,0 +1,41 @@ | |||
| 1 | /* | ||
| 2 | The MIT License (MIT) | ||
| 3 | |||
| 4 | Copyright (c) 2016 Fred Sundvik | ||
| 5 | |||
| 6 | Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| 7 | of this software and associated documentation files (the "Software"), to deal | ||
| 8 | in the Software without restriction, including without limitation the rights | ||
| 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| 10 | copies of the Software, and to permit persons to whom the Software is | ||
| 11 | furnished to do so, subject to the following conditions: | ||
| 12 | |||
| 13 | The above copyright notice and this permission notice shall be included in all | ||
| 14 | copies or substantial portions of the Software. | ||
| 15 | |||
| 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| 22 | SOFTWARE. | ||
| 23 | */ | ||
| 24 | |||
| 25 | #ifndef TMK_VISUALIZER_LED_TEST_H_ | ||
| 26 | #define TMK_VISUALIZER_LED_TEST_H_ | ||
| 27 | |||
| 28 | #include "visualizer.h" | ||
| 29 | |||
| 30 | bool keyframe_fade_in_all_leds(keyframe_animation_t* animation, visualizer_state_t* state); | ||
| 31 | bool keyframe_fade_out_all_leds(keyframe_animation_t* animation, visualizer_state_t* state); | ||
| 32 | bool keyframe_led_left_to_right_gradient(keyframe_animation_t* animation, visualizer_state_t* state); | ||
| 33 | bool keyframe_led_top_to_bottom_gradient(keyframe_animation_t* animation, visualizer_state_t* state); | ||
| 34 | bool keyframe_led_crossfade(keyframe_animation_t* animation, visualizer_state_t* state); | ||
| 35 | bool keyframe_mirror_led_orientation(keyframe_animation_t* animation, visualizer_state_t* state); | ||
| 36 | bool keyframe_normal_led_orientation(keyframe_animation_t* animation, visualizer_state_t* state); | ||
| 37 | |||
| 38 | extern keyframe_animation_t led_test_animation; | ||
| 39 | |||
| 40 | |||
| 41 | #endif /* TMK_VISUALIZER_LED_TEST_H_ */ | ||
diff --git a/quantum/visualizer/visualizer.c b/quantum/visualizer/visualizer.c index 605be3059..c24073405 100644 --- a/quantum/visualizer/visualizer.c +++ b/quantum/visualizer/visualizer.c | |||
| @@ -23,9 +23,11 @@ SOFTWARE. | |||
| 23 | */ | 23 | */ |
| 24 | 24 | ||
| 25 | #include "visualizer.h" | 25 | #include "visualizer.h" |
| 26 | #include "ch.h" | ||
| 27 | #include "config.h" | 26 | #include "config.h" |
| 28 | #include <string.h> | 27 | #include <string.h> |
| 28 | #ifdef PROTOCOL_CHIBIOS | ||
| 29 | #include "ch.h" | ||
| 30 | #endif | ||
| 29 | 31 | ||
| 30 | #ifdef LCD_ENABLE | 32 | #ifdef LCD_ENABLE |
| 31 | #include "gfx.h" | 33 | #include "gfx.h" |
| @@ -68,7 +70,6 @@ static bool same_status(visualizer_keyboard_status_t* status1, visualizer_keyboa | |||
| 68 | status1->suspended == status2->suspended; | 70 | status1->suspended == status2->suspended; |
| 69 | } | 71 | } |
| 70 | 72 | ||
| 71 | static event_source_t layer_changed_event; | ||
| 72 | static bool visualizer_enabled = false; | 73 | static bool visualizer_enabled = false; |
| 73 | 74 | ||
| 74 | #define MAX_SIMULTANEOUS_ANIMATIONS 4 | 75 | #define MAX_SIMULTANEOUS_ANIMATIONS 4 |
| @@ -83,6 +84,18 @@ static remote_object_t* remote_objects[] = { | |||
| 83 | 84 | ||
| 84 | #endif | 85 | #endif |
| 85 | 86 | ||
| 87 | GDisplay* LCD_DISPLAY = 0; | ||
| 88 | GDisplay* LED_DISPLAY = 0; | ||
| 89 | |||
| 90 | __attribute__((weak)) | ||
| 91 | GDisplay* get_lcd_display(void) { | ||
| 92 | return gdispGetDisplay(0); | ||
| 93 | } | ||
| 94 | |||
| 95 | __attribute__((weak)) | ||
| 96 | GDisplay* get_led_display(void) { | ||
| 97 | return gdispGetDisplay(1); | ||
| 98 | } | ||
| 86 | 99 | ||
| 87 | void start_keyframe_animation(keyframe_animation_t* animation) { | 100 | void start_keyframe_animation(keyframe_animation_t* animation) { |
| 88 | animation->current_frame = -1; | 101 | animation->current_frame = -1; |
| @@ -106,6 +119,8 @@ void stop_keyframe_animation(keyframe_animation_t* animation) { | |||
| 106 | animation->current_frame = animation->num_frames; | 119 | animation->current_frame = animation->num_frames; |
| 107 | animation->time_left_in_frame = 0; | 120 | animation->time_left_in_frame = 0; |
| 108 | animation->need_update = true; | 121 | animation->need_update = true; |
| 122 | animation->first_update_of_frame = false; | ||
| 123 | animation->last_update_of_frame = false; | ||
| 109 | for (int i=0;i<MAX_SIMULTANEOUS_ANIMATIONS;i++) { | 124 | for (int i=0;i<MAX_SIMULTANEOUS_ANIMATIONS;i++) { |
| 110 | if (animations[i] == animation) { | 125 | if (animations[i] == animation) { |
| 111 | animations[i] = NULL; | 126 | animations[i] = NULL; |
| @@ -120,12 +135,15 @@ void stop_all_keyframe_animations(void) { | |||
| 120 | animations[i]->current_frame = animations[i]->num_frames; | 135 | animations[i]->current_frame = animations[i]->num_frames; |
| 121 | animations[i]->time_left_in_frame = 0; | 136 | animations[i]->time_left_in_frame = 0; |
| 122 | animations[i]->need_update = true; | 137 | animations[i]->need_update = true; |
| 138 | animations[i]->first_update_of_frame = false; | ||
| 139 | animations[i]->last_update_of_frame = false; | ||
| 123 | animations[i] = NULL; | 140 | animations[i] = NULL; |
| 124 | } | 141 | } |
| 125 | } | 142 | } |
| 126 | } | 143 | } |
| 127 | 144 | ||
| 128 | static bool update_keyframe_animation(keyframe_animation_t* animation, visualizer_state_t* state, systime_t delta, systime_t* sleep_time) { | 145 | static bool update_keyframe_animation(keyframe_animation_t* animation, visualizer_state_t* state, systemticks_t delta, systemticks_t* sleep_time) { |
| 146 | // TODO: Clean up this messy code | ||
| 129 | dprintf("Animation frame%d, left %d, delta %d\n", animation->current_frame, | 147 | dprintf("Animation frame%d, left %d, delta %d\n", animation->current_frame, |
| 130 | animation->time_left_in_frame, delta); | 148 | animation->time_left_in_frame, delta); |
| 131 | if (animation->current_frame == animation->num_frames) { | 149 | if (animation->current_frame == animation->num_frames) { |
| @@ -136,16 +154,20 @@ static bool update_keyframe_animation(keyframe_animation_t* animation, visualize | |||
| 136 | animation->current_frame = 0; | 154 | animation->current_frame = 0; |
| 137 | animation->time_left_in_frame = animation->frame_lengths[0]; | 155 | animation->time_left_in_frame = animation->frame_lengths[0]; |
| 138 | animation->need_update = true; | 156 | animation->need_update = true; |
| 157 | animation->first_update_of_frame = true; | ||
| 139 | } else { | 158 | } else { |
| 140 | animation->time_left_in_frame -= delta; | 159 | animation->time_left_in_frame -= delta; |
| 141 | while (animation->time_left_in_frame <= 0) { | 160 | while (animation->time_left_in_frame <= 0) { |
| 142 | int left = animation->time_left_in_frame; | 161 | int left = animation->time_left_in_frame; |
| 143 | if (animation->need_update) { | 162 | if (animation->need_update) { |
| 144 | animation->time_left_in_frame = 0; | 163 | animation->time_left_in_frame = 0; |
| 164 | animation->last_update_of_frame = true; | ||
| 145 | (*animation->frame_functions[animation->current_frame])(animation, state); | 165 | (*animation->frame_functions[animation->current_frame])(animation, state); |
| 166 | animation->last_update_of_frame = false; | ||
| 146 | } | 167 | } |
| 147 | animation->current_frame++; | 168 | animation->current_frame++; |
| 148 | animation->need_update = true; | 169 | animation->need_update = true; |
| 170 | animation->first_update_of_frame = true; | ||
| 149 | if (animation->current_frame == animation->num_frames) { | 171 | if (animation->current_frame == animation->num_frames) { |
| 150 | if (animation->loop) { | 172 | if (animation->loop) { |
| 151 | animation->current_frame = 0; | 173 | animation->current_frame = 0; |
| @@ -162,16 +184,32 @@ static bool update_keyframe_animation(keyframe_animation_t* animation, visualize | |||
| 162 | } | 184 | } |
| 163 | if (animation->need_update) { | 185 | if (animation->need_update) { |
| 164 | animation->need_update = (*animation->frame_functions[animation->current_frame])(animation, state); | 186 | animation->need_update = (*animation->frame_functions[animation->current_frame])(animation, state); |
| 187 | animation->first_update_of_frame = false; | ||
| 165 | } | 188 | } |
| 166 | 189 | ||
| 167 | int wanted_sleep = animation->need_update ? 10 : animation->time_left_in_frame; | 190 | systemticks_t wanted_sleep = animation->need_update ? gfxMillisecondsToTicks(10) : (unsigned)animation->time_left_in_frame; |
| 168 | if ((unsigned)wanted_sleep < *sleep_time) { | 191 | if (wanted_sleep < *sleep_time) { |
| 169 | *sleep_time = wanted_sleep; | 192 | *sleep_time = wanted_sleep; |
| 170 | } | 193 | } |
| 171 | 194 | ||
| 172 | return true; | 195 | return true; |
| 173 | } | 196 | } |
| 174 | 197 | ||
| 198 | void run_next_keyframe(keyframe_animation_t* animation, visualizer_state_t* state) { | ||
| 199 | int next_frame = animation->current_frame + 1; | ||
| 200 | if (next_frame == animation->num_frames) { | ||
| 201 | next_frame = 0; | ||
| 202 | } | ||
| 203 | keyframe_animation_t temp_animation = *animation; | ||
| 204 | temp_animation.current_frame = next_frame; | ||
| 205 | temp_animation.time_left_in_frame = animation->frame_lengths[next_frame]; | ||
| 206 | temp_animation.first_update_of_frame = true; | ||
| 207 | temp_animation.last_update_of_frame = false; | ||
| 208 | temp_animation.need_update = false; | ||
| 209 | visualizer_state_t temp_state = *state; | ||
| 210 | (*temp_animation.frame_functions[next_frame])(&temp_animation, &temp_state); | ||
| 211 | } | ||
| 212 | |||
| 175 | bool keyframe_no_operation(keyframe_animation_t* animation, visualizer_state_t* state) { | 213 | bool keyframe_no_operation(keyframe_animation_t* animation, visualizer_state_t* state) { |
| 176 | (void)animation; | 214 | (void)animation; |
| 177 | (void)state; | 215 | (void)state; |
| @@ -303,12 +341,13 @@ bool enable_visualization(keyframe_animation_t* animation, visualizer_state_t* s | |||
| 303 | } | 341 | } |
| 304 | 342 | ||
| 305 | // TODO: Optimize the stack size, this is probably way too big | 343 | // TODO: Optimize the stack size, this is probably way too big |
| 306 | static THD_WORKING_AREA(visualizerThreadStack, 1024); | 344 | static DECLARE_THREAD_STACK(visualizerThreadStack, 1024); |
| 307 | static THD_FUNCTION(visualizerThread, arg) { | 345 | static DECLARE_THREAD_FUNCTION(visualizerThread, arg) { |
| 308 | (void)arg; | 346 | (void)arg; |
| 309 | 347 | ||
| 310 | event_listener_t event_listener; | 348 | GListener event_listener; |
| 311 | chEvtRegister(&layer_changed_event, &event_listener, 0); | 349 | geventListenerInit(&event_listener); |
| 350 | geventAttachSource(&event_listener, (GSourceHandle)¤t_status, 0); | ||
| 312 | 351 | ||
| 313 | visualizer_keyboard_status_t initial_status = { | 352 | visualizer_keyboard_status_t initial_status = { |
| 314 | .default_layer = 0xFFFFFFFF, | 353 | .default_layer = 0xFFFFFFFF, |
| @@ -335,12 +374,12 @@ static THD_FUNCTION(visualizerThread, arg) { | |||
| 335 | LCD_INT(state.current_lcd_color)); | 374 | LCD_INT(state.current_lcd_color)); |
| 336 | #endif | 375 | #endif |
| 337 | 376 | ||
| 338 | systime_t sleep_time = TIME_INFINITE; | 377 | systemticks_t sleep_time = TIME_INFINITE; |
| 339 | systime_t current_time = chVTGetSystemTimeX(); | 378 | systemticks_t current_time = gfxSystemTicks(); |
| 340 | 379 | ||
| 341 | while(true) { | 380 | while(true) { |
| 342 | systime_t new_time = chVTGetSystemTimeX(); | 381 | systemticks_t new_time = gfxSystemTicks(); |
| 343 | systime_t delta = new_time - current_time; | 382 | systemticks_t delta = new_time - current_time; |
| 344 | current_time = new_time; | 383 | current_time = new_time; |
| 345 | bool enabled = visualizer_enabled; | 384 | bool enabled = visualizer_enabled; |
| 346 | if (!same_status(&state.status, ¤t_status)) { | 385 | if (!same_status(&state.status, ¤t_status)) { |
| @@ -373,6 +412,13 @@ static THD_FUNCTION(visualizerThread, arg) { | |||
| 373 | update_keyframe_animation(animations[i], &state, delta, &sleep_time); | 412 | update_keyframe_animation(animations[i], &state, delta, &sleep_time); |
| 374 | } | 413 | } |
| 375 | } | 414 | } |
| 415 | #ifdef LED_ENABLE | ||
| 416 | gdispGFlush(LED_DISPLAY); | ||
| 417 | #endif | ||
| 418 | |||
| 419 | #ifdef EMULATOR | ||
| 420 | draw_emulator(); | ||
| 421 | #endif | ||
| 376 | // The animation can enable the visualizer | 422 | // The animation can enable the visualizer |
| 377 | // And we might need to update the state when that happens | 423 | // And we might need to update the state when that happens |
| 378 | // so don't sleep | 424 | // so don't sleep |
| @@ -380,7 +426,7 @@ static THD_FUNCTION(visualizerThread, arg) { | |||
| 380 | sleep_time = 0; | 426 | sleep_time = 0; |
| 381 | } | 427 | } |
| 382 | 428 | ||
| 383 | systime_t after_update = chVTGetSystemTimeX(); | 429 | systemticks_t after_update = gfxSystemTicks(); |
| 384 | unsigned update_delta = after_update - current_time; | 430 | unsigned update_delta = after_update - current_time; |
| 385 | if (sleep_time != TIME_INFINITE) { | 431 | if (sleep_time != TIME_INFINITE) { |
| 386 | if (sleep_time > update_delta) { | 432 | if (sleep_time > update_delta) { |
| @@ -391,12 +437,24 @@ static THD_FUNCTION(visualizerThread, arg) { | |||
| 391 | } | 437 | } |
| 392 | } | 438 | } |
| 393 | dprintf("Update took %d, last delta %d, sleep_time %d\n", update_delta, delta, sleep_time); | 439 | dprintf("Update took %d, last delta %d, sleep_time %d\n", update_delta, delta, sleep_time); |
| 394 | chEvtWaitOneTimeout(EVENT_MASK(0), sleep_time); | 440 | #ifdef PROTOCOL_CHIBIOS |
| 441 | // The gEventWait function really takes milliseconds, even if the documentation says ticks. | ||
| 442 | // Unfortunately there's no generic ugfx conversion from system time to milliseconds, | ||
| 443 | // so let's do it in a platform dependent way. | ||
| 444 | |||
| 445 | // On windows the system ticks is the same as milliseconds anyway | ||
| 446 | if (sleep_time != TIME_INFINITE) { | ||
| 447 | sleep_time = ST2MS(sleep_time); | ||
| 448 | } | ||
| 449 | #endif | ||
| 450 | geventEventWait(&event_listener, sleep_time); | ||
| 395 | } | 451 | } |
| 396 | #ifdef LCD_ENABLE | 452 | #ifdef LCD_ENABLE |
| 397 | gdispCloseFont(state.font_fixed5x8); | 453 | gdispCloseFont(state.font_fixed5x8); |
| 398 | gdispCloseFont(state.font_dejavusansbold12); | 454 | gdispCloseFont(state.font_dejavusansbold12); |
| 399 | #endif | 455 | #endif |
| 456 | |||
| 457 | return 0; | ||
| 400 | } | 458 | } |
| 401 | 459 | ||
| 402 | void visualizer_init(void) { | 460 | void visualizer_init(void) { |
| @@ -411,16 +469,26 @@ void visualizer_init(void) { | |||
| 411 | #ifdef USE_SERIAL_LINK | 469 | #ifdef USE_SERIAL_LINK |
| 412 | add_remote_objects(remote_objects, sizeof(remote_objects) / sizeof(remote_object_t*) ); | 470 | add_remote_objects(remote_objects, sizeof(remote_objects) / sizeof(remote_object_t*) ); |
| 413 | #endif | 471 | #endif |
| 472 | |||
| 473 | #ifdef LCD_ENABLE | ||
| 474 | LCD_DISPLAY = get_lcd_display(); | ||
| 475 | #endif | ||
| 476 | #ifdef LED_ENABLE | ||
| 477 | LED_DISPLAY = get_led_display(); | ||
| 478 | #endif | ||
| 479 | |||
| 414 | // We are using a low priority thread, the idea is to have it run only | 480 | // 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 | 481 | // when the main thread is sleeping during the matrix scanning |
| 416 | chEvtObjectInit(&layer_changed_event); | 482 | gfxThreadCreate(visualizerThreadStack, sizeof(visualizerThreadStack), |
| 417 | (void)chThdCreateStatic(visualizerThreadStack, sizeof(visualizerThreadStack), | ||
| 418 | VISUALIZER_THREAD_PRIORITY, visualizerThread, NULL); | 483 | VISUALIZER_THREAD_PRIORITY, visualizerThread, NULL); |
| 419 | } | 484 | } |
| 420 | 485 | ||
| 421 | void update_status(bool changed) { | 486 | void update_status(bool changed) { |
| 422 | if (changed) { | 487 | if (changed) { |
| 423 | chEvtBroadcast(&layer_changed_event); | 488 | GSourceListener* listener = geventGetSourceListener((GSourceHandle)¤t_status, NULL); |
| 489 | if (listener) { | ||
| 490 | geventSendEvent(listener); | ||
| 491 | } | ||
| 424 | } | 492 | } |
| 425 | #ifdef USE_SERIAL_LINK | 493 | #ifdef USE_SERIAL_LINK |
| 426 | static systime_t last_update = 0; | 494 | static systime_t last_update = 0; |
diff --git a/quantum/visualizer/visualizer.h b/quantum/visualizer/visualizer.h index 22798cda6..45cfa9aa9 100644 --- a/quantum/visualizer/visualizer.h +++ b/quantum/visualizer/visualizer.h | |||
| @@ -45,8 +45,18 @@ 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 |
| 46 | void visualizer_resume(void); | 46 | void visualizer_resume(void); |
| 47 | 47 | ||
| 48 | // If you need support for more than 8 keyframes per animation, you can change this | 48 | // These functions are week, so they can be overridden by the keyboard |
| 49 | #define MAX_VISUALIZER_KEY_FRAMES 8 | 49 | // if needed |
| 50 | GDisplay* get_lcd_display(void); | ||
| 51 | GDisplay* get_led_display(void); | ||
| 52 | |||
| 53 | // For emulator builds, this function need to be implemented | ||
| 54 | #ifdef EMULATOR | ||
| 55 | void draw_emulator(void); | ||
| 56 | #endif | ||
| 57 | |||
| 58 | // If you need support for more than 16 keyframes per animation, you can change this | ||
| 59 | #define MAX_VISUALIZER_KEY_FRAMES 16 | ||
| 50 | 60 | ||
| 51 | struct keyframe_animation_t; | 61 | struct keyframe_animation_t; |
| 52 | 62 | ||
| @@ -95,12 +105,20 @@ typedef struct keyframe_animation_t { | |||
| 95 | // keyframe update functions | 105 | // keyframe update functions |
| 96 | int current_frame; | 106 | int current_frame; |
| 97 | int time_left_in_frame; | 107 | int time_left_in_frame; |
| 108 | bool first_update_of_frame; | ||
| 109 | bool last_update_of_frame; | ||
| 98 | bool need_update; | 110 | bool need_update; |
| 99 | 111 | ||
| 100 | } keyframe_animation_t; | 112 | } keyframe_animation_t; |
| 101 | 113 | ||
| 114 | extern GDisplay* LCD_DISPLAY; | ||
| 115 | extern GDisplay* LED_DISPLAY; | ||
| 116 | |||
| 102 | void start_keyframe_animation(keyframe_animation_t* animation); | 117 | void start_keyframe_animation(keyframe_animation_t* animation); |
| 103 | void stop_keyframe_animation(keyframe_animation_t* animation); | 118 | void stop_keyframe_animation(keyframe_animation_t* animation); |
| 119 | // This runs the next keyframe, but does not update the animation state | ||
| 120 | // Useful for crossfades for example | ||
| 121 | void run_next_keyframe(keyframe_animation_t* animation, visualizer_state_t* state); | ||
| 104 | 122 | ||
| 105 | // Some predefined keyframe functions that can be used by the user code | 123 | // Some predefined keyframe functions that can be used by the user code |
| 106 | // Does nothing, useful for adding delays | 124 | // Does nothing, useful for adding delays |
| @@ -121,7 +139,7 @@ bool keyframe_enable_lcd_and_backlight(keyframe_animation_t* animation, visualiz | |||
| 121 | // directly from the initalize_user_visualizer function (the animation can be null) | 139 | // directly from the initalize_user_visualizer function (the animation can be null) |
| 122 | bool enable_visualization(keyframe_animation_t* animation, visualizer_state_t* state); | 140 | bool enable_visualization(keyframe_animation_t* animation, visualizer_state_t* state); |
| 123 | 141 | ||
| 124 | // These two functions have to be implemented by the user | 142 | // These functions have to be implemented by the user |
| 125 | void initialize_user_visualizer(visualizer_state_t* state); | 143 | void initialize_user_visualizer(visualizer_state_t* state); |
| 126 | void update_user_visualizer_state(visualizer_state_t* state); | 144 | void update_user_visualizer_state(visualizer_state_t* state); |
| 127 | void user_visualizer_suspend(visualizer_state_t* state); | 145 | void user_visualizer_suspend(visualizer_state_t* state); |
diff --git a/quantum/visualizer/visualizer.mk b/quantum/visualizer/visualizer.mk index 13c5d3158..56525ffd9 100644 --- a/quantum/visualizer/visualizer.mk +++ b/quantum/visualizer/visualizer.mk | |||
| @@ -21,21 +21,41 @@ | |||
| 21 | # SOFTWARE. | 21 | # SOFTWARE. |
| 22 | 22 | ||
| 23 | GFXLIB = $(VISUALIZER_DIR)/ugfx | 23 | GFXLIB = $(VISUALIZER_DIR)/ugfx |
| 24 | SRC += $(VISUALIZER_DIR)/visualizer.c | ||
| 25 | UINCDIR += $(GFXINC) $(VISUALIZER_DIR) | ||
| 26 | |||
| 24 | ifdef LCD_ENABLE | 27 | ifdef LCD_ENABLE |
| 25 | include $(GFXLIB)/gfx.mk | ||
| 26 | UDEFS += -DLCD_ENABLE | 28 | UDEFS += -DLCD_ENABLE |
| 27 | ULIBS += -lm | 29 | ULIBS += -lm |
| 30 | USE_UGFX = yes | ||
| 28 | endif | 31 | endif |
| 29 | SRC += $(GFXSRC) $(VISUALIZER_DIR)/visualizer.c | ||
| 30 | UINCDIR += $(GFXINC) $(VISUALIZER_DIR) | ||
| 31 | 32 | ||
| 32 | ifdef LCD_BACKLIGHT_ENABLE | 33 | ifdef LCD_BACKLIGHT_ENABLE |
| 33 | SRC += $(VISUALIZER_DIR)/lcd_backlight.c | 34 | SRC += $(VISUALIZER_DIR)/lcd_backlight.c |
| 35 | ifndef EMULATOR | ||
| 34 | SRC += lcd_backlight_hal.c | 36 | SRC += lcd_backlight_hal.c |
| 37 | endif | ||
| 35 | UDEFS += -DLCD_BACKLIGHT_ENABLE | 38 | UDEFS += -DLCD_BACKLIGHT_ENABLE |
| 36 | endif | 39 | endif |
| 37 | 40 | ||
| 41 | ifdef LED_ENABLE | ||
| 42 | SRC += $(VISUALIZER_DIR)/led_test.c | ||
| 43 | UDEFS += -DLED_ENABLE | ||
| 44 | USE_UGFX = yes | ||
| 45 | endif | ||
| 46 | |||
| 47 | ifdef USE_UGFX | ||
| 48 | include $(GFXLIB)/gfx.mk | ||
| 49 | SRC += $(GFXSRC) | ||
| 50 | UDEFS += $(patsubst %,-D%,$(patsubst -D%,%,$(GFXDEFS))) | ||
| 51 | ULIBS += $(patsubst %,-l%,$(patsubst -l%,%,$(GFXLIBS))) | ||
| 52 | endif | ||
| 53 | |||
| 38 | ifndef VISUALIZER_USER | 54 | ifndef VISUALIZER_USER |
| 39 | VISUALIZER_USER = visualizer_user.c | 55 | VISUALIZER_USER = visualizer_user.c |
| 40 | endif | 56 | endif |
| 41 | SRC += $(VISUALIZER_USER) \ No newline at end of file | 57 | SRC += $(VISUALIZER_USER) |
| 58 | |||
| 59 | ifdef EMULATOR | ||
| 60 | UINCDIR += $(TMK_DIR)/common | ||
| 61 | endif \ No newline at end of file | ||
