Further refine the flame animation
This commit is contained in:
parent
74a1f9f945
commit
307617b1b2
|
@ -39,7 +39,7 @@ int time_line_next(time_line_t *self) {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
while (error < 0) {
|
while (error < 0) {
|
||||||
self->y -= self->inc;
|
self->y += self->inc;
|
||||||
error += 2 * self->dt;
|
error += 2 * self->dt;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,14 @@
|
||||||
#ifndef __ANIMATION_H__
|
#ifndef __ANIMATION_H__
|
||||||
#define __ANIMATION_H__
|
#define __ANIMATION_H__
|
||||||
|
|
||||||
|
#ifdef __AVR__
|
||||||
|
#include <avr/io.h>
|
||||||
|
#else
|
||||||
|
|
||||||
|
typedef int uint8_t;
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
// line_t provides a simplified implementation of Bresenham's line drawing
|
// line_t provides a simplified implementation of Bresenham's line drawing
|
||||||
// technique. At this time, it assumes a transition of x = 0 -> 60, and a dy
|
// technique. At this time, it assumes a transition of x = 0 -> 60, and a dy
|
||||||
// between -59 and 59. I am not sure I understand Bresenham's algorithm yet,
|
// between -59 and 59. I am not sure I understand Bresenham's algorithm yet,
|
||||||
|
@ -23,6 +31,6 @@ time_line_t time_line_new(int, int, int, int);
|
||||||
int time_line_next(time_line_t *);
|
int time_line_next(time_line_t *);
|
||||||
|
|
||||||
// Diagnostic printout of the current state of line_t.
|
// Diagnostic printout of the current state of line_t.
|
||||||
// void line_print(line_t *line);
|
void time_line_print(time_line_t *);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -18,7 +18,9 @@ You should have received a copy of the GNU General Public License along with thi
|
||||||
#include <avr/sleep.h>
|
#include <avr/sleep.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#define ANIMATION_FRAMES 5
|
||||||
#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
|
||||||
|
@ -42,16 +44,18 @@ uint8_t bound_uint8(int value, uint8_t min, uint8_t max) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t random_step(uint8_t value, uint8_t min, uint8_t max, rng_t *rng) {
|
uint8_t random_step(display_t *display, 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) % 40) - 20;
|
||||||
|
int8_t step = (rand() % 40) - 20;
|
||||||
int new_value = value + step;
|
int new_value = value + step;
|
||||||
if (new_value > max) {
|
|
||||||
return max;
|
// char msg[20];
|
||||||
} else if (new_value < min) {
|
// snprintf(msg, 20, "[%x] %x -> %x", step, value, new_value);
|
||||||
return min;
|
// display_clear(display);
|
||||||
} else {
|
// display_write_message(display, msg);
|
||||||
return new_value;
|
// _delay_ms(1000);
|
||||||
}
|
|
||||||
|
return bound_uint8(new_value, min, max);
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
@ -108,6 +112,7 @@ typedef struct {
|
||||||
animation_t animations[LIGHT_COUNT];
|
animation_t animations[LIGHT_COUNT];
|
||||||
} lantern_t;
|
} lantern_t;
|
||||||
|
|
||||||
|
void display_lantern(display_t *display, lantern_t *lantern);
|
||||||
|
|
||||||
lantern_t lantern_new(sk9822_t lights) {
|
lantern_t lantern_new(sk9822_t lights) {
|
||||||
sk9822_init(&lights);
|
sk9822_init(&lights);
|
||||||
|
@ -128,38 +133,38 @@ void lantern_start_test(lantern_t *self) {
|
||||||
255,
|
255,
|
||||||
0,
|
0,
|
||||||
0,
|
0,
|
||||||
15);
|
ANIMATION_FRAMES);
|
||||||
animation_begin(
|
animation_begin(
|
||||||
&self->animations[1],
|
&self->animations[1],
|
||||||
0,
|
0,
|
||||||
255,
|
255,
|
||||||
0,
|
0,
|
||||||
15);
|
ANIMATION_FRAMES);
|
||||||
animation_begin(
|
animation_begin(
|
||||||
&self->animations[2],
|
&self->animations[2],
|
||||||
0,
|
0,
|
||||||
0,
|
0,
|
||||||
255,
|
255,
|
||||||
15);
|
ANIMATION_FRAMES);
|
||||||
}
|
}
|
||||||
|
|
||||||
void lantern_start_normal(lantern_t *self, rng_t *rng) {
|
void lantern_start_normal(display_t *display, lantern_t *self, rng_t *rng) {
|
||||||
animation_begin(
|
animation_begin(
|
||||||
&self->animations[0],
|
&self->animations[0],
|
||||||
random_step(self->animations[0].color.r, 180, 255, rng),
|
random_step(display, self->animations[0].color.r, 180, 255, rng),
|
||||||
random_step(self->animations[0].color.g, 20, 50, rng),
|
random_step(display, self->animations[0].color.g, 20, 50, rng),
|
||||||
0,
|
0,
|
||||||
FPS);
|
FPS);
|
||||||
animation_begin(
|
animation_begin(
|
||||||
&self->animations[1],
|
&self->animations[1],
|
||||||
random_step(self->animations[1].color.r, 160, 230, rng),
|
random_step(display, self->animations[1].color.r, 160, 230, rng),
|
||||||
random_step(self->animations[1].color.g, 10, 30, rng),
|
random_step(display, self->animations[1].color.g, 10, 30, rng),
|
||||||
0,
|
0,
|
||||||
FPS);
|
FPS);
|
||||||
animation_begin(
|
animation_begin(
|
||||||
&self->animations[2],
|
&self->animations[2],
|
||||||
random_step(self->animations[2].color.r, 140, 170, rng),
|
random_step(display, self->animations[2].color.r, 140, 170, rng),
|
||||||
random_step(self->animations[2].color.g, 0, 10, rng),
|
random_step(display, self->animations[2].color.g, 0, 10, rng),
|
||||||
0,
|
0,
|
||||||
FPS);
|
FPS);
|
||||||
}
|
}
|
||||||
|
@ -168,14 +173,15 @@ void lantern_set_mode(lantern_t *self, color_scheme_e scheme) {
|
||||||
self->color_scheme = scheme;
|
self->color_scheme = scheme;
|
||||||
}
|
}
|
||||||
|
|
||||||
void lantern_step(lantern_t *self, rng_t *rng, rgb_t colors[LIGHT_COUNT]) {
|
void lantern_step(lantern_t *self, display_t *display, rng_t *rng, rgb_t colors[LIGHT_COUNT]) {
|
||||||
if (!animation_running(&self->animations[0])) {
|
if (!animation_running(&self->animations[0])) {
|
||||||
|
// display_lantern(display, self);
|
||||||
switch (self->color_scheme) {
|
switch (self->color_scheme) {
|
||||||
case test:
|
case test:
|
||||||
lantern_start_test(self);
|
lantern_start_test(self);
|
||||||
break;
|
break;
|
||||||
case normal:
|
case normal:
|
||||||
lantern_start_normal(self, rng);
|
lantern_start_normal(display, self, rng);
|
||||||
break;
|
break;
|
||||||
case creepy:
|
case creepy:
|
||||||
break;
|
break;
|
||||||
|
@ -253,21 +259,17 @@ int main(void) {
|
||||||
|
|
||||||
rng_t rng = rng_new(15);
|
rng_t rng = rng_new(15);
|
||||||
|
|
||||||
display_clear(&display);
|
// display_clear(&display);
|
||||||
display_write_message(&display, "ready");
|
// display_write_message(&display, "ready");
|
||||||
|
|
||||||
_delay_ms(5000);
|
// _delay_ms(5000);
|
||||||
|
|
||||||
display_lantern(&display, &lantern);
|
lantern_step(&lantern, &display, &rng, colors);
|
||||||
_delay_ms(1000);
|
|
||||||
|
|
||||||
lantern_step(&lantern, &rng, colors);
|
|
||||||
sk9822_send(&lights, colors, LIGHT_COUNT);
|
sk9822_send(&lights, colors, LIGHT_COUNT);
|
||||||
|
|
||||||
while(1) {
|
while(1) {
|
||||||
lantern_step(&lantern, &rng, colors);
|
lantern_step(&lantern, &display, &rng, colors);
|
||||||
sk9822_send(&lights, colors, LIGHT_COUNT);
|
sk9822_send(&lights, colors, LIGHT_COUNT);
|
||||||
display_lantern(&display, &lantern);
|
|
||||||
_delay_ms(FRAME_DELAY_MS);
|
_delay_ms(FRAME_DELAY_MS);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue