Subversion Repositories group.electronics

Rev

Rev 145 | Rev 155 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
139 pfowler 1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel;
4
using System.Data;
5
using System.Drawing;
6
using System.Text;
7
using System.Threading.Tasks;
8
using System.Windows.Forms;
9
 
153 pfowler 10
using Microsoft.FlightSimulator.SimConnect;
11
using System.Runtime.InteropServices;
12
 
139 pfowler 13
namespace NITNavComm
14
{
15
    public partial class MainForm : Form
16
    {
17
 
18
        private NITPanels panels = new NITPanels();
153 pfowler 19
        private SimConnect hSimConnect = null;
139 pfowler 20
 
153 pfowler 21
        private SimData simdata = new SimData();
22
 
23
        private struct SimData {
24
            public uint objectid;
25
            public Aircraft_Data aircraft;
26
        }
27
 
139 pfowler 28
        public MainForm() {
29
            InitializeComponent();
30
 
31
            Devices_Rescan();
153 pfowler 32
            txtStatus.Text = "Running";
139 pfowler 33
 
34
        }
35
 
153 pfowler 36
        private enum DATA_REQUESTS {
37
            AIRCRAFT, 
38
            AVIONICS,
39
            NAVCOM1_REQ,
40
            NAVCOM2_REQ
41
        }
42
 
43
        private enum DEFINITIONS {
44
            AIRCRAFT,
45
            AVIONICS,
46
            NAVCOM1_DATA,
47
            NAVCOM2_DATA
48
        }
49
 
50
        private enum EVENT_ID {
51
            COM1_SWAP_FREQ,
52
            NAV1_SWAP_FREQ,
53
            COM_RADIO_WHOLE_INC,
54
            COM_RADIO_WHOLE_DEC,
55
        }
56
 
57
        private enum GROUP_ID {
58
            NAVCOM1
59
        }
60
 
61
        [StructLayout(LayoutKind.Sequential, Pack = 1)]
62
        public struct NavCom_Data {
63
            public double Freq0;
64
            public double Freq1;
65
            public double Freq2;
66
            public double Freq3;
67
        }
68
 
69
        [StructLayout(LayoutKind.Sequential, Pack = 1)]
70
        public struct Avionics_Data {
71
            public bool avionics_master;
72
            public double com_status;
73
        }
74
 
75
        [StructLayout(LayoutKind.Sequential, Pack = 1)]
76
        public struct Aircraft_Data {
77
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
78
            public string atc_type;
79
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
80
            public string atc_model;
81
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
82
            public string atc_id;
83
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
84
            public string title;
85
        }
86
 
87
        private void SimConnect_Connect() {
88
            if (hSimConnect == null) {
89
                try {
90
                    hSimConnect = new SimConnect("Managed Data Request", base.Handle, 0x402, null, 0);
91
                    txtSimConnect.Text = "Available";
92
                    this.SimConnect_InitDataRequest();
93
                } catch (COMException) {
94
                    Log("Could not connect to FSX");
95
                    txtSimConnect.Text = "Failed";
96
                }
97
            }
98
        }
99
 
100
        private void SimConnect_Disconnect() {
101
            this.InitFsxClosed();
102
 
103
            if (hSimConnect != null) {
104
                hSimConnect.Dispose();
105
                txtSimConnect.Text = "Available";
106
            } else {
107
                txtSimConnect.Text = "Failed";
108
            }
109
            hSimConnect = null;
110
        }
111
 
112
        protected override void DefWndProc(ref Message m) {
113
            if (m.Msg == 0x402) {
114
                if (hSimConnect != null) {
115
                    hSimConnect.ReceiveMessage();
116
                }
117
            } else {
118
                base.DefWndProc(ref m);
119
            }
120
        }
121
 
122
        private void SimConnect_InitDataRequest() {
123
            try {
124
 
125
                hSimConnect.MapClientEventToSimEvent(EVENT_ID.COM1_SWAP_FREQ, "COM_STBY_RADIO_SWAP");
126
                hSimConnect.MapClientEventToSimEvent(EVENT_ID.NAV1_SWAP_FREQ, "NAV1_RADIO_SWAP");
127
                hSimConnect.MapClientEventToSimEvent(EVENT_ID.COM_RADIO_WHOLE_INC, "COM_RADIO_WHOLE_INC");
128
                hSimConnect.MapClientEventToSimEvent(EVENT_ID.COM_RADIO_WHOLE_DEC, "COM_RADIO_WHOLE_DEC");
129
 
130
                hSimConnect.AddToDataDefinition(DEFINITIONS.AIRCRAFT, "Atc Type", "", SIMCONNECT_DATATYPE.STRING32, 0.0f, SimConnect.SIMCONNECT_UNUSED);
131
                hSimConnect.AddToDataDefinition(DEFINITIONS.AIRCRAFT, "ATC Model", "", SIMCONNECT_DATATYPE.STRING32, 0.0f, SimConnect.SIMCONNECT_UNUSED);
132
                hSimConnect.AddToDataDefinition(DEFINITIONS.AIRCRAFT, "ATC ID", "", SIMCONNECT_DATATYPE.STRING32, 0.0f, SimConnect.SIMCONNECT_UNUSED);
133
                hSimConnect.AddToDataDefinition(DEFINITIONS.AIRCRAFT, "Title", "", SIMCONNECT_DATATYPE.STRING128, 0.0f, SimConnect.SIMCONNECT_UNUSED);
134
                hSimConnect.RegisterDataDefineStruct<Aircraft_Data>(DEFINITIONS.AIRCRAFT);
135
 
136
                hSimConnect.AddToDataDefinition(DEFINITIONS.AVIONICS, "Avionics Master Switch", "bool", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
137
                hSimConnect.AddToDataDefinition(DEFINITIONS.AVIONICS, "Com Status", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
138
                hSimConnect.RegisterDataDefineStruct<Avionics_Data>(DEFINITIONS.AVIONICS);
139
 
140
                hSimConnect.AddToDataDefinition(DEFINITIONS.NAVCOM1_DATA, "Com Active Frequency:1", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
141
                hSimConnect.AddToDataDefinition(DEFINITIONS.NAVCOM1_DATA, "Com Standby Frequency:1", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
142
                hSimConnect.AddToDataDefinition(DEFINITIONS.NAVCOM1_DATA, "Nav Active Frequency:1", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
143
                hSimConnect.AddToDataDefinition(DEFINITIONS.NAVCOM1_DATA, "Nav Standby Frequency:1", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
144
                hSimConnect.RegisterDataDefineStruct<NavCom_Data>(DEFINITIONS.NAVCOM1_DATA);
145
 
146
                hSimConnect.AddToDataDefinition(DEFINITIONS.NAVCOM2_DATA, "Com Active Frequency:2", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
147
                hSimConnect.AddToDataDefinition(DEFINITIONS.NAVCOM2_DATA, "Com Standby Frequency:2", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
148
                hSimConnect.AddToDataDefinition(DEFINITIONS.NAVCOM2_DATA, "Nav Active Frequency:2", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
149
                hSimConnect.AddToDataDefinition(DEFINITIONS.NAVCOM2_DATA, "Nav Standby Frequency:2", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
150
                hSimConnect.RegisterDataDefineStruct<NavCom_Data>(DEFINITIONS.NAVCOM2_DATA);
151
 
152
                hSimConnect.OnRecvOpen += new SimConnect.RecvOpenEventHandler(SimConnect_OnRecvOpen);
153
                hSimConnect.OnRecvQuit += new SimConnect.RecvQuitEventHandler(SimConnect_OnRecvQuit);
154
                hSimConnect.OnRecvException += new SimConnect.RecvExceptionEventHandler(SimConnect_OnRecvException);
155
                hSimConnect.OnRecvSimobjectData += new SimConnect.RecvSimobjectDataEventHandler(SimConnect_OnRecvSimObjectData);
156
                hSimConnect.OnRecvSimobjectDataBytype += new SimConnect.RecvSimobjectDataBytypeEventHandler(SimConnect_OnRecvSimObjectDataByType);
157
 
158
                // Request for aircrat data. This will also configure auto-updates for other required data.
159
                hSimConnect.RequestDataOnSimObjectType(DATA_REQUESTS.AIRCRAFT, DEFINITIONS.AIRCRAFT, 0, SIMCONNECT_SIMOBJECT_TYPE.USER);
160
 
161
            } catch (COMException e) {
162
                Log(e.Message);
163
            }
164
        }
165
 
166
        private void SimConnect_OnRecvOpen(SimConnect sender, SIMCONNECT_RECV_OPEN data) {
167
            Log("Connected to FSX.");
168
            txtSimConnect.Text = "Connected";
169
        }
170
 
171
        private void SimConnect_OnRecvQuit(SimConnect sender, SIMCONNECT_RECV data) {
172
            Log("FSX has exited.");
173
            this.SimConnect_Disconnect();
174
        }
175
 
176
        private void SimConnect_OnRecvException(SimConnect sender, SIMCONNECT_RECV_EXCEPTION data) {
177
            Log("SimConnect Exception: " + (uint)(data.dwException));
178
        }
179
 
180
        private void SimConnect_OnRecvSimObjectData(SimConnect sender, SIMCONNECT_RECV_SIMOBJECT_DATA data) {
181
            switch (data.dwRequestID) {
182
 
183
                // Automatically provided updates on NavCom1
184
                case ((uint)DATA_REQUESTS.NAVCOM1_REQ): {
185
                        NavCom_Data navcomdata = (NavCom_Data)data.dwData[0];
186
                        NITNavCommDevice navcomm = (NITNavCommDevice)panels.devices[0];
187
                        navcomm.setNavComFreqs(navcomdata);
188
                        break;
189
                }
190
            }
191
        }
192
 
193
        private void SimConnect_OnRecvSimObjectDataByType(SimConnect sender, SIMCONNECT_RECV_SIMOBJECT_DATA_BYTYPE data) {
194
            switch (data.dwRequestID) {
195
 
196
                // Request basic aircraft data to get the object ID
197
                case ((uint)DATA_REQUESTS.AIRCRAFT): {
198
                        uint ObjectID = data.dwObjectID;
199
                        Aircraft_Data aircraftdata = (Aircraft_Data)data.dwData[0];
200
 
201
                        this.simdata.objectid = ObjectID;
202
                        this.simdata.aircraft = aircraftdata;
203
 
204
                        this.InitFsxReady();                      
205
 
206
                        break;
207
                    }
208
            }           
209
        }
210
 
211
        private void InitFsxReady() {
212
 
213
 
214
            Log("Aircraft: "
215
                + this.simdata.aircraft.atc_id + ", "
216
                + this.simdata.aircraft.atc_model + ", "
217
                + this.simdata.aircraft.atc_type + ", "
218
                + this.simdata.aircraft.title
219
            );
220
            hSimConnect.RequestDataOnSimObject(
221
                DATA_REQUESTS.NAVCOM1_REQ,
222
                DEFINITIONS.NAVCOM1_DATA,
223
                this.simdata.objectid,
224
                SIMCONNECT_PERIOD.SIM_FRAME,
225
                SIMCONNECT_DATA_REQUEST_FLAG.CHANGED,
226
                0, 0, 0);
227
 
228
            inputTimer.Enabled = true;
229
        }
230
 
231
        private void InitFsxClosed() {
232
            inputTimer.Enabled = false;
233
 
234
        }
235
 
139 pfowler 236
        private void Devices_Rescan() {
237
            Log("Scanning for devices...");
238
 
239
            panels.UsbScan();
240
 
241
            this.deviceGrid.DataSource = panels.devices;
242
            this.txtDevices.Text = panels.devices.Count.ToString();
243
 
244
            if (panels.devices.Count > 0) {
245
                this.cmDevTest.Enabled = true;
246
                Log(panels.devices.Count.ToString() + " devices found during scan.");
247
            } else {
248
                Log("No devices found, check connections and rescan.");
249
            }
153 pfowler 250
 
251
            NITNavCommDevice navcomm = (NITNavCommDevice)panels.devices[0];
252
            navcomm.resetRotary(0, 0);
139 pfowler 253
        }
254
 
255
        private void cmdRescan_Click(object sender, EventArgs e) {
256
            this.Devices_Rescan();
257
        }
258
 
259
        private void cmdDevRescan_Click(object sender, EventArgs e) {
260
            this.Devices_Rescan();
261
        }
262
 
263
        private void cmDevTest_Click(object sender, EventArgs e) {
153 pfowler 264
            //NITDevice device = (NITDevice) this.deviceGrid.CurrentRow.DataBoundItem;
265
            NITNavCommDevice navcomm = (NITNavCommDevice)this.deviceGrid.CurrentRow.DataBoundItem;;
139 pfowler 266
 
267
            if (!navcomm.Open()) {
153 pfowler 268
                Log("Could not open device " + navcomm.type + "(" + navcomm.serial + ").");
139 pfowler 269
                return;
270
            }
271
            navcomm.Close();
272
 
273
            NITCommNavForm form = new NITCommNavForm();
274
            form.setDevice(navcomm);
275
            form.Show(this);
276
 
277
        }
278
 
279
        public void Log(string msg) {
280
            txtLog.AppendText(msg + "\r\n");
281
        }
282
 
283
        public void Log(byte[] buffer) {
284
            StringBuilder sb = new StringBuilder();
285
            foreach (byte data in buffer) {
286
                sb.Append(data.ToString("X") + " ");
287
            }
288
            Log(sb.ToString());
289
        } 
290
 
291
        private void quitToolStripMenuItem_Click(object sender, EventArgs e) {
292
            if (QuestionBox("Exit NIT Panels?", "Exiting will disable NIT Panels. Continue?")) {
293
                Application.Exit();
294
            }
295
        }
296
 
297
        public bool QuestionBox(string caption, string message) {
298
            MessageBoxButtons buttons = MessageBoxButtons.YesNo;
299
            DialogResult result;
300
 
301
            result = MessageBox.Show(message, caption, buttons);
302
 
303
            if (result == System.Windows.Forms.DialogResult.Yes) {
304
                return true;
305
            }
306
            return false;
307
        }
308
 
309
        private void txtLog_VisibleChanged(object sender, EventArgs e) {
310
            if (txtLog.Visible) {
311
                txtLog.SelectionStart = txtLog.TextLength;
312
                txtLog.ScrollToCaret();
313
            }
314
        }
315
 
153 pfowler 316
        private void cmdSimConnect_Click(object sender, EventArgs e) {
317
            if (hSimConnect != null)
318
                this.SimConnect_Disconnect();
319
 
320
            this.SimConnect_Connect();
321
        }
322
 
323
        private void cmdRequest_Click(object sender, EventArgs e) {
324
            hSimConnect.TransmitClientEvent(
325
                this.simdata.objectid, 
326
                EVENT_ID.COM1_SWAP_FREQ, 1, 
327
                GROUP_ID.NAVCOM1, 
328
                SIMCONNECT_EVENT_FLAG.GROUPID_IS_PRIORITY
329
                );
330
        }
331
 
332
        private void inputTimer_Tick(object sender, EventArgs e) {
333
            NITNavCommDevice navcomm = (NITNavCommDevice)panels.devices[0];
334
            navcomm.updateInput();
335
 
336
            if (navcomm.isSwapSet(0))
337
                hSimConnect.TransmitClientEvent(
338
                this.simdata.objectid,
339
                EVENT_ID.COM1_SWAP_FREQ, 1,
340
                GROUP_ID.NAVCOM1,
341
                SIMCONNECT_EVENT_FLAG.GROUPID_IS_PRIORITY
342
                );
343
 
344
            if (navcomm.isSwapSet(1))
345
                hSimConnect.TransmitClientEvent(
346
                this.simdata.objectid,
347
                EVENT_ID.NAV1_SWAP_FREQ, 1,
348
                GROUP_ID.NAVCOM1,
349
                SIMCONNECT_EVENT_FLAG.GROUPID_IS_PRIORITY
350
                );
351
 
352
            sbyte delta = navcomm.getRotary(0, 0);
353
            navcomm.resetRotary(0, 0);
354
            if (delta != 0) {
355
                //sbyte start = 0;
356
                //sbyte finish = 0;
357
                if (delta < 0) {
358
                    //start = delta;
359
                    //for (sbyte i = start; start < finish; i++) {
360
                    delta = (sbyte)-delta;
361
                        hSimConnect.TransmitClientEvent(
362
                            this.simdata.objectid,
363
                            EVENT_ID.COM_RADIO_WHOLE_DEC, 1,
364
                            GROUP_ID.NAVCOM1,
365
                            SIMCONNECT_EVENT_FLAG.GROUPID_IS_PRIORITY
366
                        );
367
                    //}
368
                } else {
369
                    //finish = delta;
370
                    //for (sbyte i = start; start < finish; i++) {
371
                        hSimConnect.TransmitClientEvent(
372
                            this.simdata.objectid,
373
                            EVENT_ID.COM_RADIO_WHOLE_INC, 1,
374
                            GROUP_ID.NAVCOM1,
375
                            SIMCONNECT_EVENT_FLAG.GROUPID_IS_PRIORITY
376
                        );
377
                    //}
378
                }
379
 
380
            }
381
 
382
 
383
        }
384
 
139 pfowler 385
    }
386
}