Subversion Repositories group.NITPanels

Rev

Rev 4 | Go to most recent revision | Details | Last modification | View Log | RSS feed

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