Subversion Repositories group.electronics

Rev

Rev 155 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 155 Rev 156
Line 20... Line 20...
20
 
20
 
21
        private FSXObject fsx = new FSXObject();
21
        private FSXObject fsx = new FSXObject();
22
 
22
 
23
        private NITNavCommDevice comm1;
23
        private NITNavCommDevice comm1;
24
        private NITNavCommDevice comm2;
24
        private NITNavCommDevice comm2;
-
 
25
        private NITAudioSelDevice audiosel;
25
 
26
 
26
        public MainForm() {
27
        public MainForm() {
27
            InitializeComponent();
28
            InitializeComponent();
28
 
29
 
29
            Devices_Rescan();
30
            Devices_Rescan();
Line 60... Line 61...
60
            }
61
            }
61
        }
62
        }
62
 
63
 
63
        private void SimConnect_InitDataRequest() {
64
        private void SimConnect_InitDataRequest() {
64
            try {
65
            try {
-
 
66
                if (audiosel != null && audiosel.isOpen())
-
 
67
                    audiosel.MapEvents(fsx);
-
 
68
 
65
 
69
 
66
                hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.COM1_SWAP_FREQ, "COM_STBY_RADIO_SWAP");
70
                hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.COM1_SWAP_FREQ, "COM_STBY_RADIO_SWAP");
67
                hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.NAV1_SWAP_FREQ, "NAV1_RADIO_SWAP");
71
                hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.NAV1_SWAP_FREQ, "NAV1_RADIO_SWAP");
68
                hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.COM_RADIO_WHOLE_INC, "COM_RADIO_WHOLE_INC");
72
                hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.COM_RADIO_WHOLE_INC, "COM_RADIO_WHOLE_INC");
69
                hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.COM_RADIO_WHOLE_DEC, "COM_RADIO_WHOLE_DEC");
73
                hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.COM_RADIO_WHOLE_DEC, "COM_RADIO_WHOLE_DEC");
Line 92... Line 96...
92
                hSimConnect.RegisterDataDefineStruct<FSXObject.Aircraft_Data>(FSXObject.DEFINITIONS.AIRCRAFT);
96
                hSimConnect.RegisterDataDefineStruct<FSXObject.Aircraft_Data>(FSXObject.DEFINITIONS.AIRCRAFT);
93
 
97
 
94
                hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.AVIONICS, "Avionics Master Switch", "bool", SIMCONNECT_DATATYPE.INT32, 0, SimConnect.SIMCONNECT_UNUSED);
98
                hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.AVIONICS, "Avionics Master Switch", "bool", SIMCONNECT_DATATYPE.INT32, 0, SimConnect.SIMCONNECT_UNUSED);
95
                hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.AVIONICS, "Com Status:1", "Enum", SIMCONNECT_DATATYPE.FLOAT64, 0, SimConnect.SIMCONNECT_UNUSED);
99
                hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.AVIONICS, "Com Status:1", "Enum", SIMCONNECT_DATATYPE.FLOAT64, 0, SimConnect.SIMCONNECT_UNUSED);
96
                hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.AVIONICS, "Com Status:2", "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);
-
 
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);
97
                hSimConnect.RegisterDataDefineStruct<FSXObject.Avionics_Data>(FSXObject.DEFINITIONS.AVIONICS);
103
                hSimConnect.RegisterDataDefineStruct<FSXObject.Avionics_Data>(FSXObject.DEFINITIONS.AVIONICS);
98
 
104
 
99
                // NavComm1 Frequencies
105
                // NavComm1 Frequencies
100
                hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.NAVCOM1_DATA, "Com Active Frequency:1", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
106
                hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.NAVCOM1_DATA, "Com Active Frequency:1", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
101
                hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.NAVCOM1_DATA, "Com Standby 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);
Line 138... Line 144...
138
            Log("SimConnect Exception: " + (uint)(data.dwException));
144
            Log("SimConnect Exception: " + (uint)(data.dwException));
139
        }
145
        }
140
 
146
 
141
        // Automatic updates on various variables. Set through InitFsxReady
