|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System.Collections; |
| 6 | +using System.IO; |
| 7 | + |
| 8 | +namespace System.Device.Gpio |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// Represents a general-purpose I/O (GPIO) controller. |
| 12 | + /// </summary> |
| 13 | + public sealed class GpioController : IDisposable |
| 14 | + { |
| 15 | + private Windows.Devices.Gpio.GpioController _controller; |
| 16 | + private static Windows.Devices.Gpio.GpioPin[] _gpioPins; |
| 17 | + private static PinEventTypes[] _gpioEvents; |
| 18 | + private static PinChangeEventHandler[] _gpioPinChange; |
| 19 | + private static PinEventTypes[] _gpioEventsHappening; |
| 20 | + |
| 21 | + // this is used as the lock object |
| 22 | + // a lock is required because multiple threads can access the GPIO controller |
| 23 | + static object _syncLock; |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// Initializes a new instance of the System.Device.Gpio.GpioController class that |
| 27 | + /// will use the logical pin numbering scheme as default. |
| 28 | + /// </summary> |
| 29 | + public GpioController() |
| 30 | + { |
| 31 | + GetController(); |
| 32 | + } |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Initializes a new instance of the System.Device.Gpio.GpioController class that |
| 36 | + /// will use the specified numbering scheme. The controller will default to use the |
| 37 | + /// driver that best applies given the platform the program is executing on. |
| 38 | + /// </summary> |
| 39 | + /// <param name="numberingScheme">The numbering scheme used to represent pins provided by the controller.</param> |
| 40 | + public GpioController(PinNumberingScheme numberingScheme) : this() |
| 41 | + { |
| 42 | + NumberingScheme = numberingScheme; |
| 43 | + } |
| 44 | + |
| 45 | + private void GetController() |
| 46 | + { |
| 47 | + if (_syncLock == null) |
| 48 | + { |
| 49 | + _syncLock = new object(); |
| 50 | + } |
| 51 | + |
| 52 | + lock (_syncLock) |
| 53 | + { |
| 54 | + _controller = Windows.Devices.Gpio.GpioController.GetDefault(); |
| 55 | + if (_gpioPins == null) |
| 56 | + { |
| 57 | + _gpioPins = new Windows.Devices.Gpio.GpioPin[_controller.PinCount]; |
| 58 | + _gpioEvents = new PinEventTypes[_controller.PinCount]; |
| 59 | + _gpioEventsHappening = new PinEventTypes[_controller.PinCount]; |
| 60 | + _gpioPinChange = new PinChangeEventHandler[_controller.PinCount]; |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + /// <summary> |
| 66 | + /// The numbering scheme used to represent pins provided by the controller. |
| 67 | + /// </summary> |
| 68 | + public PinNumberingScheme NumberingScheme { get; internal set; } |
| 69 | + |
| 70 | + /// <summary> |
| 71 | + /// The number of pins provided by the controller. |
| 72 | + /// </summary> |
| 73 | + public int PinCount => _controller.PinCount; |
| 74 | + |
| 75 | + /// <summary> |
| 76 | + /// Closes an open pin. |
| 77 | + /// </summary> |
| 78 | + /// <param name="pinNumber">The pin number in the controller's numbering scheme.</param> |
| 79 | + public void ClosePin(int pinNumber) |
| 80 | + { |
| 81 | + |
| 82 | + if (_gpioPins[pinNumber] != null) |
| 83 | + { |
| 84 | + _gpioPins[pinNumber].Dispose(); |
| 85 | + _gpioPins[pinNumber] = null; |
| 86 | + } |
| 87 | + else |
| 88 | + { |
| 89 | + throw new IOException($"Port {pinNumber} is not open"); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + /// <summary> |
| 94 | + /// Dispose the controller |
| 95 | + /// </summary> |
| 96 | + public void Dispose() |
| 97 | + { |
| 98 | + for (int i = 0; i < _gpioPins.Length; i++) |
| 99 | + { |
| 100 | + ClosePin(i); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + /// <summary> |
| 105 | + /// Gets the mode of a pin. |
| 106 | + /// </summary> |
| 107 | + /// <param name="pinNumber">The pin number in the controller's numbering scheme.</param> |
| 108 | + /// <returns>The mode of the pin.</returns> |
| 109 | + public PinMode GetPinMode(int pinNumber) |
| 110 | + { |
| 111 | + if (_gpioPins[pinNumber] == null) |
| 112 | + { |
| 113 | + throw new IOException($"Port {pinNumber} is not open"); |
| 114 | + } |
| 115 | + |
| 116 | + // It is safe to cast, enums are the same |
| 117 | + return (PinMode)_gpioPins[pinNumber].GetDriveMode(); |
| 118 | + } |
| 119 | + |
| 120 | + /// <summary> |
| 121 | + /// Checks if a pin supports a specific mode. |
| 122 | + /// </summary> |
| 123 | + /// <param name="pinNumber">The pin number in the controller's numbering scheme.</param> |
| 124 | + /// <param name="mode">The mode to check.</param> |
| 125 | + /// <returns>The status if the pin supports the mode.</returns> |
| 126 | + public bool IsPinModeSupported(int pinNumber, PinMode mode) => _gpioPins[pinNumber].IsDriveModeSupported((Windows.Devices.Gpio.GpioPinDriveMode)mode); |
| 127 | + |
| 128 | + /// <summary> |
| 129 | + /// Checks if a specific pin is open. |
| 130 | + /// </summary> |
| 131 | + /// <param name="pinNumber">The pin number in the controller's numbering scheme.</param> |
| 132 | + /// <returns>The status if the pin is open or closed.</returns> |
| 133 | + public bool IsPinOpen(int pinNumber) => _gpioPins[pinNumber] != null; |
| 134 | + |
| 135 | + /// <summary> |
| 136 | + /// Opens a pin in order for it to be ready to use. |
| 137 | + /// </summary> |
| 138 | + /// <param name="pinNumber">The pin number in the controller's numbering scheme.</param> |
| 139 | + public void OpenPin(int pinNumber) |
| 140 | + { |
| 141 | + if (IsPinOpen(pinNumber)) |
| 142 | + { |
| 143 | + throw new IOException($"Pin {pinNumber} already open"); |
| 144 | + } |
| 145 | + |
| 146 | + _gpioPins[pinNumber] = _controller.OpenPin(pinNumber); |
| 147 | + } |
| 148 | + |
| 149 | + /// <summary> |
| 150 | + /// Opens a pin and sets it to a specific mode. |
| 151 | + /// </summary> |
| 152 | + /// <param name="pinNumber">The pin number in the controller's numbering scheme.</param> |
| 153 | + /// <param name="mode">The mode to be set.</param> |
| 154 | + public void OpenPin(int pinNumber, PinMode mode) |
| 155 | + { |
| 156 | + OpenPin(pinNumber); |
| 157 | + SetPinMode(pinNumber, mode); |
| 158 | + } |
| 159 | + |
| 160 | + /// <summary> |
| 161 | + /// Reads the current value of a pin. |
| 162 | + /// </summary> |
| 163 | + /// <param name="pinNumber">The pin number in the controller's numbering scheme.</param> |
| 164 | + /// <returns>The value of the pin.</returns> |
| 165 | + public PinValue Read(int pinNumber) => _gpioPins[pinNumber].Read() == Windows.Devices.Gpio.GpioPinValue.High ? PinValue.High : PinValue.Low; |
| 166 | + |
| 167 | + /// <summary> |
| 168 | + /// Adds a callback that will be invoked when pinNumber has an event of type eventType. |
| 169 | + /// </summary> |
| 170 | + /// <param name="pinNumber">The pin number in the controller's numbering scheme.</param> |
| 171 | + /// <param name="eventTypes">The event types to wait for.</param> |
| 172 | + /// <param name="callback">The callback method that will be invoked.</param> |
| 173 | + public void RegisterCallbackForPinValueChangedEvent(int pinNumber, PinEventTypes eventTypes, PinChangeEventHandler callback) |
| 174 | + { |
| 175 | + _gpioEvents[pinNumber] = eventTypes; |
| 176 | + _gpioPinChange[pinNumber] = callback; |
| 177 | + _gpioPins[pinNumber].ValueChanged += GpioControllerValueChanged; |
| 178 | + } |
| 179 | + |
| 180 | + private void GpioControllerValueChanged(object sender, Windows.Devices.Gpio.GpioPinValueChangedEventArgs e) |
| 181 | + { |
| 182 | + var gpioPinNumber = ((Windows.Devices.Gpio.GpioPin)sender).PinNumber; |
| 183 | + if ((e.Edge == Windows.Devices.Gpio.GpioPinEdge.FallingEdge) && (_gpioEvents[gpioPinNumber] == PinEventTypes.Falling)) |
| 184 | + { |
| 185 | + _gpioPinChange[gpioPinNumber].Invoke(this, new PinValueChangedEventArgs(PinEventTypes.Falling, gpioPinNumber)); |
| 186 | + } |
| 187 | + |
| 188 | + if ((e.Edge == Windows.Devices.Gpio.GpioPinEdge.RisingEdge) && (_gpioEvents[gpioPinNumber] == PinEventTypes.Rising)) |
| 189 | + { |
| 190 | + _gpioPinChange[gpioPinNumber].Invoke(this, new PinValueChangedEventArgs(PinEventTypes.Rising, gpioPinNumber)); |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + /// <summary> |
| 195 | + /// Sets the mode to a pin. |
| 196 | + /// </summary> |
| 197 | + /// <param name="pinNumber">The pin number in the controller's numbering scheme</param> |
| 198 | + /// <param name="mode">The mode to be set.</param> |
| 199 | + public void SetPinMode(int pinNumber, PinMode mode) |
| 200 | + { |
| 201 | + if (!IsPinOpen(pinNumber)) |
| 202 | + { |
| 203 | + throw new IOException($"Pin {pinNumber} needs to be open"); |
| 204 | + } |
| 205 | + |
| 206 | + // Safe cast, same enum on nanoFramework |
| 207 | + _gpioPins[pinNumber].SetDriveMode((Windows.Devices.Gpio.GpioPinDriveMode)mode); |
| 208 | + } |
| 209 | + |
| 210 | + /// <summary> |
| 211 | + /// Removes a callback that was being invoked for pin at pinNumber. |
| 212 | + /// </summary> |
| 213 | + /// <param name="pinNumber">The pin number in the controller's numbering scheme.</param> |
| 214 | + /// <param name="callback">The callback method that will be invoked.</param> |
| 215 | + public void UnregisterCallbackForPinValueChangedEvent(int pinNumber, PinChangeEventHandler callback) |
| 216 | + { |
| 217 | + _gpioEvents[pinNumber] = PinEventTypes.None; |
| 218 | + _gpioPins[pinNumber].ValueChanged -= GpioControllerValueChanged; |
| 219 | + } |
| 220 | + |
| 221 | + /// <summary> |
| 222 | + /// Blocks execution until an event of type eventType is received or a period of |
| 223 | + /// time has expired. |
| 224 | + /// </summary> |
| 225 | + /// <param name="pinNumber">The pin number in the controller's numbering scheme.</param> |
| 226 | + /// <param name="eventTypes">The event types to wait for.</param> |
| 227 | + /// <param name="timeout">The time to wait for the event.</param> |
| 228 | + /// <returns>A structure that contains the result of the waiting operation.</returns> |
| 229 | + public WaitForEventResult WaitForEvent(int pinNumber, PinEventTypes eventTypes, TimeSpan timeout) |
| 230 | + { |
| 231 | + _gpioEvents[pinNumber] = eventTypes; |
| 232 | + _gpioEventsHappening[pinNumber] = PinEventTypes.None; |
| 233 | + _gpioPins[pinNumber].ValueChanged += GpioControllerWaitForEvents; |
| 234 | + DateTime dtTimeout = DateTime.UtcNow.Add(timeout); |
| 235 | + while (DateTime.UtcNow < dtTimeout) |
| 236 | + { |
| 237 | + if (_gpioEventsHappening[pinNumber] != PinEventTypes.None) |
| 238 | + { |
| 239 | + break; |
| 240 | + } |
| 241 | + } |
| 242 | + _gpioPins[pinNumber].ValueChanged -= GpioControllerWaitForEvents; |
| 243 | + |
| 244 | + if (_gpioEventsHappening[pinNumber] != PinEventTypes.None) |
| 245 | + { |
| 246 | + return new WaitForEventResult() { EventTypes = _gpioEventsHappening[pinNumber], TimedOut = false }; |
| 247 | + } |
| 248 | + |
| 249 | + return new WaitForEventResult() { EventTypes = PinEventTypes.None, TimedOut = true }; |
| 250 | + } |
| 251 | + |
| 252 | + private void GpioControllerWaitForEvents(object sender, Windows.Devices.Gpio.GpioPinValueChangedEventArgs e) |
| 253 | + { |
| 254 | + var gpioPinNumber = ((Windows.Devices.Gpio.GpioPin)sender).PinNumber; |
| 255 | + _gpioEventsHappening[gpioPinNumber] = e.Edge == Windows.Devices.Gpio.GpioPinEdge.FallingEdge ? PinEventTypes.Rising : PinEventTypes.Falling; |
| 256 | + } |
| 257 | + |
| 258 | + /// <summary> |
| 259 | + /// Writes a value to a pin. |
| 260 | + /// </summary> |
| 261 | + /// <param name="pinNumber">The pin number in the controller's numbering scheme.</param> |
| 262 | + /// <param name="value">The value to be written to the pin.</param> |
| 263 | + public void Write(int pinNumber, PinValue value) |
| 264 | + { |
| 265 | + _gpioPins[pinNumber].Write(value == PinValue.High ? Windows.Devices.Gpio.GpioPinValue.High : Windows.Devices.Gpio.GpioPinValue.Low); |
| 266 | + } |
| 267 | + } |
| 268 | +} |
0 commit comments