Add Tizen.Peripheral API (#1939)
[platform/core/csapi/tizenfx.git] / internals / src / Tizen.Peripheral / Tizen.Peripheral / Adc.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 NativeAdc = Interop.Peripheral.Adc;
19
20 namespace Tizen.Peripheral
21 {
22     /// <summary>
23     /// The class allows applications to use the platform ADC peripheral.
24     /// </summary>
25     /// <privilege>http://tizen.org/privilege/peripheralio</privilege>
26     public class Adc : IDisposable
27     {
28         /// <summary>
29         /// Native handle to ADC.
30         /// </summary>
31         private IntPtr _handle;
32         private bool _disposed = false;
33
34         /// <summary>
35         /// Opens the ADC pin.
36         /// </summary>
37         /// <param name="device">The ADC device number.</param>
38         /// <param name="channel">The ADC channel number to control.</param>
39         public Adc(int device, int channel)
40         {
41             var ret = NativeAdc.Open(device, channel, out IntPtr handle);
42             if (ret != Internals.Errors.ErrorCode.None)
43                 throw ExceptionFactory.CreateException(ret);
44
45             _handle= handle;
46         }
47
48         /// <summary>
49         /// Closes the ADC pin.
50         /// </summary>
51         ~Adc()
52         {
53             Dispose(false);
54         }
55
56         /// <summary>
57         /// Gets the current value of the ADC pin.
58         /// </summary>
59         public uint ReadValue()
60         {
61             var ret = NativeAdc.Read(_handle, out uint adcValue);
62             if (ret != Internals.Errors.ErrorCode.None)
63                 throw ExceptionFactory.CreateException(ret);
64
65             return adcValue;
66         }
67
68         /// <summary>
69         /// Closes the ADC pin.
70         /// </summary>
71        public void Close() => Dispose();
72
73         /// <summary>
74         /// Disposes the ADC pin.
75         /// </summary>
76         public void Dispose()
77         {
78             Dispose(true);
79             GC.SuppressFinalize(this);
80         }
81
82         /// <summary>
83         /// Disposes the ADC pin.
84         /// </summary>
85         protected virtual void Dispose(bool disposing)
86         {
87             if (_disposed)
88             {
89                 return;
90             }
91
92             NativeAdc.Close(_handle);
93             _disposed = true;
94         }
95     }
96 }