Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Iot.Device.Bindings/Iot.Device.Bindings.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
Expand Down
39 changes: 39 additions & 0 deletions src/devices/Common/Iot/Device/Graphics/BitmapImage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Drawing;

namespace Iot.Device.Graphics
{
public abstract class BitmapImage
{
protected BitmapImage(byte[] data, int width, int height, int stride)
{
_data = data;
Width = width;
Height = height;
Stride = stride;
}

private readonly byte[] _data;
public Span<byte> Data => _data;
public int Width { get; }
public int Height { get; }
public int Stride { get; }

public abstract void SetPixel(int x, int y, Color c);

public virtual void Clear(Color c = default)
{
for (int y = 0; y < Height; y++)
{
for (int x = 0; x < Width; x++)
{
SetPixel(x, y, c);
}
}
}
}
}
58 changes: 58 additions & 0 deletions src/devices/Ws2812B/BitmapImageNeo3.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Iot.Device.Graphics;
using System.Drawing;

namespace Iot.Device.Ws2812b
{
/// <summary>
/// Special 24bit RGB format for Neo pixel LEDs where each bit is converted to 3 bits.
/// A one is converted to 110, a zero is converted to 100.
/// </summary>
public class BitmapImageNeo3 : BitmapImage
{
private const int BytesPerComponent = 3;
private const int BytesPerPixel = BytesPerComponent * 3;
// The Neo Pixels require a 50us delay (all zeros) after. Since Spi freq is not exactly
// as requested 100us is used here with good practical results. 100us @ 2.4Mbps and 8bit
// data means we have to add 30 bytes of zero padding.
private const int ResetDelayInBytes = 30;

public BitmapImageNeo3(int width, int height)
: base(new byte[width * height * BytesPerPixel + ResetDelayInBytes], width, height, width * BytesPerPixel)
{
}

public override void SetPixel(int x, int y, Color c)
{
var offset = y * Stride + x * BytesPerPixel;
Data[offset++] = _lookup[c.G * BytesPerComponent + 0];
Data[offset++] = _lookup[c.G * BytesPerComponent + 1];
Data[offset++] = _lookup[c.G * BytesPerComponent + 2];
Data[offset++] = _lookup[c.R * BytesPerComponent + 0];
Data[offset++] = _lookup[c.R * BytesPerComponent + 1];
Data[offset++] = _lookup[c.R * BytesPerComponent + 2];
Data[offset++] = _lookup[c.B * BytesPerComponent + 0];
Data[offset++] = _lookup[c.B * BytesPerComponent + 1];
Data[offset++] = _lookup[c.B * BytesPerComponent + 2];
}

private static readonly byte[] _lookup = new byte[256 * BytesPerComponent];
static BitmapImageNeo3()
{
for (int i = 0; i < 256; i++)
{
int data = 0;
for (int j = 7; j >= 0; j--)
{
data = (data << 3) | 0b100 | ((i >> j) << 1) & 2;
}
_lookup[i * BytesPerComponent + 0] = unchecked((byte)(data >> 16));
_lookup[i * BytesPerComponent + 1] = unchecked((byte)(data >> 8));
_lookup[i * BytesPerComponent + 2] = unchecked((byte)(data >> 0));
}
}
}
}
27 changes: 27 additions & 0 deletions src/devices/Ws2812B/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Ws2812b

## Summary

This binding allows you to update the RGB LEDs on Ws2812b based strips and matrices.

To see how to use the binding in code, see the [sample](samples/README.md).

## Device Family

