2 * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 using Tizen.Internals.Errors;
19 using NativeGpio = Interop.Peripheral.Gpio;
21 namespace Tizen.Peripheral.Gpio
24 /// Enumeration of GPIO direction options.
26 public enum GpioPinDriveMode
34 /// Output mode with high value.
39 /// Output mode with low value.
45 /// Enumeration of GPIO values.
47 public enum GpioPinValue
61 /// Enumeration of edge types for the GPIO interrupt.
63 public enum GpioChangePolarity
66 /// No interrupt on GPIO.
71 /// Interrupt on rising only.
76 /// Interrupt on falling only.
81 /// Interrupt on rising and falling.
87 /// The class allows applications to use the platform Digital Pins as Input/Output.
89 /// <privilege>http://tizen.org/privilege/peripheralio</privilege>
90 public class Gpio : IDisposable
93 private GpioChangePolarity _polarityType;
94 private NativeGpio.InterruptedEventCallback _interruptedEventCallback;
97 /// Native handle to Gpio.
99 private IntPtr _handle;
100 private bool _disposed = false;
103 /// Invoked when Gpio pin was interrupted.
105 public event EventHandler<PinUpdatedEventArgs> ValueChanged;
108 /// Opens a GPIO pin.
110 /// <param name="pinNumber">The GPIO pin number.</param>
111 /// <param name="mode">GPIO direction.</param>
112 public Gpio(int pinNumber, GpioPinDriveMode mode)
114 var ret = NativeGpio.Open(pinNumber, out IntPtr handle);
115 if (ret != ErrorCode.None)
116 throw ExceptionFactory.CreateException(ret);
119 PinNumber = pinNumber;
124 case GpioPinDriveMode.Input:
125 ret = NativeGpio.SetEdgeMode(handle, NativeGpio.EdgeType.Both);
126 if (ret != ErrorCode.None)
127 throw ExceptionFactory.CreateException(ret);
129 ret = NativeGpio.SetDirection(handle, NativeGpio.Direction.In);
130 if (ret != ErrorCode.None)
131 throw ExceptionFactory.CreateException(ret);
132 SetIntteruptedCallback();
134 case GpioPinDriveMode.OutputInitiallyLow:
135 ret = NativeGpio.SetDirection(handle, NativeGpio.Direction.OutLow);
136 if (ret != ErrorCode.None)
137 throw ExceptionFactory.CreateException(ret);
139 case GpioPinDriveMode.OutputInitiallyHigh:
140 ret = NativeGpio.SetDirection(handle, NativeGpio.Direction.OutHigh);
141 if (ret != ErrorCode.None)
142 throw ExceptionFactory.CreateException(ret);
145 } catch(Exception e) {
153 /// Closes the GPIO pin.
160 private void SetIntteruptedCallback()
162 _interruptedEventCallback = OnInterrupted;
163 var ret = NativeGpio.SetInterruptedCb(_handle, _interruptedEventCallback, IntPtr.Zero);
164 if (ret != ErrorCode.None)
166 throw ExceptionFactory.CreateException(ret);
170 private void OnInterrupted(IntPtr handle, ErrorCode error, IntPtr data)
172 ValueChanged?.Invoke(this, new PinUpdatedEventArgs(PinNumber, Read()));
176 /// Closes a GPIO pin.
178 public void Close() => Dispose();
183 public void Dispose()
186 GC.SuppressFinalize(this);
192 protected virtual void Dispose(bool disposing)
199 NativeGpio.UnsetInterruptedCb(_handle);
200 NativeGpio.Close(_handle);
207 public int PinNumber { get; }
212 /// <returns>Pin value</returns>
213 public GpioPinValue Read()
215 var ret = NativeGpio.Read(_handle, out uint value);
216 if (ret != ErrorCode.None)
217 throw ExceptionFactory.CreateException(ret);
219 return value == 1 ? GpioPinValue.High : GpioPinValue.Low;
225 public void Write(GpioPinValue value)
227 var ret = NativeGpio.Write(_handle, value == GpioPinValue.High ? 1 : (uint)0);
228 if (ret != ErrorCode.None)
229 throw ExceptionFactory.CreateException(ret);
233 /// Sets or gets the GPIO edge mode.
235 /// <remarks>Get value is initialized after successful Set call.</remarks>
236 public GpioChangePolarity Polarity
238 get => _polarityType;
241 var ret = NativeGpio.SetEdgeMode(_handle, (NativeGpio.EdgeType)value);
242 if (ret != ErrorCode.None)
243 throw ExceptionFactory.CreateException(ret);
245 _polarityType = value;