Subversion Repositories group.electronics

Rev

Rev 167 | Rev 181 | 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;
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");
158 pfowler 59
                }
60
            }
61
 
159 pfowler 62
 
158 pfowler 63
        }
64
 
164 pfowler 65
        void display_Elapsed(object sender, ElapsedEventArgs e) {
66
            Double avg = ts.TotalMilliseconds / refreshCount;
67
            Console.WriteLine("Average Refresh: " + avg);
68
        }
69
 
158 pfowler 70
        private Boolean enabled;
71
        public Boolean Enabled {
72
            get {
73
                return enabled;
74
            }
75
            set {
163 pfowler 76
                if (panels.Count == 0) {
161 pfowler 77
                    this.enabled = false;
78
                    refresh.Enabled = false;
159 pfowler 79
                    return;
161 pfowler 80
                }
159 pfowler 81
 
161 pfowler 82
                this.enabled = value;
83
                refresh.Enabled = value;
158 pfowler 84
            }
85
        }
86
 
159 pfowler 87
 
88
 
158 pfowler 89
        void refresh_Elapsed(object sender, ElapsedEventArgs e) {
90
            if (!Enabled)
91
                return;
92
 
164 pfowler 93
            Stopwatch sw = new Stopwatch();
94
            sw.Start();
159 pfowler 95
            refresh.Enabled = false;
161 pfowler 96
            foreach (Panel panel in panels) {
163 pfowler 97
                if (this.SelectDev(panel.id) != 0)
98
                    continue;
159 pfowler 99
 
167 pfowler 100
                if (!panel.data.changed)
161 pfowler 101
                    panel.Refresh();
159 pfowler 102
 
167 pfowler 103
                if (panel.data.changed)
161 pfowler 104
                    panel.Input();
172 pfowler 105
 
159 pfowler 106
            }
161 pfowler 107
            refresh.Enabled = true;
164 pfowler 108
            sw.Stop();
109
            ts += sw.Elapsed;
110
            refreshCount++;
159 pfowler 111
        }
112
 
163 pfowler 113
        public int SelectDev(int id) {
114
 
115
            int rslt = usbi2c.Management.SelectDev(id);
116
            usbi2c.Settings.GetConnectionStatus();
117
 
118
            return rslt;
119
        }
120
 
158 pfowler 121
        public byte ReadGpio(byte pinNum) {
122
            return Convert.ToByte(usbi2c.Functions.ReadGpioPinValue(pinNum));
123
        }
124
 
125
        public ushort ReadADC(byte pinNum) {
126
            ushort[] adcData = { 0, 0, 0, 0, 0, 0 };
127
 
128
            int rslt = usbi2c.Functions.GetAdcData(adcData);
129
 
130
            return adcData[pinNum];
131
        }
132
 
133
        public void WriteGpio(byte pinNum, byte value) {
134
            this.usbi2c.Functions.WriteGpioPinValue(pinNum, value);
135
        }
136
 
137
        public int WriteI2cData(byte address, byte[] data, uint count) {
138
            int rslt = this.usbi2c.Functions.WriteI2cData(address, data, count, this.speed);
139
            return rslt;
140
        }
141
 
142
        public int ReadI2CData(byte address, ref byte[] data, uint count) {
143
            int rslt = this.usbi2c.Functions.ReadI2cData(address, data, count, this.speed);
144
            return rslt;
145
        }
146
 
147
    }
148
}