147
        // Automatic updates on various variables. Set through InitFsxReady
142
        private void SimConnect_OnRecvSimObjectData(SimConnect sender, SIMCONNECT_RECV_SIMOBJECT_DATA data) {
148
        private void SimConnect_OnRecvSimObjectData(SimConnect sender, SIMCONNECT_RECV_SIMOBJECT_DATA data) {
143
            
-
 
144
            switch (data.dwRequestID) {
-
 
145
 
149
 
-
 
150
            foreach (NITDevice device in panels.devices) {
-
 
151
                if (device.isOpen())
-
 
152
                    device.FsxEvent(fsx, data);
-
 
153
            } 
146
 
154
 
147
                case ((uint)FSXObject.DATA_REQUESTS.NAVCOM1_REQ): {
-
 
148
                        FSXObject.NavCom_Data navcomdata = (FSXObject.NavCom_Data)data.dwData[0];
-
 
149
                        if (comm1 != null && comm1.isOpen())
-
 
150
                            comm1.setNavComFreqs(navcomdata);
-
 
151
                        break;
-
 
152
                }
155
            /* 
153
 
-
 
154
                case ((uint)FSXObject.DATA_REQUESTS.NAVCOM2_REQ): {
-
 
155
                        FSXObject.NavCom_Data navcomdata = (FSXObject.NavCom_Data)data.dwData[0];
-
 
156
                        if (comm2 != null && comm2.isOpen())
-
 
157
                            comm2.setNavComFreqs(navcomdata);
-
 
158
                        break;
156
            switch (data.dwRequestID) {
159
                }
-
 
160
 
-
 
161
                case ((uint)FSXObject.DATA_REQUESTS.AVIONICS): {
157
                case ((uint)FSXObject.DATA_REQUESTS.AVIONICS): {
162
                    FSXObject.Avionics_Data avionics = (FSXObject.Avionics_Data)data.dwData[0];
158
                    FSXObject.Avionics_Data avionics = (FSXObject.Avionics_Data)data.dwData[0];
163
                        /*
-
 
164
                        Log("Avionics: "
-
 
165
                            + avionics.avionics_master + ", "
-
 
166
                            + "Com1: " + avionics.com1_status + ", "
-
 
167
                            + "Com2: " + avionics.com2_status + ", "
-
 
168
                        );
-
 
169
                        */
-
 
170
                         
-
 
171
                        if (!avionics.avionics_master || avionics.com1_status != 0) {       // If avionics master off or com1 in shutdown
-
 
172
                            if (comm1 != null && comm1.isOpen() && comm1.simStatus == 0)    // and the device is connected
-
 
173
                                comm1.powerDown();                                          // Power down the device
-
 
174
                        }
-
 
175
 
-
 
176
                        if (comm1 != null && comm1.isOpen()         // If comm1 device is active
-
 
177
                                && comm1.simStatus != 0             // and connected to fsx
-
 
178
                                && avionics.avionics_master         // and avionics master is on
-
 
179
                                && avionics.com1_status == 0) {     // and comm1 functioning
-
 
180
                            comm1.powerUp();                        // Power it up
-
 
181
 
-
 
182
                            hSimConnect.RequestDataOnSimObject(
-
 
183
                                FSXObject.DATA_REQUESTS.NAVCOM1_REQ,
-
 
184
                                FSXObject.DEFINITIONS.NAVCOM1_DATA,
-
 
185
                                this.fsx.simdata.objectid,
-
 
186
                                SIMCONNECT_PERIOD.ONCE,
-
 
187
                                SIMCONNECT_DATA_REQUEST_FLAG.DEFAULT,
-
 
188
                                0, 0, 0);
-
 
189
                        }
-
 
190
 
-
 
191
                        if (!avionics.avionics_master || avionics.com2_status != 0) {       // If avionics master off or com1 in shutdown
-
 
192
                            if (comm2 != null && comm2.isOpen() && comm2.simStatus == 0)    // and the device is connected
-
 
193
                                comm2.powerDown();                                          // Power down the device
-
 
194
                        }
-
 
195
 
-
 
196
                        if (comm2 != null && comm2.isOpen()         // If comm1 device is active
-
 
197
                                && comm2.simStatus != 0             // and connected to fsx
-
 
198
                                && avionics.avionics_master         // and avionics master is on
-
 
199
                                && avionics.com2_status == 0) {     // and comm1 functioning
-
 
200
                            comm2.powerUp();                        // Power it up
-
 
201
 
-
 
202
                            hSimConnect.RequestDataOnSimObject(
-
 
203
                                FSXObject.DATA_REQUESTS.NAVCOM2_REQ,
-
 
204
                                FSXObject.DEFINITIONS.NAVCOM2_DATA,
-
 
205
                                this.fsx.simdata.objectid,
-
 
206
                                SIMCONNECT_PERIOD.ONCE,
-
 
207
                                SIMCONNECT_DATA_REQUEST_FLAG.DEFAULT,
-
 
208
                                0, 0, 0);
-
 
209
                        }
-
 
210
 
-
 
211
                    break;
159
                    break;
212
                }
160
                }
213
            }
