Merge remote-tracking branch 'origin/master' into tizen
[platform/core/csapi/tizenfx.git] / internals / src / Tizen.Peripheral / Tizen.Peripheral / I2c.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 NativeI2c = Interop.Peripheral.I2c;
19
20 namespace Tizen.Peripheral
21 {
22     /// <summary>
23     /// The class allows applications to communicate via i2c platform's bus.
24     /// </summary>
25     /// <privilege>http://tizen.org/privilege/peripheralio</privilege>
26     public class I2c : IDisposable
27     {
28         /// <summary>
29         /// Native handle to I2c.
30         /// </summary>
31         private IntPtr _handle;
32         private bool _disposed = false;
33
34         /// <summary>
35         /// Opens the connection to the I2C slave device.
36         /// </summary>
37         /// <param name="bus">The I2C bus number that the slave device is connected.</param>
38         /// <param name="address">The address of the slave device.</param>
39         public I2c(int bus, int address)
40         {
41             var ret = NativeI2c.Open(bus, address, 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 connection to the I2C slave device.
50         /// </summary>
51         ~I2c()
52         {
53             Dispose(false);
54         }
55
56         /// <summary>
57         /// Closes the connection to the I2C slave device.
58         /// </summary>
59         public void Close() => Dispose();
60
61         /// <summary>
62         /// Disposes the I2c.
63         /// </summary>
64         public void Dispose()
65         {
66             Dispose(true);
67             GC.SuppressFinalize(this);
68         }
69
70         /// <summary>
71         /// Disposes the I2c.
72         /// </summary>
73         protected virtual void Dispose(bool disposing)
74         {
75             if (_disposed)
76             {
77                 return;
78             }
79
80             NativeI2c.Close(_handle);
81             _disposed = true;
82         }
83
84         /// <summary>
85         /// Reads the bytes data from the I2C slave device.
86         /// </summary>
87         /// <param name="dataOut">The output byte array.</param>
88         public void Read(byte[] dataOut)
89         {
90             var length = Convert.ToUInt32(dataOut.Length);
91             var ret = NativeI2c.Read(_handle, dataOut, length);
92             if (ret != Internals.Errors.ErrorCode.None)
93                 throw ExceptionFactory.CreateException(ret);
94         }
95
96         /// <summary>
97         /// Writes the bytes data to the I2C slave device.
98         /// </summary>
99         /// <param name="data"></param>
100         public void Write(byte[] data)
101         {
102             var length = Convert.ToUInt32(data.Length);
103             var ret = NativeI2c.Write(_handle, data, length);
104             if (ret != Internals.Errors.ErrorCode.None)
105                 throw ExceptionFactory.CreateException(ret);
106         }
107
108         /// <summary>
109         /// Reads single byte data from the register of the I2C slave device.
110         /// </summary>
111         /// <param name="register">The register address of the I2C slave device to read.</param>
112         /// <returns>The single byte data.</returns>
113         public byte ReadRegisterByte(byte register)
114         {
115             var ret = NativeI2c.ReadRegisterByte(_handle, register, out byte data);
116             if (ret != Internals.Errors.ErrorCode.None)
117                 throw ExceptionFactory.CreateException(ret);
118
119             return data;
120         }
121
122         /// <summary>
123         /// Writes single byte data to the register of the I2C slave device.
124         /// </summary>
125         /// <param name="register">The register address of the I2C slave device to write.</param>
126         /// <param name="data">The single byte data to write.</param>
127         public void WriteRegisterByte(byte register, byte data)
128         {
129             var ret = NativeI2c.WriteRegisterByte(_handle, register, data);
130             if (ret != Internals.Errors.ErrorCode.None)
131                 throw ExceptionFactory.CreateException(ret);
132         }
133
134         /// <summary>
135         /// Reads word data from the register of the I2C slave device.
136         /// </summary>
137         /// <param name="register">The register address of the I2C slave device to read.</param>
138         /// <returns>The word (2 bytes) data.</returns>
139         public ushort ReadRegisterWord(byte register)
140         {
141             var ret = NativeI2c.ReadRegisterWord(_handle, register, out ushort data);
142             if (ret != Internals.Errors.ErrorCode.None)
143                 throw ExceptionFactory.CreateException(ret);
144
145             return data;
146         }
147
148         /// <summary>
149         /// Writes word data to the register of the I2C slave device.
150         /// </summary>
151         /// <param name="register">The register address of the I2C slave device to write.</param>
152         /// <param name="data">The word (2 bytes) data to write.</param>
153         public void WriteRegisterWord(byte register, ushort data)
154         {
155             var ret = NativeI2c.WriteRegisterWord(_handle, register, data);
156             if (ret != Internals.Errors.ErrorCode.None)
157                 throw ExceptionFactory.CreateException(ret);
158         }
159     }
160 }