| 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);
|
| 184 |
pfowler |
11 |
void SetGpio(byte pin, byte data);
|
| 163 |
pfowler |
12 |
}
|
|
|
13 |
|
| 184 |
pfowler |
14 |
public class i2cUtils {
|
|
|
15 |
public static byte addressToWrite(byte address) {
|
|
|
16 |
byte write = (byte)(address << 1);
|
|
|
17 |
return write;
|
|
|
18 |
}
|
|
|
19 |
|
|
|
20 |
public static byte addressToRead(byte address) {
|
|
|
21 |
byte read = (byte)((address << 1) | 0x01);
|
|
|
22 |
return read;
|
|
|
23 |
}
|
|
|
24 |
}
|
|
|
25 |
|
| 163 |
pfowler |
26 |
public class mcp23017 {
|
|
|
27 |
public byte address { get; set; }
|
|
|
28 |
private i2cmaster dev;
|
|
|
29 |
|
|
|
30 |
public mcp23017(i2cmaster dev, byte address) {
|
|
|
31 |
this.dev = dev;
|
|
|
32 |
this.address = address;
|
|
|
33 |
}
|
|
|
34 |
|
|
|
35 |
public int SetIODirection(byte gpioa, byte gpiob) {
|
|
|
36 |
byte[] data = new byte[] { 0x00, gpioa, gpiob };
|
|
|
37 |
int rslt = dev.WriteI2cData(this.addressToWrite(), data, 3);
|
|
|
38 |
return rslt;
|
|
|
39 |
}
|
|
|
40 |
|
| 164 |
pfowler |
41 |
public int SetIOPolarity(byte gpioa, byte gpiob) {
|
|
|
42 |
byte[] data = new byte[] { 0x02, gpioa, gpiob };
|
|
|
43 |
int rslt = dev.WriteI2cData(this.addressToWrite(), data, 3);
|
|
|
44 |
return rslt;
|
|
|
45 |
}
|
|
|
46 |
|
| 163 |
pfowler |
47 |
public int SetIOPullups(byte gpioa, byte gpiob) {
|
|
|
48 |
byte[] data = new byte[] { 0x0c, gpioa, gpiob };
|
|
|
49 |
int rslt = dev.WriteI2cData(this.addressToWrite(), data, 3);
|
|
|
50 |
return rslt;
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
public int GetIO(out byte[] data) {
|
|
|
54 |
int rslt = 0;
|
|
|
55 |
byte[] cmd = new byte[] { 0x12 };
|
|
|
56 |
rslt = dev.WriteI2cData(this.addressToWrite(), cmd, 1);
|
|
|
57 |
|
|
|
58 |
data = new byte[] { 0xff, 0xff };
|
|
|
59 |
rslt = dev.ReadI2CData(this.addressToRead(), ref data, 2);
|
|
|
60 |
return 1;
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
public int SetIO(byte gpioa, byte gpiob) {
|
|
|
64 |
int rslt = 0;
|
|
|
65 |
byte[] cmd = new byte[] { 0x12, gpioa, gpiob };
|
|
|
66 |
rslt = dev.WriteI2cData(this.addressToWrite(), cmd, 3);
|
|
|
67 |
return rslt;
|
|
|
68 |
}
|
|
|
69 |
|
| 171 |
pfowler |
70 |
public int SetBank(int bank, byte gpio) {
|
|
|
71 |
int rslt = 0;
|
|
|
72 |
if (bank == 0) {
|
|
|
73 |
byte[] cmd = new byte[] { 0x12, gpio };
|
|
|
74 |
rslt = dev.WriteI2cData(this.addressToWrite(), cmd, 2);
|
|
|
75 |
} else if (bank == 1) {
|
|
|
76 |
byte[] cmd = new byte[] { 0x13, gpio };
|
|
|
77 |
rslt = dev.WriteI2cData(this.addressToWrite(), cmd, 2);
|
|
|
78 |
}
|
|
|
79 |
return rslt;
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
public int GetBank(int bank, byte gpio, out byte data) {
|
|
|
83 |
int rslt = 0;
|
|
|
84 |
byte[] tmpdata = { 0x00 };
|
|
|
85 |
data = 0;
|
|
|
86 |
if (bank == 0) {
|
|
|
87 |
byte[] cmd = new byte[] { 0x12 };
|
|
|
88 |
rslt = dev.WriteI2cData(this.addressToWrite(), cmd, 1);
|
|
|
89 |
|
|
|
90 |
cmd = new byte[] { 0xff };
|
|
|
91 |
rslt = dev.ReadI2CData(this.addressToRead(), ref tmpdata, 1);
|
|
|
92 |
data = tmpdata[0];
|
|
|
93 |
} else if (bank == 1) {
|
|
|
94 |
byte[] cmd = new byte[] { 0x13 };
|
|
|
95 |
rslt = dev.WriteI2cData(this.addressToWrite(), cmd, 1);
|
|
|
96 |
|
|
|
97 |
cmd = new byte[] { 0xff };
|
|
|
98 |
rslt = dev.ReadI2CData(this.addressToRead(), ref tmpdata, 1);
|
|
|
99 |
data = tmpdata[0];
|
|
|
100 |
}
|
|
|
101 |
return rslt;
|
|
|
102 |
}
|
|
|
103 |
|
| 163 |
pfowler |
104 |
private byte addressToWrite() {
|
|
|
105 |
byte write = (byte)(this.address << 1);
|
|
|
106 |
return write;
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
private byte addressToRead() {
|
|
|
110 |
byte read = (byte)((this.address << 1) | 0x01);
|
|
|
111 |
return read;
|
|
|
112 |
}
|
|
|
113 |
}
|
|
|
114 |
}
|