161
            }*/
214
        }
162
        }
215
 
163
 
216
        private void SimConnect_OnRecvSimObjectDataByType(SimConnect sender, SIMCONNECT_RECV_SIMOBJECT_DATA_BYTYPE data) {
164
        private void SimConnect_OnRecvSimObjectDataByType(SimConnect sender, SIMCONNECT_RECV_SIMOBJECT_DATA_BYTYPE data) {
217
            switch (data.dwRequestID) {
165
            switch (data.dwRequestID) {
218
 
166
 
Line 239... Line 187...
239
                + this.fsx.simdata.aircraft.atc_model + ", "
187
                + this.fsx.simdata.aircraft.atc_model + ", "
240
                + this.fsx.simdata.aircraft.atc_type + ", "
188
                + this.fsx.simdata.aircraft.atc_type + ", "
241
                + this.fsx.simdata.aircraft.title
189
                + this.fsx.simdata.aircraft.title
242
            );
190
            );
243
 
191
 
-
 
192
            foreach (NITDevice device in panels.devices) {
-
 
193
                if (device.isOpen())
-
 
194
                    device.fsx = this.fsx;
-
 
195
                device.FsxReady(this.fsx);
-
 
196
            } 
-
 
197
 
244
            hSimConnect.RequestDataOnSimObject(
198
            fsx.hSimConnect.RequestDataOnSimObject(
245
                FSXObject.DATA_REQUESTS.NAVCOM1_REQ,
199
                FSXObject.DATA_REQUESTS.NAVCOM1_REQ,
246
                FSXObject.DEFINITIONS.NAVCOM1_DATA,
200
                FSXObject.DEFINITIONS.NAVCOM1_DATA,
247
                this.fsx.simdata.objectid,
201
                fsx.simdata.objectid,
248
                SIMCONNECT_PERIOD.SIM_FRAME,
202
                SIMCONNECT_PERIOD.SIM_FRAME,
249
                SIMCONNECT_DATA_REQUEST_FLAG.CHANGED,
203
                SIMCONNECT_DATA_REQUEST_FLAG.CHANGED,
250
                0, 0, 0);
204
                0, 0, 0);
251
 
205
 
252
            hSimConnect.RequestDataOnSimObject(
206
            fsx.hSimConnect.RequestDataOnSimObject(
253
                FSXObject.DATA_REQUESTS.NAVCOM2_REQ,
207
                FSXObject.DATA_REQUESTS.NAVCOM2_REQ,
254
                FSXObject.DEFINITIONS.NAVCOM2_DATA,
208
                FSXObject.DEFINITIONS.NAVCOM2_DATA,
255
                this.fsx.simdata.objectid,
209
                fsx.simdata.objectid,
256
                SIMCONNECT_PERIOD.SIM_FRAME,
210
                SIMCONNECT_PERIOD.SIM_FRAME,
257
                SIMCONNECT_DATA_REQUEST_FLAG.CHANGED,
211
                SIMCONNECT_DATA_REQUEST_FLAG.CHANGED,
258
                0, 0, 0);
212
                0, 0, 0);            
259
 
213
 
260
            hSimConnect.RequestDataOnSimObject(
214
            hSimConnect.RequestDataOnSimObject(
261
                FSXObject.DATA_REQUESTS.AVIONICS,
215
                FSXObject.DATA_REQUESTS.AVIONICS,
262
                FSXObject.DEFINITIONS.AVIONICS,
216
                FSXObject.DEFINITIONS.AVIONICS,
263
                this.fsx.simdata.objectid,
217
                this.fsx.simdata.objectid,
Line 267... Line 221...
267
 
221
 
268
            inputTimer.Enabled = true;
222
            inputTimer.Enabled = true;
269
        }
223
        }
270
 
224
 
271
        private void InitFsxClosed() {
225
        private void InitFsxClosed() {
272
 
-
 
273
            if (comm1 != null && comm1.isOpen())
-
 
274
                comm1.blankDisplay();
-
 
275
 
-
 
276
            if (comm2 != null && comm2.isOpen())
-
 
277
                comm2.blankDisplay();
-
 
278
 
-
 
279
            if (hSimConnect != null) {
226
            if (hSimConnect != null) {
280
                hSimConnect.Dispose();
227
                hSimConnect.Dispose();
281
                txtSimConnect.Text = "Available";
228
                txtSimConnect.Text = "Available";
282
            } else {
229
            } else {
283
                txtSimConnect.Text = "Failed";
230
                txtSimConnect.Text = "Failed";
284
                hSimConnect = null;
231
                hSimConnect = null;
285
            }
232
            }
286
 
-
 
287
        }
233
        }
288
 
234
 
289
        private void Devices_Rescan() {
235
        private void Devices_Rescan() {
290
            Log("Scanning for devices...");
236
            Log("Scanning for devices...");
291
 
237
 
Line 306... Line 252...
306
                        } else if (comm2 == null) {
252
                        } else if (comm2 == null) {
307
                            comm2 = (NITNavCommDevice)device;
253
                            comm2 = (NITNavCommDevice)device;
308
                            comm2.assigned = 2;
254
                            comm2.assigned = 2;
309
                        }
255
                        }
310
                    } else if (device.type == "NITAudioSel") {
256
                    } else if (device.type == "NITAudioSel") {
311
                        // @TODO: Add other devices
257
                        if (audiosel == null) {
-
 
258
                            audiosel = (NITAudioSelDevice)device;
-
 
259
                            audiosel.assigned = 1;
-
 
260
                        }
312
                    }
261
                    }
