Subversion Repositories group.electronics

Rev

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

Rev Author Line No. Line
158 pfowler 1
using MCP2221;
2
using System;
3
using System.Collections.Generic;
4
using System.Linq;
5
using System.Reflection;
6
using System.Text;
7
using System.Threading.Tasks;
8
using System.Timers;
159 pfowler 9
using WindowsInput;
10
using WindowsInput.Native;
158 pfowler 11
 
12
namespace nitdcscore {
13
 
163 pfowler 14
    public class mcp2221 : i2cmaster {
160 pfowler 15
        public MchpUsbI2c usbi2c = new MchpUsbI2c();
158 pfowler 16
 
163 pfowler 17
        public List<Panel> panels = new List<Panel>();
158 pfowler 18
        public uint speed { get; set; }
19
        Timer refresh = new Timer();
20
 
21
 
22
        public mcp2221() {
23
            speed = 400000;
24
 
162 pfowler 25
            refresh.Interval = 20;
159 pfowler 26
            refresh.AutoReset = true;
27
            refresh.Elapsed += refresh_Elapsed;
28
            refresh.Enabled = false;
29
 
158 pfowler 30
            usbi2c.Settings.GetConnectionStatus();
163 pfowler 31
            int devcount = usbi2c.Management.GetDevCount();
158 pfowler 32
            Console.WriteLine(devcount.ToString() + " devices found");
33
            for (int i = 0; i < devcount; i++) {
163 pfowler 34
 
35
                if (this.SelectDev(i) != 0) {
36
                    Console.WriteLine("Could not attach device #: " + i);
37
                    continue;
38
                }
39
 
158 pfowler 40
                string usbDescriptor = this.usbi2c.Settings.GetUsbStringDescriptor();
41
                if (usbDescriptor == "AHCP/FSCP Panel") {
161 pfowler 42
                    panels.Add(new Panel_AHFS(this));
162 pfowler 43
                    Console.WriteLine("Found AHCP/FSCP Panel");
158 pfowler 44
                } else if (usbDescriptor == "AAP Panel") {
161 pfowler 45
                    panels.Add(new Panel_AAP(this));
162 pfowler 46
                    Console.WriteLine("Found AAP Panel");
47
                } else if (usbDescriptor == "Multi-Panel Combo") {
48
                    panels.Add(new NitMultiPanel(this));
49
                    Console.WriteLine("Found Multi-Panel Combo");
158 pfowler 50
                }
51
            }
52
 
159 pfowler 53
 
158 pfowler 54
        }
55
 
56
        private Boolean enabled;
57
        public Boolean Enabled {
58
            get {
59
                return enabled;
60
            }
61
            set {
163 pfowler 62
                if (panels.Count == 0) {
161 pfowler 63
                    this.enabled = false;
64
                    refresh.Enabled = false;
159 pfowler 65
                    return;
161 pfowler 66
                }
159 pfowler 67
 
161 pfowler 68
                this.enabled = value;
69
                refresh.Enabled = value;
158 pfowler 70
            }
71
        }
72
 
159 pfowler 73
 
74
 
158 pfowler 75
        void refresh_Elapsed(object sender, ElapsedEventArgs e) {
76
            if (!Enabled)
77
                return;
78
 
159 pfowler 79
            refresh.Enabled = false;
161 pfowler 80
            foreach (Panel panel in panels) {
163 pfowler 81
                if (this.SelectDev(panel.id) != 0)
82
                    continue;
159 pfowler 83
 
161 pfowler 84
                if (!panel.inputChanged)
85
                    panel.Refresh();
159 pfowler 86
 
161 pfowler 87
                if (panel.inputChanged)
88
                    panel.Input();
159 pfowler 89
            }
161 pfowler 90
            refresh.Enabled = true;
158 pfowler 91
 
159 pfowler 92
        }
93
 
163 pfowler 94
        public int SelectDev(int id) {
95
 
96
            int rslt = usbi2c.Management.SelectDev(id);
97
            usbi2c.Settings.GetConnectionStatus();
98
 
99
            return rslt;
100
        }
101
 
158 pfowler 102
        public byte ReadGpio(byte pinNum) {
103
            return Convert.ToByte(usbi2c.Functions.ReadGpioPinValue(pinNum));
104
        }
105
 
106
        public ushort ReadADC(byte pinNum) {
107
            ushort[] adcData = { 0, 0, 0, 0, 0, 0 };
108
 
109
            int rslt = usbi2c.Functions.GetAdcData(adcData);
110
 
111
            return adcData[pinNum];
112
        }
113
 
114
        public void WriteGpio(byte pinNum, byte value) {
115
            this.usbi2c.Functions.WriteGpioPinValue(pinNum, value);
116
        }
117
 
118
        public int WriteI2cData(byte address, byte[] data, uint count) {
119
            int rslt = this.usbi2c.Functions.WriteI2cData(address, data, count, this.speed);
120
            return rslt;
121
        }
122
 
123
        public int ReadI2CData(byte address, ref byte[] data, uint count) {
124
            int rslt = this.usbi2c.Functions.ReadI2cData(address, data, count, this.speed);
125
            return rslt;
126
        }
127
 
128
    }
129
}