/* Copyright 2022, Savanni D'Gerinel This file is part of Savanni's AVR library. This AVR library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This AVR library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this AVR library. If not, see . */ #include #include #include #include #include "lantern.h" #include #include #include #include #include #include #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 TCCR1A = 0; TCCR1B = _BV(3) | _BV(2) | _BV(0); // Set the top for the counter OCR1AH = FPS_TIMEOUT >> 8; OCR1AL = FPS_TIMEOUT & 0xff; // 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; EICRA |= 1 << ISC21 | 1 << ISC20; EICRB |= 1 << ISC61 | 1 << ISC60; status_flags.frame_timeout = false; // Disable unneeded modules PRR0 = _BV(7) | _BV(5) | _BV(2); PRR1 = _BV(7) | _BV(4) | _BV(3) | _BV(0); rfm_t radio = (rfm_t){ .spi = { .clock = { .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 1 }, .data_out = { .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 2 }, .data_in = { .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 3 }, .chip_select = { .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 4 }, }, .irq = { .ddr = &DDRE, .port = &PORTE, .pin = &PINE, .addr = 6 }, .reset = { .ddr = &DDRD, .port = &PORTD, .pin = &PIND, .addr = 4 }, }; rfm_error_e error; rfm_init(&radio, (uint8_t [4]){ 0xde, 0xca, 0xfb, 0xad }, 4, &error); rfm_listen(&radio); sk9822_t lights = { .clock_pin = { .ddr = &DDRC, .port = &PORTC, .pin = &PINC, .addr = 7 }, .data_pin = { .ddr = &DDRD, .port = &PORTD, .pin = &PIND, .addr = 6 } }; 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, normal); // 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); 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) { uint8_t msg[60]; uint8_t length; rfm_receive(&radio, (uint8_t *)msg, &length); 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; }