Subversion Repositories group.electronics

Rev

Rev 163 | Rev 171 | 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
 
57
        private byte addressToWrite() {
58
            byte write = (byte)(this.address << 1);
59
            return write;
60
        }
61
 
62
        private byte addressToRead() {
63
            byte read = (byte)((this.address << 1) | 0x01);
64
            return read;
65
        }
66
    }
67
}