Save power by putting the module to sleep

uart
Savanni D'Gerinel 2022-07-19 00:32:08 -04:00
parent cf21388bd8
commit e19bf772cb
1 changed files with 45 additions and 8 deletions

View File

@ -10,23 +10,34 @@ This AVR library is distributed in the hope that it will be useful, but WITHOUT
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 <dio.h>
#include <sk9822.h>
#include <rng.h>
#include <animation.h>
#include <display.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <dio.h>
#include <display.h>
#include <rng.h>
#include <sk9822.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#define FPS 15
#define FRAME_DELAY_MS 1000 / FPS
// #define FRAME_DELAY_MS 1000
#define TICKS_PER_SECOND 7812
#define FPS_TIMEOUT TICKS_PER_SECOND / FPS
#define LIGHT_COUNT 3
#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 enum {
normal,
spooky,
@ -264,10 +275,27 @@ typedef struct {
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_t status_light = { .ddr = &DDRC, .port = &PORTC, .pin = &PINC, .addr = 7 };
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);
@ -306,12 +334,20 @@ int main(void) {
lantern_step(&lantern, &display, &rng, colors);
sk9822_send(&lights, colors, LIGHT_COUNT);
power_pulse_t timer = { .on = false, .timeout = PULSE_OFF_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, &display, &rng, colors);
sk9822_send(&lights, colors, LIGHT_COUNT);
sei();
sleep_mode();
cli();
/*
if (timer.timeout > 0) {
timer.timeout--;
} else {
@ -327,6 +363,7 @@ int main(void) {
}
_delay_ms(FRAME_DELAY_MS);
*/
}
return 0;