Subversion Repositories group.NITPanels

Rev

Rev 3 | Rev 5 | Go to most recent revision | 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();
121
                        this.InitFsxReady();
122
 
123
                        break;
124
                    }
125
            }           
126
        }
127
 
128
        private void InitFsxReady() {
129
 
130
            Log("Aircraft: "
131
                + this.fsx.simdata.aircraft.atc_id + ", "
132
                + this.fsx.simdata.aircraft.atc_model + ", "
133
                + this.fsx.simdata.aircraft.atc_type + ", "
134
                + this.fsx.simdata.aircraft.title
135
            );
136
 
137
            inputTimer.Enabled = true;
138
        }
139
 
140
        private void InitFsxClosed() {
141
            if (fsx.hSimConnect != null) {
142
                fsx.hSimConnect.Dispose();
143
                txtSimConnect.Text = "Available";
144
            } else {
145
                txtSimConnect.Text = "Failed";
146
                fsx.hSimConnect = null;
147
            }
148
        }
149
 
150
        private void Devices_Rescan() {
151
            Log("Scanning for devices...");
152
 
153
            panels.UsbScan();
154
 
155
            this.deviceGrid.DataSource = panels.devices;
156
            this.txtDevices.Text = panels.devices.Count.ToString();
157
 
158
            if (panels.devices.Count > 0) {
159
                this.cmDevTest.Enabled = true;
160
                Log(panels.devices.Count.ToString() + " devices found during scan.");
161
 
162
            } else {
163
                Log("No devices found, check connections and rescan.");
164
            }
165
        }
166
 
167
 
168
 
169
        private void cmdRescan_Click(object sender, EventArgs e) {
170
            this.Devices_Rescan();
171
        }
172
 
173
        private void cmdDevRescan_Click(object sender, EventArgs e) {
174
            this.Devices_Rescan();
175
        }
176
 
177
        private void cmDevTest_Click(object sender, EventArgs e) {
178
            NITDevice device = (NITDevice) this.deviceGrid.CurrentRow.DataBoundItem;
179
 
180
            if (!device.Open()) {
181
                Log("Could not open device " + device.type + "(" + device.serial + ").");
182
                return;
183
            }
184
            if (device.type == "NITNavComm") {
185
                NITCommNavForm form = new NITCommNavForm();
186
                form.setDevice((NITNavCommDevice)device);
187
                form.Show(this);
188
            } else if (device.type == "NITAudioSel") {
189
                NITAudioSelForm form = new NITAudioSelForm();
190
                form.setDevice((NITAudioSelDevice)device);
191
                form.Show(this);
192
            } else {
193
                Log("No device type for " + device.type + " (" + device.serial + ").");
194
            }
195
        }
196
 
197
        public void Log(string msg) {
198
            txtLog.AppendText(msg + "\r\n");
199
        }
200
 
201
        public void Log(byte[] buffer) {
202
            StringBuilder sb = new StringBuilder();
203
            foreach (byte data in buffer) {
204
                sb.Append(data.ToString("X") + " ");
205
            }
206
            Log(sb.ToString());
207
        }
208
 
209
        private void quitNITPanels() {        
210
            inputTimer.Enabled = false;
211
 
212
            foreach (NITDevice device in panels.devices) {
213
                if (device.isOpen())
214
                    device.Close();
215
            }        
216
 
217
            Application.Exit();
218
        }
219
 
220
        private void quitToolStripMenuItem_Click(object sender, EventArgs e) {
221
            if (MainForm.QuestionBox("Exit NIT Panels?", "Exiting will disable NIT Panels. Continue?")) {
222
                this.quitNITPanels();
223
            }
224
        }
225
 
226
 
227
        public static bool QuestionBox(string caption, string message) {
228
            MessageBoxButtons buttons = MessageBoxButtons.YesNo;
229
            DialogResult result;
230
 
231
            result = MessageBox.Show(message, caption, buttons);
232
 
233
            if (result == System.Windows.Forms.DialogResult.Yes) {
234
                return true;
235
            }
236
            return false;
237
        }
238
 
239
        public static void NITMessageBox(string caption, string message) {
240
            MessageBoxButtons buttons = MessageBoxButtons.OK;
241
            MessageBox.Show(message, caption, buttons);
242
        }
243
 
244
        private void txtLog_VisibleChanged(object sender, EventArgs e) {
245
            if (txtLog.Visible) {
246
                txtLog.SelectionStart = txtLog.TextLength;
247
                txtLog.ScrollToCaret();
248
            }
249
        }
250
 
251
        private void cmdSimConnect_Click(object sender, EventArgs e) {
252
            if (fsx.hSimConnect != null)
253
                this.SimConnect_Disconnect();
254
 
255
            this.SimConnect_Connect();
256
        }
257
 
258
        private void cmdRequest_Click(object sender, EventArgs e) {
259
 
260
        }
261
 
262
        private void inputTimer_Tick(object sender, EventArgs e) {
263
            foreach (NITDevice device in panels.devices) {
264
                if (device.isOpen())
265
                    device.SimButtons();
266
            }
267
 
268
        }
269
 
270
        private void MainForm_FormClosing(object sender, FormClosingEventArgs e) {
271
            this.quitNITPanels();
272
        }
273
 
274
        private void debugTimer_Tick(object sender, EventArgs e) {
275
 
276
        }
277
 
278
    }
279
}