158 |
pfowler |
1 |
using MCP2221;
|
|
|
2 |
using System;
|
|
|
3 |
using System.Collections.Generic;
|
|
|
4 |
using System.Linq;
|
|
|
5 |
using System.Text;
|
|
|
6 |
using System.Threading.Tasks;
|
|
|
7 |
using System.Timers;
|
|
|
8 |
using WindowsInput;
|
|
|
9 |
using WindowsInput.Native;
|
|
|
10 |
|
|
|
11 |
namespace nitdcscore{
|
|
|
12 |
class NITAHFSDevice {
|
|
|
13 |
|
|
|
14 |
DcsBios _bios;
|
|
|
15 |
Bridge_mcp2221 _mcp;
|
|
|
16 |
|
|
|
17 |
InputSimulator sendkey = new InputSimulator();
|
|
|
18 |
|
|
|
19 |
private readonly uint pot_threshold = 5;
|
|
|
20 |
private readonly Object _lockObject = new object();
|
|
|
21 |
private Boolean dev_init = false;
|
|
|
22 |
|
|
|
23 |
uint _prev_inputs = 0;
|
|
|
24 |
public uint inputs { get; set; }
|
|
|
25 |
|
|
|
26 |
ushort _prev_adc { get; set; }
|
|
|
27 |
public ushort adc { get; set; }
|
|
|
28 |
|
|
|
29 |
public NITAHFSDevice(ref DcsBios bios, Bridge_mcp2221 mcp) {
|
|
|
30 |
_bios = bios;
|
|
|
31 |
_mcp = mcp;
|
|
|
32 |
|
|
|
33 |
}
|
|
|
34 |
|
|
|
35 |
public void refresh(object sender, ElapsedEventArgs e) {
|
|
|
36 |
this.GetInputs();
|
|
|
37 |
|
|
|
38 |
if (_prev_inputs != inputs) {
|
|
|
39 |
Console.WriteLine("AHFS Input: " + (inputs).ToString("X"));
|
|
|
40 |
|
|
|
41 |
// Train - Safe - Arm
|
|
|
42 |
Switch3Pos(8, 9, "AHCP_MASTER_ARM");
|
|
|
43 |
|
|
|
44 |
// Gunarm - Safe - Arm
|
|
|
45 |
Switch3Pos(10, 11, "AHCP_GUNPAC");
|
|
|
46 |
|
|
|
47 |
// Train - Safe - Arm
|
|
|
48 |
Switch3Pos(12, 13, "AHCP_LASER_ARM");
|
|
|
49 |
|
|
|
50 |
// Off - On
|
|
|
51 |
Switch2Pos(14, "AHCP_TGP");
|
|
|
52 |
|
|
|
53 |
// Radar - Delta - Baro
|
|
|
54 |
Switch3Pos(0, 1, "AHCP_ALT_SCE");
|
|
|
55 |
|
|
|
56 |
// Night - Day
|
|
|
57 |
Switch2Pos(2, "AHCP_HUD_DAYNIGHT");
|
|
|
58 |
|
|
|
59 |
//Stby - Norm
|
|
|
60 |
Switch2Pos(15, "AHCP_HUD_MODE");
|
|
|
61 |
|
|
|
62 |
// Off - On
|
|
|
63 |
Switch2Pos(3, "AHCP_CICU");
|
|
|
64 |
|
|
|
65 |
// Off - On
|
|
|
66 |
Switch2Pos(4, "AHCP_JTRS");
|
|
|
67 |
|
|
|
68 |
//Off - Test - On
|
|
|
69 |
Switch3Pos(6, 5, "AHCP_IFFCC");
|
|
|
70 |
|
|
|
71 |
//
|
|
|
72 |
Switch2Pos(7, "HARS_FAST_ERECT");
|
|
|
73 |
|
|
|
74 |
// Fuel System
|
|
|
75 |
|
|
|
76 |
//
|
|
|
77 |
Switch2Pos(16, "FSCP_AMPL");
|
|
|
78 |
//
|
|
|
79 |
Switch2Pos(24, "FSCP_BOOST_MAIN_L");
|
|
|
80 |
//
|
|
|
81 |
Switch2Pos(25, "FSCP_BOOST_MAIN_R");
|
|
|
82 |
//
|
|
|
83 |
Switch2Pos(26, "FSCP_BOOST_WING_L");
|
|
|
84 |
//
|
|
|
85 |
Switch2Pos(27, "FSCP_BOOST_WING_R");
|
|
|
86 |
//
|
|
|
87 |
Switch2Pos(28, "FSCP_CROSSFEED");
|
|
|
88 |
//
|
|
|
89 |
Switch2Pos(30, "FSCP_EXT_TANKS_FUS");
|
|
|
90 |
//
|
|
|
91 |
Switch2Pos(31, "FSCP_EXT_TANKS_WING");
|
|
|
92 |
//
|
|
|
93 |
Switch2Pos(20, "FSCP_FD_MAIN_L", true);
|
|
|
94 |
//
|
|
|
95 |
Switch2Pos(21, "FSCP_FD_MAIN_R", true);
|
|
|
96 |
//
|
|
|
97 |
Switch2Pos(18, "FSCP_FD_WING_L", true);
|
|
|
98 |
//
|
|
|
99 |
Switch2Pos(19, "FSCP_FD_WING_R", true);
|
|
|
100 |
//
|
|
|
101 |
Switch2Pos(17, "FSCP_LINE_CHECK");
|
|
|
102 |
//
|
|
|
103 |
Switch2Pos(23, "FSCP_RCVR_LEVER"); // Technically a 3pos, but 3rd not needed
|
|
|
104 |
//
|
|
|
105 |
Switch2Pos(29, "FSCP_TK_GATE");
|
|
|
106 |
|
|
|
107 |
|
|
|
108 |
_prev_inputs = inputs;
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
// Do the Refueling light adc
|
|
|
112 |
ushort lowerval = 0;
|
|
|
113 |
if (_prev_adc >= 5)
|
|
|
114 |
lowerval = (ushort)(_prev_adc - pot_threshold);
|
|
|
115 |
|
|
|
116 |
ushort upperval = 1023;
|
|
|
117 |
if (_prev_adc <= 1018)
|
|
|
118 |
upperval = (ushort)(_prev_adc + pot_threshold);
|
|
|
119 |
|
|
|
120 |
if ((adc <= lowerval) || (adc >= upperval)) {
|
|
|
121 |
_prev_adc = adc;
|
|
|
122 |
//Console.WriteLine("ADC Input: " + (adc).ToString());
|
|
|
123 |
ushort refuellight = map_ushort(adc, 0, 930, 0, 0xffff);
|
|
|
124 |
_bios.SendData("ALCP_RCVR_LTS " + refuellight.ToString() + "\n");
|
|
|
125 |
}
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
public uint Switch2Pos(int pin, String cmd, Boolean invert = false) {
|
|
|
129 |
uint chg = (uint)(this._prev_inputs >> pin) & 0x01;
|
|
|
130 |
uint norm = (uint)(this.inputs >> pin) & 0x01;
|
|
|
131 |
uint value = 0;
|
|
|
132 |
|
|
|
133 |
if ((uint)(norm) == 1) {
|
|
|
134 |
value = (uint)(invert ? 0 : 1);
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
if (norm != chg) {
|
|
|
138 |
_bios.SendData(cmd + " " + value.ToString() + "\n");
|
|
|
139 |
Console.WriteLine(cmd + ":" + value.ToString());
|
|
|
140 |
}
|
|
|
141 |
return value;
|
|
|
142 |
}
|
|
|
143 |
|
|
|
144 |
public uint Switch3Pos(int pin0, int pin1, String cmd, Boolean invert = false) {
|
|
|
145 |
uint value = 1;
|
|
|
146 |
uint chg0 = (uint)(this._prev_inputs >> pin0) & 0x01;
|
|
|
147 |
uint chg1 = (uint)(this._prev_inputs >> pin1) & 0x01;
|
|
|
148 |
uint nrm0 = (uint)(this.inputs >> pin0) & 0x01;
|
|
|
149 |
uint nrm1 = (uint)(this.inputs >> pin1) & 0x01;
|
|
|
150 |
|
|
|
151 |
if ((uint)nrm0 == 1)
|
|
|
152 |
value = (uint)(invert ? 2 : 0);
|
|
|
153 |
else if ((uint)nrm1 == 1)
|
|
|
154 |
value = (uint)(invert ? 0 : 2);
|
|
|
155 |
|
|
|
156 |
if ((nrm0 != chg0) || (nrm1 != chg1)) {
|
|
|
157 |
_bios.SendData(cmd + " " + value.ToString() + "\n");
|
|
|
158 |
Console.WriteLine(cmd + ":" + value.ToString());
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
return value;
|
|
|
162 |
}
|
|
|
163 |
|
|
|
164 |
public ushort map_ushort(ushort x, ushort in_min, ushort in_max, ushort out_min, ushort out_max) {
|
|
|
165 |
return (ushort)((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min);
|
|
|
166 |
}
|
|
|
167 |
|
|
|
168 |
public int init() {
|
|
|
169 |
// Select our CDU power module
|
|
|
170 |
int rslt = _mcp.SelectDevice("AHCP/FSCP Panel");
|
|
|
171 |
if (rslt != 0) {
|
|
|
172 |
return rslt;
|
|
|
173 |
}
|
|
|
174 |
|
|
|
175 |
// Get the initial ADC value
|
|
|
176 |
this._prev_adc = this.adc = _mcp.ReadADC(1);
|
|
|
177 |
|
|
|
178 |
|
|
|
179 |
// Enable the mcp23017
|
|
|
180 |
_mcp.WriteGpio(3, 1);
|
|
|
181 |
|
|
|
182 |
// Set io dir, pullups and rev polarity
|
|
|
183 |
byte[] data;
|
|
|
184 |
// Soldered a couple of inputs wrong, the f0 is to reverse them (fuel boost switches)
|
|
|
185 |
data = new byte[] { 0x00, 0xff, 0xff, 0xf0, 0xff }; // All inputs & polarity
|
|
|
186 |
_mcp.WriteI2cData(0x40, data, 5);
|
|
|
187 |
data = new byte[] { 0x0c, 0xff, 0xff }; // All pullups = on
|
|
|
188 |
_mcp.WriteI2cData(0x40, data, 3);
|
|
|
189 |
|
|
|
190 |
data = new byte[] { 0x00, 0xff, 0xff, 0xff, 0xff }; // All inputs & polarity
|
|
|
191 |
_mcp.WriteI2cData(0x42, data, 5);
|
|
|
192 |
data = new byte[] { 0x0c, 0xff, 0xff }; // All pullups = on
|
|
|
193 |
_mcp.WriteI2cData(0x42, data, 3);
|
|
|
194 |
|
|
|
195 |
this.GetInputs();
|
|
|
196 |
this._prev_inputs = this.inputs;
|
|
|
197 |
|
|
|
198 |
this.dev_init = true;
|
|
|
199 |
return 0;
|
|
|
200 |
}
|
|
|
201 |
|
|
|
202 |
public void Enable() {
|
|
|
203 |
if (!this.dev_init)
|
|
|
204 |
this.init();
|
|
|
205 |
|
|
|
206 |
}
|
|
|
207 |
public void Disable() {
|
|
|
208 |
|
|
|
209 |
}
|
|
|
210 |
|
|
|
211 |
public void GetInputs() {
|
|
|
212 |
|
|
|
213 |
lock (_lockObject) {
|
|
|
214 |
this.adc = _mcp.ReadADC(1);
|
|
|
215 |
}
|
|
|
216 |
|
|
|
217 |
byte[] data;
|
|
|
218 |
data = new byte[] { 0x12 }; // Select GPIOA register
|
|
|
219 |
_mcp.WriteI2cData(0x40, data, 1);
|
|
|
220 |
|
|
|
221 |
data = new byte[] { 0x00, 0x00 }; // Read two bytes
|
|
|
222 |
int rslt = _mcp.ReadI2CData(0x41, ref data, 2);
|
|
|
223 |
|
|
|
224 |
this.inputs = (uint)data[0] << 24;
|
|
|
225 |
this.inputs |= (uint)data[1] << 16;
|
|
|
226 |
|
|
|
227 |
data = new byte[] { 0x12 }; // Select GPIOA register
|
|
|
228 |
_mcp.WriteI2cData(0x42, data, 1);
|
|
|
229 |
|
|
|
230 |
data = new byte[] { 0x00, 0x00 }; // Read two bytes
|
|
|
231 |
rslt = _mcp.ReadI2CData(0x43, ref data, 2);
|
|
|
232 |
|
|
|
233 |
this.inputs += (uint)data[0] << 8;
|
|
|
234 |
this.inputs |= (uint)data[1];
|
|
|
235 |
|
|
|
236 |
|
|
|
237 |
}
|
|
|
238 |
}
|
|
|
239 |
}
|