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;
|
|
|
9 |
|
|
|
10 |
namespace nitdcscore {
|
|
|
11 |
public struct devdata {
|
|
|
12 |
public uint prev_input;
|
|
|
13 |
public uint cur_input;
|
|
|
14 |
|
|
|
15 |
public int id;
|
|
|
16 |
public string name;
|
|
|
17 |
public string function;
|
|
|
18 |
}
|
|
|
19 |
|
|
|
20 |
|
|
|
21 |
|
|
|
22 |
public class mcp2221 {
|
|
|
23 |
DcsBios bios = new DcsBios();
|
|
|
24 |
MchpUsbI2c usbi2c = new MchpUsbI2c();
|
|
|
25 |
public Boolean hasAHFS { get; set; }
|
|
|
26 |
public Boolean hasAAP { get; set; }
|
|
|
27 |
|
|
|
28 |
int idx { get; set; }
|
|
|
29 |
int devcount { get; set; }
|
|
|
30 |
|
|
|
31 |
public uint speed { get; set; }
|
|
|
32 |
|
|
|
33 |
Timer refresh = new Timer();
|
|
|
34 |
|
|
|
35 |
private readonly Object lockobject = new object();
|
|
|
36 |
|
|
|
37 |
public devdata[] devices = new devdata[5];
|
|
|
38 |
|
|
|
39 |
|
|
|
40 |
public mcp2221() {
|
|
|
41 |
speed = 400000;
|
|
|
42 |
idx = 0;
|
|
|
43 |
|
|
|
44 |
bios.InitUDP();
|
|
|
45 |
|
|
|
46 |
usbi2c.Settings.GetConnectionStatus();
|
|
|
47 |
devcount = usbi2c.Management.GetDevCount();
|
|
|
48 |
Console.WriteLine(devcount.ToString() + " devices found");
|
|
|
49 |
for (int i = 0; i < devcount; i++) {
|
|
|
50 |
int rslt = usbi2c.Management.SelectDev(i);
|
|
|
51 |
usbi2c.Settings.GetConnectionStatus();
|
|
|
52 |
string usbDescriptor = this.usbi2c.Settings.GetUsbStringDescriptor();
|
|
|
53 |
if (usbDescriptor == "AHCP/FSCP Panel") {
|
|
|
54 |
this.hasAHFS = true;
|
|
|
55 |
devices[i].cur_input = 0;
|
|
|
56 |
devices[i].prev_input = 0;
|
|
|
57 |
devices[i].id = i;
|
|
|
58 |
devices[i].name = "AHCP/FSCP Panel";
|
|
|
59 |
devices[i].function = "refresh_ahfs";
|
|
|
60 |
|
|
|
61 |
init_ahfs(ref devices[i]);
|
|
|
62 |
} else if (usbDescriptor == "AAP Panel") {
|
|
|
63 |
this.hasAAP = true;
|
|
|
64 |
devices[i].cur_input = 0;
|
|
|
65 |
devices[i].prev_input = 0;
|
|
|
66 |
devices[i].id = i;
|
|
|
67 |
devices[i].name = "AAP Panel";
|
|
|
68 |
devices[i].function = "refresh_aap";
|
|
|
69 |
|
|
|
70 |
init_aap(ref devices[i]);
|
|
|
71 |
}
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
refresh.Interval = 500;
|
|
|
75 |
refresh.AutoReset = true;
|
|
|
76 |
refresh.Elapsed += refresh_Elapsed;
|
|
|
77 |
refresh.Enabled = false;
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
private Boolean enabled;
|
|
|
81 |
public Boolean Enabled {
|
|
|
82 |
get {
|
|
|
83 |
return enabled;
|
|
|
84 |
}
|
|
|
85 |
set {
|
|
|
86 |
this.enabled = true;
|
|
|
87 |
refresh.Enabled = true;
|
|
|
88 |
}
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
void refresh_Elapsed(object sender, ElapsedEventArgs e) {
|
|
|
92 |
if (!Enabled)
|
|
|
93 |
return;
|
|
|
94 |
devdata dev = devices[idx];
|
|
|
95 |
|
|
|
96 |
usbi2c.Management.SelectDev(dev.id);
|
|
|
97 |
|
|
|
98 |
|
|
|
99 |
Type type = this.GetType();
|
|
|
100 |
MethodInfo callfunc = type.GetMethod(dev.function);
|
|
|
101 |
ParameterInfo[] parameters = callfunc.GetParameters();
|
|
|
102 |
object[] parms = { dev };
|
|
|
103 |
callfunc.Invoke(this, parms);
|
|
|
104 |
|
|
|
105 |
|
|
|
106 |
|
|
|
107 |
idx++;
|
|
|
108 |
if (idx >= devcount)
|
|
|
109 |
idx = 0;
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
public void init_aap(ref devdata dev) {
|
|
|
113 |
// Enable the mcp23017
|
|
|
114 |
WriteGpio(3, 1);
|
|
|
115 |
|
|
|
116 |
// Set io dir, pullups and rev polarity
|
|
|
117 |
byte[] data;
|
|
|
118 |
data = new byte[] { 0x00, 0xff, 0xff, 0xff, 0xff }; // All inputs & polarity
|
|
|
119 |
WriteI2cData(0x40, data, 5);
|
|
|
120 |
data = new byte[] { 0x0c, 0xff, 0xff }; // All pullups = on
|
|
|
121 |
WriteI2cData(0x40, data, 3);
|
|
|
122 |
|
|
|
123 |
refresh_aap(ref dev);
|
|
|
124 |
dev.prev_input = dev.cur_input;
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
public void refresh_aap(ref devdata dev) {
|
|
|
128 |
Console.WriteLine("In aap " + usbi2c.Management.GetSelectedDevNum().ToString() + " : " + dev.name + " " + dev.cur_input.ToString("X"));
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
public void init_ahfs(ref devdata dev) {
|
|
|
132 |
// Get the initial ADC value
|
|
|
133 |
//this._prev_adc = this.adc = _mcp.ReadADC(1);
|
|
|
134 |
|
|
|
135 |
// Enable the mcp23017
|
|
|
136 |
WriteGpio(3, 1);
|
|
|
137 |
|
|
|
138 |
// Set io dir, pullups and rev polarity
|
|
|
139 |
byte[] data;
|
|
|
140 |
// Soldered a couple of inputs wrong, the f0 is to reverse them (fuel boost switches)
|
|
|
141 |
data = new byte[] { 0x00, 0xff, 0xff, 0xf0, 0xff }; // All inputs & polarity
|
|
|
142 |
WriteI2cData(0x40, data, 5);
|
|
|
143 |
data = new byte[] { 0x0c, 0xff, 0xff }; // All pullups = on
|
|
|
144 |
WriteI2cData(0x40, data, 3);
|
|
|
145 |
|
|
|
146 |
data = new byte[] { 0x00, 0xff, 0xff, 0xff, 0xff }; // All inputs & polarity
|
|
|
147 |
WriteI2cData(0x42, data, 5);
|
|
|
148 |
data = new byte[] { 0x0c, 0xff, 0xff }; // All pullups = on
|
|
|
149 |
WriteI2cData(0x42, data, 3);
|
|
|
150 |
|
|
|
151 |
refresh_ahfs(ref dev);
|
|
|
152 |
dev.prev_input = dev.cur_input;
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
public void refresh_ahfs(ref devdata dev) {
|
|
|
156 |
byte[] data;
|
|
|
157 |
lock (lockobject) {
|
|
|
158 |
data = new byte[] { 0x12 }; // Select GPIOA register
|
|
|
159 |
WriteI2cData(0x40, data, 1);
|
|
|
160 |
|
|
|
161 |
data = new byte[] { 0x00, 0x00 }; // Read two bytes
|
|
|
162 |
int rslt = ReadI2CData(0x41, ref data, 2);
|
|
|
163 |
|
|
|
164 |
dev.cur_input = (uint)data[0] << 24;
|
|
|
165 |
dev.cur_input |= (uint)data[1] << 16;
|
|
|
166 |
|
|
|
167 |
data = new byte[] { 0x12 }; // Select GPIOA register
|
|
|
168 |
WriteI2cData(0x42, data, 1);
|
|
|
169 |
|
|
|
170 |
data = new byte[] { 0x00, 0x00 }; // Read two bytes
|
|
|
171 |
rslt = ReadI2CData(0x43, ref data, 2);
|
|
|
172 |
|
|
|
173 |
dev.cur_input |= (uint)data[0] << 8;
|
|
|
174 |
dev.cur_input |= (uint)data[1];
|
|
|
175 |
}
|
|
|
176 |
|
|
|
177 |
Console.WriteLine("In ahfs " + usbi2c.Management.GetSelectedDevNum().ToString() + " : " + dev.name + " " + dev.cur_input.ToString("X"));
|
|
|
178 |
}
|
|
|
179 |
|
|
|
180 |
|
|
|
181 |
public byte ReadGpio(byte pinNum) {
|
|
|
182 |
return Convert.ToByte(usbi2c.Functions.ReadGpioPinValue(pinNum));
|
|
|
183 |
}
|
|
|
184 |
|
|
|
185 |
public ushort ReadADC(byte pinNum) {
|
|
|
186 |
ushort[] adcData = { 0, 0, 0, 0, 0, 0 };
|
|
|
187 |
|
|
|
188 |
int rslt = usbi2c.Functions.GetAdcData(adcData);
|
|
|
189 |
|
|
|
190 |
return adcData[pinNum];
|
|
|
191 |
}
|
|
|
192 |
|
|
|
193 |
public void WriteGpio(byte pinNum, byte value) {
|
|
|
194 |
this.usbi2c.Functions.WriteGpioPinValue(pinNum, value);
|
|
|
195 |
}
|
|
|
196 |
|
|
|
197 |
public int WriteI2cData(byte address, byte[] data, uint count) {
|
|
|
198 |
int rslt = this.usbi2c.Functions.WriteI2cData(address, data, count, this.speed);
|
|
|
199 |
return rslt;
|
|
|
200 |
}
|
|
|
201 |
|
|
|
202 |
public int ReadI2CData(byte address, ref byte[] data, uint count) {
|
|
|
203 |
int rslt = this.usbi2c.Functions.ReadI2cData(address, data, count, this.speed);
|
|
|
204 |
return rslt;
|
|
|
205 |
}
|
|
|
206 |
|
|
|
207 |
public uint Switch2Pos(int pin, String cmd, devdata data, Boolean invert = false) {
|
|
|
208 |
uint chg = (uint)(data.prev_input >> pin) & 0x01;
|
|
|
209 |
uint norm = (uint)(data.cur_input >> pin) & 0x01;
|
|
|
210 |
uint value = 0;
|
|
|
211 |
|
|
|
212 |
if ((uint)(norm) == 1) {
|
|
|
213 |
value = (uint)(invert ? 0 : 1);
|
|
|
214 |
}
|
|
|
215 |
|
|
|
216 |
if (norm != chg) {
|
|
|
217 |
bios.SendData(cmd + " " + value.ToString() + "\n");
|
|
|
218 |
Console.WriteLine(cmd + ":" + value.ToString());
|
|
|
219 |
}
|
|
|
220 |
return value;
|
|
|
221 |
}
|
|
|
222 |
|
|
|
223 |
public uint Switch3Pos(int pin0, int pin1, String cmd, devdata data, Boolean invert = false) {
|
|
|
224 |
uint value = 1;
|
|
|
225 |
uint chg0 = (uint)(data.prev_input >> pin0) & 0x01;
|
|
|
226 |
uint chg1 = (uint)(data.prev_input >> pin1) & 0x01;
|
|
|
227 |
uint nrm0 = (uint)(data.cur_input >> pin0) & 0x01;
|
|
|
228 |
uint nrm1 = (uint)(data.cur_input >> pin1) & 0x01;
|
|
|
229 |
|
|
|
230 |
if ((uint)nrm0 == 1)
|
|
|
231 |
value = (uint)(invert ? 2 : 0);
|
|
|
232 |
else if ((uint)nrm1 == 1)
|
|
|
233 |
value = (uint)(invert ? 0 : 2);
|
|
|
234 |
|
|
|
235 |
if ((nrm0 != chg0) || (nrm1 != chg1)) {
|
|
|
236 |
bios.SendData(cmd + " " + value.ToString() + "\n");
|
|
|
237 |
Console.WriteLine(cmd + ":" + value.ToString());
|
|
|
238 |
}
|
|
|
239 |
|
|
|
240 |
return value;
|
|
|
241 |
}
|
|
|
242 |
|
|
|
243 |
public ushort map_ushort(ushort x, ushort in_min, ushort in_max, ushort out_min, ushort out_max) {
|
|
|
244 |
return (ushort)((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min);
|
|
|
245 |
}
|
|
|
246 |
|
|
|
247 |
|
|
|
248 |
}
|
|
|
249 |
}
|