Subversion Repositories group.electronics

Rev

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

Rev 153 Rev 155
Line 16... Line 16...
16
    {
16
    {
17
 
17
 
18
        private NITPanels panels = new NITPanels();
18
        private NITPanels panels = new NITPanels();
19
        private SimConnect hSimConnect = null;
19
        private SimConnect hSimConnect = null;
20
 
20
 
21
        private SimData simdata = new SimData();
21
        private FSXObject fsx = new FSXObject();
22
 
22
 
23
        private struct SimData {
23
        private NITNavCommDevice comm1;
24
            public uint objectid;
24
        private NITNavCommDevice comm2;
25
            public Aircraft_Data aircraft;
-
 
26
        }
-
 
27
 
25
 
28
        public MainForm() {
26
        public MainForm() {
29
            InitializeComponent();
27
            InitializeComponent();
30
 
28
 
31
            Devices_Rescan();
29
            Devices_Rescan();
32
            txtStatus.Text = "Running";
30
            txtStatus.Text = "Running";
33
 
31
 
34
        }
32
        }
35
 
33
 
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
 
34
 
87
        private void SimConnect_Connect() {
35
        private void SimConnect_Connect() {
88
            if (hSimConnect == null) {
36
            if (hSimConnect == null) {
89
                try {
37
                try {
90
                    hSimConnect = new SimConnect("Managed Data Request", base.Handle, 0x402, null, 0);
38
                    hSimConnect = new SimConnect("Managed Data Request", base.Handle, 0x402, null, 0);
-
 
39
                    fsx.hSimConnect = this.hSimConnect;
91
                    txtSimConnect.Text = "Available";
40
                    txtSimConnect.Text = "Available";
92
                    this.SimConnect_InitDataRequest();
41
                    this.SimConnect_InitDataRequest();
93
                } catch (COMException) {
42
                } catch (COMException) {
94
                    Log("Could not connect to FSX");
43
                    Log("Could not connect to FSX");
95
                    txtSimConnect.Text = "Failed";
44
                    txtSimConnect.Text = "Failed";
Line 97... Line 46...
97
            }
46
            }
98
        }
47
        }
99
 
48
 
100
        private void SimConnect_Disconnect() {
49
        private void SimConnect_Disconnect() {
101
            this.InitFsxClosed();
50
            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
        }
51
        }
111
 
52
 
112
        protected override void DefWndProc(ref Message m) {
53
        protected override void DefWndProc(ref Message m) {
113
            if (m.Msg == 0x402) {
54
            if (m.Msg == 0x402) {
114
                if (hSimConnect != null) {
55
                if (hSimConnect != null) {
Line 120... Line 61...
120
        }
61
        }
121
 
62
 
122
        private void SimConnect_InitDataRequest() {
63
        private void SimConnect_InitDataRequest() {
123
            try {
64
            try {
124
 
65
 
125
                hSimConnect.MapClientEventToSimEvent(EVENT_ID.COM1_SWAP_FREQ, "COM_STBY_RADIO_SWAP");
66
                hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.COM1_SWAP_FREQ, "COM_STBY_RADIO_SWAP");
126
                hSimConnect.MapClientEventToSimEvent(EVENT_ID.NAV1_SWAP_FREQ, "NAV1_RADIO_SWAP");
67
                hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.NAV1_SWAP_FREQ, "NAV1_RADIO_SWAP");
127
                hSimConnect.MapClientEventToSimEvent(EVENT_ID.COM_RADIO_WHOLE_INC, "COM_RADIO_WHOLE_INC");
68
                hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.COM_RADIO_WHOLE_INC, "COM_RADIO_WHOLE_INC");
128
                hSimConnect.MapClientEventToSimEvent(EVENT_ID.COM_RADIO_WHOLE_DEC, "COM_RADIO_WHOLE_DEC");
69
                hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.COM_RADIO_WHOLE_DEC, "COM_RADIO_WHOLE_DEC");
-
 
70
                hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.COM_RADIO_FRACT_INC, "COM_RADIO_FRACT_INC");
-
 
71
                hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.COM_RADIO_FRACT_DEC, "COM_RADIO_FRACT_DEC");
-
 
72
                hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.NAV1_RADIO_WHOLE_INC, "NAV1_RADIO_WHOLE_INC");
-
 
73
                hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.NAV1_RADIO_WHOLE_DEC, "NAV1_RADIO_WHOLE_DEC");
-
 
74
                hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.NAV1_RADIO_FRACT_INC, "NAV1_RADIO_FRACT_INC");
-
 
75
                hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.NAV1_RADIO_FRACT_DEC, "NAV1_RADIO_FRACT_DEC");
-
 
76
 
-
 
77
                hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.COM2_SWAP_FREQ, "COM2_RADIO_SWAP");
-
 
78
                hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.NAV2_SWAP_FREQ, "NAV2_RADIO_SWAP");
