Subversion Repositories group.electronics

Rev

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

Rev 145 Rev 153
Line 1... Line 1...
1
using System;
1
using System;
2
using System.Collections.Generic;
2
using System.Collections.Generic;
3
using System.ComponentModel;
3
using System.ComponentModel;
4
using System.Data;
4
using System.Data;
5
using System.Drawing;
5
using System.Drawing;
6
using System.Linq;
-
 
7
using System.Text;
6
using System.Text;
8
using System.Threading.Tasks;
7
using System.Threading.Tasks;
9
using System.Windows.Forms;
8
using System.Windows.Forms;
10
 
9
 
-
 
10
using Microsoft.FlightSimulator.SimConnect;
-
 
11
using System.Runtime.InteropServices;
-
 
12
 
11
namespace NITNavComm
13
namespace NITNavComm
12
{
14
{
13
    public partial class MainForm : Form
15
    public partial class MainForm : Form
14
    {
16
    {
15
 
17
 
16
        private NITPanels panels = new NITPanels();
18
        private NITPanels panels = new NITPanels();
-
 
19
        private SimConnect hSimConnect = null;
-
 
20
 
-
 
21
        private SimData simdata = new SimData();
-
 
22
 
-
 
23
        private struct SimData {
-
 
24
            public uint objectid;
-
 
25
            public Aircraft_Data aircraft;
-
 
26
        }
17
 
27
 
18
        public MainForm() {
28
        public MainForm() {
19
            InitializeComponent();
29
            InitializeComponent();
20
 
30
 
21
            Devices_Rescan();
31
            Devices_Rescan();
-
 
32
            txtStatus.Text = "Running";
-
 
33
 
-
 
34
        }
-
 
35
 
-
 
36
        private enum DATA_REQUESTS {
-
 
37
            AIRCRAFT, 
-
 
38
            AVIONICS,
-
 
39
            NAVCOM1_REQ,
-
 
40
            NAVCOM2_REQ
-
 
41
        }
-
 
42
 
-
 
43
        private enum DEFINITIONS {
-
 
44
            AIRCRAFT,
-
 
45
            AVIONICS,
-
 
46
            NAVCOM1_DATA,
-
 
47
            NAVCOM2_DATA
-
 
48
        }
-
 
49
 
-
 
50
        private enum EVENT_ID {
-
 
51
            COM1_SWAP_FREQ,
-
 
52
            NAV1_SWAP_FREQ,
-
 
53
            COM_RADIO_WHOLE_INC,
-
 
54
            COM_RADIO_WHOLE_DEC,
-
 
55
        }
-
 
56
 
-
 
57
        private enum GROUP_ID {
-
 
58
            NAVCOM1
-
 
59
        }
-
 
60
 
-
 
61
        [StructLayout(LayoutKind.Sequential, Pack = 1)]
-
 
62
        public struct NavCom_Data {
-
 
63
            public double Freq0;
-
 
64
            public double Freq1;
-
 
65
            public double Freq2;
-
 
66
            public double Freq3;
-
 
67
        }
-
 
68
 
-
 
69
        [StructLayout(LayoutKind.Sequential, Pack = 1)]
-
 
70
        public struct Avionics_Data {
-
 
71
            public bool avionics_master;
-
 
72
            public double com_status;
-
 
73
        }
-
 
74
 
-
 
75
        [StructLayout(LayoutKind.Sequential, Pack = 1)]
-
 
76
        public struct Aircraft_Data {
-
 
77
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
-
 
78
            public string atc_type;
-
 
79
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
-
 
80
            public string atc_model;
-
 
81
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
-
 
82
            public string atc_id;
-
 
83
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
-
 
84
            public string title;
-
 
85
        }
-
 
86
 
-
 
87
        private void SimConnect_Connect() {
-
 
88
            if (hSimConnect == null) {
-
 
89
                try {
-
 
90
                    hSimConnect = new SimConnect("Managed Data Request", base.Handle, 0x402, null, 0);
-
 
91
                    txtSimConnect.Text = "Available";
-
 
92
                    this.SimConnect_InitDataRequest();
-
 
93
                } catch (COMException) {
-
 
94
                    Log("Could not connect to FSX");
-
 
95
                    txtSimConnect.Text = "Failed";
-
 
96
                }
-
 
97
            }
-
 
98
        }
-
 
99
 
-
 
100
        private void SimConnect_Disconnect() {
-
 
101
            this.InitFsxClosed();
-
 
102
 
-
 
103
            if (hSimConnect != null) {
-
 
104
                hSimConnect.Dispose();
-
 
105
                txtSimConnect.Text = "Available";
-
 
106
            } else {
-
 
107
                txtSimConnect.Text = "Failed";
-
 
108
            }
-
 
109
            hSimConnect = null;
-
 
110
        }
-
 
111
 
-
 
112
        protected override void DefWndProc(ref Message m) {
-
 
113
            if (m.Msg == 0x402) {
-
 
114
                if (hSimConnect != null) {
-
 
115
                    hSimConnect.ReceiveMessage();
-
 
116
                }
-
 
117
            } else {
-
 
118
                base.DefWndProc(ref m);
-
 
119
            }
-
 
120
        }
-
 
121
 
-
 
122
        private void SimConnect_InitDataRequest() {
-
 
123
            try {
-
 
124
 
-
 
125
                hSimConnect.MapClientEventToSimEvent(EVENT_ID.COM1_SWAP_FREQ, "COM_STBY_RADIO_SWAP");
-
 
126
                hSimConnect.MapClientEventToSimEvent(EVENT_ID.NAV1_SWAP_FREQ, "NAV1_RADIO_SWAP");
-
 
127
                hSimConnect.MapClientEventToSimEvent(EVENT_ID.COM_RADIO_WHOLE_INC, "COM_RADIO_WHOLE_INC");
-
 
128
                hSimConnect.MapClientEventToSimEvent(EVENT_ID.COM_RADIO_WHOLE_DEC, "COM_RADIO_WHOLE_DEC");
-
 
129
 
-
 
130
                hSimConnect.AddToDataDefinition(DEFINITIONS.AIRCRAFT, "Atc Type", "", SIMCONNECT_DATATYPE.STRING32, 0.0f, SimConnect.SIMCONNECT_UNUSED);
-
 
131
                hSimConnect.AddToDataDefinition(DEFINITIONS.AIRCRAFT, "ATC Model", "", SIMCONNECT_DATATYPE.STRING32, 0.0f, SimConnect.SIMCONNECT_UNUSED);
-
 
132
                hSimConnect.AddToDataDefinition(DEFINITIONS.AIRCRAFT, "ATC ID", "", SIMCONNECT_DATATYPE.STRING32, 0.0f, SimConnect.SIMCONNECT_UNUSED);
-
 
133
                hSimConnect.AddToDataDefinition(DEFINITIONS.AIRCRAFT, "Title", "", SIMCONNECT_DATATYPE.STRING128, 0.0f, SimConnect.SIMCONNECT_UNUSED);
-
 
134
                hSimConnect.RegisterDataDefineStruct<Aircraft_Data>(DEFINITIONS.AIRCRAFT);
-
 
135
 
-
 
136
                hSimConnect.AddToDataDefinition(DEFINITIONS.AVIONICS, "Avionics Master Switch", "bool", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
-
 
137
                hSimConnect.AddToDataDefinition(DEFINITIONS.AVIONICS, "Com Status", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
-
 
138
                hSimConnect.RegisterDataDefineStruct<Avionics_Data>(DEFINITIONS.AVIONICS);
-
 
139
 
-
 
140
                hSimConnect.AddToDataDefinition(DEFINITIONS.NAVCOM1_DATA, "Com Active Frequency:1", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
-
 
141
                hSimConnect.AddToDataDefinition(DEFINITIONS.NAVCOM1_DATA, "Com Standby Frequency:1", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
-
 
142
                hSimConnect.AddToDataDefinition(DEFINITIONS.NAVCOM1_DATA, "Nav Active Frequency:1", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
-
 
143
                hSimConnect.AddToDataDefinition(DEFINITIONS.NAVCOM1_DATA, "Nav Standby Frequency:1", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
-
 
144
                hSimConnect.RegisterDataDefineStruct<NavCom_Data>(DEFINITIONS.NAVCOM1_DATA);
-
 
145
 
-
 
146
                hSimConnect.AddToDataDefinition(DEFINITIONS.NAVCOM2_DATA, "Com Active Frequency:2", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
-
 
147
                hSimConnect.AddToDataDefinition(DEFINITIONS.NAVCOM2_DATA, "Com Standby Frequency:2", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
-
 
148
                hSimConnect.AddToDataDefinition(DEFINITIONS.NAVCOM2_DATA, "Nav Active Frequency:2", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
-
 
149
                hSimConnect.AddToDataDefinition(DEFINITIONS.NAVCOM2_DATA, "Nav Standby Frequency:2", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
-
 
150
                hSimConnect.RegisterDataDefineStruct<NavCom_Data>(DEFINITIONS.NAVCOM2_DATA);
-
 
151
 
-
 
152
                hSimConnect.OnRecvOpen += new SimConnect.RecvOpenEventHandler(SimConnect_OnRecvOpen);
-
 
153
                hSimConnect.OnRecvQuit += new SimConnect.RecvQuitEventHandler(SimConnect_OnRecvQuit);
-
 
154
                hSimConnect.OnRecvException += new SimConnect.RecvExceptionEventHandler(SimConnect_OnRecvException);
-
 
155
                hSimConnect.OnRecvSimobjectData += new SimConnect.RecvSimobjectDataEventHandler(SimConnect_OnRecvSimObjectData);
-
 
156
                hSimConnect.OnRecvSimobjectDataBytype += new SimConnect.RecvSimobjectDataBytypeEventHandler(SimConnect_OnRecvSimObjectDataByType);
-
 
157
 
-
 
158
                // Request for aircrat data. This will also configure auto-updates for other required data.
-
 
159
                hSimConnect.RequestDataOnSimObjectType(DATA_REQUESTS.AIRCRAFT, DEFINITIONS.AIRCRAFT, 0, SIMCONNECT_SIMOBJECT_TYPE.USER);
-
 
160
 
-
 
161
            } catch (COMException e) {
-
 
162
                Log(e.Message);
-
 
163
            }
-
 
164
        }
-
 
165
 
-
 
166
        private void SimConnect_OnRecvOpen(SimConnect sender, SIMCONNECT_RECV_OPEN data) {
-
 
167
            Log("Connected to FSX.");
-
 
168
            txtSimConnect.Text = "Connected";
-
 
169
        }
-
 
170
 
-
 
171
        private void SimConnect_OnRecvQuit(SimConnect sender, SIMCONNECT_RECV data) {
-
 
172
            Log("FSX has exited.");
-
 
173
            this.SimConnect_Disconnect();
-
 
174
        }
-
 
175
 
-
 
176
        private void SimConnect_OnRecvException(SimConnect sender, SIMCONNECT_RECV_EXCEPTION data) {
-
 
177
            Log("SimConnect Exception: " + (uint)(data.dwException));
-
 
178
        }
-
 
179
 
-
 
180
        private void SimConnect_OnRecvSimObjectData(SimConnect sender, SIMCONNECT_RECV_SIMOBJECT_DATA data) {
-
 
181
            switch (data.dwRequestID) {
-
 
182
 
-
 
183
                // Automatically provided updates on NavCom1
-
 
184
                case ((uint)DATA_REQUESTS.NAVCOM1_REQ): {
-
 
185
                        NavCom_Data navcomdata = (NavCom_Data)data.dwData[0];
-
 
186
                        NITNavCommDevice navcomm = (NITNavCommDevice)panels.devices[0];
-
 
187
                        navcomm.setNavComFreqs(navcomdata);
-
 
188
                        break;
-
 
189
                }
-
 
190
            }
-
 
191
        }
-
 
192
 
-
 
193
        private void SimConnect_OnRecvSimObjectDataByType(SimConnect sender, SIMCONNECT_RECV_SIMOBJECT_DATA_BYTYPE data) {
-
 
194
            switch (data.dwRequestID) {
-
 
195
 
-
 
196
                // Request basic aircraft data to get the object ID
-
 
197
                case ((uint)DATA_REQUESTS.AIRCRAFT): {
-
 
198
                        uint ObjectID = data.dwObjectID;
-
 
199
                        Aircraft_Data aircraftdata = (Aircraft_Data)data.dwData[0];
-
 
200
                        
-
 
201
                        this.simdata.objectid = ObjectID;
-
 
202
                        this.simdata.aircraft = aircraftdata;
-
 
203
 
-
 
204
                        this.InitFsxReady();                      
-
 
205
 
-
 
206
                        break;
-
 
207
                    }
-
 
208
            }           
-
 
209
        }
-
 
210
 
-
 
211
        private void InitFsxReady() {
-
 
212
 
-
 
213
 
-
 
214
            Log("Aircraft: "
-
 
215
                + this.simdata.aircraft.atc_id + ", "
-
 
216
                + this.simdata.aircraft.atc_model + ", "
-
 
217
                + this.simdata.aircraft.atc_type + ", "
-
 
218
                + this.simdata.aircraft.title
-
 
219
            );
-
 
220
            hSimConnect.RequestDataOnSimObject(
-
 
221
                DATA_REQUESTS.NAVCOM1_REQ,
-
 
222
                DEFINITIONS.NAVCOM1_DATA,
-
 
223
                this.simdata.objectid,
-
 
224
                SIMCONNECT_PERIOD.SIM_FRAME,
-
 
225
                SIMCONNECT_DATA_REQUEST_FLAG.CHANGED,
-
 
226
                0, 0, 0);
-
 
227
 
-
 
228
            inputTimer.Enabled = true;
-
 
229
        }
-
 
230
 
-
 
231
        private void InitFsxClosed() {
-
 
232
            inputTimer.Enabled = false;
22
 
233
 
23
        }
234
        }
24
 
235
 
25
        private void Devices_Rescan() {
236
        private void Devices_Rescan() {
26
            Log("Scanning for devices...");
237
            Log("Scanning for devices...");
Line 34... Line 245...
34
                this.cmDevTest.Enabled = true;
245
                this.cmDevTest.Enabled = true;
35
                Log(panels.devices.Count.ToString() + " devices found during scan.");
246
                Log(panels.devices.Count.ToString() + " devices found during scan.");
36
            } else {
247
            } else {
37
                Log("No devices found, check connections and rescan.");
248
                Log("No devices found, check connections and rescan.");
38
            }
249
            }
-
 
250
 
-
 
251
            NITNavCommDevice navcomm = (NITNavCommDevice)panels.devices[0];
-
 
252
            navcomm.resetRotary(0, 0);
39
        }
253
        }
40
 
254
 
41
        private void cmdRescan_Click(object sender, EventArgs e) {
255
        private void cmdRescan_Click(object sender, EventArgs e) {
42
            this.Devices_Rescan();
256
            this.Devices_Rescan();
43
        }
257
        }
Line 45... Line 259...
45
        private void cmdDevRescan_Click(object sender, EventArgs e) {
259
        private void cmdDevRescan_Click(object sender, EventArgs e) {
46
            this.Devices_Rescan();
260
            this.Devices_Rescan();
47
        }
261
        }
48
 
262
 
49
        private void cmDevTest_Click(object sender, EventArgs e) {
263
        private void cmDevTest_Click(object sender, EventArgs e) {
50
            NITDevice device = (NITDevice) this.deviceGrid.CurrentRow.DataBoundItem;
264
            //NITDevice device = (NITDevice) this.deviceGrid.CurrentRow.DataBoundItem;
51
            NITNavCommDevice navcomm = new NITNavCommDevice(device);
265
            NITNavCommDevice navcomm = (NITNavCommDevice)this.deviceGrid.CurrentRow.DataBoundItem;;
52
 
266
 
53
            if (!navcomm.Open()) {
267
            if (!navcomm.Open()) {
54
                Log("Could not open device " + device.type + "(" + device.serial + ").");
268
                Log("Could not open device " + navcomm.type + "(" + navcomm.serial + ").");
55
                return;
269
                return;
56
            }
270
            }
57
            navcomm.Close();
271
            navcomm.Close();
58
 
272
 
59
            NITCommNavForm form = new NITCommNavForm();
273
            NITCommNavForm form = new NITCommNavForm();
Line 97... Line 311...
97
                txtLog.SelectionStart = txtLog.TextLength;
311
                txtLog.SelectionStart = txtLog.TextLength;
98
                txtLog.ScrollToCaret();
312
                txtLog.ScrollToCaret();
99
            }
313
            }
100
        }
314
        }
101
 
315
 
-
 
316
        private void cmdSimConnect_Click(object sender, EventArgs e) {
-
 
317
            if (hSimConnect != null)
-
 
318
                this.SimConnect_Disconnect();
-
 
319
 
-
 
320
            this.SimConnect_Connect();
-
 
321
        }
-
 
322
 
-
 
323
        private void cmdRequest_Click(object sender, EventArgs e) {
-
 
324
            hSimConnect.TransmitClientEvent(
-
 
325
                this.simdata.objectid, 
-
 
326
                EVENT_ID.COM1_SWAP_FREQ, 1, 
-
 
327
                GROUP_ID.NAVCOM1, 
-
 
328
                SIMCONNECT_EVENT_FLAG.GROUPID_IS_PRIORITY
-
 
329
                );
-
 
330
        }
-
 
331
 
-
 
332
        private void inputTimer_Tick(object sender, EventArgs e) {
-
 
333
            NITNavCommDevice navcomm = (NITNavCommDevice)panels.devices[0];
-
 
334
            navcomm.updateInput();
-
 
335
 
-
 
336
            if (navcomm.isSwapSet(0))
-
 
337
                hSimConnect.TransmitClientEvent(
-
 
338
                this.simdata.objectid,
-
 
339
                EVENT_ID.COM1_SWAP_FREQ, 1,
-
 
340
                GROUP_ID.NAVCOM1,
-
 
341
                SIMCONNECT_EVENT_FLAG.GROUPID_IS_PRIORITY
-
 
342
                );
-
 
343
 
-
 
344
            if (navcomm.isSwapSet(1))
-
 
345
                hSimConnect.TransmitClientEvent(
-
 
346
                this.simdata.objectid,
-
 
347
                EVENT_ID.NAV1_SWAP_FREQ, 1,
-
 
348
                GROUP_ID.NAVCOM1,
-
 
349
                SIMCONNECT_EVENT_FLAG.GROUPID_IS_PRIORITY
-
 
350
                );
-
 
351
 
-
 
352
            sbyte delta = navcomm.getRotary(0, 0);
-
 
353
            navcomm.resetRotary(0, 0);
-
 
354
            if (delta != 0) {
-
 
355
                //sbyte start = 0;
-
 
356
                //sbyte finish = 0;
-
 
357
                if (delta < 0) {
-
 
358
                    //start = delta;
-
 
359
                    //for (sbyte i = start; start < finish; i++) {
-
 
360
                    delta = (sbyte)-delta;
-
 
361
                        hSimConnect.TransmitClientEvent(
-
 
362
                            this.simdata.objectid,
-
 
363
                            EVENT_ID.COM_RADIO_WHOLE_DEC, 1,
-
 
364
                            GROUP_ID.NAVCOM1,
-
 
365
                            SIMCONNECT_EVENT_FLAG.GROUPID_IS_PRIORITY
-
 
366
                        );
-
 
367
                    //}
-
 
368
                } else {
-
 
369
                    //finish = delta;
-
 
370
                    //for (sbyte i = start; start < finish; i++) {
-
 
371
                        hSimConnect.TransmitClientEvent(
-
 
372
                            this.simdata.objectid,
-
 
373
                            EVENT_ID.COM_RADIO_WHOLE_INC, 1,
-
 
374
                            GROUP_ID.NAVCOM1,
-
 
375
                            SIMCONNECT_EVENT_FLAG.GROUPID_IS_PRIORITY
-
 
376
                        );
-
 
377
                    //}
-
 
378
                }
-
 
379
                
-
 
380
            }
-
 
381
 
-
 
382
 
-
 
383
        }
-
 
384
 
102
    }
385
    }
103
}
386
}