diff --git a/animation/animation.c b/animation/animation.c index 70c177f..60a9c3e 100644 --- a/animation/animation.c +++ b/animation/animation.c @@ -1,6 +1,17 @@ #include "animation.h" #include +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) { int dt = t1 - t0; 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; } - 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) { @@ -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; return self->y; } diff --git a/animation/animation.h b/animation/animation.h index e562e8f..0bb3385 100644 --- a/animation/animation.h +++ b/animation/animation.h @@ -12,6 +12,8 @@ typedef struct { int inc; int dt; int dy; + int y0; + int y1; } time_line_t; // Construct a new line object. x1 must be greater than x0, and dy must be less than dx. diff --git a/lantern/main.c b/lantern/main.c index 2cad31e..072e06e 100644 --- a/lantern/main.c +++ b/lantern/main.c @@ -20,8 +20,8 @@ You should have received a copy of the GNU General Public License along with thi #include #define FPS 15 -// #define FRAME_DELAY_MS 1000 / FPS -#define FRAME_DELAY_MS 1000 +#define FRAME_DELAY_MS 1000 / FPS +// #define FRAME_DELAY_MS 1000 #define LIGHT_COUNT 3 typedef enum { @@ -32,6 +32,16 @@ typedef enum { flash } 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) { int8_t step = (rng_sample(rng) % 20) - 10; int new_value = value + step; @@ -81,9 +91,9 @@ void animation_step(animation_t *self) { self->duration = 0; } else { self->frame++; - self->color.r = time_line_next(&self->red_line); - self->color.g = time_line_next(&self->green_line); - self->color.b = time_line_next(&self->blue_line); + self->color.r = bound_uint8(time_line_next(&self->red_line), 0, 255); + self->color.g = bound_uint8(time_line_next(&self->green_line), 0, 255); + 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, 0, 0, - 120); + 15); animation_begin( &self->animations[1], 0, 255, 0, - 120); + 15); animation_begin( &self->animations[2], 0, 0, 255, - 120); + 15); } void lantern_start_normal(lantern_t *self, rng_t *rng) { @@ -239,7 +249,7 @@ int main(void) { rgb_t colors[LIGHT_COUNT]; lantern_t lantern = lantern_new(lights); - lantern_set_mode(&lantern, test); + lantern_set_mode(&lantern, normal); rng_t rng = rng_new(15);