Subversion Repositories group.NITPanels

Rev

Rev 5 | Details | Compare with Previous | 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
 
4 pfowler 71
                fsx.MapEvents();
3 pfowler 72
 
73
                fsx.hSimConnect.OnRecvOpen += new SimConnect.RecvOpenEventHandler(SimConnect_OnRecvOpen);
74
                fsx.hSimConnect.OnRecvQuit += new SimConnect.RecvQuitEventHandler(SimConnect_OnRecvQuit);
75
                fsx.hSimConnect.OnRecvException += new SimConnect.RecvExceptionEventHandler(SimConnect_OnRecvException);
76
                fsx.hSimConnect.OnRecvSimobjectData += new SimConnect.RecvSimobjectDataEventHandler(SimConnect_OnRecvSimObjectData);
77
                fsx.hSimConnect.OnRecvSimobjectDataBytype += new SimConnect.RecvSimobjectDataBytypeEventHandler(SimConnect_OnRecvSimObjectDataByType);
78
 
79
                // Request for aircrat data. This will also configure auto-updates for other required data.
80
                fsx.hSimConnect.RequestDataOnSimObjectType(FSXObject.DATA_REQUESTS.AIRCRAFT, FSXObject.DEFINITIONS.AIRCRAFT, 0, SIMCONNECT_SIMOBJECT_TYPE.USER);
81
 
82
            } catch (COMException e) {
83
                Log(e.Message);
84
            }
85
        }
86
 
87
        private void SimConnect_OnRecvOpen(SimConnect sender, SIMCONNECT_RECV_OPEN data) {
88
            Log("Connected to FSX.");
89
            txtSimConnect.Text = "Connected";
90
        }
91
 
92
        private void SimConnect_OnRecvQuit(SimConnect sender, SIMCONNECT_RECV data) {
93
            Log("FSX has exited.");
94
            this.SimConnect_Disconnect();
95
        }
96
 
97
        private void SimConnect_OnRecvException(SimConnect sender, SIMCONNECT_RECV_EXCEPTION data) {
98
            Log("SimConnect Exception: " + (uint)(data.dwException));
99
        }
100
 
101
        // Automatic updates on various variables. Set through InitFsxReady
102
        private void SimConnect_OnRecvSimObjectData(SimConnect sender, SIMCONNECT_RECV_SIMOBJECT_DATA data) {
103
            foreach (NITDevice device in panels.devices) {
104
                if (device.isOpen()) {
105
                    device.FsxEvent(data);
106
                }
107
            } 
108
        }
109
 
110
        private void SimConnect_OnRecvSimObjectDataByType(SimConnect sender, SIMCONNECT_RECV_SIMOBJECT_DATA_BYTYPE data) {
111
            switch (data.dwRequestID) {
112
                // Request basic aircraft data to get the object ID
113
                case ((uint)FSXObject.DATA_REQUESTS.AIRCRAFT): {
114
                        uint ObjectID = data.dwObjectID;
115
                        FSXObject.Aircraft_Data aircraftdata = (FSXObject.Aircraft_Data)data.dwData[0];
116
 
117
                        this.fsx.simdata.objectid = ObjectID;
118
                        this.fsx.simdata.aircraft = aircraftdata;
119
 
120
                        this.fsx.FsxReady();
5 pfowler 121
                        foreach (NITDevice device in panels.devices)
122
                            if (device.Open())
123
                                device.FsxReady();
124
 
3 pfowler 125
                        this.InitFsxReady();
126
 
127
                        break;
128
                    }
129
            }           
130
        }
131
 
132
        private void InitFsxReady() {
133
 
134
            Log("Aircraft: "
135
                + this.fsx.simdata.aircraft.atc_id + ", "
136
                + this.fsx.simdata.aircraft.atc_model + ", "
137
                + this.fsx.simdata.aircraft.atc_type + ", "
138
                + this.fsx.simdata.aircraft.title
139
            );
140
 
141
            inputTimer.Enabled = true;
142
        }
143
 
144
        private void InitFsxClosed() {
145
            if (fsx.hSimConnect != null) {
146
                fsx.hSimConnect.Dispose();
147
                txtSimConnect.Text = "Available";
148
            } else {
149
                txtSimConnect.Text = "Failed";
150
                fsx.hSimConnect = null;
151
            }
152
        }
153
 
154
        private void Devices_Rescan() {
155
            Log("Scanning for devices...");
156
 
157
            panels.UsbScan();
158
 
159
            this.deviceGrid.DataSource = panels.devices;
160
            this.txtDevices.Text = panels.devices.Count.ToString();
161
 
162
            if (panels.devices.Count > 0) {
163
                this.cmDevTest.Enabled = true;
164
                Log(panels.devices.Count.ToString() + " devices found during scan.");
165
 
166
            } else {
167
                Log("No devices found, check connections and rescan.");
168
            }
169
        }