-
 
79
                hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.COM2_RADIO_WHOLE_INC, "COM2_RADIO_WHOLE_INC");
-
 
80
                hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.COM2_RADIO_WHOLE_DEC, "COM2_RADIO_WHOLE_DEC");
-
 
81
                hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.COM2_RADIO_FRACT_INC, "COM2_RADIO_FRACT_INC");
-
 
82
                hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.COM2_RADIO_FRACT_DEC, "COM2_RADIO_FRACT_DEC");
-
 
83
                hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.NAV2_RADIO_WHOLE_INC, "NAV2_RADIO_WHOLE_INC");
-
 
84
                hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.NAV2_RADIO_WHOLE_DEC, "NAV2_RADIO_WHOLE_DEC");
-
 
85
                hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.NAV2_RADIO_FRACT_INC, "NAV2_RADIO_FRACT_INC");
-
 
86
                hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.NAV2_RADIO_FRACT_DEC, "NAV2_RADIO_FRACT_DEC");
129
 
87
 
130
                hSimConnect.AddToDataDefinition(DEFINITIONS.AIRCRAFT, "Atc Type", "", SIMCONNECT_DATATYPE.STRING32, 0.0f, SimConnect.SIMCONNECT_UNUSED);
88
                hSimConnect.AddToDataDefinition(FSXObject.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);
89
                hSimConnect.AddToDataDefinition(FSXObject.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);
90
                hSimConnect.AddToDataDefinition(FSXObject.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);
91
                hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.AIRCRAFT, "Title", "", SIMCONNECT_DATATYPE.STRING128, 0.0f, SimConnect.SIMCONNECT_UNUSED);
134
                hSimConnect.RegisterDataDefineStruct<Aircraft_Data>(DEFINITIONS.AIRCRAFT);
92
                hSimConnect.RegisterDataDefineStruct<FSXObject.Aircraft_Data>(FSXObject.DEFINITIONS.AIRCRAFT);
135
 
93
 
136
                hSimConnect.AddToDataDefinition(DEFINITIONS.AVIONICS, "Avionics Master Switch", "bool", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
94
                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);
137
                hSimConnect.AddToDataDefinition(DEFINITIONS.AVIONICS, "Com Status", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
96
                hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.AVIONICS, "Com Status:2", "Enum", SIMCONNECT_DATATYPE.FLOAT64, 0, SimConnect.SIMCONNECT_UNUSED);
138
                hSimConnect.RegisterDataDefineStruct<Avionics_Data>(DEFINITIONS.AVIONICS);
97
                hSimConnect.RegisterDataDefineStruct<FSXObject.Avionics_Data>(FSXObject.DEFINITIONS.AVIONICS);
139
 
98
 
-
 
99
                // NavComm1 Frequencies
