avr/ws2812/src/main.c

172 lines
3.7 KiB
C

#include <avr/io.h>
#include <util/delay.h>
#include "ws2812.h"
#include "np_common.c"
#include <simavr/avr/avr_mcu_section.h>
AVR_MCU(F_CPU, "attiny85");
const struct avr_mmcu_vcd_trace_t _mytrace[] _MMCU_ = {
{ AVR_MCU_VCD_SYMBOL("DDRB"), .what = (void*)&DDRB, },
{ AVR_MCU_VCD_SYMBOL("PORTB"), .what = (void*)&PORTB, },
};
#define PIXEL_COUNT 7
/*
const uint8_t pixels_0[PIXEL_COUNT * 3] =
{ 255, 0, 255,
255, 255, 0,
255, 255, 255 };
*/
const uint8_t pixels_1[PIXEL_COUNT * 4] =
{ 0, 0, 0, 0,
32, 0, 0, 0,
64, 0, 0, 0,
96, 0, 0, 0,
128, 0, 0, 0,
160, 0, 0, 0,
192, 0, 0, 0 };
const uint8_t pixels_2[PIXEL_COUNT * 4] =
{ 0, 0, 0, 0,
0, 32, 0, 0,
0, 64, 0, 0,
0, 96, 0, 0,
0, 128, 0, 0,
0, 160, 0, 0,
0, 192, 0, 0 };
const uint8_t pixels_3[PIXEL_COUNT * 4] =
{ 0, 0, 0, 0,
0, 0, 32, 0,
0, 0, 64, 0,
0, 0, 96, 0,
0, 0, 128, 0,
0, 0, 160, 0,
0, 0, 192, 0 };
const uint8_t pixels_4[PIXEL_COUNT * 4] =
{ 0, 0, 0, 0,
0, 0, 0, 32,
0, 0, 0, 64,
0, 0, 0, 96,
0, 0, 0, 128,
0, 0, 0, 160,
0, 0, 0, 192 };
void blink(void) {
PORTB |= _BV(2);
_delay_ms(100);
PORTB &= ~(_BV(2));
}
/*
void fade_in(const uint8_t *pixels);
void fade_out(void);
void fade_in(const uint8_t *pixels) {
uint8_t current[PIXEL_COUNT * 4];
for (int i = 0; i < 255; i++) {
for (int idx = 0; idx < PIXEL_COUNT * 4; idx++) {
if (current[idx] < pixels[idx]) {
current[idx] += 1;
}
}
write_pixels(current, PIXEL_COUNT * 4);
_delay_ms(5);
}
}
void fade_out() {
uint8_t current[PIXEL_COUNT * 4];
for (int i = 0; i < 255; i++) {
for (int idx = 0; idx < PIXEL_COUNT * 4; idx++) {
if (current[idx] > 0) {
current[idx] -= 1;
}
}
write_pixels(current, PIXEL_COUNT * 4);
_delay_ms(5);
}
}
*/
int main (void) {
PORTB = 0;
DDRB = _BV(0) | _BV(1) | _BV(2) | _BV(3);
_delay_ms(50);
blink();
_delay_ms(500);
/*
while (1) {
blink();
write_pixels(pixels_1, PIXEL_COUNT * 4);
_delay_ms(1000);
write_pixels(pixels_2, PIXEL_COUNT * 4);
_delay_ms(1000);
write_pixels(pixels_3, PIXEL_COUNT * 4);
_delay_ms(1000);
write_pixels(pixels_4, PIXEL_COUNT * 4);
_delay_ms(1000);
}
*/
uint8_t pixels[7 * 3];
for (uint8_t i = 0; i < 7 * 3; i++) {
pixels[i] = 0;
}
int8_t r_step = 1;
int8_t g_step = 0;
int8_t b_step = 0;
while (1) {
pixels[0] += r_step;
pixels[3] += r_step;
pixels[6] += r_step;
pixels[9] += r_step;
pixels[12] += r_step;
pixels[15] += r_step;
pixels[18] += r_step;
// pixels[1] += g_step;
// pixels[2] += b_step;
write_pixels(pixels, 7 * 3);
if (pixels[0] == 255) {
r_step = -1;
} else if (pixels[0] == 0) {
r_step = 1;
}
/*
if (pixels[0] == 255) {
r_step = -1;
g_step = 1;
} else if (pixels[0] == 0 && r_step == -1) {
r_step = 0;
}
*/
/*
if (pixels[1] == 255) {
g_step = -1;
b_step = 1;
} else if (pixels[1] == 0 && g_step == -1) {
g_step = 0;
}
if (pixels[2] == 255) {
b_step = -1;
r_step = 1;
} else if (pixels[2] == 0 && b_step == -1) {
b_step = 0;
}
*/
_delay_ms(5);
}
return 0;
}