Subversion Repositories group.electronics

Rev

Rev 160 | Rev 162 | 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
 
161 pfowler 47
            refresh.Interval = 500;
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));
158 pfowler 62
                } else if (usbDescriptor == "AAP Panel") {
63
                    this.hasAAP = true;
161 pfowler 64
                    panels.Add(new Panel_AAP(this));
158 pfowler 65
                }
66
            }
67
 
159 pfowler 68
 
158 pfowler 69
        }
70
 
71
        private Boolean enabled;
72
        public Boolean Enabled {
73
            get {
74
                return enabled;
75
            }
76
            set {
161 pfowler 77
                if (this.devcount == 0) {
78
                    this.enabled = false;
79
                    refresh.Enabled = false;
159 pfowler 80
                    return;
161 pfowler 81
                }
159 pfowler 82
 
161 pfowler 83
                this.enabled = value;
84
                refresh.Enabled = value;
158 pfowler 85
            }
86
        }
87
 
159 pfowler 88
 
89
 
158 pfowler 90
        void refresh_Elapsed(object sender, ElapsedEventArgs e) {
91
            if (!Enabled)
92
                return;
93
 
159 pfowler 94
            refresh.Enabled = false;
161 pfowler 95
            foreach (Panel panel in panels) {
96
                usbi2c.Management.SelectDev(panel.id);
97
                usbi2c.Settings.GetConnectionStatus();
159 pfowler 98
 
161 pfowler 99
                if (!panel.inputChanged)
100
                    panel.Refresh();
159 pfowler 101
 
161 pfowler 102
                if (panel.inputChanged)
103
                    panel.Input();
159 pfowler 104
            }
161 pfowler 105
            refresh.Enabled = true;
158 pfowler 106
 
159 pfowler 107
        }
108
 
158 pfowler 109
        public byte ReadGpio(byte pinNum) {
110
            return Convert.ToByte(usbi2c.Functions.ReadGpioPinValue(pinNum));
111
        }
112
 
113
        public ushort ReadADC(byte pinNum) {
114
            ushort[] adcData = { 0, 0, 0, 0, 0, 0 };
115
 
116
            int rslt = usbi2c.Functions.GetAdcData(adcData);
117
 
118
            return adcData[pinNum];
119
        }
120
 
121
        public void WriteGpio(byte pinNum, byte value) {
122
            this.usbi2c.Functions.WriteGpioPinValue(pinNum, value);
123
        }
124
 
125
        public int WriteI2cData(byte address, byte[] data, uint count) {
126
            int rslt = this.usbi2c.Functions.WriteI2cData(address, data, count, this.speed);
127
            return rslt;
128
        }
129
 
130
        public int ReadI2CData(byte address, ref byte[] data, uint count) {
131
            int rslt = this.usbi2c.Functions.ReadI2cData(address, data, count, this.speed);
132
            return rslt;
133
        }
134
 
135
    }
136
}