140
                hSimConnect.AddToDataDefinition(DEFINITIONS.NAVCOM1_DATA, "Com Active Frequency:1", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
100
                hSimConnect.AddToDataDefinition(FSXObject.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);
101
                hSimConnect.AddToDataDefinition(FSXObject.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);
102
                hSimConnect.AddToDataDefinition(FSXObject.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);
103
                hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.NAVCOM1_DATA, "Nav Standby Frequency:1", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
144
                hSimConnect.RegisterDataDefineStruct<NavCom_Data>(DEFINITIONS.NAVCOM1_DATA);
104
                hSimConnect.RegisterDataDefineStruct<FSXObject.NavCom_Data>(FSXObject.DEFINITIONS.NAVCOM1_DATA);
145
 
105
 
-
 
106
                // NavComm2 Frequencies
146
                hSimConnect.AddToDataDefinition(DEFINITIONS.NAVCOM2_DATA, "Com Active Frequency:2", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
107
                hSimConnect.AddToDataDefinition(FSXObject.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);
108
                hSimConnect.AddToDataDefinition(FSXObject.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);
109
                hSimConnect.AddToDataDefinition(FSXObject.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);
110
                hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.NAVCOM2_DATA, "Nav Standby Frequency:2", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
150
                hSimConnect.RegisterDataDefineStruct<NavCom_Data>(DEFINITIONS.NAVCOM2_DATA);
111
                hSimConnect.RegisterDataDefineStruct<FSXObject.NavCom_Data>(FSXObject.DEFINITIONS.NAVCOM2_DATA);
151
 
112
 
152
                hSimConnect.OnRecvOpen += new SimConnect.RecvOpenEventHandler(SimConnect_OnRecvOpen);
113
                hSimConnect.OnRecvOpen += new SimConnect.RecvOpenEventHandler(SimConnect_OnRecvOpen);
153
                hSimConnect.OnRecvQuit += new SimConnect.RecvQuitEventHandler(SimConnect_OnRecvQuit);
114
                hSimConnect.OnRecvQuit += new SimConnect.RecvQuitEventHandler(SimConnect_OnRecvQuit);
154
                hSimConnect.OnRecvException += new SimConnect.RecvExceptionEventHandler(SimConnect_OnRecvException);
115
                hSimConnect.OnRecvException += new SimConnect.RecvExceptionEventHandler(SimConnect_OnRecvException);
155
                hSimConnect.OnRecvSimobjectData += new SimConnect.RecvSimobjectDataEventHandler(SimConnect_OnRecvSimObjectData);
116
                hSimConnect.OnRecvSimobjectData += new SimConnect.RecvSimobjectDataEventHandler(SimConnect_OnRecvSimObjectData);
156
                hSimConnect.OnRecvSimobjectDataBytype += new SimConnect.RecvSimobjectDataBytypeEventHandler(SimConnect_OnRecvSimObjectDataByType);
117
                hSimConnect.OnRecvSimobjectDataBytype += new SimConnect.RecvSimobjectDataBytypeEventHandler(SimConnect_OnRecvSimObjectDataByType);
157
 
118
 
158
                // Request for aircrat data. This will also configure auto-updates for other required data.
119
                // 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);
120
                hSimConnect.RequestDataOnSimObjectType(FSXObject.DATA_REQUESTS.AIRCRAFT, FSXObject.DEFINITIONS.AIRCRAFT, 0, SIMCONNECT_SIMOBJECT_TYPE.USER);
160
 
121
 
161
            } catch (COMException e) {
122
            } catch (COMException e) {
162
                Log(e.Message);
123
                Log(e.Message);
163
            }
124
            }
164
        }
125
        }
Line 175... Line 136...
175
 
136
 
176
        private void SimConnect_OnRecvException(SimConnect sender, SIMCONNECT_RECV_EXCEPTION data) {
137
        private void SimConnect_OnRecvException(SimConnect sender, SIMCONNECT_RECV_EXCEPTION data) {
177
            Log("SimConnect Exception: " + (uint)(data.dwException));
138
            Log("SimConnect Exception: " + (uint)(data.dwException));
178
        }
139
        }
179
 
140
 
-
 
141
        // Automatic updates on various variables. Set through InitFsxReady
180
        private void SimConnect_OnRecvSimObjectData(SimConnect sender, SIMCONNECT_RECV_SIMOBJECT_DATA data) {
142
        private void SimConnect_OnRecvSimObjectData(SimConnect sender, SIMCONNECT_RECV_SIMOBJECT_DATA data) {
-
 
143
            
181
            switch (data.dwRequestID) {
144
            switch (data.dwRequestID) {
182
 
145
 
-
 
146
 
-
 
147
                case ((uint)FSXObject.DATA_REQUESTS.NAVCOM1_REQ): {
-
 
148
                        FSXObject.NavCom_Data navcomdata = (FSXObject.NavCom_Data)data.dwData[0];
183
                // Automatically provided updates on NavCom1
149
                        if (comm1 != null && comm1.isOpen())
-
 
150
                            comm1.setNavComFreqs(navcomdata);
-
 
151
                        break;
-
 
152
                }
-
 
153
 
184
                case ((uint)DATA_REQUESTS.NAVCOM1_REQ): {
154
                case ((uint)FSXObject.DATA_REQUESTS.NAVCOM2_REQ): {
185
                        NavCom_Data navcomdata = (NavCom_Data)data.dwData[0];
155
                        FSXObject.NavCom_Data navcomdata = (FSXObject.NavCom_Data)data.dwData[0];
186
                        NITNavCommDevice navcomm = (NITNavCommDevice)panels.devices[0];
156
                        if (comm2 != null && comm2.isOpen())
187
                        navcomm.setNavComFreqs(navcomdata);
157
                            comm2.setNavComFreqs(navcomdata);
188
                        break;
158
                        break;
189
                }
159
                }
-
 
160
 
-
 
161
                case ((uint)FSXObject.DATA_REQUESTS.AVIONICS): {
-
 
162
                    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;
-
 
212
                }
190
            }
213
            }
191
        }
214
        }
