| 163 |
pfowler |
1 |
using System;
|
|
|
2 |
using System.Collections.Generic;
|
|
|
3 |
using System.Linq;
|
|
|
4 |
using System.Text;
|
|
|
5 |
using System.Threading.Tasks;
|
|
|
6 |
|
|
|
7 |
namespace nitdcscore {
|
|
|
8 |
public interface i2cmaster {
|
|
|
9 |
int WriteI2cData(byte address, byte[] data, uint count);
|
|
|
10 |
int ReadI2CData(byte address, ref byte[] data, uint count);
|
|
|
11 |
}
|
|
|
12 |
|
|
|
13 |
public class mcp23017 {
|
|
|
14 |
public byte address { get; set; }
|
|
|
15 |
private i2cmaster dev;
|
|
|
16 |
|
|
|
17 |
public mcp23017(i2cmaster dev, byte address) {
|
|
|
18 |
this.dev = dev;
|
|
|
19 |
this.address = address;
|
|
|
20 |
}
|
|
|
21 |
|
|
|
22 |
public int SetIODirection(byte gpioa, byte gpiob) {
|
|
|
23 |
byte[] data = new byte[] { 0x00, gpioa, gpiob };
|
|
|
24 |
int rslt = dev.WriteI2cData(this.addressToWrite(), data, 3);
|
|
|
25 |
return rslt;
|
|
|
26 |
}
|
|
|
27 |
|
|
|
28 |
public int SetIOPullups(byte gpioa, byte gpiob) {
|
|
|
29 |
byte[] data = new byte[] { 0x0c, gpioa, gpiob };
|
|
|
30 |
int rslt = dev.WriteI2cData(this.addressToWrite(), data, 3);
|
|
|
31 |
return rslt;
|
|
|
32 |
}
|
|
|
33 |
|
|
|
34 |
public int GetIO(out byte[] data) {
|
|
|
35 |
int rslt = 0;
|
|
|
36 |
byte[] cmd = new byte[] { 0x12 };
|
|
|
37 |
rslt = dev.WriteI2cData(this.addressToWrite(), cmd, 1);
|
|
|
38 |
|
|
|
39 |
data = new byte[] { 0xff, 0xff };
|
|
|
40 |
rslt = dev.ReadI2CData(this.addressToRead(), ref data, 2);
|
|
|
41 |
return 1;
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
public int SetIO(byte gpioa, byte gpiob) {
|
|
|
45 |
int rslt = 0;
|
|
|
46 |
byte[] cmd = new byte[] { 0x12, gpioa, gpiob };
|
|
|
47 |
rslt = dev.WriteI2cData(this.addressToWrite(), cmd, 3);
|
|
|
48 |
return rslt;
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
private byte addressToWrite() {
|
|
|
52 |
byte write = (byte)(this.address << 1);
|
|
|
53 |
return write;
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
private byte addressToRead() {
|
|
|
57 |
byte read = (byte)((this.address << 1) | 0x01);
|
|
|
58 |
return read;
|
|
|
59 |
}
|
|
|
60 |
}
|
|
|
61 |
}
|