Subversion Repositories group.electronics

Rev

Rev 161 | Rev 163 | 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
    public struct devdata {
14
        public uint prev_input;
15
        public uint cur_input;
159 pfowler 16
        public ushort cur_adc;
17
        public ushort prev_adc;
18
        public ushort max_adc;
19
        public ushort adc_thres;
158 pfowler 20
 
21
        public int id;
22
        public string name;
23
        public string function;
24
    }
25
 
26
    public class mcp2221 {
160 pfowler 27
        public MchpUsbI2c usbi2c = new MchpUsbI2c();
158 pfowler 28
        public Boolean hasAHFS { get; set; }
29
        public Boolean hasAAP { get; set; }
30
 
31
        int idx { get; set; }
32
        int devcount { get; set; }
33
 
34
        public uint speed { get; set; }
35
 
36
        Timer refresh = new Timer();
159 pfowler 37
        Timer delay = new Timer();
158 pfowler 38
 
39
        private readonly Object lockobject = new object();
161 pfowler 40
        public List<Panel> panels = new List<Panel>();
158 pfowler 41
 
42
 
43
        public mcp2221() {
44
            speed = 400000;
45
            idx = 0;
46
 
162 pfowler 47
            refresh.Interval = 20;
159 pfowler 48
            refresh.AutoReset = true;
49
            refresh.Elapsed += refresh_Elapsed;
50
            refresh.Enabled = false;
51
 
158 pfowler 52
            usbi2c.Settings.GetConnectionStatus();
53
            devcount = usbi2c.Management.GetDevCount();
54
            Console.WriteLine(devcount.ToString() + " devices found");
55
            for (int i = 0; i < devcount; i++) {
56
                int rslt = usbi2c.Management.SelectDev(i);
57
                usbi2c.Settings.GetConnectionStatus();
58
                string usbDescriptor = this.usbi2c.Settings.GetUsbStringDescriptor();
59
                if (usbDescriptor == "AHCP/FSCP Panel") {
60
                    this.hasAHFS = true;
161 pfowler 61
                    panels.Add(new Panel_AHFS(this));
162 pfowler 62
                    Console.WriteLine("Found AHCP/FSCP Panel");
158 pfowler 63
                } else if (usbDescriptor == "AAP Panel") {
64
                    this.hasAAP = true;
161 pfowler 65
                    panels.Add(new Panel_AAP(this));
162 pfowler 66
                    Console.WriteLine("Found AAP Panel");
67
                } else if (usbDescriptor == "Multi-Panel Combo") {
68
                    panels.Add(new NitMultiPanel(this));
69
                    Console.WriteLine("Found Multi-Panel Combo");
158 pfowler 70
                }
71
            }
72
 
159 pfowler 73
 
158 pfowler 74
        }
75
 
76
        private Boolean enabled;
77
        public Boolean Enabled {
78
            get {
79
                return enabled;
80
            }
81
            set {
161 pfowler 82
                if (this.devcount == 0) {
83
                    this.enabled = false;
84
                    refresh.Enabled = false;
159 pfowler 85
                    return;
161 pfowler 86
                }
159 pfowler 87
 
161 pfowler 88
                this.enabled = value;
89
                refresh.Enabled = value;
158 pfowler 90
            }
91
        }
92
 
159 pfowler 93
 
94
 
158 pfowler 95
        void refresh_Elapsed(object sender, ElapsedEventArgs e) {
96
            if (!Enabled)
97
                return;
98
 
159 pfowler 99
            refresh.Enabled = false;
161 pfowler 100
            foreach (Panel panel in panels) {
101
                usbi2c.Management.SelectDev(panel.id);
102
                usbi2c.Settings.GetConnectionStatus();
159 pfowler 103
 
161 pfowler 104
                if (!panel.inputChanged)
105
                    panel.Refresh();
159 pfowler 106
 
161 pfowler 107
                if (panel.inputChanged)
108
                    panel.Input();
159 pfowler 109
            }
161 pfowler 110
            refresh.Enabled = true;
158 pfowler 111
 
159 pfowler 112
        }
113
 
158 pfowler 114
        public byte ReadGpio(byte pinNum) {
115
            return Convert.ToByte(usbi2c.Functions.ReadGpioPinValue(pinNum));
116
        }
117
 
118
        public ushort ReadADC(byte pinNum) {
119
            ushort[] adcData = { 0, 0, 0, 0, 0, 0 };
120
 
121
            int rslt = usbi2c.Functions.GetAdcData(adcData);
122
 
123
            return adcData[pinNum];
124
        }
125
 
126
        public void WriteGpio(byte pinNum, byte value) {
127
            this.usbi2c.Functions.WriteGpioPinValue(pinNum, value);
128
        }
129
 
130
        public int WriteI2cData(byte address, byte[] data, uint count) {
131
            int rslt = this.usbi2c.Functions.WriteI2cData(address, data, count, this.speed);
132
            return rslt;
133
        }
134
 
135
        public int ReadI2CData(byte address, ref byte[] data, uint count) {
136
            int rslt = this.usbi2c.Functions.ReadI2cData(address, data, count, this.speed);
137
            return rslt;
138
        }
139
 
140
    }
141
}