170
 
171
 
172
 
173
        private void cmdRescan_Click(object sender, EventArgs e) {
174
            this.Devices_Rescan();
175
        }
176
 
177
        private void cmdDevRescan_Click(object sender, EventArgs e) {
178
            this.Devices_Rescan();
179
        }
180
 
181
        private void cmDevTest_Click(object sender, EventArgs e) {
182
            NITDevice device = (NITDevice) this.deviceGrid.CurrentRow.DataBoundItem;
183
 
184
            if (!device.Open()) {
185
                Log("Could not open device " + device.type + "(" + device.serial + ").");
186
                return;
187
            }
188
            if (device.type == "NITNavComm") {
189
                NITCommNavForm form = new NITCommNavForm();
190
                form.setDevice((NITNavCommDevice)device);
191
                form.Show(this);
192
            } else if (device.type == "NITAudioSel") {
193
                NITAudioSelForm form = new NITAudioSelForm();
194
                form.setDevice((NITAudioSelDevice)device);
195
                form.Show(this);
196
            } else {
197
                Log("No device type for " + device.type + " (" + device.serial + ").");
198
            }
199
        }
200
 
201
        public void Log(string msg) {
202
            txtLog.AppendText(msg + "\r\n");
203
        }
204
 
205
        public void Log(byte[] buffer) {
206
            StringBuilder sb = new StringBuilder();
207
            foreach (byte data in buffer) {
208
                sb.Append(data.ToString("X") + " ");
209
            }
210
            Log(sb.ToString());
211
        }
212
 
213
        private void quitNITPanels() {        
214
            inputTimer.Enabled = false;
215
 
216
            foreach (NITDevice device in panels.devices) {
217
                if (device.isOpen())
218
                    device.Close();
219
            }        
220
 
221
            Application.Exit();
222
        }
223
 
224
        private void quitToolStripMenuItem_Click(object sender, EventArgs e) {
225
            if (MainForm.QuestionBox("Exit NIT Panels?", "Exiting will disable NIT Panels. Continue?")) {
226
                this.quitNITPanels();
227
            }
228
        }
229
 
230
 
231
        public static bool QuestionBox(string caption, string message) {
232
            MessageBoxButtons buttons = MessageBoxButtons.YesNo;
233
            DialogResult result;
234
 
235
            result = MessageBox.Show(message, caption, buttons);
236
 
237
            if (result == System.Windows.Forms.DialogResult.Yes) {
238
                return true;
239
            }
240
            return false;
241
        }
242
 
243
        public static void NITMessageBox(string caption, string message) {
244
            MessageBoxButtons buttons = MessageBoxButtons.OK;
245
            MessageBox.Show(message, caption, buttons);
246
        }
247
 
248
        private void txtLog_VisibleChanged(object sender, EventArgs e) {
249
            if (txtLog.Visible) {
250
                txtLog.SelectionStart = txtLog.TextLength;
251
                txtLog.ScrollToCaret();
252
            }
253
        }
254
 
255
        private void cmdSimConnect_Click(object sender, EventArgs e) {
256
            if (fsx.hSimConnect != null)
257
                this.SimConnect_Disconnect();
258
 
259
            this.SimConnect_Connect();
260
        }
261
 
262
        private void cmdRequest_Click(object sender, EventArgs e) {
263
 
264
        }
265
 
266
        private void inputTimer_Tick(object sender, EventArgs e) {
5 pfowler 267
            // @TODO: Each device now has its own input timer, remove the below
268
            //foreach (NITDevice device in panels.devices) {
269
            //    if (device.isOpen())
270
            //        device.SimButtons();
271
            //}
3 pfowler 272
 
273
        }
274
 
275
        private void MainForm_FormClosing(object sender, FormClosingEventArgs e) {
276
            this.quitNITPanels();
277
        }
278
 
279
        private void debugTimer_Tick(object sender, EventArgs e) {
280
 
281
        }
282
 
7 pfowler 283
        private void cmdAssign_Click(object sender, EventArgs e) {
284
            NITDevice device = (NITDevice)this.deviceGrid.CurrentRow.DataBoundItem;
285
            int value = NITPanelsForms.ReAssignDevice(device);
286
            device.assigned = value;
287
            this.deviceGrid.Update();
288
            this.deviceGrid.Refresh();
289
            device.reAssign();
290
        }
3 pfowler 291
    }
292
}