ATmega328P driver library for WS2812/WS2812B LED strips. Features: not many. Includes tasty AVR assembly. Tested on an Arduino Uno with the WS2812B chip.
#define __AVR_ATmega32U4__
#include <avr/io.h>
#include <inttypes.h>
#include <stddef.h>
#define F_CPU 16000000UL
#include <util/delay.h>
#include "ws2812b.h"
#define DELAY_US 10000
#define DATA_PIN 0
#define PIXELS 30
int main ()
{
ws2812b_setup_pin(DATA_PIN);
_delay_us(DELAY_US);
pixel buf[PIXELS] = { 0 };
const size_t pixels = sizeof(buf) / sizeof(pixel);
// clear any pixels by sending empty buffer
ws2812b_send_pixels(DATA_PIN, buf, pixels);
const pixel red = { .r = 0xFF, .g = 0x00, .b = 0x00 };
buf[0] = red;
ws2812b_initialize_pixel_buf(buf, pixels, red);
while(1) {
// shift all pixels one right for visual effect
pixel last = buf[pixels - 1];
for (size_t i = pixels - 1; i > 0; i--) {
buf[i] = buf[i - 1];
}
buf[0] = last;
// transmit our pixel buffer to the LED strip
ws2812b_send_pixels(DATA_PIN, buf, pixels);
// wait 10ms
_delay_us(DELAY_US);
}
}
🐧: gcc-avr
, avr-libc
, binutils
, avrdude
, cmake
🍎: avr-gcc
, avr-binutils
, avrdude
, cmake
(install via Homebrew)
All: Check and adjust the
avrdude
port and programmer arguments before executingrun.sh
.
Linux users: You may need to modify
build.sh
andrun.sh
so that the-DCMAKE_TOOLCHAIN_FILE
argument reflects the full path to theavr.cmake
file.
Run build.sh
to build. Run run.sh
to build and upload with avrdude
.
For more on how to transmit data to the WS2812(B) LED strips, see cpldcpu's excellent analysis and his light_ws2812 library, which applies broadly the same approach as the above and works for many more platforms.
The datasheets for the WS2812 and WS2812B chips are available here:
For more on the AVR assembly language and how to incorporate it into C with the GNU C compiler for AVR: