Subversion Repositories group.NITPanels

Rev

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.Linq;
4
using System.Text;
5
using System.Threading.Tasks;
6
 
7
using Microsoft.FlightSimulator.SimConnect;
8
using System.Runtime.InteropServices;
9
 
10
namespace NITNavComm {
11
    public class FSXObject {
12
 
13
        public SimData simdata;
14
        public SimConnect hSimConnect = null;
15
 
16
        public enum DATA_REQUESTS {
17
            AIRCRAFT,
18
            AVIONICS,
19
            NAVCOM1_REQ,
20
            NAVCOM2_REQ,
21
            AUDIOSEL_REQ
22
        }
23
 
24
        public enum DEFINITIONS {
25
            AIRCRAFT,
26
            AVIONICS,
27
            NAVCOM1_DATA,
28
            NAVCOM2_DATA,
29
            AUDIOSEL_DATA
30
        }
31
 
32
        public enum EVENT_ID {
33
            // AudioSel Events, these go first to make mapping easy (0-7)
34
            COM1_TRANSMIT_SELECT,
35
            COM2_TRANSMIT_SELECT,
36
            COM_RECEIVE_ALL_TOGGLE,
37
            RADIO_VOR1_IDENT_TOGGLE,
38
            RADIO_VOR2_IDENT_TOGGLE,
39
            MARKER_SOUND_TOGGLE,
40
            RADIO_DME1_IDENT_TOGGLE,
41
            RADIO_ADF_IDENT_TOGGLE,
42
 
43
            // NavCom1 Events
44
            COM1_SWAP_FREQ,
45
            NAV1_SWAP_FREQ,
46
            COM_RADIO_WHOLE_INC,
47
            COM_RADIO_WHOLE_DEC,
48
            COM_RADIO_FRACT_INC,
49
            COM_RADIO_FRACT_DEC,
50
            NAV1_RADIO_WHOLE_INC,
51
            NAV1_RADIO_WHOLE_DEC,
52
            NAV1_RADIO_FRACT_INC,
53
            NAV1_RADIO_FRACT_DEC,
54
 
55
            // NavComm2 Events
56
            COM2_SWAP_FREQ,
57
            NAV2_SWAP_FREQ,
58
            COM2_RADIO_WHOLE_INC,
59
            COM2_RADIO_WHOLE_DEC,
60
            COM2_RADIO_FRACT_INC,
61
            COM2_RADIO_FRACT_DEC,
62
            NAV2_RADIO_WHOLE_INC,
63
            NAV2_RADIO_WHOLE_DEC,
64
            NAV2_RADIO_FRACT_INC,
65
            NAV2_RADIO_FRACT_DEC,
66
        }
67
 
68
        public enum GROUP_ID {
69
            NAVCOM1,
70
            NAVCOM2,
71
            AUDIOSEL
72
        }
73
 
74
        public enum MENU_ID {
75
            NIT_PANELS,
76
        }
77
 
78
        [StructLayout(LayoutKind.Sequential, Pack = 1)]
79
        public struct SimData {
80
            public uint objectid;
81
            public Aircraft_Data aircraft;
82
        }
83
 
84
        [StructLayout(LayoutKind.Sequential, Pack = 1)]
85
        public struct NavCom_Data {
86
            public double Freq0;
87
            public double Freq1;
88
            public double Freq2;
89
            public double Freq3;
90
        }
91
 
92
        [StructLayout(LayoutKind.Sequential, Pack = 1)]
93
        public struct AudioSel_Data {
94
            public double com1;
95
            public double com2;
96
            public double comb;
97
            public double vor1;
98
            public double vor2;
99
            public double mkr;
100
            public double dme;
101
            public double adf;
102
        }
103
 
104
        [StructLayout(LayoutKind.Sequential, Pack = 1)]
105
        public struct Avionics_Data {
106
            public bool avionics_master;
107
            public double com1_status;
108
            public double com2_status;
109
            public bool nav1_available;
110
            public bool nav2_available;
111
        }
112
 
113
        [StructLayout(LayoutKind.Sequential, Pack = 1)]
114
        public struct Aircraft_Data {
115
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
116
            public string atc_type;
117
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
118
            public string atc_model;
119
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
120
            public string atc_id;
121
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
122
            public string title;
123
        }
124
 
125
        public FSXObject() {
126
            this.simdata = new SimData();
127
        }
128
 
129
        private void transmitEvent(Enum eventid, Enum groupid) {
130
            this.transmitEvent(eventid, groupid, 1);
131
        }
132
 
133
        private void transmitEvent(Enum eventid, Enum groupid, uint data) {
134
            this.hSimConnect.TransmitClientEvent(
135
                this.simdata.objectid,
136
                eventid, data, groupid,
137
                SIMCONNECT_EVENT_FLAG.GROUPID_IS_PRIORITY
138
            );
139
        }
140
 
141
        private void requestData(Enum dataRequest, Enum definition) {
142
            hSimConnect.RequestDataOnSimObject(
143
                dataRequest,
144
                definition,
145
                this.simdata.objectid,
146
                SIMCONNECT_PERIOD.ONCE,
147
                SIMCONNECT_DATA_REQUEST_FLAG.DEFAULT,
148
                0, 0, 0);
149
        }
150
 
151
        public void FsxInit() {
152
            this.hSimConnect.MenuAddItem("NITPanels", FSXObject.MENU_ID.NIT_PANELS, 0);
153
 
154
        }
155
 
156
        public void FsxReady() {
157
            this.hSimConnect.RequestDataOnSimObject(
158
                FSXObject.DATA_REQUESTS.AUDIOSEL_REQ,
159
                FSXObject.DEFINITIONS.AUDIOSEL_DATA,
160
                this.simdata.objectid,
161
                SIMCONNECT_PERIOD.SIM_FRAME,
162
                SIMCONNECT_DATA_REQUEST_FLAG.CHANGED,
163
                0, 0, 0);
164
            this.hSimConnect.RequestDataOnSimObject(
165
                FSXObject.DATA_REQUESTS.NAVCOM1_REQ,
166
                FSXObject.DEFINITIONS.NAVCOM1_DATA,
167
                this.simdata.objectid,
168
                SIMCONNECT_PERIOD.SIM_FRAME,
169
                SIMCONNECT_DATA_REQUEST_FLAG.CHANGED,
170
                0, 0, 0);
171
 
172
            this.hSimConnect.RequestDataOnSimObject(
173
                FSXObject.DATA_REQUESTS.NAVCOM2_REQ,
174
                FSXObject.DEFINITIONS.NAVCOM2_DATA,
175
                this.simdata.objectid,
176
                SIMCONNECT_PERIOD.SIM_FRAME,
177
                SIMCONNECT_DATA_REQUEST_FLAG.CHANGED,
178
                0, 0, 0);
179
 
180
            hSimConnect.RequestDataOnSimObject(
181
                FSXObject.DATA_REQUESTS.AVIONICS,
182
                FSXObject.DEFINITIONS.AVIONICS,
183
                this.simdata.objectid,
184
                SIMCONNECT_PERIOD.SIM_FRAME,
185
                SIMCONNECT_DATA_REQUEST_FLAG.CHANGED,
186
                0, 0, 0);
187
        }
188
 
189
        // Individual data requests
190
        public void requestNavComm1Data() {
191
            this.requestData(FSXObject.DATA_REQUESTS.NAVCOM1_REQ, FSXObject.DEFINITIONS.NAVCOM1_DATA);
192
        }
193
 
194
        public void requestNavComm2Data() {
195
            this.requestData(FSXObject.DATA_REQUESTS.NAVCOM2_REQ, FSXObject.DEFINITIONS.NAVCOM2_DATA);
196
        }
197
 
198
        public void requestAudioSelData() {
199
            this.requestData(FSXObject.DATA_REQUESTS.AUDIOSEL_REQ, FSXObject.DEFINITIONS.AUDIOSEL_DATA);
200
        }
201
 
202
        // AudioSel Events
203
        public void AudioSelButton(int button) {
204
            this.transmitEvent((FSXObject.EVENT_ID)button, FSXObject.GROUP_ID.AUDIOSEL);
205
        }
206
 
207
 
208
        // NavComm1 Events
209
        public void Comm1DecFract() {
210
            this.transmitEvent(FSXObject.EVENT_ID.COM_RADIO_FRACT_DEC, FSXObject.GROUP_ID.NAVCOM1);
211
        }
212
 
213
        public void Comm1DecWhole() {
214
            this.transmitEvent(FSXObject.EVENT_ID.COM_RADIO_WHOLE_DEC, FSXObject.GROUP_ID.NAVCOM1);
215
        }
216
 
217
        public void Comm1IncFract() {
218
            this.transmitEvent(FSXObject.EVENT_ID.COM_RADIO_FRACT_INC, FSXObject.GROUP_ID.NAVCOM1);
219
        }
220
 
221
        public void Comm1IncWhole() {
222
            this.transmitEvent(FSXObject.EVENT_ID.COM_RADIO_WHOLE_INC, FSXObject.GROUP_ID.NAVCOM1);
223
        }
224
 
225
        public void Comm1SwapFreq() {
226
            this.transmitEvent(FSXObject.EVENT_ID.COM1_SWAP_FREQ, FSXObject.GROUP_ID.NAVCOM1);
227
        }
228
 
229
        public void Nav1DecFract() {
230
            this.transmitEvent(FSXObject.EVENT_ID.NAV1_RADIO_FRACT_DEC, FSXObject.GROUP_ID.NAVCOM1);
231
        }
232
 
233
        public void Nav1DecWhole() {
234
            this.transmitEvent(FSXObject.EVENT_ID.NAV1_RADIO_WHOLE_DEC, FSXObject.GROUP_ID.NAVCOM1);
235
        }
236
 
237
        public void Nav1IncFract() {
238
            this.transmitEvent(FSXObject.EVENT_ID.NAV1_RADIO_FRACT_INC, FSXObject.GROUP_ID.NAVCOM1);
239
        }
240
 
241
        public void Nav1IncWhole() {
242
            this.transmitEvent(FSXObject.EVENT_ID.NAV1_RADIO_WHOLE_INC, FSXObject.GROUP_ID.NAVCOM1);
243
        }
244
 
245
        public void Nav1SwapFreq() {
246
            this.transmitEvent(FSXObject.EVENT_ID.NAV1_SWAP_FREQ, FSXObject.GROUP_ID.NAVCOM1);
247
        }
248
 
249
 
250
        // NavComm2 Events
251
        public void Comm2DecFract() {
252
            this.transmitEvent(FSXObject.EVENT_ID.COM2_RADIO_FRACT_DEC, FSXObject.GROUP_ID.NAVCOM2);
253
        }
254
 
255
        public void Comm2DecWhole() {
256
            this.transmitEvent(FSXObject.EVENT_ID.COM2_RADIO_WHOLE_DEC, FSXObject.GROUP_ID.NAVCOM2);
257
        }
258
 
259
        public void Comm2IncFract() {
260
            this.transmitEvent(FSXObject.EVENT_ID.COM2_RADIO_FRACT_INC, FSXObject.GROUP_ID.NAVCOM2);
261
        }
262
 
263
        public void Comm2IncWhole() {
264
            this.transmitEvent(FSXObject.EVENT_ID.COM2_RADIO_WHOLE_INC, FSXObject.GROUP_ID.NAVCOM2);
265
        }
266
 
267
        public void Comm2SwapFreq() {
268
            this.transmitEvent(FSXObject.EVENT_ID.COM2_SWAP_FREQ, FSXObject.GROUP_ID.NAVCOM2);
269
        }
270
 
271
        public void Nav2DecFract() {
272
            this.transmitEvent(FSXObject.EVENT_ID.NAV2_RADIO_FRACT_DEC, FSXObject.GROUP_ID.NAVCOM2);
273
        }
274
 
275
        public void Nav2DecWhole() {
276
            this.transmitEvent(FSXObject.EVENT_ID.NAV2_RADIO_WHOLE_DEC, FSXObject.GROUP_ID.NAVCOM2);
277
        }
278
 
279
        public void Nav2IncFract() {
280
            this.transmitEvent(FSXObject.EVENT_ID.NAV2_RADIO_FRACT_INC, FSXObject.GROUP_ID.NAVCOM2);
281
        }
282
 
283
        public void Nav2IncWhole() {
284
            this.transmitEvent(FSXObject.EVENT_ID.NAV2_RADIO_WHOLE_INC, FSXObject.GROUP_ID.NAVCOM2);
285
        }
286
 
287
        public void Nav2SwapFreq() {
288
            this.transmitEvent(FSXObject.EVENT_ID.NAV2_SWAP_FREQ, FSXObject.GROUP_ID.NAVCOM2);
289
        }
290
 
291
 
292
 
293
    }
294
}