192
 
215
 
193
        private void SimConnect_OnRecvSimObjectDataByType(SimConnect sender, SIMCONNECT_RECV_SIMOBJECT_DATA_BYTYPE data) {
216
        private void SimConnect_OnRecvSimObjectDataByType(SimConnect sender, SIMCONNECT_RECV_SIMOBJECT_DATA_BYTYPE data) {
194
            switch (data.dwRequestID) {
217
            switch (data.dwRequestID) {
195
 
218
 
196
                // Request basic aircraft data to get the object ID
219
                // Request basic aircraft data to get the object ID
197
                case ((uint)DATA_REQUESTS.AIRCRAFT): {
220
                case ((uint)FSXObject.DATA_REQUESTS.AIRCRAFT): {
198
                        uint ObjectID = data.dwObjectID;
221
                        uint ObjectID = data.dwObjectID;
199
                        Aircraft_Data aircraftdata = (Aircraft_Data)data.dwData[0];
222
                        FSXObject.Aircraft_Data aircraftdata = (FSXObject.Aircraft_Data)data.dwData[0];
200
                        
-
 
-
 
223
 
201
                        this.simdata.objectid = ObjectID;
224
                        this.fsx.simdata.objectid = ObjectID;
202
                        this.simdata.aircraft = aircraftdata;
225
                        this.fsx.simdata.aircraft = aircraftdata;
203
 
226
 
204
                        this.InitFsxReady();                      
227
                        this.InitFsxReady();                      
205
 
228
 
206
                        break;
229
                        break;
207
                    }
230
                    }
Line 210... Line 233...
210
 
233
 
211
        private void InitFsxReady() {
234
        private void InitFsxReady() {
212
 
235
 
213
 
236
 
214
            Log("Aircraft: "
237
            Log("Aircraft: "
215
                + this.simdata.aircraft.atc_id + ", "
238
                + this.fsx.simdata.aircraft.atc_id + ", "
216
                + this.simdata.aircraft.atc_model + ", "
239
                + this.fsx.simdata.aircraft.atc_model + ", "
217
                + this.simdata.aircraft.atc_type + ", "
240
                + this.fsx.simdata.aircraft.atc_type + ", "
218
                + this.simdata.aircraft.title
241
                + this.fsx.simdata.aircraft.title
219
            );
242
            );
-
 
243
 
-
 
244
            hSimConnect.RequestDataOnSimObject(
-
 
245
                FSXObject.DATA_REQUESTS.NAVCOM1_REQ,
-
 
246
                FSXObject.DEFINITIONS.NAVCOM1_DATA,
-
 
247
                this.fsx.simdata.objectid,
-
 
248
                SIMCONNECT_PERIOD.SIM_FRAME,
-
 
249
                SIMCONNECT_DATA_REQUEST_FLAG.CHANGED,
-
 
250
                0, 0, 0);
-
 
251
 
220
            hSimConnect.RequestDataOnSimObject(
252
            hSimConnect.RequestDataOnSimObject(
221
                DATA_REQUESTS.NAVCOM1_REQ,
253
                FSXObject.DATA_REQUESTS.NAVCOM2_REQ,
222
                DEFINITIONS.NAVCOM1_DATA,
254
                FSXObject.DEFINITIONS.NAVCOM2_DATA,
-
 
255
                this.fsx.simdata.objectid,
-
 
256
                SIMCONNECT_PERIOD.SIM_FRAME,
-
 
257
                SIMCONNECT_DATA_REQUEST_FLAG.CHANGED,
-
 
258
                0, 0, 0);
-
 
259
 
-
 
260
            hSimConnect.RequestDataOnSimObject(
-
 
261
                FSXObject.DATA_REQUESTS.AVIONICS,
-
 
262
                FSXObject.DEFINITIONS.AVIONICS,
223
                this.simdata.objectid,
263
                this.fsx.simdata.objectid,
224
                SIMCONNECT_PERIOD.SIM_FRAME,
264
                SIMCONNECT_PERIOD.SIM_FRAME,
225
                SIMCONNECT_DATA_REQUEST_FLAG.CHANGED,
265
                SIMCONNECT_DATA_REQUEST_FLAG.CHANGED,
226
                0, 0, 0);
266
                0, 0, 0);
227
 
267
 
228
            inputTimer.Enabled = true;
268
            inputTimer.Enabled = true;
229
        }
269
        }
230
 
270
 
231
        private void InitFsxClosed() {
271
        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) {
-
 
280
                hSimConnect.Dispose();
-
 
281
                txtSimConnect.Text = "Available";
-
 
282
            } else {
-
 
283
                txtSimConnect.Text = "Failed";
232
            inputTimer.Enabled = false;
284
                hSimConnect = null;
-
 
285
            }
233
 
286
 
234
        }
287
        }
235
 
288
 
236
        private void Devices_Rescan() {
289
        private void Devices_Rescan() {
237
            Log("Scanning for devices...");
290
            Log("Scanning for devices...");
Line 242... Line 295...
242
            this.txtDevices.Text = panels.devices.Count.ToString();
295
            this.txtDevices.Text = panels.devices.Count.ToString();
243
 
296
 
244
            if (panels.devices.Count > 0) {
297
            if (panels.devices.Count > 0) {
245
                this.cmDevTest.Enabled = true;
298
                this.cmDevTest.Enabled = true;
246
                Log(panels.devices.Count.ToString() + " devices found during scan.");
299
                Log(panels.devices.Count.ToString() + " devices found during scan.");
-
 
300
 
-
 
301
                foreach (NITDevice device in panels.devices) {
-
 
302
                    if (device.type == "NITNavComm") {
-
 
303
                        if (comm1 == null) {
-
 
304
                            comm1 = (NITNavCommDevice)device;
-
 
305
                            comm1.assigned = 1;
-
 
306
                        } else if (comm2 == null) {
-
 
307
                            comm2 = (NITNavCommDevice)device;
-
 
308
                            comm2.assigned = 2;
-
 
309
                        }
-
 
310
                    } else if (device.type == "NITAudioSel") {
-
 
311
                        // @TODO: Add other devices
-
 
312
                    }
-
 
313
                }
-
 
314
 
247
            } else {
315
            } else {
248
                Log("No devices found, check connections and rescan.");
316
                Log("No devices found, check connections and rescan.");
249
            }
317
            }
250
 
318
 
251
            NITNavCommDevice navcomm = (NITNavCommDevice)panels.devices[0];
-
 
252
            navcomm.resetRotary(0, 0);
319
            
253
        }
320
        }
254
 
321
 
255
        private void cmdRescan_Click(object sender, EventArgs e) {
322
        private void cmdRescan_Click(object sender, EventArgs e) {
256
            this.Devices_Rescan();
323
            this.Devices_Rescan();
257
        }
324
        }
Line 259... Line 326...
259
        private void cmdDevRescan_Click(object sender, EventArgs e) {
326
        private void cmdDevRescan_Click(object sender, EventArgs e) {
260
            this.Devices_Rescan();
327
            this.Devices_Rescan();
261
        }
328
        }
262
 
329
 
263
        private void cmDevTest_Click(object sender, EventArgs e) {
330
        private void cmDevTest_Click(object sender, EventArgs e) {
264
            //NITDevice device = (NITDevice) this.deviceGrid.CurrentRow.DataBoundItem;
331
            NITDevice device = (NITDevice) this.deviceGrid.CurrentRow.DataBoundItem;
265
            NITNavCommDevice navcomm = (NITNavCommDevice)this.deviceGrid.CurrentRow.DataBoundItem;;
-
 
266
 
332
 
267
            if (!navcomm.Open()) {
333
            if (!device.Open()) {
268
                Log("Could not open device " + navcomm.type + "(" + navcomm.serial + ").");
334
                Log("Could not open device " + device.type + "(" + device.serial + ").");
269
                return;
335
                return;
270
            }
336
            }
271
            navcomm.Close();
337
            if (device.type == "NITNavComm") {
272
 
-
 
273
            NITCommNavForm form = new NITCommNavForm();
338
                NITCommNavForm form = new NITCommNavForm();
274
            form.setDevice(navcomm);
339
                form.setDevice((NITNavCommDevice)device);
275
            form.Show(this);
340
                form.Show(this);
-
 
341
            } else if (device.type == "NITAudioSel") {
-
 
342
                Log("Not implemented for NITAudioSel");
-
 
343
            } else {
-
 
344
                Log("No device type for " + device.type + " (" + device.serial + ").");
276
 
345
            }
277
        }
346
        }
278
 
347
 
279
        public void Log(string msg) {
348
        public void Log(string msg) {
280
            txtLog.AppendText(msg + "\r\n");
349
            txtLog.AppendText(msg + "\r\n");
281
        }
350
        }
Line 284... Line 353...
284
            StringBuilder sb = new StringBuilder();
353
            StringBuilder sb = new StringBuilder();
285
            foreach (byte data in buffer) {
354
            foreach (byte data in buffer) {
286
                sb.Append(data.ToString("X") + " ");
355
                sb.Append(data.ToString("X") + " ");
287
            }
356
            }
288
            Log(sb.ToString());
357
            Log(sb.ToString());
289
        } 
358
        }
