d8ahazard / rpi-ws281x-csharp

:strawberry: C# / .NET library wrapper for the rpi-ws281x library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Nuget Framework Framework Framework License Nuget

rpi-ws281x-csharp

This fork contains the following changes:

  • Add proper channel creation for various GPIO Pins, auto controller selection based on GPIO PIN input
  • Add clamping method for RGB+W LEDs to return proper white value and avoid brownouts
  • Add on-the-fly changes for strip brightness and count without needing to re-initialize the strip
  • Added fix for .Net Core Exception Issue.
  • Added default Gamma Correction Map based on this Adafruit document.
  • Added support for additional LED strips.

Please review latest Release Notes for most recent changes.

This is a C# wrapper for the great C assembly by Jeremy Garff to control WS281X LEDs by a Raspberry PI (https://github.com/jgarff/rpi_ws281x). It uses the P/Invoke calls to access the native assembly.

Usage

It is very easy to use the wrapper in your own C# / .NET project. Just see the example below:

// The default settings uses a frequency of 800000 Hz and the DMA channel 10, and enables gamma correction.
// Pass false to the constructor to disable gamma correction.
var settings = Settings.CreateDefaultSettings(false);

//Use 16 LEDs and GPIO Pin 18.
//Set brightness to maximum (255)
//Use Unknown as strip type. Then the type will be set in the native assembly.
var controller = settings.AddController(16, Pin.Gpio18, StripType.WS2812_STRIP, ControllerType.PWM0, 255, false)

using (var rpi = new WS281x(settings))
{
  //Set the color of the first LED of controller 0 to blue
  controller.SetLED(0, Color.Blue);
  //Set the color of the second LED of controller 0 to red
  controller.SetLED(1, Color.Red);
  rpi.Render();
  // Retrieve the existing brightness for controller 0
  var brightness = rpi.GetBrightness(0);
  OR
  var brightness = rpi.GetBrightness(); // Default ID is 0
  // Set the strip brightness to 128 for controller 0 - render is automatically called
  rpi.SetBrightness(128, 0);
  OR
  rpi.SetBrightness(128); // Default is 0
  // Retrieve existing LED Count for controller 1
  var ledCount = rpi.GetLedCount(1);
  // Update the strip count to 32 for controller 1 - render is automatically called
  rpi.SetLedCount(32, 1);
  // Set all the LED colors to Red - render is automatically called
  rpi.SetAll(Color.Green);
  // Reset the strip and destroy it
  rpi.Reset();
  rpi.Dispose();
}

Please have a look at the example program and get familiar with the usage.

Installation

The library can be downloaded from NuGet: Nuget

Package Manager

PM> Install-Package digitalhigh.rpi-ws281x-csharp

.NET CLI

> dotnet add package digitalhigh.rpi-ws281x-csharp

In order to get the wrapper working, you need build the shared C library first. Please refer to the README in the lib folder for more information.

The P/Invoke functionality has a special search pattern to find the required assembly. For my tests, I copied the ws2811.so assembly to /usr/lib folder (as suggested by the link above).

Test status

The wrapper was tested with following setup:

Raspberry model Controller GPIO Pin DMA channel Result
3 Model B Rev 2 WS2812B 18 5, 10 Success
4 Model B Rev 2 WS2812B 18, 19 5, 10 Success

Please feel free to add some more test cases.

About

:strawberry: C# / .NET library wrapper for the rpi-ws281x library

License:Other


Languages

Language:C# 98.4%Language:Smalltalk 1.6%