A .Net Standard 2.0 wrapper for the rpi_ws281x library for controlling WS281X LEDs on a Raspberry Pi. This library has been tested on a Raspberry Pi 3 B+.
This library adapts to the python wrapper of rpi_ws281x. The API is almost the same.
// Initialize a new instance of the wrapper
var neopixel = new ws281x.Net.Neopixel(ledCount: 42, pin: 18);
// You can also choose a custom color order
neopixel = new ws281x.Net.Neopixel(ledCount: 42, pin: 18, stripType: rpi_ws281x.WS2811_STRIP_RBG);
// Always initialize the wrapper first
neopixel.Begin();
// Set color of all LEDs to red
for (var i = 0; i < neopixel.GetNumberOfPixels(); i++)
{
neopixel.SetPixelColor(i, System.Drawing.Color.Red);
}
// Apply changes to the led
neopixel.Show();
// Dispose after use
neopixel.Dispose();
The application has to be run in sudo mode to be able to access the GPIO pins.
As this is just a wrapper for a native library you have to put the native binary into the lib order of your Raspberry Pi (which should be /usr/local/lib
in most cases).
Take the prebuild binary from src/ws281x.Net/Native/librpi_ws281x.so
and copy it to the library path of your Pi.
Afterwards make sure to update the library links:
sudo ldconfig
Make sure gcc
is installed on your Pi.
sudo apt-get install -y gcc
Copy all files from src/ws281x.Net/Native
to a temporary build folder on your pi.
Compile the sources first:
gcc -c -fpic ws2811.c rpi_ws281x_wrap.c
Then link them to a library
gcc -shared ws2811.o rpi_ws281x_wrap.o -o librpi_ws281x.so
Copy the output library librpi_ws281x.so
to your lib folder. Afterwards make sure to update the library links:
sudo ldconfig
Make sure you started your program is started with sudo
. Afterwards check the connections to your LED strip.
This is the most annoying error as debugging is quite difficult. Ensure you placed the native library at the right place (usually /usr/local/lib
), it is named librpi_ws281x.so
and run sudo ldconfig
to update the library links.
This projects are using ws281x.Net:
If you want your project to be added to this list just create an issue.
The native interface is generated by swig. The wrapper classes are already created. If you change one of the native libraries or the wrapper interface rpi_ws281x.i
you have to run swig -csharp rpi_ws281x.i
and then copy the output C# classes to the Types folder.
Afterwards, a normal compile will do the job.
dotnet build -c Release