Subversion Repositories group.electronics

Rev

Rev 164 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
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
 
164 pfowler 28
        public int SetIOPolarity(byte gpioa, byte gpiob) {
29
            byte[] data = new byte[] { 0x02, gpioa, gpiob };
30
            int rslt = dev.WriteI2cData(this.addressToWrite(), data, 3);
31
            return rslt;
32
        }
33
 
163 pfowler 34
        public int SetIOPullups(byte gpioa, byte gpiob) {
35
            byte[] data = new byte[] { 0x0c, gpioa, gpiob };
36
            int rslt = dev.WriteI2cData(this.addressToWrite(), data, 3);
37
            return rslt;
38
        }
39
 
40
        public int GetIO(out byte[] data) {
41
            int rslt = 0;
42
            byte[] cmd = new byte[] { 0x12 };
43
            rslt = dev.WriteI2cData(this.addressToWrite(), cmd, 1);
44
 
45
            data = new byte[] { 0xff, 0xff };
46
            rslt = dev.ReadI2CData(this.addressToRead(), ref data, 2);
47
            return 1;
48
        }
49
 
50
        public int SetIO(byte gpioa, byte gpiob) {
51
            int rslt = 0;
52
            byte[] cmd = new byte[] { 0x12, gpioa, gpiob };
53
            rslt = dev.WriteI2cData(this.addressToWrite(), cmd, 3);
54
            return rslt;
55
        }
56
 
171 pfowler 57
        public int SetBank(int bank, byte gpio) {
58
            int rslt = 0;
59
            if (bank == 0) {
60
                byte[] cmd = new byte[] { 0x12, gpio };
61
                rslt = dev.WriteI2cData(this.addressToWrite(), cmd, 2);
62
            } else if (bank == 1) {
63
                byte[] cmd = new byte[] { 0x13, gpio };
64
                rslt = dev.WriteI2cData(this.addressToWrite(), cmd, 2);
65
            }
66
            return rslt;
67
        }
68
 
69
        public int GetBank(int bank, byte gpio, out byte data) {
70
            int rslt = 0;
71
            byte[] tmpdata = { 0x00 };
72
            data = 0;
73
            if (bank == 0) {
74
                byte[] cmd = new byte[] { 0x12 };
75
                rslt = dev.WriteI2cData(this.addressToWrite(), cmd, 1);
76
 
77
                cmd = new byte[] { 0xff };
78
                rslt = dev.ReadI2CData(this.addressToRead(), ref tmpdata, 1);
79
                data = tmpdata[0];
80
            } else if (bank == 1) {
81
                byte[] cmd = new byte[] { 0x13 };
82
                rslt = dev.WriteI2cData(this.addressToWrite(), cmd, 1);
83
 
84
                cmd = new byte[] { 0xff };
85
                rslt = dev.ReadI2CData(this.addressToRead(), ref tmpdata, 1);
86
                data = tmpdata[0];
87
            }
88
            return rslt;
89
        }
90
 
163 pfowler 91
        private byte addressToWrite() {
92
            byte write = (byte)(this.address << 1);
93
            return write;
94
        }
95
 
96
        private byte addressToRead() {
97
            byte read = (byte)((this.address << 1) | 0x01);
98
            return read;
99
        }
100
    }
101
}