avr/flame/main.c

102 lines
2.7 KiB
C
Raw Permalink Normal View History

2022-05-07 23:49:54 +00:00
#include <avr/io.h>
#include <util/delay.h>
#include <base.h>
2022-05-08 00:16:20 +00:00
#include <sk9822.h>
#include <rng.h>
#include <animation.h>
2022-05-07 23:49:54 +00:00
2022-05-11 23:25:19 +00:00
#define FPS 60
#define FRAME_DELAY_MS 1000 / FPS
2022-05-07 23:49:54 +00:00
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;
2022-05-07 23:49:54 +00:00
if (new_value > max) {
return max;
} else if (new_value < min) {
return min;
} else {
return new_value;
}
}
2022-05-11 23:25:19 +00:00
typedef enum {
normal,
creepy,
} color_scheme_e;
typedef struct {
rgb_t lamp;
color_scheme_e color_scheme;
2022-07-18 03:19:13 +00:00
time_line_t red_line;
time_line_t green_line;
time_line_t blue_line;
2022-05-11 23:25:19 +00:00
uint8_t frame;
uint8_t duration;
} animation_t;
2022-07-13 02:42:42 +00:00
animation_t animation_new(void) {
2022-05-11 23:25:19 +00:00
return (animation_t){
.lamp = (rgb_t){ .brightness = 1, .r = 200, .g = 30, .b = 0 },
2022-05-11 23:25:19 +00:00
.color_scheme = normal,
2022-07-18 03:19:13 +00:00
.red_line = time_line_new(0, 0, 0, 0),
.green_line = time_line_new(0, 0, 0, 0),
.blue_line = time_line_new(0, 0, 0, 0),
2022-05-11 23:25:19 +00:00
.frame = 0,
.duration = 0,
};
}
void animation_flicker(animation_t *animation, rng_t *rng) {
uint8_t rdest;
uint8_t gdest;
switch (animation->color_scheme) {
case normal:
rdest = random_step(animation->lamp.r, 180, 255, rng);
gdest = random_step(animation->lamp.g, 20, 50, rng);
2022-07-18 03:19:13 +00:00
animation->red_line = time_line_new(0, animation->lamp.r, 30, rdest);
animation->green_line = time_line_new(0, animation->lamp.g, 30, gdest);
animation->duration = 30;
2022-05-11 23:25:19 +00:00
break;
case creepy:
break;
}
}
void animation_step(animation_t *animation, rng_t *rng) {
if (animation->duration == 0) {
animation_flicker(animation, rng);
} else {
if (animation->frame == animation->duration) {
animation->frame = 0;
animation->duration = 0;
} else {
animation->frame++;
2022-07-18 03:19:13 +00:00
animation->lamp.r = time_line_next(&animation->red_line);
animation->lamp.g = time_line_next(&animation->green_line);
animation->lamp.b = time_line_next(&animation->blue_line);
2022-05-11 23:25:19 +00:00
}
}
}
2022-05-07 23:49:54 +00:00
int main (void) {
DDRB = _BV(2) | _BV(1) | _BV(0);
PORTB = 0;
_delay_ms(50);
2022-07-13 02:42:42 +00:00
dio_t data_pin = { .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 0 };
dio_t clock_pin = { .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 2 };
2022-05-07 23:49:54 +00:00
rng_t rng = rng_new(0);
2022-05-11 23:25:19 +00:00
animation_t animation = animation_new();
2022-05-07 23:49:54 +00:00
while (1) {
2022-05-11 23:25:19 +00:00
animation_step(&animation, &rng);
send_pixels(data_pin, clock_pin, &animation.lamp, 1);
_delay_ms(FRAME_DELAY_MS);
2022-05-07 23:49:54 +00:00
}
}