Compare commits

...

5 Commits

5 changed files with 142 additions and 70 deletions

View File

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

View File

@ -5,6 +5,13 @@
#include <stdbool.h>
#include <string.h>
typedef enum {
lme_off,
lme_normal,
lme_spooky,
lme_eerie,
} lantern_msg_e;
dio_t buttons[4] = {
{ .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 5 },
{ .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 6 },
@ -30,6 +37,10 @@ int main(void) {
EIMSK = 1 << INT2;
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){
.spi = {
.clock = { .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 1 },
@ -50,22 +61,24 @@ int main(void) {
sleep_mode();
cli();
char msg[60];
uint8_t msg;
if (status_flags.button_press[0]) {
status_flags.button_press[0] = false;
strcpy(msg, "normal");
msg = lme_normal;
} else if (status_flags.button_press[1]) {
status_flags.button_press[1] = false;
strcpy(msg, "spooky");
msg = lme_spooky;
} else if (status_flags.button_press[2]) {
status_flags.button_press[2] = false;
strcpy(msg, "eerie");
msg = lme_eerie;
} else if (status_flags.button_press[3]) {
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);
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) {
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),
.green_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);
return (lantern_t){
.lights = lights,
.color_scheme = normal,
.color_scheme = cse_off,
.animations = {
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) {
switch (light_idx) {
case 0:
@ -157,19 +166,19 @@ void lantern_start_spooky(lantern_t *self, int light_idx) {
animation_begin(
&self->animations[0],
0,
random_step(self->animations[0].color.g, 80, 120),
random_step(self->animations[0].color.g, 40, 140),
0,
5);
animation_begin(
&self->animations[3],
0,
random_step(self->animations[3].color.g, 80, 120),
random_step(self->animations[3].color.g, 40, 140),
0,
5);
animation_begin(
&self->animations[6],
0,
random_step(self->animations[6].color.g, 80, 120),
random_step(self->animations[6].color.g, 40, 140),
0,
5);
break;
@ -177,19 +186,19 @@ void lantern_start_spooky(lantern_t *self, int light_idx) {
animation_begin(
&self->animations[1],
0,
random_step(self->animations[1].color.g, 60, 100),
random_step(self->animations[1].color.g, 20, 80),
0,
5);
animation_begin(
&self->animations[4],
0,
random_step(self->animations[4].color.g, 60, 100),
random_step(self->animations[4].color.g, 20, 80),
0,
5);
animation_begin(
&self->animations[7],
0,
random_step(self->animations[7].color.g, 60, 100),
random_step(self->animations[7].color.g, 20, 80),
0,
5);
break;
@ -197,19 +206,19 @@ void lantern_start_spooky(lantern_t *self, int light_idx) {
animation_begin(
&self->animations[2],
0,
random_step(self->animations[2].color.g, 20, 40),
random_step(self->animations[2].color.g, 10, 40),
0,
5);
animation_begin(
&self->animations[5],
0,
random_step(self->animations[5].color.g, 20, 40),
random_step(self->animations[5].color.g, 10, 40),
0,
5);
animation_begin(
&self->animations[8],
0,
random_step(self->animations[8].color.g, 20, 40),
random_step(self->animations[8].color.g, 10, 40),
0,
5);
break;
@ -289,16 +298,19 @@ void lantern_step(lantern_t *self, rgb_t colors[LIGHT_COUNT]) {
for (int i = 0; i < LIGHT_COUNT; i++) {
if (!animation_running(&self->animations[i])) {
switch (self->color_scheme) {
case normal:
case cse_off:
lantern_start_off(self, i);
break;
case cse_normal:
lantern_start_normal(self, i);
break;
case spooky:
case cse_spooky:
lantern_start_spooky(self, i);
break;
case eerie:
case cse_eerie:
lantern_start_eerie(self, i);
break;
case flash:
case cse_flash:
break;
}
}

View File

@ -31,10 +31,11 @@ typedef struct {
} animation_t;
typedef enum {
normal,
spooky,
eerie,
flash
cse_off,
cse_normal,
cse_spooky,
cse_eerie,
cse_flash
} color_scheme_e;
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 TICKS_PER_SECOND 7812
#define FPS_TIMEOUT TICKS_PER_SECOND / FPS
#define FPS_MS 1000 / FPS
#define PULSE_OFF_COUNT FPS * 5
#define PULSE_ON_COUNT 5
typedef enum {
lme_off,
lme_normal,
lme_spooky,
lme_eerie,
} lantern_msg_e;
typedef struct {
bool frame_timeout;
} status_flags_t;
status_flags_t status_flags;
/*
ISR(TIMER1_COMPA_vect) {
status_flags.frame_timeout = true;
}
*/
ISR(INT6_vect) {
}
/*
void setup_fps_timer(void) {
// WGM = 0100: CTC with OCR1nA top
// CS = 101: clock / 1024
@ -54,6 +65,34 @@ void setup_fps_timer(void) {
// Enable compare on A match interrupt
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) {
EIMSK = 1 << INT6 | 1 << INT2;
@ -86,23 +125,29 @@ int main(void) {
.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] = {
{ .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 },
{ .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 },
@ -110,57 +155,58 @@ int main(void) {
{ .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) {
/*
if (status_flags.frame_timeout) {
status_flags.frame_timeout = false;
lantern_step(&lantern, colors);
sk9822_send(&lights, colors, LIGHT_COUNT);
}
*/
lantern_step(&lantern, colors);
sk9822_send(&lights, colors, LIGHT_COUNT);
interrupt_flags_t flags = rfm_interrupts(&radio);
while (flags.fifo_not_empty) {
/*
sk9822_send(&lights, off, LIGHT_COUNT);
_delay_ms(100);
*/
char msg[60] = "";
uint8_t msg[60];
uint8_t length;
rfm_receive(&radio, (uint8_t *)msg, &length);
if (length == strlen("normal") + 1 && !strncmp(msg, "normal", length)) {
lantern_set_mode(&lantern, normal);
} else if (length == strlen("spooky") + 1 && !strncmp(msg, "spooky", length)) {
lantern_set_mode(&lantern, spooky);
} else if (length == strlen("eerie") + 1 && !strncmp(msg, "eerie", length)) {
lantern_set_mode(&lantern, eerie);
/*
} else {
sk9822_send(&lights, error_indicator, LIGHT_COUNT);
_delay_ms(500);
*/
if (length == 1) {
switch (msg[0]) {
case lme_off:
lantern_set_mode(&lantern, cse_off);
break;
case lme_normal:
lantern_set_mode(&lantern, cse_normal);
break;
case lme_spooky:
lantern_set_mode(&lantern, cse_spooky);
break;
case lme_eerie:
lantern_set_mode(&lantern, cse_eerie);
break;
default:
break;
}
}
flags = rfm_interrupts(&radio);
}
_delay_ms(FPS_MS);
/*
sei();
sleep_mode();
cli();
*/
}
sk9822_send(&lights, error_indicator, LIGHT_COUNT);
_delay_ms(1000);
return 0;
}