-
Notifications
You must be signed in to change notification settings - Fork 585
/
LibGpiodDriver.cs
176 lines (150 loc) · 6.39 KB
/
LibGpiodDriver.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics.CodeAnalysis;
using System.Threading;
namespace System.Device.Gpio.Drivers;
/// <summary>
/// This driver uses libgpiod to get user-level access to the GPIO ports.
/// It supersedes the SysFsDriver, but requires that libgpiod is installed.
/// </summary>
public class LibGpiodDriver : UnixDriver
{
private GpioDriver _driver;
/// <summary>
/// Creates an instance of the LibGpiodDriver
/// </summary>
/// <param name="gpioChip">The number of the GPIO chip to drive</param>
/// <remarks>
/// The driver version is chosen based on the installed libgpiod version on the system. To select a specific library version use the
/// other constructor or specify the environment variable DOTNET_IOT_LIBGPIOD_DRIVER_VERSION, see documentation
/// </remarks>
public LibGpiodDriver(int gpioChip = 0)
{
#pragma warning disable SDGPIO0001 // Suppressing diagnostic for using experimental APIs from this same repository.
LibGpiodDriverFactory.VersionedLibgpiodDriver versionedLibgpiodDriver = LibGpiodDriverFactory.Instance.Create(gpioChip);
_driver = versionedLibgpiodDriver.LibGpiodDriver;
Version = versionedLibgpiodDriver.DriverVersion;
#pragma warning restore SDGPIO0001 // Suppressing diagnostic for using experimental APIs from this same repository.
}
/// <summary>
/// Creates an instance of the LigGpiodDriver that targets specific version/s of libgpiod
/// <see cref="LibGpiodDriverVersion.V1"/> supports libgpiod.so.1.x to libgpiod.so.2.x
/// and <see cref="LibGpiodDriverVersion.V2"/> supports libgpiod.so.3.x
/// </summary>
/// <param name="gpioChip">The number of the GPIO chip to drive</param>
/// <param name="driverVersion">Version of the libgpiod driver to create</param>
/// <remarks>Alternatively, specify the environment variable DOTNET_IOT_LIBGPIOD_DRIVER_VERSION, see documentation</remarks>
[Experimental(DiagnosticIds.SDGPIO0001, UrlFormat = DiagnosticIds.UrlFormat)]
public LibGpiodDriver(int gpioChip, LibGpiodDriverVersion driverVersion)
{
LibGpiodDriverFactory.VersionedLibgpiodDriver versionedLibgpiodDriver = LibGpiodDriverFactory.Instance.Create(gpioChip, driverVersion);
_driver = versionedLibgpiodDriver.LibGpiodDriver;
Version = versionedLibgpiodDriver.DriverVersion;
}
/// <summary>
/// Version of the libgpiod driver
/// </summary>
[Experimental(DiagnosticIds.SDGPIO0001, UrlFormat = DiagnosticIds.UrlFormat)]
public LibGpiodDriverVersion Version { get; protected set; }
/// <summary>
/// A collection of driver versions that correspond to the installed versions of libgpiod on this system. Each driver is dependent
/// on specific libgpiod version/s. If the collection is empty, it indicates that libgpiod might not be installed or could not be detected.
/// </summary>
[Experimental(DiagnosticIds.SDGPIO0001, UrlFormat = DiagnosticIds.UrlFormat)]
public static LibGpiodDriverVersion[] GetAvailableVersions() => LibGpiodDriverFactory.Instance.DriverCandidates;
/// <inheritdoc/>
protected internal override int PinCount => GetDriver().PinCount;
/// <inheritdoc/>
protected internal override void AddCallbackForPinValueChangedEvent(int pinNumber, PinEventTypes eventTypes, PinChangeEventHandler callback)
{
GetDriver().AddCallbackForPinValueChangedEvent(pinNumber, eventTypes, callback);
}
/// <inheritdoc/>
protected internal override void ClosePin(int pinNumber)
{
GetDriver().ClosePin(pinNumber);
}
/// <inheritdoc/>
protected internal override int ConvertPinNumberToLogicalNumberingScheme(int pinNumber)
{
return GetDriver().ConvertPinNumberToLogicalNumberingScheme(pinNumber);
}
/// <inheritdoc/>
protected internal override PinMode GetPinMode(int pinNumber)
{
return GetDriver().GetPinMode(pinNumber);
}
/// <inheritdoc/>
protected internal override bool IsPinModeSupported(int pinNumber, PinMode mode)
{
return GetDriver().IsPinModeSupported(pinNumber, mode);
}
/// <inheritdoc/>
protected internal override void OpenPin(int pinNumber)
{
GetDriver().OpenPin(pinNumber);
}
/// <inheritdoc/>
protected internal override PinValue Read(int pinNumber)
{
return GetDriver().Read(pinNumber);
}
/// <inheritdoc/>
protected internal override void Toggle(int pinNumber)
{
GetDriver().Toggle(pinNumber);
}
/// <inheritdoc/>
protected internal override void RemoveCallbackForPinValueChangedEvent(int pinNumber, PinChangeEventHandler callback)
{
GetDriver().RemoveCallbackForPinValueChangedEvent(pinNumber, callback);
}
/// <inheritdoc/>
protected internal override void SetPinMode(int pinNumber, PinMode mode)
{
GetDriver().SetPinMode(pinNumber, mode);
}
/// <inheritdoc />
protected internal override void SetPinMode(int pinNumber, PinMode mode, PinValue initialValue)
{
GetDriver().SetPinMode(pinNumber, mode, initialValue);
}
/// <inheritdoc/>
protected internal override WaitForEventResult WaitForEvent(int pinNumber, PinEventTypes eventTypes, CancellationToken cancellationToken)
{
return GetDriver().WaitForEvent(pinNumber, eventTypes, cancellationToken);
}
/// <inheritdoc/>
protected internal override void Write(int pinNumber, PinValue value)
{
GetDriver().Write(pinNumber, value);
}
/// <inheritdoc />
public override ComponentInformation QueryComponentInformation()
{
var ret = new ComponentInformation(this, "Libgpiod Wrapper driver");
#pragma warning disable SDGPIO0001
ret.Properties.Add("Version", Version.ToString());
ret.AddSubComponent(GetDriver().QueryComponentInformation());
#pragma warning restore SDGPIO0001
return ret;
}
private GpioDriver GetDriver()
{
if (_driver == null)
{
throw new ObjectDisposedException(nameof(LibGpiodDriver));
}
return _driver;
}
/// <inheritdoc/>
protected override void Dispose(bool disposing)
{
if (_driver != null && disposing)
{
_driver.Dispose();
_driver = null!;
}
base.Dispose(disposing);
}
}