2022-07-13 04:13:05 +00:00
|
|
|
/*
|
|
|
|
Copyright 2022, Savanni D'Gerinel <savanni@luminescent-dreams.com>
|
|
|
|
|
|
|
|
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 <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2022-07-19 04:32:08 +00:00
|
|
|
#include <avr/interrupt.h>
|
2022-07-14 22:21:40 +00:00
|
|
|
#include <avr/sleep.h>
|
2022-07-19 04:32:08 +00:00
|
|
|
#include <dio.h>
|
|
|
|
#include <display.h>
|
2022-08-06 15:10:04 +00:00
|
|
|
#include "lantern.h"
|
2022-07-19 04:32:08 +00:00
|
|
|
#include <sk9822.h>
|
2022-07-15 23:14:16 +00:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
2022-07-18 04:39:38 +00:00
|
|
|
#include <stdlib.h>
|
2022-07-14 22:21:40 +00:00
|
|
|
|
2022-07-19 02:39:19 +00:00
|
|
|
#define FPS 15
|
2022-07-19 04:32:08 +00:00
|
|
|
#define TICKS_PER_SECOND 7812
|
|
|
|
#define FPS_TIMEOUT TICKS_PER_SECOND / FPS
|
2022-07-19 02:39:19 +00:00
|
|
|
#define PULSE_OFF_COUNT FPS * 5
|
|
|
|
#define PULSE_ON_COUNT 5
|
2022-07-14 22:21:40 +00:00
|
|
|
|
2022-08-06 15:10:04 +00:00
|
|
|
typedef struct {
|
|
|
|
bool frame_timeout;
|
|
|
|
} status_flags_t;
|
|
|
|
|
|
|
|
status_flags_t status_flags;
|
|
|
|
|
2022-07-19 04:32:08 +00:00
|
|
|
ISR(TIMER1_COMPA_vect) {
|
2022-08-06 15:10:04 +00:00
|
|
|
status_flags.frame_timeout = true;
|
|
|
|
}
|
|
|
|
|
2022-07-19 04:32:08 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-07-13 04:13:05 +00:00
|
|
|
int main(void) {
|
2022-08-06 15:10:04 +00:00
|
|
|
EIMSK = 1 << INT2;
|
|
|
|
EICRA |= 1 << ISC21 | 1 << ISC20;
|
|
|
|
|
2022-08-06 18:18:14 +00:00
|
|
|
/*
|
2022-08-06 15:10:04 +00:00
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
status_flags.button_press[i] = false;
|
|
|
|
}
|
2022-08-06 18:18:14 +00:00
|
|
|
*/
|
2022-08-06 15:10:04 +00:00
|
|
|
status_flags.frame_timeout = false;
|
|
|
|
|
2022-07-19 04:32:08 +00:00
|
|
|
// Disable unneeded modules
|
|
|
|
PRR0 = _BV(7) | _BV(5) | _BV(2);
|
|
|
|
PRR1 = _BV(7) | _BV(4) | _BV(3) | _BV(0);
|
|
|
|
|
2022-07-15 23:14:16 +00:00
|
|
|
display_t display = {
|
|
|
|
.reg = {
|
|
|
|
.output = { .ddr = &DDRF, .port = &PORTF, .pin = &PINF, .addr = 7 },
|
|
|
|
.shift_clock = { .ddr = &DDRF, .port = &PORTF, .pin = &PINF, .addr = 6 },
|
|
|
|
.latch_clock = { .ddr = &DDRF, .port = &PORTF, .pin = &PINF, .addr = 5 },
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
display_init(&display);
|
2022-08-05 18:59:58 +00:00
|
|
|
display_write_message(&display, "ready");
|
|
|
|
_delay_ms(1000);
|
2022-07-15 23:14:16 +00:00
|
|
|
|
2022-08-06 18:18:14 +00:00
|
|
|
/*
|
2022-07-14 22:21:40 +00:00
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
dio_set_direction(&buttons[i], LINE_IN);
|
|
|
|
}
|
2022-08-06 18:18:14 +00:00
|
|
|
*/
|
2022-07-14 22:21:40 +00:00
|
|
|
|
|
|
|
sk9822_t lights = {
|
2022-08-06 18:18:14 +00:00
|
|
|
.clock_pin = { .ddr = &DDRC, .port = &PORTC, .pin = &PINC, .addr = 7 },
|
|
|
|
.data_pin = { .ddr = &DDRD, .port = &PORTD, .pin = &PIND, .addr = 6 }
|
2022-07-14 22:21:40 +00:00
|
|
|
};
|
|
|
|
|
2022-07-15 23:14:16 +00:00
|
|
|
rgb_t colors[LIGHT_COUNT];
|
|
|
|
|
|
|
|
lantern_t lantern = lantern_new(lights);
|
2022-07-19 02:58:14 +00:00
|
|
|
lantern_set_mode(&lantern, normal);
|
2022-07-15 23:14:16 +00:00
|
|
|
|
2022-08-05 14:15:09 +00:00
|
|
|
lantern_step(&lantern, colors);
|
2022-07-15 23:14:16 +00:00
|
|
|
sk9822_send(&lights, colors, LIGHT_COUNT);
|
|
|
|
|
2022-07-19 04:32:08 +00:00
|
|
|
setup_fps_timer();
|
|
|
|
|
|
|
|
set_sleep_mode(SLEEP_MODE_IDLE);
|
2022-07-15 23:14:16 +00:00
|
|
|
while(1) {
|
2022-08-06 18:18:14 +00:00
|
|
|
if (status_flags.frame_timeout) {
|
|
|
|
status_flags.frame_timeout = false;
|
|
|
|
lantern_step(&lantern, colors);
|
|
|
|
sk9822_send(&lights, colors, LIGHT_COUNT);
|
|
|
|
}
|
|
|
|
/*
|
2022-08-06 15:10:04 +00:00
|
|
|
if (status_flags.button_press[0]) {
|
|
|
|
status_flags.button_press[0] = false;
|
|
|
|
lantern_set_mode(&lantern, normal);
|
|
|
|
} else if (status_flags.button_press[1]) {
|
|
|
|
status_flags.button_press[1] = false;
|
|
|
|
lantern_set_mode(&lantern, spooky);
|
|
|
|
} else if (status_flags.button_press[2]) {
|
|
|
|
status_flags.button_press[2] = false;
|
|
|
|
lantern_set_mode(&lantern, eerie);
|
2022-08-06 18:18:14 +00:00
|
|
|
*/
|
2022-07-15 23:14:16 +00:00
|
|
|
|
2022-07-19 04:32:08 +00:00
|
|
|
sei();
|
|
|
|
sleep_mode();
|
|
|
|
cli();
|
2022-07-14 22:21:40 +00:00
|
|
|
}
|
|
|
|
|
2022-07-13 04:13:05 +00:00
|
|
|
return 0;
|
|
|
|
}
|