-
 
359
 
-
 
360
        private void quitNITPanels() {        
-
 
361
            //this.InitFsxClosed();
-
 
362
 
-
 
363
            inputTimer.Enabled = false;
-
 
364
 
-
 
365
            if (comm1 != null && comm1.isOpen())
-
 
366
                comm1.Close();
-
 
367
 
-
 
368
            if (comm2 != null && comm2.isOpen())
-
 
369
                comm2.Close();
-
 
370
 
-
 
371
            Application.Exit();
-
 
372
        }
290
 
373
 
291
        private void quitToolStripMenuItem_Click(object sender, EventArgs e) {
374
        private void quitToolStripMenuItem_Click(object sender, EventArgs e) {
292
            if (QuestionBox("Exit NIT Panels?", "Exiting will disable NIT Panels. Continue?")) {
375
            if (QuestionBox("Exit NIT Panels?", "Exiting will disable NIT Panels. Continue?")) {
293
                Application.Exit();
376
                this.quitNITPanels();
294
            }
377
            }
295
        }
378
        }
296
 
379
 
297
        public bool QuestionBox(string caption, string message) {
380
        public bool QuestionBox(string caption, string message) {
298
            MessageBoxButtons buttons = MessageBoxButtons.YesNo;
381
            MessageBoxButtons buttons = MessageBoxButtons.YesNo;
Line 319... Line 402...
319
 
402
 
320
            this.SimConnect_Connect();
403
            this.SimConnect_Connect();
321
        }
404
        }
322
 
405
 
323
        private void cmdRequest_Click(object sender, EventArgs e) {
406
        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
                );
-
 
-
 
407
 
330
        }
408
        }
331
 
409
 
332
        private void inputTimer_Tick(object sender, EventArgs e) {
410
        private void inputTimer_Tick(object sender, EventArgs e) {
333
            NITNavCommDevice navcomm = (NITNavCommDevice)panels.devices[0];
411
            if (comm1 != null && comm1.isOpen()) {
334
            navcomm.updateInput();
412
                comm1.updateInput();
-
 
413
                comm1.simButtons(this.fsx);
-
 
414
            }
335
 
415
 
336
            if (navcomm.isSwapSet(0))
416
            if (comm2 != null && comm2.isOpen()) {
337
                hSimConnect.TransmitClientEvent(
-
 
338
                this.simdata.objectid,
417
                comm2.updateInput();
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,
418
                comm2.simButtons(this.fsx);
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
            }
419
            }
381
 
420
 
-
 
421
        }
382
 
422
 
-
 
423
        private void MainForm_FormClosing(object sender, FormClosingEventArgs e) {
-
 
424
            this.quitNITPanels();
383
        }
425
        }
384
 
426
 
385
    }
427
    }
386
}
428
}