diff options
| author | Fred Sundvik <fsundvik@gmail.com> | 2016-04-24 16:20:00 +0300 |
|---|---|---|
| committer | Fred Sundvik <fsundvik@gmail.com> | 2016-04-24 16:20:00 +0300 |
| commit | 0530ebb77d6961a7edc14f3a5b943165a8b52497 (patch) | |
| tree | 7179305c91735e796bd3b48b974ad2dcc0007bfc | |
| parent | 891edbd533acdffec66416c59f4b6066e5e18aaa (diff) | |
| download | qmk_firmware-0530ebb77d6961a7edc14f3a5b943165a8b52497.tar.gz qmk_firmware-0530ebb77d6961a7edc14f3a5b943165a8b52497.zip | |
Add led crossfading
| -rw-r--r-- | led_test.c | 68 | ||||
| -rw-r--r-- | led_test.h | 2 |
2 files changed, 64 insertions, 6 deletions
diff --git a/led_test.c b/led_test.c index 197550fdd..8c0de604a 100644 --- a/led_test.c +++ b/led_test.c | |||
| @@ -26,27 +26,41 @@ SOFTWARE. | |||
| 26 | #include "math.h" | 26 | #include "math.h" |
| 27 | 27 | ||
| 28 | keyframe_animation_t led_test_animation = { | 28 | keyframe_animation_t led_test_animation = { |
| 29 | .num_frames = 4, | 29 | .num_frames = 8, |
| 30 | .loop = true, | 30 | .loop = true, |
| 31 | .frame_lengths = { | 31 | .frame_lengths = { |
| 32 | MS2ST(1000), | 32 | MS2ST(1000), // fade in |
| 33 | MS2ST(1000), | 33 | MS2ST(1000), // no op (leds on) |
| 34 | MS2ST(1000), | 34 | MS2ST(1000), // fade out |
| 35 | MS2ST(3000)}, | 35 | MS2ST(1000), // crossfade |
| 36 | MS2ST(3000), // left to rigt | ||
| 37 | MS2ST(1000), // crossfade | ||
| 38 | MS2ST(3000), // top_to_bottom | ||
| 39 | MS2ST(1000), // crossfade | ||
| 40 | }, | ||
| 36 | .frame_functions = { | 41 | .frame_functions = { |
| 37 | keyframe_fade_in_all_leds, | 42 | keyframe_fade_in_all_leds, |
| 38 | keyframe_no_operation, | 43 | keyframe_no_operation, |
| 39 | keyframe_fade_out_all_leds, | 44 | keyframe_fade_out_all_leds, |
| 45 | keyframe_led_crossfade, | ||
| 40 | keyframe_led_left_to_right_gradient, | 46 | keyframe_led_left_to_right_gradient, |
| 47 | keyframe_led_crossfade, | ||
| 48 | keyframe_led_top_to_bottom_gradient, | ||
| 49 | keyframe_led_crossfade | ||
| 41 | }, | 50 | }, |
| 42 | }; | 51 | }; |
| 43 | 52 | ||
| 44 | static void keyframe_fade_all_leds_from_to(keyframe_animation_t* animation, uint8_t from, uint8_t to) { | 53 | static uint8_t fade_led_color(keyframe_animation_t* animation, uint8_t from, uint8_t to) { |
| 45 | int frame_length = animation->frame_lengths[animation->current_frame]; | 54 | int frame_length = animation->frame_lengths[animation->current_frame]; |
| 46 | int current_pos = frame_length - animation->time_left_in_frame; | 55 | int current_pos = frame_length - animation->time_left_in_frame; |
| 47 | int delta = to - from; | 56 | int delta = to - from; |
| 48 | int luma = (delta * current_pos) / frame_length; | 57 | int luma = (delta * current_pos) / frame_length; |
| 49 | luma += from; | 58 | luma += from; |
| 59 | return luma; | ||
| 60 | } | ||
| 61 | |||
| 62 | static void keyframe_fade_all_leds_from_to(keyframe_animation_t* animation, uint8_t from, uint8_t to) { | ||
| 63 | uint8_t luma = fade_led_color(animation, from, to); | ||
| 50 | color_t color = LUMA2COLOR(luma); | 64 | color_t color = LUMA2COLOR(luma); |
| 51 | gdispGClear(LED_DISPLAY, color); | 65 | gdispGClear(LED_DISPLAY, color); |
| 52 | gdispGFlush(LED_DISPLAY); | 66 | gdispGFlush(LED_DISPLAY); |
| @@ -56,6 +70,9 @@ static void keyframe_fade_all_leds_from_to(keyframe_animation_t* animation, uint | |||
| 56 | #define NUM_ROWS 7 | 70 | #define NUM_ROWS 7 |
| 57 | #define NUM_COLS 7 | 71 | #define NUM_COLS 7 |
| 58 | 72 | ||
| 73 | static uint8_t crossfade_start_frame[NUM_ROWS][NUM_COLS]; | ||
| 74 | static uint8_t crossfade_end_frame[NUM_ROWS][NUM_COLS]; | ||
| 75 | |||
| 59 | static uint8_t compute_gradient_color(float t, float index, float num) { | 76 | static uint8_t compute_gradient_color(float t, float index, float num) { |
| 60 | float d = fabs(index - t); | 77 | float d = fabs(index - t); |
| 61 | if (d > num / 2.0f) { | 78 | if (d > num / 2.0f) { |
| @@ -85,5 +102,44 @@ bool keyframe_led_left_to_right_gradient(keyframe_animation_t* animation, visual | |||
| 85 | uint8_t color = compute_gradient_color(t, i, NUM_COLS); | 102 | uint8_t color = compute_gradient_color(t, i, NUM_COLS); |
| 86 | gdispGDrawLine(LED_DISPLAY, i, 0, i, NUM_ROWS - 1, LUMA2COLOR(color)); | 103 | gdispGDrawLine(LED_DISPLAY, i, 0, i, NUM_ROWS - 1, LUMA2COLOR(color)); |
| 87 | } | 104 | } |
| 105 | gdispGFlush(LED_DISPLAY); | ||
| 106 | return true; | ||
| 107 | } | ||
| 108 | |||
| 109 | bool keyframe_led_top_to_bottom_gradient(keyframe_animation_t* animation, visualizer_state_t* state) { | ||
| 110 | (void)state; | ||
| 111 | int frame_length = animation->frame_lengths[animation->current_frame]; | ||
| 112 | int current_pos = frame_length - animation->time_left_in_frame; | ||
| 113 | float t = current_pos / frame_length; | ||
| 114 | for (int i=0; i< NUM_ROWS; i++) { | ||
| 115 | uint8_t color = compute_gradient_color(t, i, NUM_ROWS); | ||
| 116 | gdispGDrawLine(LED_DISPLAY, 0, i, NUM_COLS - 1, i, LUMA2COLOR(color)); | ||
| 117 | } | ||
| 118 | gdispGFlush(LED_DISPLAY); | ||
| 119 | return true; | ||
| 120 | } | ||
| 121 | |||
| 122 | static void copy_current_led_state(uint8_t* dest) { | ||
| 123 | for (int i=0;i<NUM_ROWS;i++) { | ||
| 124 | for (int j=0;j<NUM_COLS;j++) { | ||
| 125 | dest[i*NUM_COLS + j] = gdispGGetPixelColor(LED_DISPLAY, j, i); | ||
| 126 | } | ||
| 127 | } | ||
| 128 | } | ||
| 129 | |||
| 130 | bool keyframe_led_crossfade(keyframe_animation_t* animation, visualizer_state_t* state) { | ||
| 131 | (void)state; | ||
| 132 | if (animation->first_update_of_frame) { | ||
| 133 | copy_current_led_state(&crossfade_start_frame[0][0]); | ||
| 134 | run_next_keyframe(animation, state); | ||
| 135 | copy_current_led_state(&crossfade_end_frame[0][0]); | ||
| 136 | } | ||
| 137 | for (int i=0;i<NUM_ROWS;i++) { | ||
| 138 | for (int j=0;j<NUM_COLS;j++) { | ||
| 139 | color_t color = LUMA2COLOR(fade_led_color(animation, crossfade_start_frame[i][j], crossfade_end_frame[i][j])); | ||
| 140 | gdispGDrawPixel(LED_DISPLAY, j, i, color); | ||
| 141 | } | ||
| 142 | } | ||
| 143 | gdispGFlush(LED_DISPLAY); | ||
| 88 | return true; | 144 | return true; |
| 89 | } | 145 | } |
diff --git a/led_test.h b/led_test.h index 03737a717..e14b25e79 100644 --- a/led_test.h +++ b/led_test.h | |||
| @@ -30,6 +30,8 @@ SOFTWARE. | |||
| 30 | bool keyframe_fade_in_all_leds(keyframe_animation_t* animation, visualizer_state_t* state); | 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); | 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); | 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); | ||
| 33 | 35 | ||
| 34 | extern keyframe_animation_t led_test_animation; | 36 | extern keyframe_animation_t led_test_animation; |
| 35 | 37 | ||
