Finish the basic lantern animation

Add some bounding boxes to keep the animation doing something like the right thing
This commit is contained in:
Savanni D'Gerinel 2022-07-18 00:01:16 -04:00
parent fde5db66f7
commit 74a1f9f945
3 changed files with 39 additions and 10 deletions

View File

@ -1,6 +1,17 @@
#include "animation.h" #include "animation.h"
#include <stdio.h> #include <stdio.h>
int bound(int value, uint8_t min, uint8_t max) {
if (value > max) {
return max;
} else if (value < min) {
return min;
} else {
return value;
}
}
time_line_t time_line_new(int t0, int y0, int t1, int y1) { time_line_t time_line_new(int t0, int y0, int t1, int y1) {
int dt = t1 - t0; int dt = t1 - t0;
int dy = y1 - y0; int dy = y1 - y0;
@ -15,7 +26,7 @@ time_line_t time_line_new(int t0, int y0, int t1, int y1) {
error = de + dt; error = de + dt;
} }
return (time_line_t){ .y = y0, .dt = dt, .de = de, .error = error, .inc = inc, .dy = dy }; return (time_line_t){ .y = y0, .dt = dt, .de = de, .error = error, .inc = inc, .dy = dy, .y0 = y0, .y1 = y1 };
} }
int time_line_next(time_line_t *self) { int time_line_next(time_line_t *self) {
@ -33,6 +44,12 @@ int time_line_next(time_line_t *self) {
} }
} }
if (self->y0 < self->y1) {
self->y = bound(self->y, self->y0, self->y1);
} else {
self->y = bound(self->y, self->y1, self->y0);
}
self->error = error; self->error = error;
return self->y; return self->y;
} }

View File

@ -12,6 +12,8 @@ typedef struct {
int inc; int inc;
int dt; int dt;
int dy; int dy;
int y0;
int y1;
} time_line_t; } time_line_t;
// Construct a new line object. x1 must be greater than x0, and dy must be less than dx. // Construct a new line object. x1 must be greater than x0, and dy must be less than dx.

View File

@ -20,8 +20,8 @@ You should have received a copy of the GNU General Public License along with thi
#include <stdio.h> #include <stdio.h>
#define FPS 15 #define FPS 15
// #define FRAME_DELAY_MS 1000 / FPS #define FRAME_DELAY_MS 1000 / FPS
#define FRAME_DELAY_MS 1000 // #define FRAME_DELAY_MS 1000
#define LIGHT_COUNT 3 #define LIGHT_COUNT 3
typedef enum { typedef enum {
@ -32,6 +32,16 @@ typedef enum {
flash flash
} color_scheme_e; } color_scheme_e;
uint8_t bound_uint8(int value, uint8_t min, uint8_t max) {
if (value > max) {
return max;
} else if (value < min) {
return min;
} else {
return value;
}
}
uint8_t random_step(uint8_t value, uint8_t min, uint8_t max, rng_t *rng) { uint8_t random_step(uint8_t value, uint8_t min, uint8_t max, rng_t *rng) {
int8_t step = (rng_sample(rng) % 20) - 10; int8_t step = (rng_sample(rng) % 20) - 10;
int new_value = value + step; int new_value = value + step;
@ -81,9 +91,9 @@ void animation_step(animation_t *self) {
self->duration = 0; self->duration = 0;
} else { } else {
self->frame++; self->frame++;
self->color.r = time_line_next(&self->red_line); self->color.r = bound_uint8(time_line_next(&self->red_line), 0, 255);
self->color.g = time_line_next(&self->green_line); self->color.g = bound_uint8(time_line_next(&self->green_line), 0, 255);
self->color.b = time_line_next(&self->blue_line); self->color.b = bound_uint8(time_line_next(&self->blue_line), 0, 255);
} }
} }
@ -118,19 +128,19 @@ void lantern_start_test(lantern_t *self) {
255, 255,
0, 0,
0, 0,
120); 15);
animation_begin( animation_begin(
&self->animations[1], &self->animations[1],
0, 0,
255, 255,
0, 0,
120); 15);
animation_begin( animation_begin(
&self->animations[2], &self->animations[2],
0, 0,
0, 0,
255, 255,
120); 15);
} }
void lantern_start_normal(lantern_t *self, rng_t *rng) { void lantern_start_normal(lantern_t *self, rng_t *rng) {
@ -239,7 +249,7 @@ int main(void) {
rgb_t colors[LIGHT_COUNT]; rgb_t colors[LIGHT_COUNT];
lantern_t lantern = lantern_new(lights); lantern_t lantern = lantern_new(lights);
lantern_set_mode(&lantern, test); lantern_set_mode(&lantern, normal);
rng_t rng = rng_new(15); rng_t rng = rng_new(15);