-
Notifications
You must be signed in to change notification settings - Fork 594
/
Copy pathGpioController.cs
600 lines (526 loc) · 23.3 KB
/
GpioController.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Device.Gpio.Drivers;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Win32;
namespace System.Device.Gpio;
/// <summary>
/// Represents a general-purpose I/O (GPIO) controller.
/// </summary>
public class GpioController : IDisposable
{
// Constants used to check the hardware on linux
private const string CpuInfoPath = "/proc/cpuinfo";
private const string RaspberryPiHardware = "BCM2835";
// Constants used to check the hardware on Windows
private const string BaseBoardProductRegistryValue = @"SYSTEM\HardwareConfig\Current\BaseBoardProduct";
private const string RaspberryPi2Product = "Raspberry Pi 2";
private const string RaspberryPi3Product = "Raspberry Pi 3";
private const string RaspberryPi5Product = "Raspberry Pi 5";
/// <summary>
/// If a pin element exists, that pin is open. Uses current controller's numbering scheme
/// </summary>
private readonly ConcurrentDictionary<int, PinValue?> _openPins;
private readonly ConcurrentDictionary<int, GpioPin> _gpioPins;
private GpioDriver _driver;
/// <summary>
/// Initializes a new instance of the <see cref="GpioController"/> class that will use the logical pin numbering scheme as default.
/// </summary>
public GpioController()
#pragma warning disable CS0612 // PinNumberingScheme is obsolete
: this(PinNumberingScheme.Logical)
#pragma warning restore CS0612
{
}
/// <summary>
/// Initializes a new instance of the <see cref="GpioController"/> class that will use the specified numbering scheme and driver.
/// </summary>
/// <param name="driver">The driver that manages all of the pin operations for the controller.</param>
public GpioController(GpioDriver driver)
{
_driver = driver;
#pragma warning disable CS0612 // PinNumberingScheme is obsolete
NumberingScheme = PinNumberingScheme.Logical;
#pragma warning restore CS0612
_openPins = new ConcurrentDictionary<int, PinValue?>();
_gpioPins = new ConcurrentDictionary<int, GpioPin>();
}
/// <summary>
/// Initializes a new instance of the <see cref="GpioController"/> class that will use the specified numbering scheme and driver.
/// </summary>
/// <param name="numberingScheme">The numbering scheme used to represent pins provided by the controller.</param>
/// <param name="driver">The driver that manages all of the pin operations for the controller.</param>
[Obsolete]
public GpioController(PinNumberingScheme numberingScheme, GpioDriver driver)
{
_driver = driver;
NumberingScheme = numberingScheme;
_openPins = new ConcurrentDictionary<int, PinValue?>();
_gpioPins = new ConcurrentDictionary<int, GpioPin>();
}
/// <summary>
/// Initializes a new instance of the <see cref="GpioController"/> class that will use the specified numbering scheme.
/// The controller will default to use the driver that best applies given the platform the program is executing on.
/// </summary>
/// <param name="numberingScheme">The numbering scheme used to represent pins provided by the controller.</param>
[Obsolete]
public GpioController(PinNumberingScheme numberingScheme)
: this(numberingScheme, GetBestDriverForBoard())
{
}
/// <summary>
/// The numbering scheme used to represent pins provided by the controller.
/// </summary>
[Obsolete]
public PinNumberingScheme NumberingScheme { get; }
/// <summary>
/// The number of pins provided by the controller.
/// </summary>
public virtual int PinCount
{
get
{
CheckDriverValid();
return _driver.PinCount;
}
}
/// <summary>
/// Returns the collection of open pins
/// </summary>
private IEnumerable<GpioPin> OpenPins
{
get
{
return _gpioPins.Values;
}
}
/// <summary>
/// Gets the logical pin number in the controller's numbering scheme.
/// </summary>
/// <param name="pinNumber">The pin number</param>
/// <returns>The logical pin number in the controller's numbering scheme.</returns>
protected virtual int GetLogicalPinNumber(int pinNumber)
{
#pragma warning disable CS0612 // PinNumberingScheme is obsolete
return (NumberingScheme == PinNumberingScheme.Logical) ? pinNumber : _driver.ConvertPinNumberToLogicalNumberingScheme(pinNumber);
#pragma warning restore CS0612
}
/// <summary>
/// Opens a pin in order for it to be ready to use.
/// The driver attempts to open the pin without changing its mode or value.
/// </summary>
/// <param name="pinNumber">The pin number in the controller's numbering scheme.</param>
public GpioPin OpenPin(int pinNumber)
{
if (IsPinOpen(pinNumber))
{
return _gpioPins[pinNumber];
}
OpenPinCore(pinNumber);
_openPins.TryAdd(pinNumber, null);
_gpioPins[pinNumber] = new GpioPin(pinNumber, this);
return _gpioPins[pinNumber];
}
/// <summary>
/// Opens a pin in order for it to be ready to use.
/// </summary>
/// <param name="pinNumber">The pin number in the controller's numbering scheme.</param>
protected virtual void OpenPinCore(int pinNumber)
{
int logicalPinNumber = GetLogicalPinNumber(pinNumber);
_driver.OpenPin(logicalPinNumber);
}
/// <summary>
/// Opens a pin and sets it to a specific mode.
/// </summary>
/// <param name="pinNumber">The pin number in the controller's numbering scheme.</param>
/// <param name="mode">The mode to be set.</param>
public GpioPin OpenPin(int pinNumber, PinMode mode)
{
var pin = OpenPin(pinNumber);
SetPinMode(pinNumber, mode);
return pin;
}
/// <summary>
/// Opens a pin and sets it to a specific mode and value.
/// </summary>
/// <param name="pinNumber">The pin number in the controller's numbering scheme.</param>
/// <param name="mode">The mode to be set.</param>
/// <param name="initialValue">The initial value to be set if the mode is output. The driver will attempt to set the mode without causing glitches to the other value.
/// (if <paramref name="initialValue"/> is <see cref="PinValue.High"/>, the pin should not glitch to low during open)</param>
public GpioPin OpenPin(int pinNumber, PinMode mode, PinValue initialValue)
{
var pin = OpenPin(pinNumber);
// Set the desired initial value
_openPins[pinNumber] = initialValue;
SetPinMode(pinNumber, mode);
return pin;
}
/// <summary>
/// Closes an open pin.
/// If allowed by the driver, the state of the pin is not changed.
/// </summary>
/// <param name="pinNumber">The pin number in the controller's numbering scheme.</param>
public void ClosePin(int pinNumber)
{
if (!IsPinOpen(pinNumber))
{
throw new InvalidOperationException($"Can not close pin {pinNumber} because it is not open.");
}
ClosePinCore(pinNumber);
_openPins.TryRemove(pinNumber, out _);
}
/// <summary>
/// Closes an open pin.
/// </summary>
/// <param name="pinNumber">The pin number in the controller's numbering scheme.</param>
protected virtual void ClosePinCore(int pinNumber)
{
int logicalPinNumber = GetLogicalPinNumber(pinNumber);
_driver.ClosePin(logicalPinNumber);
_gpioPins.TryRemove(pinNumber, out _);
}
/// <summary>
/// Sets the mode to a pin.
/// </summary>
/// <param name="pinNumber">The pin number in the controller's numbering scheme.</param>
/// <param name="mode">The mode to be set.</param>
public virtual void SetPinMode(int pinNumber, PinMode mode)
{
if (!IsPinOpen(pinNumber))
{
throw new InvalidOperationException($"Can not set a mode to pin {pinNumber} because it is not open.");
}
int logicalPinNumber = GetLogicalPinNumber(pinNumber);
if (!IsPinModeSupported(pinNumber, mode))
{
throw new InvalidOperationException($"Pin {pinNumber} does not support mode {mode}.");
}
if (_openPins.TryGetValue(pinNumber, out var desired) && desired.HasValue)
{
_driver.SetPinMode(logicalPinNumber, mode, desired.Value);
}
else
{
_driver.SetPinMode(logicalPinNumber, mode);
}
}
/// <summary>
/// Gets the mode of a pin.
/// </summary>
/// <param name="pinNumber">The pin number in the controller's numbering scheme.</param>
/// <returns>The mode of the pin.</returns>
public virtual PinMode GetPinMode(int pinNumber)
{
if (!IsPinOpen(pinNumber))
{
throw new InvalidOperationException($"Can not get the mode of pin {pinNumber} because it is not open.");
}
int logicalPinNumber = GetLogicalPinNumber(pinNumber);
return _driver.GetPinMode(logicalPinNumber);
}
/// <summary>
/// Checks if a specific pin is open.
/// </summary>
/// <param name="pinNumber">The pin number in the controller's numbering scheme.</param>
/// <returns>The status if the pin is open or closed.</returns>
public virtual bool IsPinOpen(int pinNumber)
{
CheckDriverValid();
return _openPins.ContainsKey(pinNumber);
}
private void CheckDriverValid()
{
if (_driver == null)
{
throw new ObjectDisposedException(nameof(GpioController));
}
}
/// <summary>
/// Checks if a pin supports a specific mode.
/// </summary>
/// <param name="pinNumber">The pin number in the controller's numbering scheme.</param>
/// <param name="mode">The mode to check.</param>
/// <returns>The status if the pin supports the mode.</returns>
public virtual bool IsPinModeSupported(int pinNumber, PinMode mode)
{
CheckDriverValid();
int logicalPinNumber = GetLogicalPinNumber(pinNumber);
return _driver.IsPinModeSupported(logicalPinNumber, mode);
}
/// <summary>
/// Reads the current value of a pin.
/// </summary>
/// <param name="pinNumber">The pin number in the controller's numbering scheme.</param>
/// <returns>The value of the pin.</returns>
public virtual PinValue Read(int pinNumber)
{
if (!IsPinOpen(pinNumber))
{
throw new InvalidOperationException($"Can not read from pin {pinNumber} because it is not open.");
}
int logicalPinNumber = GetLogicalPinNumber(pinNumber);
return _driver.Read(logicalPinNumber);
}
/// <summary>
/// Toggle the current value of a pin.
/// </summary>
/// <param name="pinNumber">The pin number in the controller's numbering scheme.</param>
public virtual void Toggle(int pinNumber)
{
if (!IsPinOpen(pinNumber))
{
throw new InvalidOperationException($"Can not read from pin {pinNumber} because it is not open.");
}
_driver.Toggle(pinNumber);
}
/// <summary>
/// Writes a value to a pin.
/// </summary>
/// <param name="pinNumber">The pin number in the controller's numbering scheme.</param>
/// <param name="value">The value to be written to the pin.</param>
public virtual void Write(int pinNumber, PinValue value)
{
if (!IsPinOpen(pinNumber))
{
throw new InvalidOperationException($"Can not write to pin {pinNumber} because it is not open.");
}
int logicalPinNumber = GetLogicalPinNumber(pinNumber);
_openPins[pinNumber] = value;
if (_driver.GetPinMode(logicalPinNumber) != PinMode.Output)
{
return;
}
_driver.Write(logicalPinNumber, value);
}
/// <summary>
/// Blocks execution until an event of type eventType is received or a period of time has expired.
/// </summary>
/// <param name="pinNumber">The pin number in the controller's numbering scheme.</param>
/// <param name="eventTypes">The event types to wait for.</param>
/// <param name="timeout">The time to wait for the event.</param>
/// <returns>A structure that contains the result of the waiting operation.</returns>
public WaitForEventResult WaitForEvent(int pinNumber, PinEventTypes eventTypes, TimeSpan timeout)
{
using CancellationTokenSource tokenSource = new CancellationTokenSource(timeout);
return WaitForEvent(pinNumber, eventTypes, tokenSource.Token);
}
/// <summary>
/// Blocks execution until an event of type eventType is received or a cancellation is requested.
/// </summary>
/// <param name="pinNumber">The pin number in the controller's numbering scheme.</param>
/// <param name="eventTypes">The event types to wait for.</param>
/// <param name="cancellationToken">The cancellation token of when the operation should stop waiting for an event.</param>
/// <returns>A structure that contains the result of the waiting operation.</returns>
public virtual WaitForEventResult WaitForEvent(int pinNumber, PinEventTypes eventTypes, CancellationToken cancellationToken)
{
if (!IsPinOpen(pinNumber))
{
throw new InvalidOperationException($"Can not wait for events from pin {pinNumber} because it is not open.");
}
int logicalPinNumber = GetLogicalPinNumber(pinNumber);
return _driver.WaitForEvent(logicalPinNumber, eventTypes, cancellationToken);
}
/// <summary>
/// Async call to wait until an event of type eventType is received or a period of time has expired.
/// </summary>
/// <param name="pinNumber">The pin number in the controller's numbering scheme.</param>
/// <param name="eventTypes">The event types to wait for.</param>
/// <param name="timeout">The time to wait for the event.</param>
/// <returns>A task representing the operation of getting the structure that contains the result of the waiting operation.</returns>
public async ValueTask<WaitForEventResult> WaitForEventAsync(int pinNumber, PinEventTypes eventTypes, TimeSpan timeout)
{
using CancellationTokenSource tokenSource = new CancellationTokenSource(timeout);
return await WaitForEventAsync(pinNumber, eventTypes, tokenSource.Token).ConfigureAwait(false);
}
/// <summary>
/// Async call until an event of type eventType is received or a cancellation is requested.
/// </summary>
/// <param name="pinNumber">The pin number in the controller's numbering scheme.</param>
/// <param name="eventTypes">The event types to wait for.</param>
/// <param name="token">The cancellation token of when the operation should stop waiting for an event.</param>
/// <returns>A task representing the operation of getting the structure that contains the result of the waiting operation</returns>
public virtual ValueTask<WaitForEventResult> WaitForEventAsync(int pinNumber, PinEventTypes eventTypes, CancellationToken token)
{
if (!IsPinOpen(pinNumber))
{
throw new InvalidOperationException($"Can not wait for events from pin {pinNumber} because it is not open.");
}
int logicalPinNumber = GetLogicalPinNumber(pinNumber);
return _driver.WaitForEventAsync(logicalPinNumber, eventTypes, token);
}
/// <summary>
/// Adds a callback that will be invoked when pinNumber has an event of type eventType.
/// </summary>
/// <param name="pinNumber">The pin number in the controller's numbering scheme.</param>
/// <param name="eventTypes">The event types to wait for.</param>
/// <param name="callback">The callback method that will be invoked.</param>
public virtual void RegisterCallbackForPinValueChangedEvent(int pinNumber, PinEventTypes eventTypes, PinChangeEventHandler callback)
{
if (!IsPinOpen(pinNumber))
{
throw new InvalidOperationException($"Can not add callback for pin {pinNumber} because it is not open.");
}
int logicalPinNumber = GetLogicalPinNumber(pinNumber);
_driver.AddCallbackForPinValueChangedEvent(logicalPinNumber, eventTypes, callback);
}
/// <summary>
/// Removes a callback that was being invoked for pin at pinNumber.
/// </summary>
/// <param name="pinNumber">The pin number in the controller's numbering scheme.</param>
/// <param name="callback">The callback method that will be invoked.</param>
public virtual void UnregisterCallbackForPinValueChangedEvent(int pinNumber, PinChangeEventHandler callback)
{
if (!IsPinOpen(pinNumber))
{
throw new InvalidOperationException($"Can not remove callback for pin {pinNumber} because it is not open.");
}
int logicalPinNumber = GetLogicalPinNumber(pinNumber);
_driver.RemoveCallbackForPinValueChangedEvent(logicalPinNumber, callback);
}
/// <summary>
/// Disposes this instance and closes all open pins associated with this controller.
/// </summary>
/// <param name="disposing">True to dispose all instances, false to dispose only unmanaged resources</param>
protected virtual void Dispose(bool disposing)
{
foreach (int pin in _openPins.Keys)
{
// The list contains the pin in the current NumberingScheme
ClosePinCore(pin);
}
_openPins.Clear();
_gpioPins.Clear();
_driver?.Dispose();
_driver = null!;
}
/// <inheritdoc/>
public void Dispose()
{
Dispose(true);
}
/// <summary>
/// Write the given pins with the given values.
/// </summary>
/// <param name="pinValuePairs">The pin/value pairs to write.</param>
public void Write(ReadOnlySpan<PinValuePair> pinValuePairs)
{
for (int i = 0; i < pinValuePairs.Length; i++)
{
Write(pinValuePairs[i].PinNumber, pinValuePairs[i].PinValue);
}
}
/// <summary>
/// Read the given pins with the given pin numbers.
/// </summary>
/// <param name="pinValuePairs">The pin/value pairs to read.</param>
public void Read(Span<PinValuePair> pinValuePairs)
{
for (int i = 0; i < pinValuePairs.Length; i++)
{
int pin = pinValuePairs[i].PinNumber;
pinValuePairs[i] = new PinValuePair(pin, Read(pin));
}
}
/// <summary>
/// Tries to create the GPIO driver that best matches the current hardware
/// </summary>
/// <returns>An instance of a GpioDriver that best matches the current hardware</returns>
/// <exception cref="PlatformNotSupportedException">No matching driver could be found</exception>
private static GpioDriver GetBestDriverForBoard()
{
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
return GetBestDriverForBoardOnWindows();
}
else
{
return GetBestDriverForBoardOnLinux();
}
}
/// <summary>
/// Attempt to get the best applicable driver for the board the program is executing on.
/// </summary>
/// <returns>A driver that works with the board the program is executing on.</returns>
private static GpioDriver GetBestDriverForBoardOnLinux()
{
var boardInfo = RaspberryBoardInfo.LoadBoardInfo();
switch (boardInfo.BoardModel)
{
case RaspberryBoardInfo.Model.RaspberryPi3B:
case RaspberryBoardInfo.Model.RaspberryPi3APlus:
case RaspberryBoardInfo.Model.RaspberryPi3BPlus:
case RaspberryBoardInfo.Model.RaspberryPiZeroW:
case RaspberryBoardInfo.Model.RaspberryPiZero2W:
case RaspberryBoardInfo.Model.RaspberryPi4:
case RaspberryBoardInfo.Model.RaspberryPi400:
case RaspberryBoardInfo.Model.RaspberryPiComputeModule4:
case RaspberryBoardInfo.Model.RaspberryPiComputeModule3:
RaspberryPi3LinuxDriver? internalDriver = RaspberryPi3Driver.CreateInternalRaspberryPi3LinuxDriver(out _);
if (internalDriver is object)
{
return new RaspberryPi3Driver(internalDriver);
}
return UnixDriver.Create();
case RaspberryBoardInfo.Model.RaspberryPi5:
// For now, for Raspberry Pi 5, we'll use the LibGpiodDriver.
// We need to create a new driver for the Raspberry Pi 5,
// because the Raspberry Pi 5 uses an entirely different GPIO controller (RP1)
return new LibGpiodDriver(4);
default:
return UnixDriver.Create();
}
}
/// <summary>
/// Attempt to get the best applicable driver for the board the program is executing on.
/// </summary>
/// <returns>A driver that works with the board the program is executing on.</returns>
/// <remarks>
/// This really feels like it needs a driver-based pattern, where each driver exposes a static method:
/// public static bool IsSpecificToCurrentEnvironment { get; }
/// The GpioController could use reflection to find all GpioDriver-derived classes and call this
/// static method to determine if the driver considers itself to be the best match for the environment.
/// </remarks>
private static GpioDriver GetBestDriverForBoardOnWindows()
{
#pragma warning disable CA1416 // Registry.LocalMachine is only supported on Windows, but we will only hit this method if we are on Windows.
string? baseBoardProduct = Registry.LocalMachine.GetValue(BaseBoardProductRegistryValue, string.Empty)?.ToString();
#pragma warning restore CA1416
if (baseBoardProduct is null)
{
throw new Exception("Single board computer type cannot be detected.");
}
if (baseBoardProduct == RaspberryPi3Product || baseBoardProduct.StartsWith($"{RaspberryPi3Product} ") ||
baseBoardProduct == RaspberryPi2Product || baseBoardProduct.StartsWith($"{RaspberryPi2Product} "))
{
return new RaspberryPi3Driver();
}
// Default for Windows IoT Core on a non-specific device
throw new PlatformNotSupportedException();
}
/// <summary>
/// Query information about a component and its children.
/// </summary>
/// <returns>A tree of <see cref="ComponentInformation"/> instances.</returns>
/// <remarks>
/// The returned data structure (or rather, its string representation) can be used to diagnose problems with incorrect driver types or
/// other system configuration problems.
/// This method is currently reserved for debugging purposes. Its behavior its and signature are subject to change.
/// </remarks>
public virtual ComponentInformation QueryComponentInformation()
{
ComponentInformation self = new ComponentInformation(this, "Generic GPIO Controller");
if (_driver != null)
{
ComponentInformation driverInfo = _driver.QueryComponentInformation();
self.AddSubComponent(driverInfo);
}
// PinCount is not added on purpose, because the property throws NotSupportedException on some hardware
self.Properties["OpenPins"] = string.Join(", ", _openPins.Select(x => x.Key));
return self;
}
}