-
-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy pathI2cAdapter.cs
51 lines (45 loc) · 1.63 KB
/
I2cAdapter.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Device.Gpio;
using System.Device.I2c;
namespace Iot.Device.Mcp23xxx
{
public abstract partial class Mcp23xxx
{
/// <summary>
/// I2C adapter
/// </summary>
protected class I2cAdapter : BusAdapter
{
private I2cDevice _device;
/// <summary>
/// Constructs I2cAdapter instance
/// </summary>
/// <param name="device">I2C device</param>
public I2cAdapter(I2cDevice device) => _device = device;
/// <inheritdoc/>
public override void Dispose() => _device?.Dispose();
/// <inheritdoc/>
public override void Read(byte registerAddress, SpanByte buffer)
{
// Set address to register first.
Write(registerAddress, SpanByte.Empty);
_device.Read(buffer);
}
/// <inheritdoc/>
public override void Write(byte registerAddress, SpanByte data)
{
SpanByte output = new byte[data.Length + 1];
output[0] = registerAddress;
// SpanByte slice method is different to Span<Byte> slice method in case that index argument (here 1) is equal to size of slice
if (data.Length > 0)
{
// do not override output[0]
data.CopyTo(output.Slice(1));
}
_device.Write(output);
}
}
}
}