-
Notifications
You must be signed in to change notification settings - Fork 615
Add WS2812B binding #241
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add WS2812B binding #241
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5045ae5
Add WS2812B binding
maloo 05c743e
Fix merge typo
maloo 49af48e
Revert to .Net Core 2.1
maloo 5f35833
Remove WIP file refs
maloo bbf7c15
Remove ssh deploy config for CI
maloo f4c74b9
Fix review comments
maloo 9448dd4
Fix review comments
maloo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() | ||
maloo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| 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)); | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. | ||
|
|
||
|  | ||
|
|
||
| ## 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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++; | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.