313
                }
262
                }
314
 
263
 
315
            } else {
264
            } else {
316
                Log("No devices found, check connections and rescan.");
265
                Log("No devices found, check connections and rescan.");
Line 337... Line 286...
337
            if (device.type == "NITNavComm") {
286
            if (device.type == "NITNavComm") {
338
                NITCommNavForm form = new NITCommNavForm();
287
                NITCommNavForm form = new NITCommNavForm();
339
                form.setDevice((NITNavCommDevice)device);
288
                form.setDevice((NITNavCommDevice)device);
340
                form.Show(this);
289
                form.Show(this);
341
            } else if (device.type == "NITAudioSel") {
290
            } else if (device.type == "NITAudioSel") {
342
                Log("Not implemented for NITAudioSel");
291
                NITAudioSelForm form = new NITAudioSelForm();
-
 
292
                form.setDevice((NITAudioSelDevice)device);
-
 
293
                form.Show(this);
343
            } else {
294
            } else {
344
                Log("No device type for " + device.type + " (" + device.serial + ").");
295
                Log("No device type for " + device.type + " (" + device.serial + ").");
345
            }
296
            }
346
        }
297
        }
347
 
298
 
Line 356... Line 307...
356
            }
307
            }
357
            Log(sb.ToString());
308
            Log(sb.ToString());
358
        }
309
        }
359
 
310
 
