| 160 |
pfowler |
1 |
using System;
|
|
|
2 |
using System.Collections.Generic;
|
|
|
3 |
using System.Linq;
|
|
|
4 |
using System.Text;
|
|
|
5 |
using System.Threading.Tasks;
|
| 162 |
pfowler |
6 |
using WindowsInput.Native;
|
| 160 |
pfowler |
7 |
|
|
|
8 |
namespace nitdcscore {
|
| 167 |
pfowler |
9 |
|
|
|
10 |
public class jsaxis {
|
|
|
11 |
public UInt16 prev { get; set; } // The previous ADC value
|
|
|
12 |
public UInt16 value { get; set; } // The current ADC value
|
|
|
13 |
public UInt16 thres { get; set; } // Threshold to report a new position
|
|
|
14 |
public UInt16 max { get; set; } // The maximum value we've seen
|
|
|
15 |
public UInt16 mapsize { get; set; } // The mapping maximum
|
|
|
16 |
|
|
|
17 |
public jsaxis() {
|
|
|
18 |
|
|
|
19 |
}
|
|
|
20 |
|
| 168 |
pfowler |
21 |
public bool getPosition(ref UInt16 position) {
|
| 167 |
pfowler |
22 |
if (this.value > this.max)
|
|
|
23 |
this.max = this.value;
|
|
|
24 |
|
|
|
25 |
UInt16 lowerval = 0;
|
|
|
26 |
if (this.prev >= this.thres)
|
|
|
27 |
lowerval = (UInt16)(this.prev - this.thres);
|
|
|
28 |
|
|
|
29 |
ushort upperval = this.max;
|
|
|
30 |
if (this.prev <= upperval - this.thres)
|
|
|
31 |
upperval = (ushort)(this.prev + this.thres);
|
|
|
32 |
|
|
|
33 |
if ((this.value < lowerval) || (this.value >= upperval)) {
|
|
|
34 |
// Cover our min/max ranges within threshold
|
|
|
35 |
if (this.value < this.thres)
|
|
|
36 |
this.value = 0;
|
|
|
37 |
if (this.value > this.max - this.thres)
|
|
|
38 |
this.value = this.max;
|
|
|
39 |
|
|
|
40 |
this.prev = this.value;
|
|
|
41 |
|
|
|
42 |
position = Globals.map_uint16(this.value, 0, this.max, 0, this.mapsize);
|
| 168 |
pfowler |
43 |
return true;
|
| 167 |
pfowler |
44 |
//Globals.bios.SendData("ALCP_RCVR_LTS " + refuellight.ToString() + "\n");
|
|
|
45 |
//Console.WriteLine("ALCP_RCVR_LTS " + ":" + refuellight.ToString() + "(" + devices[devid].cur_adc + ")");
|
|
|
46 |
} else {
|
| 168 |
pfowler |
47 |
return false;
|
| 167 |
pfowler |
48 |
}
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
public struct jsdata {
|
|
|
55 |
public bool changed;
|
|
|
56 |
public UInt64 buttons;
|
|
|
57 |
public UInt64 prev;
|
|
|
58 |
|
|
|
59 |
public jsaxis[] axis;
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
|
| 160 |
pfowler |
63 |
public interface IPanel {
|
|
|
64 |
int Init();
|
|
|
65 |
int Refresh();
|
|
|
66 |
int Input();
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
public abstract class Panel : IPanel {
|
| 167 |
pfowler |
70 |
public jsdata data = new jsdata();
|
| 160 |
pfowler |
71 |
protected mcp2221 mcp;
|
|
|
72 |
public int id { get; set; }
|
|
|
73 |
public String name { get; set; }
|
|
|
74 |
public String serialno { get; set; }
|
|
|
75 |
|
| 161 |
pfowler |
76 |
public List<Control> controls = new List<Control>();
|
|
|
77 |
|
| 160 |
pfowler |
78 |
private Boolean enabled;
|
|
|
79 |
public Boolean Enabled {
|
|
|
80 |
get {
|
|
|
81 |
return enabled;
|
|
|
82 |
}
|
|
|
83 |
set {
|
| 161 |
pfowler |
84 |
this.enabled = value;
|
| 160 |
pfowler |
85 |
}
|
|
|
86 |
}
|
|
|
87 |
|
| 161 |
pfowler |
88 |
public Panel(mcp2221 mcp) {
|
| 160 |
pfowler |
89 |
this.mcp = mcp;
|
| 161 |
pfowler |
90 |
this.mcp.usbi2c.Settings.GetConnectionStatus();
|
| 160 |
pfowler |
91 |
this.id = this.mcp.usbi2c.Management.GetSelectedDevNum();
|
|
|
92 |
this.name = this.mcp.usbi2c.Settings.GetUsbStringDescriptor();
|
| 161 |
pfowler |
93 |
|
| 167 |
pfowler |
94 |
this.data.axis[0] = new jsaxis();
|
|
|
95 |
this.data.axis[1] = new jsaxis();
|
|
|
96 |
|
|
|
97 |
|
|
|
98 |
data.changed = false;
|
| 160 |
pfowler |
99 |
}
|
|
|
100 |
|
|
|
101 |
public virtual int Init() {
|
|
|
102 |
|
|
|
103 |
|
|
|
104 |
return 1;
|
|
|
105 |
}
|
|
|
106 |
public abstract int Refresh();
|
|
|
107 |
public abstract int Input();
|
| 161 |
pfowler |
108 |
|
|
|
109 |
public void addControl(Control control) {
|
|
|
110 |
this.controls.Add(control);
|
|
|
111 |
}
|
| 160 |
pfowler |
112 |
}
|
|
|
113 |
|
| 161 |
pfowler |
114 |
|
|
|
115 |
public class Panel_AHFS : Panel {
|
|
|
116 |
|
| 164 |
pfowler |
117 |
private mcp23017[] chips;
|
|
|
118 |
|
| 161 |
pfowler |
119 |
public Panel_AHFS(mcp2221 mcp) : base(mcp) {
|
| 164 |
pfowler |
120 |
chips = new mcp23017[2] { new mcp23017(mcp, 0x20), new mcp23017(mcp, 0x21) };
|
|
|
121 |
|
| 167 |
pfowler |
122 |
data.buttons = 0;
|
|
|
123 |
data.prev = 0;
|
| 161 |
pfowler |
124 |
|
| 167 |
pfowler |
125 |
data.axis[0].prev = data.axis[0].value = 0;
|
|
|
126 |
data.axis[0].thres = 15;
|
|
|
127 |
data.axis[0].max = 930;
|
|
|
128 |
data.axis[0].mapsize = 0xffff;
|
|
|
129 |
|
| 161 |
pfowler |
130 |
this.Init();
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
public override int Init() {
|
| 162 |
pfowler |
134 |
//AHCP
|
| 161 |
pfowler |
135 |
this.addControl(new Switch3Pos(new CommandDCS("AHCP_MASTER_ARM"), 8, 9)); // Train - Safe - Arm
|
|
|
136 |
this.addControl(new Switch3Pos(new CommandDCS("AHCP_GUNPAC"), 10, 11)); // Gunarm - Safe - Arm
|
|
|
137 |
this.addControl(new Switch3Pos(new CommandDCS("AHCP_LASER_ARM"), 12, 13)); // Train - Safe - Arm
|
|
|
138 |
this.addControl(new Switch2Pos(new CommandDCS("AHCP_TGP"), 14)); // Off - On
|
|
|
139 |
this.addControl(new Switch3Pos(new CommandDCS("AHCP_ALT_SCE"), 0, 1)); // Radar - Delta - Baro
|
|
|
140 |
this.addControl(new Switch2Pos(new CommandDCS("AHCP_HUD_DAYNIGHT"), 2)); // Night - Day
|
|
|
141 |
this.addControl(new Switch2Pos(new CommandDCS("AHCP_HUD_MODE"), 15)); // Stby - Norm
|
|
|
142 |
this.addControl(new Switch2Pos(new CommandDCS("AHCP_CICU"), 3)); // Off - On
|
|
|
143 |
this.addControl(new Switch2Pos(new CommandDCS("AHCP_JTRS"), 4)); // Off - On
|
|
|
144 |
this.addControl(new Switch3Pos(new CommandDCS("AHCP_IFFCC"), 6, 5)); // Off - Test - On
|
| 167 |
pfowler |
145 |
this.addControl(new Switch2Pos(new CommandDCS("HARS_FAST_ERECT"), 7)); // Off - On
|
|
|
146 |
this.addControl(new Potentiometer(new CommandDCS("ALCP_RCVR_LTS"), 0)); // 0x00 - 0xFFFF
|
| 161 |
pfowler |
147 |
|
| 162 |
pfowler |
148 |
// Fuel System
|
|
|
149 |
this.addControl(new Switch2Pos(new CommandDCS("FSCP_AMPL"), 16));
|
|
|
150 |
this.addControl(new Switch2Pos(new CommandDCS("FSCP_BOOST_MAIN_L"), 24));
|
|
|
151 |
this.addControl(new Switch2Pos(new CommandDCS("FSCP_BOOST_MAIN_R"), 25));
|
|
|
152 |
this.addControl(new Switch2Pos(new CommandDCS("FSCP_BOOST_WING_L"), 26));
|
|
|
153 |
this.addControl(new Switch2Pos(new CommandDCS("FSCP_BOOST_WING_R"),27 ));
|
|
|
154 |
this.addControl(new Switch2Pos(new CommandDCS("FSCP_CROSSFEED"), 28));
|
|
|
155 |
this.addControl(new Switch2Pos(new CommandDCS("FSCP_EXT_TANKS_FUS"), 30));
|
|
|
156 |
this.addControl(new Switch2Pos(new CommandDCS("FSCP_EXT_TANKS_WING"), 31));
|
|
|
157 |
this.addControl(new Switch2Pos(new CommandDCS("FSCP_FD_MAIN_L"), 20, true));
|
|
|
158 |
this.addControl(new Switch2Pos(new CommandDCS("FSCP_FD_MAIN_R"), 21, true));
|
|
|
159 |
this.addControl(new Switch2Pos(new CommandDCS("FSCP_FD_WING_L"), 18, true));
|
|
|
160 |
this.addControl(new Switch2Pos(new CommandDCS("FSCP_FD_WING_R"), 19, true));
|
|
|
161 |
this.addControl(new Switch2Pos(new CommandDCS("FSCP_LINE_CHECK"), 17));
|
|
|
162 |
this.addControl(new Switch2Pos(new CommandDCS("FSCP_RCVR_LEVER"), 23));
|
|
|
163 |
this.addControl(new Switch2Pos(new CommandDCS("FSCP_TK_GATE"), 29));
|
| 161 |
pfowler |
164 |
|
|
|
165 |
mcp.WriteGpio(3, 0);
|
| 164 |
pfowler |
166 |
Utils.delayms(10);
|
| 161 |
pfowler |
167 |
// Enable the mcp23017
|
|
|
168 |
mcp.WriteGpio(3, 1);
|
| 164 |
pfowler |
169 |
|
| 161 |
pfowler |
170 |
// Set io dir, pullups and rev polarity
|
| 164 |
pfowler |
171 |
chips[0].SetIODirection(0xff, 0xff);
|
|
|
172 |
chips[0].SetIOPolarity(0xf0, 0xff);
|
|
|
173 |
chips[0].SetIOPullups(0xff, 0xff);
|
| 161 |
pfowler |
174 |
|
| 164 |
pfowler |
175 |
chips[1].SetIODirection(0xff, 0xff);
|
|
|
176 |
chips[1].SetIOPolarity(0xff, 0xff);
|
|
|
177 |
chips[1].SetIOPullups(0xff, 0xff);
|
| 161 |
pfowler |
178 |
|
| 167 |
pfowler |
179 |
// Get the initial values
|
| 161 |
pfowler |
180 |
this.Refresh();
|
| 167 |
pfowler |
181 |
data.prev = data.buttons;
|
| 169 |
pfowler |
182 |
data.axis[0].prev = data.axis[0].value;
|
|
|
183 |
data.changed = false;
|
| 161 |
pfowler |
184 |
|
| 164 |
pfowler |
185 |
return 0;
|
| 161 |
pfowler |
186 |
}
|
|
|
187 |
|
|
|
188 |
public override int Refresh() {
|
|
|
189 |
|
| 167 |
pfowler |
190 |
byte[] bytes;
|
| 164 |
pfowler |
191 |
int rslt = 0;
|
| 167 |
pfowler |
192 |
rslt = chips[0].GetIO(out bytes);
|
| 164 |
pfowler |
193 |
|
| 167 |
pfowler |
194 |
data.buttons = (uint)bytes[0] << 24;
|
|
|
195 |
data.buttons |= (uint)bytes[1] << 16;
|
| 161 |
pfowler |
196 |
|
| 167 |
pfowler |
197 |
rslt = chips[1].GetIO(out bytes);
|
| 161 |
pfowler |
198 |
|
| 167 |
pfowler |
199 |
data.buttons |= (uint)bytes[0] << 8;
|
|
|
200 |
data.buttons |= (uint)bytes[1];
|
| 161 |
pfowler |
201 |
|
| 167 |
pfowler |
202 |
data.axis[0].value = mcp.ReadADC(1);
|
| 161 |
pfowler |
203 |
|
| 167 |
pfowler |
204 |
if ((data.buttons != data.prev) || (data.axis[0].prev != data.axis[0].value))
|
|
|
205 |
data.changed = true;
|
| 161 |
pfowler |
206 |
else
|
| 167 |
pfowler |
207 |
data.changed = false;
|
| 161 |
pfowler |
208 |
|
|
|
209 |
return 1;
|
|
|
210 |
}
|
|
|
211 |
|
|
|
212 |
public override int Input() {
|
| 164 |
pfowler |
213 |
|
| 161 |
pfowler |
214 |
foreach (Control control in this.controls) {
|
| 167 |
pfowler |
215 |
control.data = this.data;
|
| 161 |
pfowler |
216 |
control.Tick();
|
|
|
217 |
}
|
| 167 |
pfowler |
218 |
data.prev = data.buttons;
|
|
|
219 |
data.axis[0].prev = data.axis[0].value;
|
|
|
220 |
data.changed = false;
|
| 161 |
pfowler |
221 |
return 1;
|
|
|
222 |
}
|
| 160 |
pfowler |
223 |
}
|
|
|
224 |
|
|
|
225 |
public class Panel_AAP : Panel {
|
|
|
226 |
|
| 164 |
pfowler |
227 |
private mcp23017 chip0;
|
| 160 |
pfowler |
228 |
|
| 161 |
pfowler |
229 |
public Panel_AAP(mcp2221 mcp) : base(mcp) {
|
| 167 |
pfowler |
230 |
data.buttons = 0;
|
|
|
231 |
data.prev = 0;
|
| 160 |
pfowler |
232 |
|
| 164 |
pfowler |
233 |
chip0 = new mcp23017(mcp, 0x20);
|
|
|
234 |
|
| 161 |
pfowler |
235 |
this.Init();
|
| 160 |
pfowler |
236 |
}
|
|
|
237 |
|
|
|
238 |
public override int Init() {
|
| 162 |
pfowler |
239 |
this.addControl(new Switch2Pos(new CommandTrackIRKey(VirtualKeyCode.F9), 16));
|
|
|
240 |
this.addControl(new Switch2Pos(new CommandTrackIRKey(VirtualKeyCode.F11), 17));
|
|
|
241 |
this.addControl(new Switch2Pos(new CommandTrackIRKey(VirtualKeyCode.F7), 12));
|
|
|
242 |
this.addControl(new Switch2Pos(new CommandVKey(VirtualKeyCode.SPACE), 13));
|
|
|
243 |
this.addControl(new Switch2Pos(new CommandVKey(VirtualKeyCode.RETURN), 14));
|
|
|
244 |
this.addControl(new Switch2Pos(new CommandVKey(VirtualKeyCode.VOLUME_MUTE), 15));
|
| 161 |
pfowler |
245 |
this.addControl(new Switch2Pos(new CommandDCS("AAP_CDUPWR"), 11));
|
|
|
246 |
this.addControl(new Switch2Pos(new CommandDCS("AAP_EGIPWR"), 10));
|
|
|
247 |
this.addControl(new Switch3Pos(new CommandDCS("AAP_STEER"), 9, 8));
|
|
|
248 |
this.addControl(new Selector(new CommandDCS("AAP_STEERPT"), new int[] { 4, 5, 6 }));
|
|
|
249 |
this.addControl(new Selector(new CommandDCS("AAP_PAGE"), new int[] { 0, 1, 2, 3 }));
|
|
|
250 |
|
| 160 |
pfowler |
251 |
// Enable the mcp23017
|
|
|
252 |
mcp.WriteGpio(3, 0);
|
| 164 |
pfowler |
253 |
Utils.delayms(10);
|
| 160 |
pfowler |
254 |
mcp.WriteGpio(3, 1);
|
|
|
255 |
|
|
|
256 |
// Set io dir, pullups and rev polarity
|
| 164 |
pfowler |
257 |
chip0.SetIODirection(0xff, 0xff);
|
|
|
258 |
chip0.SetIOPolarity(0xff, 0xff);
|
|
|
259 |
chip0.SetIOPullups(0xff, 0xff);
|
| 160 |
pfowler |
260 |
|
| 161 |
pfowler |
261 |
this.Refresh();
|
| 167 |
pfowler |
262 |
data.prev = data.buttons;
|
| 160 |
pfowler |
263 |
|
|
|
264 |
return 1;
|
|
|
265 |
}
|
|
|
266 |
public override int Refresh() {
|
| 167 |
pfowler |
267 |
byte[] bytes;
|
| 164 |
pfowler |
268 |
int rslt = 0;
|
| 167 |
pfowler |
269 |
rslt = chip0.GetIO(out bytes);
|
| 160 |
pfowler |
270 |
|
| 161 |
pfowler |
271 |
// Join all our buttons into a single inputs
|
|
|
272 |
uint gpio = (uint)((1 - mcp.ReadGpio(0)) | ((1 - mcp.ReadGpio(1)) << 1));
|
| 167 |
pfowler |
273 |
data.buttons = (uint)gpio << 16;
|
|
|
274 |
data.buttons |= (uint)bytes[0] << 8;
|
|
|
275 |
data.buttons |= (uint)bytes[1];
|
| 161 |
pfowler |
276 |
|
| 167 |
pfowler |
277 |
if (data.buttons != data.prev)
|
|
|
278 |
data.changed = true;
|
| 161 |
pfowler |
279 |
else
|
| 167 |
pfowler |
280 |
data.changed = false;
|
| 161 |
pfowler |
281 |
|
|
|
282 |
return rslt;
|
| 160 |
pfowler |
283 |
}
|
|
|
284 |
|
|
|
285 |
public override int Input() {
|
| 162 |
pfowler |
286 |
//Console.WriteLine(input.curr.ToString("X"));
|
| 161 |
pfowler |
287 |
foreach (Control control in this.controls) {
|
| 167 |
pfowler |
288 |
control.data = this.data;
|
| 161 |
pfowler |
289 |
control.Tick();
|
|
|
290 |
}
|
| 167 |
pfowler |
291 |
data.prev = data.buttons;
|
|
|
292 |
data.changed = false;
|
| 160 |
pfowler |
293 |
return 1;
|
|
|
294 |
}
|
|
|
295 |
}
|
|
|
296 |
}
|