251209_neopixel

This commit is contained in:
Maciej Bowszys 2025-12-09 21:52:53 +01:00
parent 02aaf2b804
commit 8ee91bc2c9

View File

@ -25,3 +25,78 @@
#define UART_BAUD 9600
#define UART_BIT_DELAY_US (1000000UL / UART_BAUD)
typedef struct {
uint8_t g;
uint8_t r;
uint8_t b;
} RGBColor;
RGBColor pixels[NUMPIXELS];
// !time carefully for 800kHz (1.25us cycle) on 8MHz F_CPU
void ws2812_send() {
uint8_t *ptr = (uint8_t *)pixels;
uint16_t count = NUMPIXELS * 3;
uint8_t mask = _BV(NEOPIXEL_PIN);
uint8_t cli_mask = ~mask;
NEOPIXEL_DDR |= mask; //set to output
uint8_t sreg = SREG;
cli(); // disable interrupts
NEOPIXEL_PORT &= cli_mask;
__asm__ __volatile__ (
"nop\n\t" "nop\n\t" "nop\n\t" "nop\n\t"
"nop\n\t" "nop\n\t" "nop\n\t" "nop\n\t"
"nop\n\t" "nop\n\t" "nop\n\t" "nop\n\t"
);
while (count--) {
uint8_t data = *ptr++;
for (uint8_t i = 8; i--; ) {
if (data & 0x01) {
/*__asm__ __volatile__ (
"nop\n\t" "nop\n\t" "nop\n\t" "nop\n\t"
"nop\n\t" "nop\n\t" "nop\n\t" "nop\n\t"
);*/
NEOPIXEL_PORT |= mask;
__asm__ __volatile__ (
"nop\n\t" "nop\n\t" "nop\n\t" "nop\n\t"
"nop\n\t" "nop\n\t" "nop\n\t" "nop\n\t"
);
NEOPIXEL_PORT &= cli_mask;
} else {
NEOPIXEL_PORT |= mask;
__asm__ __volatile__ (
"nop\n\t" "nop\n\t"
);
NEOPIXEL_PORT &= cli_mask;
}
data <<= 1;
__asm__ __volatile__ (
"nop\n\t" "nop\n\t" "nop\n\t" "nop\n\t"
"nop\n\t" "nop\n\t" "nop\n\t" "nop\n\t"
);
}
}
SREG = sreg;
sei(); // enable interrupts
}
void setPixelColor(uint8_t n, uint8_t r, uint8_t g, uint8_t b) {
if (n < NUMPIXELS) {
pixels[n].r = r;
pixels[n].g = g;
pixels[n].b = b;
}
}