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 NativeI2c = Interop.Peripheral.I2c;
20 namespace Tizen.Peripheral
23 /// The class allows applications to communicate via i2c platform's bus.
25 /// <privilege>http://tizen.org/privilege/peripheralio</privilege>
26 public class I2c : IDisposable
29 /// Native handle to I2c.
31 private IntPtr _handle;
32 private bool _disposed = false;
35 /// Opens the connection to the I2C slave device.
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)
41 var ret = NativeI2c.Open(bus, address, out IntPtr handle);
42 if (ret != Internals.Errors.ErrorCode.None)
43 throw ExceptionFactory.CreateException(ret);
49 /// Closes the connection to the I2C slave device.
57 /// Closes the connection to the I2C slave device.
59 public void Close() => Dispose();
67 GC.SuppressFinalize(this);
73 protected virtual void Dispose(bool disposing)
80 NativeI2c.Close(_handle);
85 /// Reads the bytes data from the I2C slave device.
87 /// <param name="dataOut">The output byte array.</param>
88 public void Read(byte[] dataOut)
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);
97 /// Writes the bytes data to the I2C slave device.
99 /// <param name="data"></param>
100 public void Write(byte[] data)
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);
109 /// Reads single byte data from the register of the I2C slave device.
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)
115 var ret = NativeI2c.ReadRegisterByte(_handle, register, out byte data);
116 if (ret != Internals.Errors.ErrorCode.None)
117 throw ExceptionFactory.CreateException(ret);
123 /// Writes single byte data to the register of the I2C slave device.
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)
129 var ret = NativeI2c.WriteRegisterByte(_handle, register, data);
130 if (ret != Internals.Errors.ErrorCode.None)
131 throw ExceptionFactory.CreateException(ret);
135 /// Reads word data from the register of the I2C slave device.
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)
141 var ret = NativeI2c.ReadRegisterWord(_handle, register, out ushort data);
142 if (ret != Internals.Errors.ErrorCode.None)
143 throw ExceptionFactory.CreateException(ret);
149 /// Writes word data to the register of the I2C slave device.
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)
155 var ret = NativeI2c.WriteRegisterWord(_handle, register, data);
156 if (ret != Internals.Errors.ErrorCode.None)
157 throw ExceptionFactory.CreateException(ret);