Subversion Repositories group.electronics

Rev

Rev 181 | 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;
164 pfowler 4
using System.Diagnostics;
158 pfowler 5
using System.Linq;
6
using System.Reflection;
7
using System.Text;
8
using System.Threading.Tasks;
9
using System.Timers;
159 pfowler 10
using WindowsInput;
11
using WindowsInput.Native;
158 pfowler 12
 
13
namespace nitdcscore {
14
 
163 pfowler 15
    public class mcp2221 : i2cmaster {
160 pfowler 16
        public MchpUsbI2c usbi2c = new MchpUsbI2c();
158 pfowler 17
 
163 pfowler 18
        public List<Panel> panels = new List<Panel>();
158 pfowler 19
        public uint speed { get; set; }
20
        Timer refresh = new Timer();
164 pfowler 21
        Timer display = new Timer();
22
        TimeSpan ts = new TimeSpan();
23
        UInt64 refreshCount = 0;
158 pfowler 24
 
25
 
26
        public mcp2221() {
27
            speed = 400000;
28
 
164 pfowler 29
            refresh.Interval = 5;
159 pfowler 30
            refresh.AutoReset = true;
31
            refresh.Elapsed += refresh_Elapsed;
32
            refresh.Enabled = false;
33
 
164 pfowler 34
            display.Interval = 1000;
35
            display.AutoReset = true;
36
            display.Enabled = false;
37
            display.Elapsed += display_Elapsed;
38
 
158 pfowler 39
            usbi2c.Settings.GetConnectionStatus();
163 pfowler 40
            int devcount = usbi2c.Management.GetDevCount();
158 pfowler 41
            Console.WriteLine(devcount.ToString() + " devices found");
42
            for (int i = 0; i < devcount; i++) {
163 pfowler 43
 
44
                if (this.SelectDev(i) != 0) {
45
                    Console.WriteLine("Could not attach device #: " + i);
46
                    continue;
47
                }
48
 
158 pfowler 49
                string usbDescriptor = this.usbi2c.Settings.GetUsbStringDescriptor();
50
                if (usbDescriptor == "AHCP/FSCP Panel") {
161 pfowler 51
                    panels.Add(new Panel_AHFS(this));
162 pfowler 52
                    Console.WriteLine("Found AHCP/FSCP Panel");
158 pfowler 53
                } else if (usbDescriptor == "AAP Panel") {
161 pfowler 54
                    panels.Add(new Panel_AAP(this));
162 pfowler 55
                    Console.WriteLine("Found AAP Panel");
56
                } else if (usbDescriptor == "Multi-Panel Combo") {
57
                    panels.Add(new NitMultiPanel(this));
58
                    Console.WriteLine("Found Multi-Panel Combo");
181 pfowler 59
                } else if (usbDescriptor == "Aux Panel") {
60
                    panels.Add(new NitAuxPanel(this));
61
                    Console.WriteLine("Found Aux Panel");
158 pfowler 62
                }
63
            }
64
 
159 pfowler 65
 
158 pfowler 66
        }
67
 
164 pfowler 68
        void display_Elapsed(object sender, ElapsedEventArgs e) {
69
            Double avg = ts.TotalMilliseconds / refreshCount;
70
            Console.WriteLine("Average Refresh: " + avg);
71
        }
72
 
158 pfowler 73
        private Boolean enabled;
74
        public Boolean Enabled {
75
            get {
76
                return enabled;
77
            }
78
            set {
163 pfowler 79
                if (panels.Count == 0) {
161 pfowler 80
                    this.enabled = false;
81
                    refresh.Enabled = false;
159 pfowler 82
                    return;
161 pfowler 83
                }
159 pfowler 84
 
161 pfowler 85
                this.enabled = value;
86
                refresh.Enabled = value;
158 pfowler 87
            }
88
        }
89
 
159 pfowler 90
 
91
 
158 pfowler 92
        void refresh_Elapsed(object sender, ElapsedEventArgs e) {
93
            if (!Enabled)
94
                return;
95
 
164 pfowler 96
            Stopwatch sw = new Stopwatch();
97
            sw.Start();
159 pfowler 98
            refresh.Enabled = false;
161 pfowler 99
            foreach (Panel panel in panels) {
163 pfowler 100
                if (this.SelectDev(panel.id) != 0)
101
                    continue;
159 pfowler 102
 
167 pfowler 103
                if (!panel.data.changed)
161 pfowler 104
                    panel.Refresh();
159 pfowler 105
 
167 pfowler 106
                if (panel.data.changed)
161 pfowler 107
                    panel.Input();
172 pfowler 108
 
159 pfowler 109
            }
161 pfowler 110
            refresh.Enabled = true;
164 pfowler 111
            sw.Stop();
112
            ts += sw.Elapsed;
113
            refreshCount++;
159 pfowler 114
        }
115
 
163 pfowler 116
        public int SelectDev(int id) {
117
 
118
            int rslt = usbi2c.Management.SelectDev(id);
119
            usbi2c.Settings.GetConnectionStatus();
120
 
121
            return rslt;
122
        }
123
 
158 pfowler 124
        public byte ReadGpio(byte pinNum) {
125
            return Convert.ToByte(usbi2c.Functions.ReadGpioPinValue(pinNum));
126
        }
127
 
128
        public ushort ReadADC(byte pinNum) {
129
            ushort[] adcData = { 0, 0, 0, 0, 0, 0 };
130
 
131
            int rslt = usbi2c.Functions.GetAdcData(adcData);
132
 
133
            return adcData[pinNum];
134
        }
135
 
136
        public void WriteGpio(byte pinNum, byte value) {
137
            this.usbi2c.Functions.WriteGpioPinValue(pinNum, value);
138
        }
139
 
140
        public int WriteI2cData(byte address, byte[] data, uint count) {
141
            int rslt = this.usbi2c.Functions.WriteI2cData(address, data, count, this.speed);
142
            return rslt;
143
        }
144
 
145
        public int ReadI2CData(byte address, ref byte[] data, uint count) {
146
            int rslt = this.usbi2c.Functions.ReadI2cData(address, data, count, this.speed);
147
            return rslt;
148
        }
149
 
184 pfowler 150
        public void SetGpio(byte pin, byte data) {
151
            this.usbi2c.Functions.WriteGpioPinValue(pin, data);
152
        }
153
 
158 pfowler 154
    }
155
}