-
-
Notifications
You must be signed in to change notification settings - Fork 106
/
CharlieplexSegment.cs
289 lines (251 loc) · 10.4 KB
/
CharlieplexSegment.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Device.Gpio;
using System.Diagnostics;
using System.Threading;
using Iot.Device.Multiplexing.Utility;
namespace Iot.Device.Multiplexing
{
/// <summary>
/// Provides support for Charlieplex multiplexing.
/// https://wikipedia.org/wiki/Charlieplexing
/// </summary>
public class CharlieplexSegment : IOutputSegment, IDisposable
{
private readonly bool _shouldDispose;
private readonly int[] _pins;
private readonly CharlieplexSegmentNode[] _nodes;
private readonly int _nodeCount;
private readonly VirtualOutputSegment _segment;
private GpioController _gpioController;
private CharlieplexSegmentNode _lastNode;
/// <summary>
/// Initializes a new Charlieplex type that can be use for multiplex over a relatively small number of GPIO pins.
/// </summary>
/// <param name="pins">The set of pins to use.</param>
/// <param name="nodeCount">The count of nodes (like LEDs) that will be addressable. If 0, then the Charlieplex maximum is used for the pins provided (n^2-n).</param>
/// <param name="gpioController">The GPIO Controller used for interrupt handling.</param>
/// <param name="shouldDispose">True (the default) if the GPIO controller shall be disposed when disposing this instance.</param>
public CharlieplexSegment(int[] pins, int nodeCount = 0, GpioController? gpioController = null, bool shouldDispose = true)
{
if (pins.Length < 2)
{
throw new ArgumentException(nameof(CharlieplexSegment), "2 or more pins must be provided.");
}
int charlieCount = (pins.Length * pins.Length) - pins.Length;
if (nodeCount > charlieCount)
{
throw new ArgumentException(nameof(CharlieplexSegment), $"Maximum count is {charlieCount} based on {pins.Length} pins. {nodeCount} was specified as the count.");
}
if (nodeCount == 0)
{
nodeCount = charlieCount;
}
_shouldDispose = shouldDispose || gpioController is null;
_gpioController = gpioController ?? new();
// first two pins will be needed as Output.
_gpioController.OpenPin(pins[0], PinMode.Output);
_gpioController.OpenPin(pins[1], PinMode.Output);
// remaining pins should be input type
// prevents participating in the circuit until needed
for (int i = 2; i < pins.Length; i++)
{
_gpioController.OpenPin(pins[i], PinMode.Input);
}
_lastNode = new CharlieplexSegmentNode()
{
Anode = pins[1],
Cathode = pins[0]
};
_pins = pins;
_nodeCount = nodeCount;
_nodes = GetNodes(pins, nodeCount);
_segment = new VirtualOutputSegment(_nodeCount);
}
/// <summary>
/// Provides the set of Charlie nodes given the set of pins and the count provided.
/// If count = 0, then the Charlieplex maximum is used for the pins provided (n^2-n).
/// </summary>
/// <param name="pins">The pins to use for the segment.</param>
/// <param name="nodeCount">The number of nodes to use. Default is the Charlieplex maximum.</param>
public static CharlieplexSegmentNode[] GetNodes(int[] pins, int nodeCount = 0)
{
int pinCount = pins.Length;
if (nodeCount == 0)
{
nodeCount = (int)Math.Pow(pinCount, 2.0) - pinCount;
}
CharlieplexSegmentNode[] nodes = new CharlieplexSegmentNode[nodeCount];
int pin = 0;
int pinJump = 1;
int resetCount = pinCount - 1;
bool firstLeg = false;
for (int i = 0; i < nodeCount; i++)
{
if ((pin > 0 && pin % resetCount == 0) || pin + pinJump > resetCount)
{
pin = 0;
pinJump++;
}
CharlieplexSegmentNode node = new CharlieplexSegmentNode();
if (!firstLeg)
{
node.Anode = pins[pin];
node.Cathode = pins[pin + pinJump];
firstLeg = true;
}
else
{
node.Anode = pins[pin + pinJump];
node.Cathode = pins[pin];
firstLeg = false;
pin++;
}
nodes[i] = node;
}
return nodes;
}
/// <summary>
/// The number of nodes (like LEDs) that can be addressed.
/// </summary>
public int NodeCount => _nodeCount;
/// <summary>
/// Write a PinValue to a node, to update Charlieplex segment.
/// Address scheme is 0-based. Given 8 nodes, addresses would be 0-7.
/// Displays nodes in their updated configuration for the specified duration.
/// </summary>
/// <param name="node">Node to update.</param>
/// <param name="value">Value to write.</param>
/// <param name="duration">Time to display segment, in milliseconds (default is 0; not displayed).</param>
public void Write(int node, PinValue value, TimeSpan duration = default(TimeSpan))
{
_nodes[node].Value = value;
if (duration == default(TimeSpan))
{
return;
}
using CancellationTokenSource cts = new CancellationTokenSource(duration);
Display(cts.Token);
}
/// <summary>
/// Displays nodes in their current configuration for the specified duration.
/// </summary>
/// <param name="token">CancellationToken used to signal when method should exit.</param>
public void Display(CancellationToken token)
{
/*
Cases to consider
node.Cathode == _lastNode.Cathode
node.Cathode == _lastNode.Anode -- drop low
node.Anode == _lastNode.Cathode
node.Anode == _lastNode.Anode
node.Anode != _lastNode.Cathode | _lastNode.Anode
node.Cathode != _lastNode.Cathode | _lastNode.Anode
*/
while (!token.IsCancellationRequested)
{
for (int i = 0; i < _nodes.Length; i++)
{
CharlieplexSegmentNode node = _nodes[i];
// skip updating pinmode when possible
if (_lastNode.Anode != node.Anode && _lastNode.Anode != node.Cathode)
{
_gpioController.SetPinMode(_lastNode.Anode, PinMode.Input);
}
if (_lastNode.Cathode != node.Anode && _lastNode.Cathode != node.Cathode)
{
_gpioController.SetPinMode(_lastNode.Cathode, PinMode.Input);
}
if (node.Cathode != _lastNode.Anode && node.Cathode != _lastNode.Cathode)
{
_gpioController.SetPinMode(node.Cathode, PinMode.Output);
}
if (node.Anode != _lastNode.Anode && node.Anode != _lastNode.Cathode)
{
_gpioController.SetPinMode(node.Anode, PinMode.Output);
}
_gpioController.Write(node.Anode, node.Value);
// It is necessary to sleep for the LED to be seen with full brightness
Thread.SpinWait(1);
_gpioController.Write(node.Anode, 0);
_lastNode.Anode = node.Anode;
_lastNode.Cathode = node.Cathode;
}
}
}
/// <summary>
/// Cleanup.
/// Failing to dispose this class, especially when callbacks are active, may lead to undefined behavior.
/// </summary>
public void Dispose()
{
// this condition only applies to GPIO devices
if (_shouldDispose)
{
_gpioController?.Dispose();
_gpioController = null!;
}
}
// IOutputSegment Implementation
// Only supported when shift register is connected with GPIO
/// <summary>
/// The length of the segment; the number of GPIO pins it exposes.
/// </summary>
public int Length => _segment.Length;
/// <summary>
/// Segment values.
/// </summary>
PinValue IOutputSegment.this[int index]
{
get => _segment[index];
set => _segment[index] = value;
}
/// <summary>
/// Writes a PinValue to a virtual segment.
/// Does not display output.
/// </summary>
void IOutputSegment.Write(int index, PinValue value)
{
_segment[index] = value;
}
/// <summary>
/// Writes discrete underlying bits to a virtual segment.
/// Writes each bit, left to right. Least significant bit will written to index 0.
/// Does not display output.
/// </summary>
void IOutputSegment.Write(byte value)
{
_segment.Write(value);
}
/// <summary>
/// Writes discrete underlying bits to a virtual output.
/// Writes each byte, left to right. Least significant bit will written to index 0.
/// Does not display output.
/// </summary>
void IOutputSegment.Write(SpanByte value)
{
_segment.Write(value);
}
/// <summary>
/// Clears shift register.
/// Performs a latch.
/// </summary>
void IOutputSegment.TurnOffAll()
{
_segment.TurnOffAll();
}
/// <summary>
/// Displays current state of segment.
/// Segment is displayed at least until token receives a cancellation signal, possibly due to a specified duration expiring.
/// </summary>
void IOutputSegment.Display(CancellationToken token)
{
for (int i = 0; i < _segment.Length; i++)
{
_nodes[i].Value = _segment[i];
}
Display(token);
}
}
}