Skip to content

Commit 4b45dfa

Browse files
Implement neo pixel (#55)
* Implement neo pixel * Update src/nf-telemetry-clients/Peripherals/NeoPixel.Peripheral/NeoPixelGauge.cs Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
1 parent f6d1322 commit 4b45dfa

File tree

8 files changed

+211
-14
lines changed

8 files changed

+211
-14
lines changed

src/nf-telemetry-clients/Clients/RipTide.Nfirmware/Program.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
using NeoPixel.Peripheral;
12
using System.Diagnostics;
3+
using System.Drawing;
24
using System.Threading;
35
using TelemetryStash.AdcSensor;
46

@@ -11,6 +13,9 @@ public static void Main()
1113
{
1214
Thread.Sleep(2000);
1315

16+
var gauge = new NeoPixelGauge(pixelsCount: 45, new[] { Color.Green, Color.Yellow, Color.Red }, pin: 11);
17+
gauge.Initialize();
18+
1419
var ss49e = new Ss49eHallSensor(new int[] { 0, 1 }, adcReadScale: 45, true);
1520
ss49e.CalibrateAdcChannelOffsets();
1621
while (true)
@@ -22,9 +27,11 @@ public static void Main()
2227
continue;
2328
}
2429

25-
Debug.WriteLine($"Value: {value}");
30+
gauge.SetPosition(value);
2631
}
2732

33+
34+
2835
Debug.WriteLine("Zzzz");
2936
Thread.Sleep(Timeout.Infinite);
3037
}

src/nf-telemetry-clients/Clients/RipTide.Nfirmware/RipTide.Nfirmware.nfproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
<Reference Include="mscorlib">
3838
<HintPath>..\..\packages\nanoFramework.CoreLibrary.1.17.1\lib\mscorlib.dll</HintPath>
3939
</Reference>
40+
<Reference Include="nanoFramework.Graphics.Core">
41+
<HintPath>..\..\packages\nanoFramework.Graphics.Core.1.2.39\lib\nanoFramework.Graphics.Core.dll</HintPath>
42+
</Reference>
4043
<Reference Include="nanoFramework.Runtime.Events">
4144
<HintPath>..\..\packages\nanoFramework.Runtime.Events.1.11.30\lib\nanoFramework.Runtime.Events.dll</HintPath>
4245
</Reference>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
33
<package id="nanoFramework.CoreLibrary" version="1.17.1" targetFramework="netnano1.0" />
4+
<package id="nanoFramework.Graphics.Core" version="1.2.39" targetFramework="netnano1.0" />
45
<package id="nanoFramework.Runtime.Events" version="1.11.30" targetFramework="netnano1.0" />
56
</packages>

src/nf-telemetry-clients/Peripherals/NeoPixel.Peripheral/NeoPixelGauge.cs

Lines changed: 88 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
using CCSWE.nanoFramework.NeoPixel;
22
using CCSWE.nanoFramework.NeoPixel.Drivers;
3+
using System;
34
using System.Drawing;
45
using System.Threading;
56

67
// https://docid81hrs3j1.cloudfront.net/medialibrary/2018/10/WS2812B_V1.4_EN_18090714224701.pdf
7-
// Enable LDO2 on UM FeatherS3
8-
//var gpioController = new GpioController();
9-
//gpioController.OpenPin(39, PinMode.Output).Write(PinValue.High);
108

119
namespace NeoPixel.Peripheral
1210
{
1311
public class NeoPixelGauge
1412
{
1513
private readonly Color[] _colors;
1614
private readonly NeoPixelStrip _pixels;
15+
private readonly Thread _gaugeThread;
16+
17+
private volatile int _currentPosition;
18+
private volatile int _requestedPosition;
1719

1820
public NeoPixelGauge(ushort pixelsCount, Color[] gaugeColors, byte pin)
1921
{
22+
// TODO: Decrease pulse transfer times
2023
_pixels = new NeoPixelStrip(pin, pixelsCount, new Ws2812B());
2124
_colors = new Color[pixelsCount];
2225

@@ -40,25 +43,98 @@ public NeoPixelGauge(ushort pixelsCount, Color[] gaugeColors, byte pin)
4043
_colors[index++] = color;
4144
}
4245
}
46+
47+
_gaugeThread = new Thread(SetPosition);
4348
}
4449

