avr/lantern/main.c

137 lines
4.0 KiB
C

/*
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/>.
*/
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <dio.h>
#include <display.h>
#include <sk9822.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include "lantern.h"
#define FPS 15
#define TICKS_PER_SECOND 7812
#define FPS_TIMEOUT TICKS_PER_SECOND / FPS
#define PULSE_OFF_COUNT FPS * 5
#define PULSE_ON_COUNT 5
uint8_t status_light_value = 0;
dio_t status_light = { .ddr = &DDRC, .port = &PORTC, .pin = &PINC, .addr = 7 };
ISR(TIMER1_COMPA_vect) {
/*
status_light_value = !status_light_value;
dio_set(&status_light, status_light_value);
*/
}
typedef struct {
bool on;
int timeout;
} power_pulse_t;
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);
}
int main(void) {
dio_set_direction(&status_light, LINE_OUT);
// Disable unneeded modules
PRR0 = _BV(7) | _BV(5) | _BV(2);
PRR1 = _BV(7) | _BV(4) | _BV(3) | _BV(0);
dio_t power_pulse = { .ddr = &DDRF, .port = &PORTF, .pin = &PINF, .addr = 0 };
dio_set_direction(&power_pulse, LINE_OUT);
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);
display_write_message(&display, "ready");
_delay_ms(1000);
dio_t buttons[4] = {
{ .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 5 },
{ .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 6 },
{ .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 7 },
{ .ddr = &DDRD, .port = &PORTD, .pin = &PIND, .addr = 6 },
};
for (int i = 0; i < 4; i++) {
dio_set_direction(&buttons[i], LINE_IN);
}
sk9822_t lights = {
.data_pin = { .ddr = &DDRD, .port = &PORTD, .pin = &PIND, .addr = 1 },
.clock_pin = { .ddr = &DDRD, .port = &PORTD, .pin = &PIND, .addr = 0 }
};
rgb_t colors[LIGHT_COUNT];
lantern_t lantern = lantern_new(lights);
lantern_set_mode(&lantern, normal);
lantern_step(&lantern, colors);
sk9822_send(&lights, colors, LIGHT_COUNT);
// power_pulse_t timer = { .on = false, .timeout = PULSE_OFF_COUNT };
setup_fps_timer();
set_sleep_mode(SLEEP_MODE_IDLE);
while(1) {
lantern_step(&lantern, colors);
sk9822_send(&lights, colors, LIGHT_COUNT);
sei();
sleep_mode();
cli();
/*
if (timer.timeout > 0) {
timer.timeout--;
} else {
if (timer.on) {
timer.on = false;
dio_set(&power_pulse, 0);
timer.timeout = PULSE_OFF_COUNT;
} else {
timer.on = true;
dio_set(&power_pulse, 1);
timer.timeout = PULSE_ON_COUNT;
}
}
_delay_ms(FRAME_DELAY_MS);
*/
}
return 0;
}