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 NativePwm = Interop.Peripheral.Pwm;
20 namespace Tizen.Peripheral.Pwm
23 /// Enumeration for PWM polarity.
25 public enum PwmPulsePolarity
28 /// PWM signal start in the active high state.
33 /// PWM signal start in the active low state.
39 /// The class allows applications to use the platform PWM peripheral.
41 /// <privilege>http://tizen.org/privilege/peripheralio</privilege>
42 public class Pwm : IDisposable
45 //TODO provide default values.
47 private uint _dutyCycle;
48 private PwmPulsePolarity _polarity;
49 private bool _enabled;
50 private bool _disposed = false;
53 /// Native handle to PWM.
55 private IntPtr _handle;
58 /// Opens the PWM pin.
60 /// <param name="chip">The PWM chip number.</param>
61 /// <param name="pin">The PWM pin (channel) number to control.</param>
62 public Pwm(int chip, int pin)
64 var ret = NativePwm.Open(chip, pin, out IntPtr handle);
65 if (ret != Internals.Errors.ErrorCode.None)
66 throw ExceptionFactory.CreateException(ret);
72 /// Closes the PWM pin.
80 /// Closes the PWM pin.
82 public void Close() => Dispose();
90 GC.SuppressFinalize(this);
96 protected virtual void Dispose(bool disposing)
103 NativePwm.Close(_handle);
108 /// Sets or gets period of the PWM pin.
110 /// <remarks>Get value is initialized after successful Set call.</remarks>
116 var ret = NativePwm.SetPeriod(_handle, value);
117 if (ret != Internals.Errors.ErrorCode.None)
118 throw ExceptionFactory.CreateException(ret);
125 /// Sets or gets duty cycle of the PWM pin.
127 /// <remarks>Get value is initialized after successful Set call.</remarks>
128 public uint DutyCycle
133 var ret = NativePwm.SetDutyCycle(_handle, value);
134 if (ret != Internals.Errors.ErrorCode.None)
135 throw ExceptionFactory.CreateException(ret);
142 /// Sets or gets polarity of the PWM pin.
144 /// <remarks>Get value is initialized after successful Set call.</remarks>
145 public PwmPulsePolarity Polarity
150 var ret = NativePwm.SetPolarity(_handle, (NativePwm.Polarity)value);
151 if (ret != Internals.Errors.ErrorCode.None)
152 throw ExceptionFactory.CreateException(ret);
159 /// Enables or disables the PWM pin.
161 /// <remarks>Get value is initialized after successful Set call.</remarks>
167 var ret = NativePwm.SetEnabled(_handle, value);
168 if (ret != Internals.Errors.ErrorCode.None)
169 throw ExceptionFactory.CreateException(ret);