Remove the strcpy usage

This commit is contained in:
Savanni D'Gerinel 2022-10-29 19:12:54 -04:00
parent 3e68db14bb
commit 196278a2fe
3 changed files with 94 additions and 42 deletions

View File

@ -5,6 +5,12 @@
#include <stdbool.h> #include <stdbool.h>
#include <string.h> #include <string.h>
typedef enum {
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 },
@ -32,6 +38,7 @@ int main(void) {
dio_t int2 = { .ddr = &DDRD, .port = &PORTD, .pin = &PIND, .addr = 2 }; dio_t int2 = { .ddr = &DDRD, .port = &PORTD, .pin = &PIND, .addr = 2 };
dio_set_direction(&int2, LINE_IN); dio_set_direction(&int2, LINE_IN);
dio_set(&int2, false);
rfm_t radio = (rfm_t){ rfm_t radio = (rfm_t){
.spi = { .spi = {
@ -53,22 +60,21 @@ 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 {
status_flags.button_press[3] = false; continue;
strcpy(msg, "flash");
} }
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),

View File

@ -25,22 +25,32 @@ 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_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 +64,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,18 +124,6 @@ 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 },
@ -110,53 +136,73 @@ int main(void) {
{ .brightness = 0, .r = 0, .g = 0, .b = 0 }, { .brightness = 0, .r = 0, .g = 0, .b = 0 },
}; };
sk9822_send(&lights, off, LIGHT_COUNT);
_delay_ms(1000);
lantern_t lantern = lantern_new(lights); lantern_t lantern = lantern_new(lights);
lantern_set_mode(&lantern, normal); lantern_set_mode(&lantern, normal);
lantern_step(&lantern, colors); // setup_fps_timer();
sk9822_send(&lights, colors, LIGHT_COUNT);
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 },
};
_delay_ms(1000);
startup_animation(&lights, (rgb_t *)&colors);
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) {
switch (msg[0]) {
case lme_normal:
lantern_set_mode(&lantern, normal); lantern_set_mode(&lantern, normal);
} else if (length == strlen("spooky") + 1 && !strncmp(msg, "spooky", length)) { break;
case lme_spooky:
lantern_set_mode(&lantern, spooky); lantern_set_mode(&lantern, spooky);
} else if (length == strlen("eerie") + 1 && !strncmp(msg, "eerie", length)) { break;
case lme_eerie:
lantern_set_mode(&lantern, eerie); lantern_set_mode(&lantern, eerie);
} else { break;
sk9822_send(&lights, error_indicator, LIGHT_COUNT); default:
_delay_ms(500); 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;
} }