I created a new more flexible led library implementation at this new project: https://github.com/ForsakenNGS/PicoLED
After not finding any libraries to address my WS2812, I took the sample code from the documentation and wrote a simple library based on it.
- Copy over the
WS2812.cpp
,WS2812.hpp
andWS2812.pio
files into your project - Add
pico_generate_pio_header(YourProject ${CMAKE_CURRENT_LIST_DIR}/WS2812.pio)
to yourCMakeLists.txt
(ReplacingYourProject
with the name of your project, duh :D) - Add the
WS2812.cpp
to youradd_executable
instruction of yourCMakeLists.txt
- Make sure
hardware_pio
is included within thetarget_link_libraries
instruction of yourCMakeLists.txt
- Initialize a
WS2812
-Object with the desired parameters (see below for more details) - Set the pixels values using
setPixelColor
orfill
- Send the pixel data to the LED strip using
show
WS2812(uint pin, uint length, PIO pio, uint sm);
WS2812(uint pin, uint length, PIO pio, uint sm, DataFormat format);
WS2812(uint pin, uint length, PIO pio, uint sm, DataByte b1, DataByte b2, DataByte b3);
WS2812(uint pin, uint length, PIO pio, uint sm, DataByte b1, DataByte b2, DataByte b3, DataByte b4);
Required:
- pin: The GPIO-pin of the pico that is connected to the data line of your LED strip
- length: The number of LEDs in your strip
- pio: Either
pio0
orpio1
representing one of the two PIO-Blocks available in the pico - sm: The index of the state machine for interfacing with the led strip (0-3) which can only be used once per PIO-Block
Optional simple:
- format: One of
FORMAT_RGB
,FORMAT_GRB
orFORMAT_WRGB
depending on your LED strip. Default isFORMAT_GRB
. If in doubt just try it out.
Optional flexible:
If none of the predefined formats matches your strip, you can define the byte order manually
- b1-b4: One of
RED
,GREEN
,BLUE
,WHITE
For example (GBR-Format):
WS2812 ledStrip(0, 6, pio0, 0, WS2812.GREEN, WS2812.BLUE, WS2812.RED);
For a quick start you can simply use this repository as a starting point. The example.cpp
contains the entry point and the CMakeLists.txt
is already configured properly. All you have to do is compile it and upload it to your Raspberry Pi Pico.