Compare commits

...

5 Commits

5 changed files with 142 additions and 70 deletions

View File

@ -2,11 +2,11 @@
"nodes": { "nodes": {
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1649944829, "lastModified": 1659446231,
"narHash": "sha256-wjOgLfjCdyoRamMOrVJceeDJk4LvJsQOxBoT3k16/7Q=", "narHash": "sha256-hekabNdTdgR/iLsgce5TGWmfIDZ86qjPhxDg/8TlzhE=",
"owner": "NixOS", "owner": "NixOS",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "2f06b87f64bc06229e05045853e0876666e1b023", "rev": "eabc38219184cc3e04a974fe31857d8e0eac098d",
"type": "github" "type": "github"
}, },
"original": { "original": {

View File

@ -5,6 +5,13 @@
#include <stdbool.h> #include <stdbool.h>
#include <string.h> #include <string.h>
typedef enum {
lme_off,
lme_normal,
lme_spooky,
lme_eerie,
} lantern_msg_e;
dio_t buttons[4] = { dio_t buttons[4] = {
{ .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 5 }, { .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 5 },
{ .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 6 }, { .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 6 },
@ -30,6 +37,10 @@ int main(void) {
EIMSK = 1 << INT2; EIMSK = 1 << INT2;
EICRA |= 1 << ISC21 | 1 << ISC20; EICRA |= 1 << ISC21 | 1 << ISC20;
dio_t int2 = { .ddr = &DDRD, .port = &PORTD, .pin = &PIND, .addr = 2 };
dio_set_direction(&int2, LINE_IN);
dio_set(&int2, false);
rfm_t radio = (rfm_t){ rfm_t radio = (rfm_t){
.spi = { .spi = {
.clock = { .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 1 }, .clock = { .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 1 },
@ -50,22 +61,24 @@ int main(void) {
sleep_mode(); sleep_mode();
cli(); cli();
char msg[60]; uint8_t msg;
if (status_flags.button_press[0]) { if (status_flags.button_press[0]) {
status_flags.button_press[0] = false; status_flags.button_press[0] = false;
strcpy(msg, "normal"); msg = lme_normal;
} else if (status_flags.button_press[1]) { } else if (status_flags.button_press[1]) {
status_flags.button_press[1] = false; status_flags.button_press[1] = false;
strcpy(msg, "spooky"); msg = lme_spooky;
} else if (status_flags.button_press[2]) { } else if (status_flags.button_press[2]) {
status_flags.button_press[2] = false; status_flags.button_press[2] = false;
strcpy(msg, "eerie"); msg = lme_eerie;
} else if (status_flags.button_press[3]) { } else if (status_flags.button_press[3]) {
status_flags.button_press[3] = false; status_flags.button_press[3] = false;
strcpy(msg, "flash"); msg = lme_off;
} else {
continue;
} }
rfm_transmit(&radio, (uint8_t *)msg, strlen(msg) + 1); rfm_transmit(&radio, &msg, 1);
interrupt_flags_t flags = rfm_interrupts(&radio); interrupt_flags_t flags = rfm_interrupts(&radio);
while(!flags.packet_sent) { while(!flags.packet_sent) {

View File

@ -33,7 +33,7 @@ uint8_t random_step(uint8_t value, uint8_t min, uint8_t max) {
animation_t animation_new(void) { animation_t animation_new(void) {
return (animation_t) { return (animation_t) {
.color = { .brightness = 7, .r = 0, .g = 0, .b = 0 }, .color = { .brightness = 16, .r = 0, .g = 0, .b = 0 },
.red_line = time_line_new(0, 0, 0, 0), .red_line = time_line_new(0, 0, 0, 0),
.green_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), .blue_line = time_line_new(0, 0, 0, 0),
@ -71,7 +71,7 @@ lantern_t lantern_new(sk9822_t lights) {
sk9822_init(&lights); sk9822_init(&lights);
return (lantern_t){ return (lantern_t){
.lights = lights, .lights = lights,
.color_scheme = normal, .color_scheme = cse_off,
.animations = { .animations = {
animation_new(), animation_new(),
animation_new(), animation_new(),
@ -86,6 +86,15 @@ lantern_t lantern_new(sk9822_t lights) {
}; };
} }
void lantern_start_off(lantern_t *self, int light_idx) {
animation_begin(
&self->animations[light_idx],
0,
0,
0,
1);
}
void lantern_start_normal(lantern_t *self, int light_idx) { void lantern_start_normal(lantern_t *self, int light_idx) {
switch (light_idx) { switch (light_idx) {
case 0: case 0:
@ -157,19 +166,19 @@ void lantern_start_spooky(lantern_t *self, int light_idx) {
animation_begin( animation_begin(
&self->animations[0], &self->animations[0],
0, 0,
random_step(self->animations[0].color.g, 80, 120), random_step(self->animations[0].color.g, 40, 140),
0, 0,
5); 5);
animation_begin( animation_begin(
&self->animations[3], &self->animations[3],
0, 0,
random_step(self->animations[3].color.g, 80, 120), random_step(self->animations[3].color.g, 40, 140),
0, 0,
5); 5);
animation_begin( animation_begin(
&self->animations[6], &self->animations[6],
0, 0,
random_step(self->animations[6].color.g, 80, 120), random_step(self->animations[6].color.g, 40, 140),
0, 0,
5); 5);
break; break;
@ -177,19 +186,19 @@ void lantern_start_spooky(lantern_t *self, int light_idx) {
animation_begin( animation_begin(
&self->animations[1], &self->animations[1],
0, 0,
random_step(self->animations[1].color.g, 60, 100), random_step(self->animations[1].color.g, 20, 80),
0, 0,
5); 5);
animation_begin( animation_begin(
&self->animations[4], &self->animations[4],
0, 0,
random_step(self->animations[4].color.g, 60, 100), random_step(self->animations[4].color.g, 20, 80),
0, 0,
5); 5);
animation_begin( animation_begin(
&self->animations[7], &self->animations[7],
0, 0,
random_step(self->animations[7].color.g, 60, 100), random_step(self->animations[7].color.g, 20, 80),
0, 0,
5); 5);
break; break;
@ -197,19 +206,19 @@ void lantern_start_spooky(lantern_t *self, int light_idx) {
animation_begin( animation_begin(
&self->animations[2], &self->animations[2],
0, 0,
random_step(self->animations[2].color.g, 20, 40), random_step(self->animations[2].color.g, 10, 40),
0, 0,
5); 5);
animation_begin( animation_begin(
&self->animations[5], &self->animations[5],
0, 0,
random_step(self->animations[5].color.g, 20, 40), random_step(self->animations[5].color.g, 10, 40),
0, 0,
5); 5);
animation_begin( animation_begin(
&self->animations[8], &self->animations[8],
0, 0,
random_step(self->animations[8].color.g, 20, 40), random_step(self->animations[8].color.g, 10, 40),
0, 0,
5); 5);
break; break;
@ -289,16 +298,19 @@ void lantern_step(lantern_t *self, rgb_t colors[LIGHT_COUNT]) {
for (int i = 0; i < LIGHT_COUNT; i++) { for (int i = 0; i < LIGHT_COUNT; i++) {
if (!animation_running(&self->animations[i])) { if (!animation_running(&self->animations[i])) {
switch (self->color_scheme) { switch (self->color_scheme) {
case normal: case cse_off:
lantern_start_off(self, i);
break;
case cse_normal:
lantern_start_normal(self, i); lantern_start_normal(self, i);
break; break;
case spooky: case cse_spooky:
lantern_start_spooky(self, i); lantern_start_spooky(self, i);
break; break;
case eerie: case cse_eerie:
lantern_start_eerie(self, i); lantern_start_eerie(self, i);
break; break;
case flash: case cse_flash:
break; break;
} }
} }

View File

@ -31,10 +31,11 @@ typedef struct {
} animation_t; } animation_t;
typedef enum { typedef enum {
normal, cse_off,
spooky, cse_normal,
eerie, cse_spooky,
flash cse_eerie,
cse_flash
} color_scheme_e; } color_scheme_e;
typedef struct { typedef struct {

View File

@ -25,22 +25,33 @@ You should have received a copy of the GNU General Public License along with thi
#define FPS 15 #define FPS 15
#define TICKS_PER_SECOND 7812 #define TICKS_PER_SECOND 7812
#define FPS_TIMEOUT TICKS_PER_SECOND / FPS #define FPS_TIMEOUT TICKS_PER_SECOND / FPS
#define FPS_MS 1000 / FPS
#define PULSE_OFF_COUNT FPS * 5 #define PULSE_OFF_COUNT FPS * 5
#define PULSE_ON_COUNT 5 #define PULSE_ON_COUNT 5
typedef enum {
lme_off,
lme_normal,
lme_spooky,
lme_eerie,
} lantern_msg_e;
typedef struct { typedef struct {
bool frame_timeout; bool frame_timeout;
} status_flags_t; } status_flags_t;
status_flags_t status_flags; status_flags_t status_flags;
/*
ISR(TIMER1_COMPA_vect) { ISR(TIMER1_COMPA_vect) {
status_flags.frame_timeout = true; status_flags.frame_timeout = true;
} }
*/
ISR(INT6_vect) { ISR(INT6_vect) {
} }
/*
void setup_fps_timer(void) { void setup_fps_timer(void) {
// WGM = 0100: CTC with OCR1nA top // WGM = 0100: CTC with OCR1nA top
// CS = 101: clock / 1024 // CS = 101: clock / 1024
@ -54,6 +65,34 @@ void setup_fps_timer(void) {
// Enable compare on A match interrupt // Enable compare on A match interrupt
TIMSK1 |= _BV(1); TIMSK1 |= _BV(1);
} }
*/
void startup_animation(sk9822_t *lights, rgb_t *colors) {
for (int i = 0; i < LIGHT_COUNT; i++) {
colors[i].brightness = 1;
}
for (int i = 0; i < LIGHT_COUNT; i++) {
colors[i].b = 0;
colors[i].r = 128;
sk9822_send(lights, colors, LIGHT_COUNT);
_delay_ms(50);
}
for (int i = 0; i < LIGHT_COUNT; i++) {
colors[i].r = 0;
colors[i].g = 128;
sk9822_send(lights, colors, LIGHT_COUNT);
_delay_ms(100);
}
for (int i = 0; i < LIGHT_COUNT; i++) {
colors[i].g = 0;
colors[i].b = 128;
sk9822_send(lights, colors, LIGHT_COUNT);
_delay_ms(100);
}
}
int main(void) { int main(void) {
EIMSK = 1 << INT6 | 1 << INT2; EIMSK = 1 << INT6 | 1 << INT2;
@ -86,23 +125,29 @@ int main(void) {
.data_pin = { .ddr = &DDRD, .port = &PORTD, .pin = &PIND, .addr = 6 } .data_pin = { .ddr = &DDRD, .port = &PORTD, .pin = &PIND, .addr = 6 }
}; };
rgb_t colors[LIGHT_COUNT];
rgb_t off[LIGHT_COUNT] = {
{ .brightness = 0, .r = 0, .g = 0, .b = 0 },
{ .brightness = 0, .r = 0, .g = 0, .b = 0 },
{ .brightness = 0, .r = 0, .g = 0, .b = 0 },
{ .brightness = 0, .r = 0, .g = 0, .b = 0 },
{ .brightness = 0, .r = 0, .g = 0, .b = 0 },
{ .brightness = 0, .r = 0, .g = 0, .b = 0 },
{ .brightness = 0, .r = 0, .g = 0, .b = 0 },
{ .brightness = 0, .r = 0, .g = 0, .b = 0 },
{ .brightness = 0, .r = 0, .g = 0, .b = 0 },
};
/*
rgb_t error_indicator[LIGHT_COUNT] = { rgb_t error_indicator[LIGHT_COUNT] = {
{ .brightness = 1, .r = 255, .g = 0, .b = 0 }, { .brightness = 1, .r = 255, .g = 0, .b = 0 },
{ .brightness = 0, .r = 0, .g = 0, .b = 0 }, { .brightness = 0, .r = 0, .g = 0, .b = 0 },
{ .brightness = 0, .r = 0, .g = 0, .b = 0 }, { .brightness = 0, .r = 0, .g = 0, .b = 0 },
{ .brightness = 1, .r = 255, .g = 0, .b = 0 },
{ .brightness = 0, .r = 0, .g = 0, .b = 0 },
{ .brightness = 0, .r = 0, .g = 0, .b = 0 },
{ .brightness = 1, .r = 255, .g = 0, .b = 0 },
{ .brightness = 0, .r = 0, .g = 0, .b = 0 },
{ .brightness = 0, .r = 0, .g = 0, .b = 0 },
};
lantern_t lantern = lantern_new(lights);
lantern_set_mode(&lantern, cse_off);
// setup_fps_timer();
// set_sleep_mode(SLEEP_MODE_IDLE);
rgb_t colors[LIGHT_COUNT] = {
{ .brightness = 0, .r = 0, .g = 0, .b = 0 },
{ .brightness = 0, .r = 0, .g = 0, .b = 0 },
{ .brightness = 0, .r = 0, .g = 0, .b = 0 },
{ .brightness = 0, .r = 0, .g = 0, .b = 0 }, { .brightness = 0, .r = 0, .g = 0, .b = 0 },
{ .brightness = 0, .r = 0, .g = 0, .b = 0 }, { .brightness = 0, .r = 0, .g = 0, .b = 0 },
{ .brightness = 0, .r = 0, .g = 0, .b = 0 }, { .brightness = 0, .r = 0, .g = 0, .b = 0 },
@ -110,57 +155,58 @@ int main(void) {
{ .brightness = 0, .r = 0, .g = 0, .b = 0 }, { .brightness = 0, .r = 0, .g = 0, .b = 0 },
{ .brightness = 0, .r = 0, .g = 0, .b = 0 }, { .brightness = 0, .r = 0, .g = 0, .b = 0 },
}; };
*/ _delay_ms(1000);
startup_animation(&lights, (rgb_t *)&colors);
sk9822_send(&lights, off, LIGHT_COUNT);
_delay_ms(500);
lantern_t lantern = lantern_new(lights);
lantern_set_mode(&lantern, normal);
lantern_step(&lantern, colors);
sk9822_send(&lights, colors, LIGHT_COUNT);
setup_fps_timer();
set_sleep_mode(SLEEP_MODE_IDLE);
while(1) { while(1) {
/*
if (status_flags.frame_timeout) { if (status_flags.frame_timeout) {
status_flags.frame_timeout = false; status_flags.frame_timeout = false;
lantern_step(&lantern, colors); lantern_step(&lantern, colors);
sk9822_send(&lights, colors, LIGHT_COUNT); sk9822_send(&lights, colors, LIGHT_COUNT);
} }
*/
lantern_step(&lantern, colors);
sk9822_send(&lights, colors, LIGHT_COUNT);
interrupt_flags_t flags = rfm_interrupts(&radio); interrupt_flags_t flags = rfm_interrupts(&radio);
while (flags.fifo_not_empty) { while (flags.fifo_not_empty) {
/* uint8_t msg[60];
sk9822_send(&lights, off, LIGHT_COUNT);
_delay_ms(100);
*/
char msg[60] = "";
uint8_t length; uint8_t length;
rfm_receive(&radio, (uint8_t *)msg, &length); rfm_receive(&radio, (uint8_t *)msg, &length);
if (length == strlen("normal") + 1 && !strncmp(msg, "normal", length)) { if (length == 1) {
lantern_set_mode(&lantern, normal); switch (msg[0]) {
} else if (length == strlen("spooky") + 1 && !strncmp(msg, "spooky", length)) { case lme_off:
lantern_set_mode(&lantern, spooky); lantern_set_mode(&lantern, cse_off);
} else if (length == strlen("eerie") + 1 && !strncmp(msg, "eerie", length)) { break;
lantern_set_mode(&lantern, eerie); case lme_normal:
/* lantern_set_mode(&lantern, cse_normal);
} else { break;
sk9822_send(&lights, error_indicator, LIGHT_COUNT); case lme_spooky:
_delay_ms(500); lantern_set_mode(&lantern, cse_spooky);
*/ break;
case lme_eerie:
lantern_set_mode(&lantern, cse_eerie);
break;
default:
break;
}
} }
flags = rfm_interrupts(&radio); flags = rfm_interrupts(&radio);
} }
_delay_ms(FPS_MS);
/*
sei(); sei();
sleep_mode(); sleep_mode();
cli(); cli();
*/
} }
sk9822_send(&lights, error_indicator, LIGHT_COUNT);
_delay_ms(1000);
return 0; return 0;
} }