Merge remote-tracking branch 'origin/master' into tizen
[platform/core/csapi/tizenfx.git] / internals / src / Tizen.Peripheral / Tizen.Peripheral / Pwm.cs
1 /*
2 * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 using System;
18 using NativePwm = Interop.Peripheral.Pwm;
19
20 namespace Tizen.Peripheral.Pwm
21 {
22     /// <summary>
23     /// Enumeration for PWM polarity.
24     /// </summary>
25     public enum PwmPulsePolarity
26     {
27         /// <summary>
28         /// PWM signal start in the active high state.
29         /// </summary>
30         ActiveHigh = 0,
31
32         /// <summary>
33         /// PWM signal start in the active low state.
34         /// </summary>
35         ActiveLow
36     }
37
38     /// <summary>
39     /// The class allows applications to use the platform PWM peripheral.
40     /// </summary>
41     /// <privilege>http://tizen.org/privilege/peripheralio</privilege>
42     public class Pwm : IDisposable
43     {
44
45         //TODO provide default values.
46         private uint _period;
47         private uint _dutyCycle;
48         private PwmPulsePolarity _polarity;
49         private bool _enabled;
50         private bool _disposed = false;
51
52         /// <summary>
53         /// Native handle to PWM.
54         /// </summary>
55         private IntPtr _handle;
56
57         /// <summary>
58         /// Opens the PWM pin.
59         /// </summary>
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)
63         {
64             var ret = NativePwm.Open(chip, pin, out IntPtr handle);
65             if (ret != Internals.Errors.ErrorCode.None)
66                 throw ExceptionFactory.CreateException(ret);
67
68             _handle = handle;
69         }
70
71         /// <summary>
72         /// Closes the PWM pin.
73         /// </summary>
74         ~Pwm()
75         {
76             Dispose(false);
77         }
78
79         /// <summary>
80         /// Closes the PWM pin.
81         /// </summary>
82         public void Close() => Dispose();
83
84         /// <summary>
85         /// Disposes the PWM.
86         /// </summary>
87         public void Dispose()
88         {
89             Dispose(true);
90             GC.SuppressFinalize(this);
91         }
92
93         /// <summary>
94         /// Disposes the PWM.
95         /// </summary>
96         protected virtual void Dispose(bool disposing)
97         {
98             if (_disposed)
99             {
100                 return;
101             }
102
103             NativePwm.Close(_handle);
104             _disposed = true;
105         }
106
107         /// <summary>
108         /// Sets or gets period of the PWM pin.
109         /// </summary>
110         /// <remarks>Get value is initialized after successful Set call.</remarks>
111         public uint Period
112         {
113             get => _period;
114             set
115             {
116                 var ret = NativePwm.SetPeriod(_handle, value);
117                 if (ret != Internals.Errors.ErrorCode.None)
118                     throw ExceptionFactory.CreateException(ret);
119
120                 _period = value;
121             }
122         }
123
124         /// <summary>
125         /// Sets or gets duty cycle of the PWM pin.
126         /// </summary>
127         /// <remarks>Get value is initialized after successful Set call.</remarks>
128         public uint DutyCycle
129         {
130             get => _dutyCycle;
131             set
132             {
133                 var ret = NativePwm.SetDutyCycle(_handle, value);
134                 if (ret != Internals.Errors.ErrorCode.None)
135                     throw ExceptionFactory.CreateException(ret);
136
137                 _dutyCycle = value;
138             }
139         }
140
141         /// <summary>
142         /// Sets or gets polarity of the PWM pin.
143         /// </summary>
144         /// <remarks>Get value is initialized after successful Set call.</remarks>
145         public PwmPulsePolarity Polarity
146         {
147             get => _polarity;
148             set
149             {
150                 var ret = NativePwm.SetPolarity(_handle, (NativePwm.Polarity)value);
151                 if (ret != Internals.Errors.ErrorCode.None)
152                     throw ExceptionFactory.CreateException(ret);
153
154                 _polarity = value;
155             }
156         }
157
158         /// <summary>
159         /// Enables or disables the PWM pin.
160         /// </summary>
161         /// <remarks>Get value is initialized after successful Set call.</remarks>
162         public bool Enabled
163         {
164             get => _enabled;
165             set
166             {
167                 var ret = NativePwm.SetEnabled(_handle, value);
168                 if (ret != Internals.Errors.ErrorCode.None)
169                     throw ExceptionFactory.CreateException(ret);
170
171                 _enabled = value;
172             }
173         }
174     }
175 }