45-
public void DemoRun()
50+
public void Initialize()
4651
{
47-
var delay = 500;
52+
_gaugeThread.Start();
53+
for (var i = 0; i < _pixels.Count; i++)
54+
{
55+
_pixels.SetLed(i, _colors[i]);
56+
_pixels.Update();
57+
Thread.Sleep(1);
58+
}
59+
60+
for (var i = 0; i < _pixels.Count; i++)
61+
{
62+
_pixels.SetLed(i, Color.Black);
63+
_pixels.Update();
64+
Thread.Sleep(1);
65+
}
66+
}
67+
68+
public void SetPosition(int position)
69+
{
70+
_requestedPosition = Math.Min(position, _colors.Length-1);
71+
}
4872

73+
private void SetPosition()
74+
{
4975
while (true)
5076
{
51-
_pixels.Clear();
52-
_pixels.Update();
77+
if (_currentPosition == _requestedPosition)
78+
{
79+
Thread.Sleep(1);
80+
continue;
81+
}
82+
83+
var currentPosition = _currentPosition;
84+
var requestedPosition = _requestedPosition;
5385

54-
for (var i = 0; i < _pixels.Count; i++)
86+
var goingUp = currentPosition < requestedPosition;
87+
if (goingUp)
5588
{
56-
_pixels.SetLed(i, _colors[i]);
57-
_pixels.Update();
58-
Thread.Sleep(10);
89+
while (currentPosition < requestedPosition)
90+
{
91+
// Increase the speed of the gauge if the requested position is far away
92+
var increments = (requestedPosition - currentPosition) switch
93+
{
94+
> 10 => 3,
95+
> 5 => 2,
96+
_ => 1,
97+
};
98+
99+
_pixels.SetLed(++currentPosition, _colors[currentPosition]);
100+
if (currentPosition % increments == 0 || currentPosition >= requestedPosition || requestedPosition != _requestedPosition)
101+
{
102+
_pixels.Update();
103+
Thread.Sleep(0);
104+
}
105+
if (requestedPosition != _requestedPosition)
106+
{
107+
break;
108+
}
109+
}
110+
}
111+
112+
// going down
113+
else
114+
{
115+
while (currentPosition > requestedPosition)
116+
{
117+
var increments = (currentPosition - requestedPosition) switch
118+
{
119+
> 5 => 2,
120+
_ => 1,
121+
};
122+
123+
_pixels.SetLed(currentPosition--, Color.Black);
124+
if (currentPosition % increments == 0 || requestedPosition == currentPosition || requestedPosition != _requestedPosition)
125+
{
126+
_pixels.Update();
127+
Thread.Sleep(0);
128+
}
129+
130+
if (requestedPosition != _requestedPosition)
131+
{
132+
break;
133+
}
134+
}
59135
}
60136

61-
Thread.Sleep(delay * 5);
137+
_currentPosition = currentPosition;
62138
}
63139
}
64140
}

