Subversion Repositories group.electronics

Rev

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