day08/ex04/src/main.c
2026-04-24 16:46:10 +02:00

70 lines
1.5 KiB
C

#include <avr/eeprom.h>
#include <avr/io.h>
#include <util/delay.h>
#include "adc.h"
#include "aht20.h"
#include "apa102.h"
#include "eeprom.h"
#include "i2c.h"
#include "mystd.h"
#include "rgb.h"
#include "spi.h"
#include "uart.h"
#include "utils.h"
#define tabsize(tab) (sizeof(tab) / sizeof(tab[0]))
static inline void set_col(rgb* rgb, uint8_t i, uint8_t val) {
if (i == 0)
rgb->r = val;
if (i == 1)
rgb->g = val;
if (i == 2)
rgb->b = val;
}
int main(void) {
init_rgb();
uart_init();
spi_init();
adc_init(ADC_AVCC, ADC_PRESCALER_128);
rgb colors[3] = {
{0x0, 0x00, 0x00}, //
{0x0, 0x00, 0x00}, //
{0x0, 0x00, 0x00}, //
};
uint8_t selected_led = 0;
uint8_t selected_col = 0;
uint8_t brightness[tabsize(colors)] = {};
for (uint8_t i = 0; i < tabsize(colors); i++)
brightness[i] = 31;
rgb display_col = {0, 0, 0};
apa102_set_color((rgb){0, 0, 0}, 31);
uint8_t prev = 0;
while (true) {
uint8_t data = adc_read_pin(ADC_ADC0);
display_col = (rgb){0, 0, 0};
set_col(&display_col, selected_col, data);
set_rgb(display_col.r, display_col.g, display_col.b);
uint8_t cur = PIND & (BV(PD2) | BV(PD4));
if ((prev & BV(PD2)) && !(cur & BV(PD2))) {
set_col(&colors[selected_led], selected_col, data);
selected_col += 1;
selected_col %= 3;
}
if ((prev & BV(PD4)) && !(cur & BV(PD4))) {
selected_led += 1;
selected_led %= 3;
}
prev = cur;
apa102_set_colors(colors, brightness, tabsize(colors));
_delay_ms(20);
}
}