* WS2812B: [Datasheet](https://cdn-shop.adafruit.com/datasheets/WS2812B.pdf)

## Binding Notes

### Raspberry Pi setup (/boot/config.txt)

* Make sure spi is enabled<br>
`dtparam=spi=on`

* Make sure SPI don't change speed fix the core clock<br>
`core_freq=250`<br>
`core_freq_min=250`

## References

* [Neo pixels guide](https://learn.adafruit.com/adafruit-neopixel-uberguide)
* [Neo pixels x8 stick](https://www.adafruit.com/product/1426)
25 changes: 25 additions & 0 deletions src/devices/Ws2812B/Ws2812B.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Device.Spi;

namespace Iot.Device.Ws2812b
{
public class Ws2812b
{
private readonly SpiDevice _spiDevice;
public BitmapImageNeo3 Image { get; }

public Ws2812b(SpiDevice spiDevice, int width, int height = 1)
{
_spiDevice = spiDevice;
_spiDevice.ConnectionSettings.ClockFrequency = 2_400_000;
_spiDevice.ConnectionSettings.Mode = SpiMode.Mode0;
_spiDevice.ConnectionSettings.DataBitLength = 8;
Image = new BitmapImageNeo3(width, height);
}

public void Update() => _spiDevice.Write(Image.Data);
}
}
17 changes: 17 additions & 0 deletions src/devices/Ws2812B/Ws2812b.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<!--Disabling default items so samples source won't get build by the main library-->
<EnableDefaultItems>false</EnableDefaultItems>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
<Compile Include="*.cs" />
<Compile Include="../Common/Iot/Device/Graphics/BitmapImage.cs" />
<None Include="README.md" />
<PackageReference Include="System.Device.Gpio" Version="0.1.0-prerelease*" />
</ItemGroup>

</Project>
26 changes: 26 additions & 0 deletions src/devices/Ws2812B/samples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Drive Neo pixel strip (x8) from Raspberry Pi

This [program](Ws2812b.Sample.cs) demonstrates how to use the [Neo pixel binding](../Ws2812b.cs) to drive an 8 Neo Pixel stick from a Raspberry Pi.

It shows how to set the colors of each LED using `SetPixel` and `Update`. Then it shows how to fade in one of the LEDs in a loop.

## Run the sample

```console
cd samples
dotnet build -c release -o out
dotnet out/Ws2812b.Samples.dll
```

## Breadboard layout

The following [fritzing diagram](rpi-neo-pixels.fzz) demonstrates how you should wire your device in order to run the program. It uses the GND, 5V and MOSI pins on the Raspberry Pi.

![Raspberry Pi Breadboard diagram](rpi-neo-pixels_bb.png)

## Hardware elements

The following elements are used in this sample:

* [Raspberry Pi 3](https://www.adafruit.com/product/3055)
* [Neo pixels x8 stick](https://www.adafruit.com/product/1426)
44 changes: 44 additions & 0 deletions src/devices/Ws2812B/samples/Ws2812b.Sample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Device.Spi;
using System.Device.Spi.Drivers;
using System.Drawing;

namespace Iot.Device.Ws2812b.Samples
{
class Program
{
static void Main()
{
// Create a Neo Pixel x8 stick on spi 0.0
var spi = new UnixSpiDevice(new SpiConnectionSettings(0, 0));
var neo = new Ws2812b(spi, 8);

// Display basic colors for 5 sec
BitmapImageNeo3 img = neo.Image;
img.SetPixel(0, 0, Color.White);
img.SetPixel(1, 0, Color.Red);
img.SetPixel(2, 0, Color.Green);
img.SetPixel(3, 0, Color.Blue);
img.SetPixel(4, 0, Color.Yellow);
img.SetPixel(5, 0, Color.Cyan);
img.SetPixel(6, 0, Color.Magenta);
img.SetPixel(7, 0, Color.FromArgb(unchecked((int)0xffff8000)));
neo.Update();
System.Threading.Thread.Sleep(5000);

// Fade in first pixel
byte b = 0;
img.Clear();
while (true)
{
img.SetPixel(0, 0, Color.FromArgb(0xff, b, b, b));
neo.Update();
System.Threading.Thread.Sleep(10);
b++;
}
}
}
}
12 changes: 12 additions & 0 deletions src/devices/Ws2812B/samples/Ws2812b.Samples.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="../Ws2812b.csproj" />
</ItemGroup>

</Project>
Binary file added src/devices/Ws2812B/samples/rpi-neo-pixels.fzz
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.