360
        private void quitNITPanels() {        
311
        private void quitNITPanels() {        
361
            //this.InitFsxClosed();
-
 
362
 
-
 
363
            inputTimer.Enabled = false;
312
            inputTimer.Enabled = false;
364
 
313
 
365
            if (comm1 != null && comm1.isOpen())
314
            foreach (NITDevice device in panels.devices) {
366
                comm1.Close();
315
                if (device.isOpen())
367
 
-
 
368
            if (comm2 != null && comm2.isOpen())
316
                    device.Close();
369
                comm2.Close();
317
            }        
370
 
318
             
371
            Application.Exit();
319
            Application.Exit();
372
        }
320
        }
373
 
321
 
374
        private void quitToolStripMenuItem_Click(object sender, EventArgs e) {
322
        private void quitToolStripMenuItem_Click(object sender, EventArgs e) {
375
            if (QuestionBox("Exit NIT Panels?", "Exiting will disable NIT Panels. Continue?")) {
323
            if (MainForm.QuestionBox("Exit NIT Panels?", "Exiting will disable NIT Panels. Continue?")) {
376
                this.quitNITPanels();
324
                this.quitNITPanels();
377
            }
325
            }
378
        }
326
        }
379
 
327
 
-
 
328
        
380
        public bool QuestionBox(string caption, string message) {
329
        public static bool QuestionBox(string caption, string message) {
381
            MessageBoxButtons buttons = MessageBoxButtons.YesNo;
330
            MessageBoxButtons buttons = MessageBoxButtons.YesNo;
382
            DialogResult result;
331
            DialogResult result;
383
 
332
 
384
            result = MessageBox.Show(message, caption, buttons);
333
            result = MessageBox.Show(message, caption, buttons);
385
 
334
 
Line 387... Line 336...
387
                return true;
336
                return true;
388
            }
337
            }
389
            return false;
338
            return false;
390
        }
339
        }
391
 
340
 
-
 
341
        public static void NITMessageBox(string caption, string message) {
-
 
342
            MessageBoxButtons buttons = MessageBoxButtons.OK;
-
 
343
            MessageBox.Show(message, caption, buttons);
-
 
344
        }
-
 
345
 
392
        private void txtLog_VisibleChanged(object sender, EventArgs e) {
346
        private void txtLog_VisibleChanged(object sender, EventArgs e) {
393
            if (txtLog.Visible) {
347
            if (txtLog.Visible) {
394
                txtLog.SelectionStart = txtLog.TextLength;
348
                txtLog.SelectionStart = txtLog.TextLength;
395
                txtLog.ScrollToCaret();
349
                txtLog.ScrollToCaret();
396
            }
350
            }
Line 406... Line 360...
406
        private void cmdRequest_Click(object sender, EventArgs e) {
360
        private void cmdRequest_Click(object sender, EventArgs e) {
407
 
361
 
408
        }
362
        }
409
 
363
 
410
        private void inputTimer_Tick(object sender, EventArgs e) {
364
        private void inputTimer_Tick(object sender, EventArgs e) {
411
            if (comm1 != null && comm1.isOpen()) {
365
            foreach (NITDevice device in panels.devices) {
412
                comm1.updateInput();
366
                if (device.isOpen())
413
                comm1.simButtons(this.fsx);
-
 
414
            }
-
 
415
 
-
 
416
            if (comm2 != null && comm2.isOpen()) {
-
 
417
                comm2.updateInput();
-
 
418
                comm2.simButtons(this.fsx);
367
                    device.SimButtons(this.fsx);
419
            }
368
            }
420
 
369
 
421
        }
370
        }
422
 
371
 
423
        private void MainForm_FormClosing(object sender, FormClosingEventArgs e) {
372
        private void MainForm_FormClosing(object sender, FormClosingEventArgs e) {
424
            this.quitNITPanels();
373
            this.quitNITPanels();
425
        }
374
        }
426
 
375
 
-
 
376
        private void debugTimer_Tick(object sender, EventArgs e) {
-
 
377
            
-
 
378
        }
-
 
379
 
427
    }
380
    }
428
}
381
}