-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathWS281x.cs
178 lines (157 loc) · 5.55 KB
/
WS281x.cs
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
using System;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using WS281x.Native;
namespace WS281x
{
/// <summary>
/// Wrapper class to controll WS281x LEDs
/// </summary>
public class WS281x : IDisposable
{
private ws2811_t _ws2811;
private GCHandle _ws2811Handle;
private bool _isDisposingAllowed;
/// <summary>
/// Initialize the wrapper
/// </summary>
/// <param name="settings">Settings used for initialization</param>
public WS281x(Settings settings)
{
//_ws2811Handle = GCHandle.Alloc(_ws2811, GCHandleType.Pinned);
_ws2811 = new ws2811_t();
//Pin the object in memory. Otherwise GC will probably move the object to another memory location.
//This would cause errors because the native library has a pointer on the memory location of the object.
_ws2811Handle = GCHandle.Alloc(_ws2811, GCHandleType.Pinned);
_ws2811.dmanum = settings.DMAChannel;
_ws2811.freq = settings.Frequency;
_ws2811.channel1 = new ws2811_channel_t();
InitChannel(settings.Channel);
// for(int i=0; i< _ws2811.channel.Length; i++)
// {
// if(settings.Channels[i] != null)
// {
// InitChannel(i, settings.Channels[i]);
// }
// }
Settings = settings;
var initResult = PInvoke.ws2811_init(ref _ws2811);
if (initResult != ws2811_return_t.WS2811_SUCCESS)
{
var returnMessage = GetMessageForStatusCode(initResult);
throw new Exception(String.Format("Error while initializing.{0}Error code: {1}{0}Message: {2}", Environment.NewLine, initResult.ToString(), returnMessage));
}
//Disposing is only allowed if the init was successfull.
//Otherwise the native cleanUp function throws an error.
_isDisposingAllowed = true;
}
/// <summary>
/// Renders the content of the channels
/// </summary>
public void Render()
{
// for(int i=0; i< Settings.Channels.Length; i++)
// {
// if (Settings.Channels[i] != null)
// {
// var ledColor = Settings.Channels[i].Leds.Select(x => x.RGBValue).ToArray();
// Marshal.Copy(ledColor, 0, _ws2811.channel[i].leds, ledColor.Count());
// }
// }
var ledColor = Settings.Channel.Leds.Select(x => x.RGBValue).ToArray();
Marshal.Copy(ledColor, 0, _ws2811.channel1.leds, ledColor.Count());
var result = PInvoke.ws2811_render(ref _ws2811);
if (result != ws2811_return_t.WS2811_SUCCESS)
{
var returnMessage = GetMessageForStatusCode(result);
throw new Exception(String.Format("Error while rendering.{0}Error code: {1}{0}Message: {2}", Environment.NewLine, result.ToString(), returnMessage));
}
}
/// <summary>
/// Sets the color of a given LED
/// </summary>
/// <param name="channelIndex">Channel which controls the LED</param>
/// <param name="ledID">ID/Index of the LED</param>
/// <param name="color">New color</param>
public void SetLEDColor(int ledID, Color color)
{
Settings.Channel.Leds[ledID].Color = color;
}
public void SetColorOnAllLEDs(Color color)
{
for(int i = 0 ; i < Settings.Channel.Leds.Count ; ++i)
{
Settings.Channel.Leds[i].Color = color;
}
}
/// <summary>
/// Returns the settings which are used to initialize the component
/// </summary>
public Settings Settings { get; private set; }
/// <summary>
/// Initialize the channel properties
/// </summary>
/// <param name="channelIndex">Index of the channel to initialize</param>
/// <param name="channelSettings">Settings for the channel</param>
private void InitChannel(Channel channelSettings)
{
_ws2811.channel1.count = channelSettings.Leds.Count;
_ws2811.channel1.gpionum = channelSettings.GPIOPin;
_ws2811.channel1.brightness = channelSettings.Brightness;
_ws2811.channel1.invert = Convert.ToInt32(channelSettings.Invert);
if(channelSettings.StripType != StripType.Unknown)
{
//Strip type is set by the native assembly if not explicitly set.
//This type defines the ordering of the colors e. g. RGB or GRB, ...
_ws2811.channel1.strip_type = (int)channelSettings.StripType;
}
}
/// <summary>
/// Returns the error message for the given status code
/// </summary>
/// <param name="statusCode">Status code to resolve</param>
/// <returns></returns>
private string GetMessageForStatusCode(ws2811_return_t statusCode)
{
var strPointer = PInvoke.ws2811_get_return_t_str((int)statusCode);
return Marshal.PtrToStringAuto(strPointer);
}
#region IDisposable Support
private bool disposedValue = false; // To detect redundant calls
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
// TODO: dispose managed state (managed objects).
}
// TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
// TODO: set large fields to null.
if(_isDisposingAllowed)
{
PInvoke.ws2811_fini(ref _ws2811);
_ws2811Handle.Free(); //until I find a way to successfully allocate the resource, this will throw exception
_isDisposingAllowed = false;
}
disposedValue = true;
}
}
// TODO: override a finalizer only if Dispose(bool disposing) above has code to free unmanaged resources.
~WS281x()
{
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
Dispose(false);
}
// This code added to correctly implement the disposable pattern.
public void Dispose()
{
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
Dispose(true);
// TODO: uncomment the following line if the finalizer is overridden above.
// GC.SuppressFinalize(this);
}
#endregion
}
}