avr/lantern/main.c

87 lines
3.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 <dio.h>
#include <sk9822.h>
#include <avr/sleep.h>
#define FPS 15
#define ANIMATION_DELAY 1000 / 15
uint8_t bound (uint8_t value, uint8_t min, uint8_t max) {
if (value < min) {
return min;
} else if (value > max) {
return max;
} else {
return value;
}
}
void flash_status(dio_t *light) {
dio_set(light, 1);
_delay_ms(100);
dio_set(light, 0);
}
int main(void) {
dio_t status_light = { .ddr = &DDRC, .port = &PORTC, .pin = &PINC, .addr = 7 };
dio_set_direction(&status_light, LINE_OUT);
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[3] = {
{ .brightness = 1, .r = 128, .g = 0, .b = 0, },
{ .brightness = 1, .r = 0, .g = 128, .b = 0, },
{ .brightness = 1, .r = 0, .g = 0, .b = 128, },
};
sk9822_init(&lights);
sk9822_send(&lights, colors, 3);
int flash_recovery = 0;
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
while (1) {
if (flash_recovery == 0) {
uint8_t value = dio_read(&buttons[0]);
if (value != 0) {
flash_status(&status_light);
flash_recovery = 128;
}
} else {
colors[0] = (rgb_t){ .brightness = bound(flash_recovery / 4, 1, 31), .r = 128 + flash_recovery, .g = flash_recovery * 2, .b = flash_recovery * 2 },
colors[1] = (rgb_t){ .brightness = bound(flash_recovery / 4, 1, 31), .r = flash_recovery * 2, .g = 128 + flash_recovery, .b = flash_recovery * 2 },
colors[2] = (rgb_t){ .brightness = bound(flash_recovery / 4, 1, 31), .r = flash_recovery * 2, .g = flash_recovery * 2, .b = 128 + flash_recovery },
sk9822_send(&lights, colors, 3);
flash_recovery -= 2;
}
_delay_ms(ANIMATION_DELAY);
}
return 0;
}