avr/sk9822/main.c

52 lines
1.2 KiB
C
Raw Normal View History

2022-05-07 23:49:54 +00:00
#include <avr/io.h>
#include <util/delay.h>
#include <base.h>
#include "sk9822.h"
uint8_t random_step(uint8_t value, uint8_t min, uint8_t max, rng_t *rng) {
int8_t step = (rng_sample(rng) % 32) - 16;
uint16_t new_value = value + step;
if (new_value > max) {
return max;
} else if (new_value < min) {
return min;
} else {
return new_value;
}
}
void flicker(rgb_t *pixel, rng_t *rng) {
pixel->r = random_step(pixel->r, 180, 255, rng);
pixel->g = random_step(pixel->g, 10, 40, rng);
}
int main (void) {
DDRB = _BV(2) | _BV(1) | _BV(0);
PORTB = 0;
_delay_ms(50);
io_pin_t data_pin = { .port = &PORTB, .pin = 0 };
io_pin_t clock_pin = { .port = &PORTB, .pin = 2 };
rgb_t lamps[2] = { { .brightness = 9, .r = 212, .g = 50, .b = 0 },
{ .brightness = 1, .r = 0, .g = 0, .b = 0 } };
int8_t step_1 = 1;
rng_t rng = rng_new(0);
while (1) {
send_pixels(data_pin, clock_pin, lamps, 2);
flicker(&lamps[0], &rng);
lamps[1].r += step_1;
if (lamps[1].r == 255) {
step_1 = -1;
} else if (lamps[1].r == 0) {
step_1 = 1;
}
_delay_ms(16);
}
}