-
Notifications
You must be signed in to change notification settings - Fork 598
/
Copy pathMcp23x1x.cs
80 lines (70 loc) · 3.17 KB
/
Mcp23x1x.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Device.Gpio;
namespace Iot.Device.Mcp23xxx
{
/// <summary>
/// Wraps 16-bit MCP I/O expanders.
/// </summary>
public abstract class Mcp23x1x : Mcp23xxx
{
/// <summary>
/// Constructs Mcp23x1x instance
/// </summary>
/// <param name="device">I2C device used to communicate with the device</param>
/// <param name="reset">Reset pin</param>
/// <param name="interruptA">Interrupt A pin</param>
/// <param name="interruptB">Interrupt B pin</param>
/// <param name="controller">
/// <see cref="GpioController"/> related with
/// <paramref name="reset"/> <paramref name="interruptA"/> and <paramref name="interruptB"/> pins
/// </param>
/// <param name="shouldDispose">True to dispose the Gpio Controller</param>
protected Mcp23x1x(BusAdapter device, int reset, int interruptA, int interruptB, GpioController? controller, bool shouldDispose = true)
: base(device, reset, interruptA, interruptB, controller, shouldDispose: shouldDispose)
{
}
/// <inheritdoc/>
protected override int PinCount => 16;
/// <summary>
/// Read a byte from the given register on the given port.
/// </summary>
public byte ReadByte(Register register, Port port) => InternalReadByte(register, port);
/// <summary>
/// Write a byte to the given register on the given port.
/// </summary>
public void WriteByte(Register register, byte value, Port port) => InternalWriteByte(register, value, port);
/// <summary>
/// Read a ushort from the given register.
/// </summary>
public ushort ReadUInt16(Register register) => InternalReadUInt16(register);
/// <summary>
/// Write a ushort to the given register. Writes the value to both ports.
/// </summary>
public void WriteUInt16(Register register, ushort value) => InternalWriteUInt16(register, value);
/// <summary>
/// Reads the interrupt pin for the given port if configured.
/// </summary>
public PinValue ReadInterrupt(Port port) => InternalReadInterrupt(port);
/// <summary>
/// Reads all bits of port A in a single operation.
/// </summary>
/// <returns>In the low byte: A bit field of the value of the first 8 GPIO ports
/// (Bit 0: GPIO 0, Bit 1: GPIO 1 etc.). Only the bits of input ports are defined.</returns>
public int ReadPortA()
{
int value = ReadByte(Register.GPIO, Port.PortA);
return value;
}
/// <summary>
/// Reads all bits of port B in a single operation.
/// </summary>
/// <returns>In the low byte: A bit field of the value of the second 8 GPIO ports
/// (Bit 0: GPIO 8, Bit 1: GPIO 9 etc.). Only the bits of input ports are defined.</returns>
public int ReadPortB()
{
int value = ReadByte(Register.GPIO, Port.PortB);
return value;
}
}
}