src/nf-telemetry-clients/Shared/Tests/Client.Services.Benchmarks/Client.Services.Benchmarks.nfproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.props')" />
2121
<ItemGroup>
2222
<Compile Include="BufferedTelemetryServiceBenchmark.cs" />
23+
<Compile Include="FrameworkBenchmark3.cs" />
24+
<Compile Include="FrameworkBenchmark2.cs" />
2325
<Compile Include="FrameworkBenchmark.cs" />
2426
<Compile Include="JsonBenchmark.cs" />
2527
<Compile Include="LocalStorageBenchmark.cs" />
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using nanoFramework.Benchmark;
2+
using nanoFramework.Benchmark.Attributes;
3+
using System;
4+
5+
namespace TelemetryStash.Services.Benchmarks
6+
{
7+
[ConsoleParser]
8+
[IterationCount(10)]
9+
public class FrameworkBenchmark2
10+
{
11+
const UInt16 About65k = UInt16.MaxValue;
12+
13+
[Benchmark, Baseline]
14+
public void FillArray_Int32()
15+
{
16+
var until = (int)About65k;
17+
var array = new int[until];
18+
for (int j = 0; j < until; j++)
19+
{
20+
array[j] = j;
21+
}
22+
}
23+
24+
[Benchmark]
25+
public void FillArray_UInt16()
26+
{
27+
var array = new UInt16[About65k];
28+
for (UInt16 j = 0; j < About65k; j++)
29+
{
30+
array[j] = j;
31+
}
32+
}
33+
34+
[Benchmark]
35+
public void FillArray_Int64()
36+
{
37+
var until = (Int64)About65k;
38+
var array = new Int64[until];
39+
for (long j = 0; j < until; j++)
40+
{
41+
array[j] = j;
42+
}
43+
}
44+
}
45+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using nanoFramework.Benchmark;
2+
using nanoFramework.Benchmark.Attributes;
3+
using System;
4+
using System.Diagnostics;
5+
using TelemetryStash.NfClient.Services;
6+
using TelemetryStash.Shared;
7+
8+
namespace TelemetryStash.Services.Benchmarks
9+
{
10+
[ConsoleParser]
11+
[IterationCount(1)]
12+
public class FrameworkBenchmark3
13+
{
14+
const UInt16 About65k = UInt16.MaxValue;
15+
16+
[Benchmark, Baseline]
17+
public void FillArray_Int32()
18+
{
19+
PrintMemoryMetrics("Pre FillArray_Int32");
20+
var until = (int)About65k;
21+
var array = new int[until];
22+
for (int i = 0; i < until; i++)
23+
{
24+
array[i] = i;
25+
}
26+
PrintMemoryMetrics("Post FillArray_Int32");
27+
}
28+
29+
[Benchmark]
30+
public void FillArray_UInt16()
31+
{
32+
PrintMemoryMetrics("Pre FillArray_UInt16");
33+
var array = new UInt16[About65k];
34+
for (UInt16 i = 0; i < About65k; i++)
35+
{
36+
array[i] = i;
37+
}
38+
PrintMemoryMetrics("Post FillArray_UInt16");
39+
}
40+
41+
[Benchmark]
42+
public void FillArray_Int64()
43+
{
44+
PrintMemoryMetrics("Pre FillArray_Int64");
45+
var until = About65k;
46+
var array = new Int64[until];
47+
for (Int64 i = 0; i < About65k; i++)
48+
{
49+
array[i] = i;
50+
}
51+
PrintMemoryMetrics("Post FillArray_Int64");
52+
}
53+
54+
private static void PrintMemoryMetrics(string info)
55+
{
56+
var metrics = DeviceMetrics.GetDeviceMetrics();
57+
var free = ((RegisterSet)metrics[2]).Registers[0].Value;
58+
Debug.WriteLine($"[{info}] Free Memory: {free}");
59+
}
60+
}
61+
}

src/nf-telemetry-clients/Shared/Tests/Client.Services.Benchmarks/Program.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ public static void Main()
1717
Debug.WriteLine("Running nanoFramework benchmarks...");
1818

1919
//BenchmarkRunner.RunClass(typeof(FrameworkBenchmark));
20-
BenchmarkRunner.RunClass(typeof(JsonBenchmark));
20+
//BenchmarkRunner.RunClass(typeof(FrameworkBenchmark2));
21+
BenchmarkRunner.RunClass(typeof(FrameworkBenchmark3));
22+
//BenchmarkRunner.RunClass(typeof(JsonBenchmark));
2123
//BenchmarkRunner.RunClass(typeof(LocalStorageBenchmark));
2224
//BenchmarkRunner.RunClass(typeof(BufferedTelemetryServiceBenchmark));
2325
//BenchmarkRunner.RunClass(typeof(MqttClientBenchmark));

0 commit comments

Comments
 (0)