From 196278a2fe1b847580ec4ece2a87d1afe37a2bd7 Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Sat, 29 Oct 2022 19:12:54 -0400 Subject: [PATCH] Remove the strcpy usage --- lantern-controller/main.c | 22 +++++--- lantern/lantern.c | 2 +- lantern/main.c | 112 +++++++++++++++++++++++++++----------- 3 files changed, 94 insertions(+), 42 deletions(-) diff --git a/lantern-controller/main.c b/lantern-controller/main.c index e91f38c..2c3fb25 100644 --- a/lantern-controller/main.c +++ b/lantern-controller/main.c @@ -5,6 +5,12 @@ #include #include +typedef enum { + 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 }, @@ -32,6 +38,7 @@ int main(void) { 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 = { @@ -53,22 +60,21 @@ 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"); - } else if (status_flags.button_press[3]) { - status_flags.button_press[3] = false; - strcpy(msg, "flash"); + msg = lme_eerie; + } 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) { diff --git a/lantern/lantern.c b/lantern/lantern.c index c5b39e3..171af1c 100644 --- a/lantern/lantern.c +++ b/lantern/lantern.c @@ -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), diff --git a/lantern/main.c b/lantern/main.c index 3dd0f71..c037547 100644 --- a/lantern/main.c +++ b/lantern/main.c @@ -25,22 +25,32 @@ 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_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 +64,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,18 +124,6 @@ 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 }, @@ -110,53 +136,73 @@ int main(void) { { .brightness = 0, .r = 0, .g = 0, .b = 0 }, }; - sk9822_send(&lights, off, LIGHT_COUNT); - _delay_ms(1000); - lantern_t lantern = lantern_new(lights); lantern_set_mode(&lantern, normal); - lantern_step(&lantern, colors); - sk9822_send(&lights, colors, LIGHT_COUNT); + // setup_fps_timer(); - 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) { + /* 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_normal: + lantern_set_mode(&lantern, normal); + break; + case lme_spooky: + lantern_set_mode(&lantern, spooky); + break; + case lme_eerie: + lantern_set_mode(&lantern, 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; }