Skip to content

Commit 736dd53

Browse files
authored
Ws28 device migration (#184)
1 parent 6cee751 commit 736dd53

27 files changed

+220
-133
lines changed

Diff for: devices/Ws28xx/BitmapImage.cs

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Drawing;
6+
7+
namespace Iot.Device.Ws28xx
8+
{
9+
/// <summary>
10+
/// Represents bitmap image
11+
/// </summary>
12+
public abstract class BitmapImage
13+
{
14+
/// <summary>
15+
/// Initializes a <see cref="T:Iot.Device.Graphics.BitmapImage" /> instance with the specified data, width, height and stride.
16+
/// </summary>
17+
/// <param name="data">Data representing the image (derived class defines a specific format)</param>
18+
/// <param name="width">Width of the image</param>
19+
/// <param name="height">Height of the image</param>
20+
/// <param name="stride">Number of bytes per row</param>
21+
protected BitmapImage(byte[] data, int width, int height, int stride)
22+
{
23+
_data = data;
24+
Width = width;
25+
Height = height;
26+
Stride = stride;
27+
}
28+
29+
private byte[] _data;
30+
31+
/// <summary>
32+
/// Data related to the image (derived class defines a specific format)
33+
/// </summary>
34+
public byte[] Data => _data;
35+
36+
/// <summary>
37+
/// Width of the image
38+
/// </summary>
39+
public int Width { get; }
40+
41+
/// <summary>
42+
/// Height of the image
43+
/// </summary>
44+
public int Height { get; }
45+
46+
/// <summary>
47+
/// Number of bytes per row
48+
/// </summary>
49+
public int Stride { get; }
50+
51+
/// <summary>
52+
/// Sets pixel at specific position
53+
/// </summary>
54+
/// <param name="x">X coordinate of the pixel</param>
55+
/// <param name="y">Y coordinate of the pixel</param>
56+
/// <param name="color">Color to set the pixel to</param>
57+
public abstract void SetPixel(int x, int y, Color color);
58+
59+
/// <summary>
60+
/// Clears the image to specific color
61+
/// </summary>
62+
/// <param name="color">Color to clear the image. Defaults to black.</param>
63+
public virtual void Clear(Color color = default)
64+
{
65+
for (int y = 0; y < Height; y++)
66+
{
67+
for (int x = 0; x < Width; x++)
68+
{
69+
SetPixel(x, y, color);
70+
}
71+
}
72+
}
73+
}
74+
}

Diff for: src/devices_generated/Ws28xx/BitmapImageNeo3.cs renamed to devices/Ws28xx/BitmapImageNeo3.cs

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Drawing;
5-
using Iot.Device.Graphics;
65

76
namespace Iot.Device.Ws28xx
87
{

Diff for: src/devices_generated/Ws28xx/BitmapImageWs2808.cs renamed to devices/Ws28xx/BitmapImageWs2808.cs

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Drawing;
5-
using Iot.Device.Graphics;
65

76
namespace Iot.Device.Ws28xx
87
{

Diff for: src/devices_generated/Ws28xx/Properties/AssemblyInfo.cs renamed to devices/Ws28xx/Properties/AssemblyInfo.cs

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Reflection;
2-
using System.Runtime.CompilerServices;
32
using System.Runtime.InteropServices;
43

54
[assembly: AssemblyTitle("Iot.Device.Ws28xx")]

Diff for: src/devices_generated/Ws28xx/README.md renamed to devices/Ws28xx/README.md

+10-22
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,11 @@ To see how to use the binding in code, see the [sample](samples/Program.cs).
1313

1414
## Board
1515

16-
### Neo pixels
16+
### WS2812B
1717

18-
![Raspberry Pi Breadboard diagram](rpi-neo-pixels_bb.png)
18+
![image](./WS2812B.png)
1919

20-
### WS2808
2120

22-
![WS2808 diagram](WS2808.png)
2321

2422
## Usage
2523

@@ -35,7 +33,14 @@ using Iot.Device.Ws28xx;
3533
const int Count = 8;
3634
Console.Clear();
3735

38-
SpiConnectionSettings settings = new(0, 0)
36+
// Must specify pin functions on ESP32
37+
Configuration.SetPinFunction(23, DeviceFunction.SPI2_MOSI);
38+
Configuration.SetPinFunction(19, DeviceFunction.SPI2_MISO);
39+
Configuration.SetPinFunction(18, DeviceFunction.SPI2_CLOCK);
40+
Configuration.SetPinFunction(22, DeviceFunction.ADC1_CH10);
41+
42+
// Using VSPI on bus 2 for ESP32 and pin 22 for chipselect
43+
SpiConnectionSettings settings = new(2, 22)
3944
{
4045
ClockFrequency = 2_400_000,
4146
Mode = SpiMode.Mode0,
@@ -66,20 +71,3 @@ void Rainbow(Ws28xx neo, int count, int iterations = 1)
6671
}
6772
}
6873
```
69-
70-
## Binding Notes
71-
72-
### Raspberry Pi setup (/boot/config.txt)
73-
74-
* Make sure spi is enabled
75-
76-
```text
77-
dtparam=spi=on
78-
```
79-
80-
* Make sure SPI don't change speed fix the core clock:
81-
82-
```text
83-
core_freq=250
84-
core_freq_min=250
85-
```

Diff for: devices/Ws28xx/WS2812B.png

223 KB
Loading
File renamed without changes.
File renamed without changes.

Diff for: src/devices_generated/Ws28xx/Ws28xx.cs renamed to devices/Ws28xx/Ws28xx.cs

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System;
55
using System.Device.Spi;
6-
using Iot.Device.Graphics;
76

87
namespace Iot.Device.Ws28xx
98
{

Diff for: src/devices_generated/Ws28xx/Ws28xx.nfproj renamed to devices/Ws28xx/Ws28xx.nfproj

+22-12
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,30 @@
2020
</PropertyGroup>
2121
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.props')" />
2222
<ItemGroup>
23-
<Reference Include="mscorlib">
24-
<HintPath>packages\nanoFramework.CoreLibrary.1.10.4-preview.11\lib\mscorlib.dll</HintPath>
23+
<Reference Include="mscorlib, Version=1.10.5.4, Culture=neutral, PublicKeyToken=c07d481e9758c731">
24+
<HintPath>packages\nanoFramework.CoreLibrary.1.10.5\lib\mscorlib.dll</HintPath>
2525
<Private>True</Private>
26+
<SpecificVersion>True</SpecificVersion>
2627
</Reference>
27-
<Reference Include="System.Device.Spi">
28-
<HintPath>packages\nanoFramework.System.Device.Spi.1.0.0-preview.38\lib\System.Device.Spi.dll</HintPath>
28+
<Reference Include="System.Device.Spi, Version=1.0.1.2, Culture=neutral, PublicKeyToken=c07d481e9758c731">
29+
<HintPath>packages\nanoFramework.System.Device.Spi.1.0.1\lib\System.Device.Spi.dll</HintPath>
2930
<Private>True</Private>
30-
</Reference>
31+
<SpecificVersion>True</SpecificVersion>
32+
</Reference>
33+
<Reference Include="nanoFramework.Runtime.Events, Version=1.9.1.3, Culture=neutral, PublicKeyToken=c07d481e9758c731">
34+
<HintPath>packages\nanoFramework.Runtime.Events.1.9.1\lib\nanoFramework.Runtime.Events.dll</HintPath>
35+
<Private>True</Private>
36+
<SpecificVersion>True</SpecificVersion>
37+
</Reference>
38+
<Reference Include="System.Device.Gpio, Version=1.0.1.2, Culture=neutral, PublicKeyToken=c07d481e9758c731">
39+
<HintPath>packages\nanoFramework.System.Device.Gpio.1.0.1\lib\System.Device.Gpio.dll</HintPath>
40+
<Private>True</Private>
41+
<SpecificVersion>True</SpecificVersion>
42+
</Reference>
3143
</ItemGroup>
3244
<ItemGroup>
3345
<None Include="packages.config" />
3446
<Compile Include="*.cs" />
35-
<Compile Include="../Common/Iot/Device/Graphics/BitmapImage.cs" />
3647
<None Include="README.md" />
3748
</ItemGroup>
3849
<ItemGroup>
@@ -41,18 +52,17 @@
4152
<None Include="*.md" />
4253
</ItemGroup>
4354
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets')" />
44-
<Import Project="..\..\src\System.Runtime.CompilerService\System.Runtime.CompilerService.projitems" Label="Shared" />
45-
<Import Project="..\..\src\System.Drawing\System.Drawing.projitems" Label="Shared" />
55+
<Import Project="..\..\src\System.Drawing\System.Drawing.projitems" Label="Shared" />
4656
<ProjectExtensions>
4757
<ProjectCapabilities>
4858
<ProjectConfigurationsDeclaredAsItems />
4959
</ProjectCapabilities>
5060
</ProjectExtensions>
51-
<Import Project="packages\Nerdbank.GitVersioning.3.4.194\build\Nerdbank.GitVersioning.targets" Condition="Exists('packages\Nerdbank.GitVersioning.3.4.194\build\Nerdbank.GitVersioning.targets')" />
52-
<Target Name = "EnsureNuGetPackageBuildImports" BeforeTargets = "PrepareForBuild">
61+
<Import Project="packages\Nerdbank.GitVersioning.3.4.240\build\Nerdbank.GitVersioning.targets" Condition="Exists('packages\Nerdbank.GitVersioning.3.4.240\build\Nerdbank.GitVersioning.targets')" />
62+
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
5363
<PropertyGroup>
5464
<ErrorText> This project references NuGet package(s) that are missing on this computer.Enable NuGet Package Restore to download them.For more information, see http://go.microsoft.com/fwlink/?LinkID=322105.The missing file is {0}.</ErrorText>
5565
</PropertyGroup>
56-
<Error Condition = "!Exists('packages\Nerdbank.GitVersioning.3.4.194\build\Nerdbank.GitVersioning.targets')" Text = "$([System.String]::Format('$(ErrorText)', 'packages\Nerdbank.GitVersioning.3.4.194\build\Nerdbank.GitVersioning.targets'))" />
66+
<Error Condition="!Exists('packages\Nerdbank.GitVersioning.3.4.240\build\Nerdbank.GitVersioning.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\Nerdbank.GitVersioning.3.4.240\build\Nerdbank.GitVersioning.targets'))" />
5767
</Target>
58-
</Project>
68+
</Project>

Diff for: src/devices_generated/Ws28xx/Ws28xx.nuspec renamed to devices/Ws28xx/Ws28xx.nuspec

+7-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@
2020
<summary>Iot.Device.Ws28xx assembly for .NET nanoFramework C# projects</summary>
2121
<tags>nanoFramework C# csharp netmf netnf Iot.Device.Ws28xx</tags>
2222
<dependencies>
23-
<dependency id="nanoFramework.CoreLibrary" version="1.10.4-preview.11" />
23+
<dependency id="nanoFramework.CoreLibrary" version="1.10.5" />
24+
<dependency id="nanoFramework.Runtime.Events" version="1.9.1" />
25+
<dependency id="nanoFramework.System.Device.Spi" version="1.0.1" />
26+
<dependency id="nanoFramework.System.Device.Gpio" version="1.0.1" />
27+
<dependency id="Nerdbank.GitVersioning" version="3.4.240" />
2428
</dependencies>
2529
</metadata>
2630
<files>
@@ -30,7 +34,7 @@
3034
<file src="bin\Release\Iot.Device.Ws28xx.pe" target="lib\Iot.Device.Ws28xx.pe" />
3135
<file src="bin\Release\Iot.Device.Ws28xx.xml" target="lib\Iot.Device.Ws28xx.xml" />
3236
<file src="readme.md" target="" />
33-
<file src="..\assets\nf-logo.png" target="images" />
34-
<file src="..\LICENSE.md" target="" />
37+
<file src="..\..\assets\nf-logo.png" target="images" />
38+
<file src="..\..\LICENSE.md" target="" />
3539
</files>
3640
</package>

Diff for: devices/Ws28xx/Ws28xx.sln

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
Microsoft Visual Studio Solution File, Format Version 12.00
2+
# Visual Studio Version 16
3+
VisualStudioVersion = 16.0.31613.86
4+
MinimumVisualStudioVersion = 15.0.26124.0
5+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{B2C3D1C3-4F59-4544-982C-E205F522DF27}"
6+
EndProject
7+
Project("{11A8DD76-328B-46DF-9F39-F559912D0360}") = "Ws28xx.Samples", "samples\Ws28xx.Samples.nfproj", "{A3696C35-5466-57C9-D338-4FC341CF1112}"
8+
EndProject
9+
Project("{11A8DD76-328B-46DF-9F39-F559912D0360}") = "Ws28xx", "Ws28xx.nfproj", "{FB1A0E97-B902-7A7F-8DA3-233D32B3C576}"
10+
EndProject
11+
Global
12+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
13+
Debug|Any CPU = Debug|Any CPU
14+
Debug|x64 = Debug|x64
15+
Debug|x86 = Debug|x86
16+
Release|Any CPU = Release|Any CPU
17+
Release|x64 = Release|x64
18+
Release|x86 = Release|x86
19+
EndGlobalSection
20+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
21+
{A3696C35-5466-57C9-D338-4FC341CF1112}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
22+
{A3696C35-5466-57C9-D338-4FC341CF1112}.Debug|Any CPU.Build.0 = Debug|Any CPU
23+
{A3696C35-5466-57C9-D338-4FC341CF1112}.Debug|x64.ActiveCfg = Debug|Any CPU
24+
{A3696C35-5466-57C9-D338-4FC341CF1112}.Debug|x64.Build.0 = Debug|Any CPU
25+
{A3696C35-5466-57C9-D338-4FC341CF1112}.Debug|x86.ActiveCfg = Debug|Any CPU
26+
{A3696C35-5466-57C9-D338-4FC341CF1112}.Debug|x86.Build.0 = Debug|Any CPU
27+
{A3696C35-5466-57C9-D338-4FC341CF1112}.Release|Any CPU.ActiveCfg = Release|Any CPU
28+
{A3696C35-5466-57C9-D338-4FC341CF1112}.Release|Any CPU.Build.0 = Release|Any CPU
29+
{A3696C35-5466-57C9-D338-4FC341CF1112}.Release|x64.ActiveCfg = Release|Any CPU
30+
{A3696C35-5466-57C9-D338-4FC341CF1112}.Release|x64.Build.0 = Release|Any CPU
31+
{A3696C35-5466-57C9-D338-4FC341CF1112}.Release|x86.ActiveCfg = Release|Any CPU
32+
{A3696C35-5466-57C9-D338-4FC341CF1112}.Release|x86.Build.0 = Release|Any CPU
33+
{FB1A0E97-B902-7A7F-8DA3-233D32B3C576}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
34+
{FB1A0E97-B902-7A7F-8DA3-233D32B3C576}.Debug|Any CPU.Build.0 = Debug|Any CPU
35+
{FB1A0E97-B902-7A7F-8DA3-233D32B3C576}.Debug|x64.ActiveCfg = Debug|Any CPU
36+
{FB1A0E97-B902-7A7F-8DA3-233D32B3C576}.Debug|x64.Build.0 = Debug|Any CPU
37+
{FB1A0E97-B902-7A7F-8DA3-233D32B3C576}.Debug|x86.ActiveCfg = Debug|Any CPU
38+
{FB1A0E97-B902-7A7F-8DA3-233D32B3C576}.Debug|x86.Build.0 = Debug|Any CPU
39+
{FB1A0E97-B902-7A7F-8DA3-233D32B3C576}.Release|Any CPU.ActiveCfg = Release|Any CPU
40+
{FB1A0E97-B902-7A7F-8DA3-233D32B3C576}.Release|Any CPU.Build.0 = Release|Any CPU
41+
{FB1A0E97-B902-7A7F-8DA3-233D32B3C576}.Release|x64.ActiveCfg = Release|Any CPU
42+
{FB1A0E97-B902-7A7F-8DA3-233D32B3C576}.Release|x64.Build.0 = Release|Any CPU
43+
{FB1A0E97-B902-7A7F-8DA3-233D32B3C576}.Release|x86.ActiveCfg = Release|Any CPU
44+
{FB1A0E97-B902-7A7F-8DA3-233D32B3C576}.Release|x86.Build.0 = Release|Any CPU
45+
EndGlobalSection
46+
GlobalSection(SolutionProperties) = preSolution
47+
HideSolutionNode = FALSE
48+
EndGlobalSection
49+
GlobalSection(NestedProjects) = preSolution
50+
{A3696C35-5466-57C9-D338-4FC341CF1112} = {B2C3D1C3-4F59-4544-982C-E205F522DF27}
51+
EndGlobalSection
52+
GlobalSection(ExtensibilityGlobals) = postSolution
53+
SolutionGuid = {4B352805-D7FE-4B6E-8984-979938027CE2}
54+
EndGlobalSection
55+
EndGlobal
File renamed without changes.

Diff for: devices/Ws28xx/packages.config

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="nanoFramework.CoreLibrary" version="1.10.5" targetFramework="netnanoframework10" />
4+
<package id="nanoFramework.Runtime.Events" version="1.9.1" targetFramework="netnanoframework10" />
5+
<package id="nanoFramework.System.Device.Gpio" version="1.0.1" targetFramework="netnanoframework10" />
6+
<package id="nanoFramework.System.Device.Spi" version="1.0.1" targetFramework="netnanoframework10" />
7+
<package id="Nerdbank.GitVersioning" version="3.4.240" developmentDependency="true" targetFramework="netnanoframework10" />
8+
</packages>
File renamed without changes.

Diff for: src/devices_generated/Ws28xx/samples/Program.cs renamed to devices/Ws28xx/samples/Program.cs

+12-13
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using Iot.Device.Ws28xx;
5+
using nanoFramework.Hardware.Esp32;
46
using System;
5-
using System.Collections.Generic;
67
using System.Device.Spi;
78
using System.Drawing;
8-
using Iot.Device.Graphics;
9-
using Iot.Device.Ws28xx;
109

1110
// Configure the count of pixels
12-
const int Count = 8;
13-
Console.Clear();
11+
const int Count = 16;
12+
13+
// Must specify pin functions on ESP32
14+
Configuration.SetPinFunction(23, DeviceFunction.SPI2_MOSI);
15+
Configuration.SetPinFunction(19, DeviceFunction.SPI2_MISO);
16+
Configuration.SetPinFunction(18, DeviceFunction.SPI2_CLOCK);
17+
// Pin 22 must be set to ADC to use as the chip selector
18+
Configuration.SetPinFunction(22, DeviceFunction.ADC1_CH10);
1419

15-
SpiConnectionSettings settings = new(0, 0)
20+
// Using VSPI on bus 2 for ESP32 and pin 22 for chipselect
21+
SpiConnectionSettings settings = new(2, 22)
1622
{
1723
ClockFrequency = 2_400_000,
1824
Mode = SpiMode.Mode0,
@@ -26,13 +32,6 @@
2632
Ws28xx neo = new Ws2812b(spi, Count);
2733
#endif
2834

29-
Console.CancelKeyPress += (o, e) =>
30-
{
31-
BitmapImage img = neo.Image;
32-
img.Clear();
33-
neo.Update();
34-
Console.Clear();
35-
};
3635

3736
while (true)
3837
{

Diff for: src/devices_generated/Ws28xx/samples/Properties/AssemblyInfo.cs renamed to devices/Ws28xx/samples/Properties/AssemblyInfo.cs

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Reflection;
2-
using System.Runtime.CompilerServices;
32
using System.Runtime.InteropServices;
43

54
[assembly: AssemblyTitle("Iot.Device.Ws28xx.Samples")]

0 commit comments

Comments
 (0)