-
Notifications
You must be signed in to change notification settings - Fork 136
/
Copy pathI2cDeviceConnection.cs
101 lines (82 loc) · 2.44 KB
/
I2cDeviceConnection.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
namespace Raspberry.IO.InterIntegratedCircuit
{
using System;
/// <summary>
/// Represents a connection to the I2C device.
/// </summary>
public class I2cDeviceConnection
{
#region Fields
private readonly I2cDriver driver;
private readonly int deviceAddress;
#endregion
#region Instance Management
internal I2cDeviceConnection(I2cDriver driver, int deviceAddress)
{
this.driver = driver;
this.deviceAddress = deviceAddress;
}
#endregion
#region Properties
/// <summary>
/// Gets the device address.
/// </summary>
/// <value>
/// The device address.
/// </value>
public int DeviceAddress
{
get { return deviceAddress; }
}
#endregion
#region Methods
/// <summary>
/// Executes the specified transaction.
/// </summary>
/// <param name="transaction">The transaction.</param>
public void Execute(I2cTransaction transaction)
{
if (transaction == null)
{
throw new ArgumentNullException("transaction");
}
driver.Execute(deviceAddress, transaction);
}
/// <summary>
/// Writes the specified buffer.
/// </summary>
/// <param name="buffer">The buffer.</param>
public void Write(params byte[] buffer)
{
Execute(new I2cTransaction(new I2cWriteAction(buffer)));
}
/// <summary>
/// Writes the specified byte.
/// </summary>
/// <param name="value">The value.</param>
public void WriteByte(byte value)
{
Execute(new I2cTransaction(new I2cWriteAction(value)));
}
/// <summary>
/// Reads the specified number of bytes.
/// </summary>
/// <param name="byteCount">The byte count.</param>
/// <returns>The buffer.</returns>
public byte[] Read(int byteCount)
{
var readAction = new I2cReadAction(new byte[byteCount]);
Execute(new I2cTransaction(readAction));
return readAction.Buffer;
}
/// <summary>
/// Reads a byte.
/// </summary>
/// <returns>The byte.</returns>
public byte ReadByte()
{
return Read(1)[0];
}
#endregion
}
}