-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNeopixelChain.cs
More file actions
93 lines (76 loc) · 2.69 KB
/
NeopixelChain.cs
File metadata and controls
93 lines (76 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using nanoFramework.Hardware.Esp32.Rmt;
namespace NFEchoWebSocketServer
{
public class NeopixelChain
{
// 80MHz / 4 => min pulse 0.00us
protected const byte ClockDivider = 4;
// one pulse duration in us
protected const float MinPulse = 1000000.0f / (80000000 / ClockDivider);
// default datasheet values
protected readonly RmtCommand OnePulse =
new RmtCommand((ushort)(0.7 / MinPulse), true, (ushort)(0.6 / MinPulse), false);
protected readonly RmtCommand ZeroPulse =
new RmtCommand((ushort)(0.35 / MinPulse), true, (ushort)(0.8 / MinPulse), false);
protected readonly RmtCommand ResCommand =
new RmtCommand((ushort)(25 / MinPulse), false, (ushort)(26 / MinPulse), false);
protected Color[] Pixels;
private readonly int _gpioPin;
public NeopixelChain(int gpioPin, uint size)
{
_gpioPin = gpioPin;
Pixels = new Color[size];
for (uint i = 0; i < size; ++i)
{
Pixels[i] = new Color();
}
}
public void Update()
{
using (var commandList = new TransmitterChannel(_gpioPin))
{
ConfigureTransmitter(commandList);
for (uint pixel = 0; pixel < Pixels.Length; ++pixel)
{
SerializeColor(Pixels[pixel].G, commandList);
SerializeColor(Pixels[pixel].R, commandList);
SerializeColor(Pixels[pixel].B, commandList);
}
commandList.AddCommand(ResCommand); // RET
commandList.Send(true);
}
}
private void SerializeColor(byte b, TransmitterChannel commandList)
{
for (var i = 0; i < 8; ++i)
{
commandList.AddCommand(((b & (1u << 7)) != 0) ? OnePulse : ZeroPulse);
b <<= 1;
}
}
protected void ConfigureTransmitter(TransmitterChannel commandList)
{
commandList.CarrierEnabled = false;
commandList.ClockDivider = ClockDivider;
commandList.SourceClock = SourceClock.APB;
commandList.IdleLevel = false;
commandList.IsChannelIdle = true;
}
public Color this[uint i]
{
get => Pixels[i];
set => Pixels[i] = value;
}
public void UpdateLed(byte r, byte g, byte b, uint index = 0)
{
this[index] = new Color() { R = r, G = g, B = b};
Update();
}
}
public class Color
{
public byte B;
public byte G;
public byte R;
}
}