Subversion Repositories group.NITPanels

Rev

Rev 3 | Blame | Compare with Previous | Last modification | View Log | RSS feed

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Microsoft.FlightSimulator.SimConnect;
using System.Runtime.InteropServices;

namespace NITNavComm {
    public class FSXObject {

        public SimData simdata;
        public SimConnect hSimConnect = null;

        public enum DATA_REQUESTS {
            AIRCRAFT,
            AVIONICS,
            NAVCOM1_REQ,
            NAVCOM2_REQ,
            AUDIOSEL_REQ
        }

        public enum DEFINITIONS {
            AIRCRAFT,
            AVIONICS,
            NAVCOM1_DATA,
            NAVCOM2_DATA,
            AUDIOSEL_DATA
        }

        public enum EVENT_ID {
            // AudioSel Events, these go first to make mapping easy (0-7)
            COM1_TRANSMIT_SELECT,
            COM2_TRANSMIT_SELECT,
            COM_RECEIVE_ALL_TOGGLE,
            RADIO_VOR1_IDENT_TOGGLE,
            RADIO_VOR2_IDENT_TOGGLE,
            MARKER_SOUND_TOGGLE,
            RADIO_DME1_IDENT_TOGGLE,
            RADIO_ADF_IDENT_TOGGLE,

            // NavCom1 Events
            COM1_SWAP_FREQ,
            NAV1_SWAP_FREQ,
            COM_RADIO_WHOLE_INC,
            COM_RADIO_WHOLE_DEC,
            COM_RADIO_FRACT_INC,
            COM_RADIO_FRACT_DEC,
            NAV1_RADIO_WHOLE_INC,
            NAV1_RADIO_WHOLE_DEC,
            NAV1_RADIO_FRACT_INC,
            NAV1_RADIO_FRACT_DEC,

            // NavComm2 Events
            COM2_SWAP_FREQ,
            NAV2_SWAP_FREQ,
            COM2_RADIO_WHOLE_INC,
            COM2_RADIO_WHOLE_DEC,
            COM2_RADIO_FRACT_INC,
            COM2_RADIO_FRACT_DEC,
            NAV2_RADIO_WHOLE_INC,
            NAV2_RADIO_WHOLE_DEC,
            NAV2_RADIO_FRACT_INC,
            NAV2_RADIO_FRACT_DEC,
        }

        public enum GROUP_ID {
            NAVCOM1,
            NAVCOM2,
            AUDIOSEL
        }

        public enum MENU_ID {
            NIT_PANELS,
        }

        [StructLayout(LayoutKind.Sequential, Pack = 1)]
        public struct SimData {
            public uint objectid;
            public Aircraft_Data aircraft;
        }

        [StructLayout(LayoutKind.Sequential, Pack = 1)]
        public struct NavCom_Data {
            public double Freq0;
            public double Freq1;
            public double Freq2;
            public double Freq3;
        }

        [StructLayout(LayoutKind.Sequential, Pack = 1)]
        public struct AudioSel_Data {
            public double com1;
            public double com2;
            public double comb;
            public double vor1;
            public double vor2;
            public double mkr;
            public double dme;
            public double adf;
        }

        [StructLayout(LayoutKind.Sequential, Pack = 1)]
        public struct Avionics_Data {
            public bool avionics_master;
            public double com1_status;
            public double com2_status;
            public bool nav1_available;
            public bool nav2_available;
        }

        [StructLayout(LayoutKind.Sequential, Pack = 1)]
        public struct Aircraft_Data {
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
            public string atc_type;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
            public string atc_model;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
            public string atc_id;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
            public string title;
        }

        public FSXObject() {
            this.simdata = new SimData();
        }

        private void transmitEvent(Enum eventid, Enum groupid) {
            this.transmitEvent(eventid, groupid, 1);
        }

        private void transmitEvent(Enum eventid, Enum groupid, uint data) {
            this.hSimConnect.TransmitClientEvent(
                this.simdata.objectid,
                eventid, data, groupid,
                SIMCONNECT_EVENT_FLAG.GROUPID_IS_PRIORITY
            );
        }

        private void requestData(Enum dataRequest, Enum definition) {
            hSimConnect.RequestDataOnSimObject(
                dataRequest,
                definition,
                this.simdata.objectid,
                SIMCONNECT_PERIOD.ONCE,
                SIMCONNECT_DATA_REQUEST_FLAG.DEFAULT,
                0, 0, 0);
        }

        public void FsxInit() {
            this.hSimConnect.MenuAddItem("NITPanels", FSXObject.MENU_ID.NIT_PANELS, 0);

        }

        public void FsxReady() {
            
            this.hSimConnect.RequestDataOnSimObject(
                FSXObject.DATA_REQUESTS.AUDIOSEL_REQ,
                FSXObject.DEFINITIONS.AUDIOSEL_DATA,
                this.simdata.objectid,
                SIMCONNECT_PERIOD.SIM_FRAME,
                SIMCONNECT_DATA_REQUEST_FLAG.CHANGED,
                0, 0, 0);

            this.hSimConnect.RequestDataOnSimObject(
                FSXObject.DATA_REQUESTS.NAVCOM1_REQ,
                FSXObject.DEFINITIONS.NAVCOM1_DATA,
                this.simdata.objectid,
                SIMCONNECT_PERIOD.SIM_FRAME,
                SIMCONNECT_DATA_REQUEST_FLAG.CHANGED,
                0, 0, 0);

            this.hSimConnect.RequestDataOnSimObject(
                FSXObject.DATA_REQUESTS.NAVCOM2_REQ,
                FSXObject.DEFINITIONS.NAVCOM2_DATA,
                this.simdata.objectid,
                SIMCONNECT_PERIOD.SIM_FRAME,
                SIMCONNECT_DATA_REQUEST_FLAG.CHANGED,
                0, 0, 0);

            hSimConnect.RequestDataOnSimObject(
                FSXObject.DATA_REQUESTS.AVIONICS,
                FSXObject.DEFINITIONS.AVIONICS,
                this.simdata.objectid,
                SIMCONNECT_PERIOD.SIM_FRAME,
                SIMCONNECT_DATA_REQUEST_FLAG.CHANGED,
                0, 0, 0);
             
        }

        public void MapEvents() {
            
            this.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.COM1_SWAP_FREQ, "COM_STBY_RADIO_SWAP");
            this.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.NAV1_SWAP_FREQ, "NAV1_RADIO_SWAP");
            this.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.COM_RADIO_WHOLE_INC, "COM_RADIO_WHOLE_INC");
            this.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.COM_RADIO_WHOLE_DEC, "COM_RADIO_WHOLE_DEC");
            this.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.COM_RADIO_FRACT_INC, "COM_RADIO_FRACT_INC");
            this.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.COM_RADIO_FRACT_DEC, "COM_RADIO_FRACT_DEC");
            this.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.NAV1_RADIO_WHOLE_INC, "NAV1_RADIO_WHOLE_INC");
            this.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.NAV1_RADIO_WHOLE_DEC, "NAV1_RADIO_WHOLE_DEC");
            this.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.NAV1_RADIO_FRACT_INC, "NAV1_RADIO_FRACT_INC");
            this.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.NAV1_RADIO_FRACT_DEC, "NAV1_RADIO_FRACT_DEC");

            this.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.COM2_SWAP_FREQ, "COM2_RADIO_SWAP");
            this.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.NAV2_SWAP_FREQ, "NAV2_RADIO_SWAP");
            this.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.COM2_RADIO_WHOLE_INC, "COM2_RADIO_WHOLE_INC");
            this.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.COM2_RADIO_WHOLE_DEC, "COM2_RADIO_WHOLE_DEC");
            this.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.COM2_RADIO_FRACT_INC, "COM2_RADIO_FRACT_INC");
            this.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.COM2_RADIO_FRACT_DEC, "COM2_RADIO_FRACT_DEC");
            this.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.NAV2_RADIO_WHOLE_INC, "NAV2_RADIO_WHOLE_INC");
            this.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.NAV2_RADIO_WHOLE_DEC, "NAV2_RADIO_WHOLE_DEC");
            this.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.NAV2_RADIO_FRACT_INC, "NAV2_RADIO_FRACT_INC");
            this.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.NAV2_RADIO_FRACT_DEC, "NAV2_RADIO_FRACT_DEC");

            this.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.COM1_TRANSMIT_SELECT, "COM1_TRANSMIT_SELECT");
            this.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.COM2_TRANSMIT_SELECT, "COM2_TRANSMIT_SELECT");
            this.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.COM_RECEIVE_ALL_TOGGLE, "COM_RECEIVE_ALL_TOGGLE");
            this.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.RADIO_VOR1_IDENT_TOGGLE, "RADIO_VOR1_IDENT_TOGGLE");
            this.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.RADIO_VOR2_IDENT_TOGGLE, "RADIO_VOR2_IDENT_TOGGLE");
            this.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.MARKER_SOUND_TOGGLE, "MARKER_SOUND_TOGGLE");
            this.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.RADIO_DME1_IDENT_TOGGLE, "RADIO_DME1_IDENT_TOGGLE");
            this.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.RADIO_ADF_IDENT_TOGGLE, "RADIO_ADF_IDENT_TOGGLE");
            
            this.hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.AUDIOSEL_DATA, "COM TRANSMIT:1", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
            this.hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.AUDIOSEL_DATA, "COM TRANSMIT:2", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
            this.hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.AUDIOSEL_DATA, "COM RECIEVE ALL", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
            this.hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.AUDIOSEL_DATA, "NAV SOUND:1", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
            this.hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.AUDIOSEL_DATA, "NAV SOUND:2", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
            this.hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.AUDIOSEL_DATA, "MARKER SOUND", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
            this.hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.AUDIOSEL_DATA, "DME SOUND", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
            this.hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.AUDIOSEL_DATA, "ADF SOUND:1", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
            this.hSimConnect.RegisterDataDefineStruct<FSXObject.AudioSel_Data>(FSXObject.DEFINITIONS.AUDIOSEL_DATA);

            this.hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.AIRCRAFT, "Atc Type", "", SIMCONNECT_DATATYPE.STRING32, 0.0f, SimConnect.SIMCONNECT_UNUSED);
            this.hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.AIRCRAFT, "ATC Model", "", SIMCONNECT_DATATYPE.STRING32, 0.0f, SimConnect.SIMCONNECT_UNUSED);
            this.hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.AIRCRAFT, "ATC ID", "", SIMCONNECT_DATATYPE.STRING32, 0.0f, SimConnect.SIMCONNECT_UNUSED);
            this.hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.AIRCRAFT, "Title", "", SIMCONNECT_DATATYPE.STRING128, 0.0f, SimConnect.SIMCONNECT_UNUSED);
            this.hSimConnect.RegisterDataDefineStruct<FSXObject.Aircraft_Data>(FSXObject.DEFINITIONS.AIRCRAFT);

            this.hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.AVIONICS, "Avionics Master Switch", "bool", SIMCONNECT_DATATYPE.INT32, 0, SimConnect.SIMCONNECT_UNUSED);
            this.hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.AVIONICS, "Com Status:1", "Enum", SIMCONNECT_DATATYPE.FLOAT64, 0, SimConnect.SIMCONNECT_UNUSED);
            this.hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.AVIONICS, "Com Status:2", "Enum", SIMCONNECT_DATATYPE.FLOAT64, 0, SimConnect.SIMCONNECT_UNUSED);
            this.hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.AVIONICS, "Nav Available:1", "bool", SIMCONNECT_DATATYPE.INT32, 0, SimConnect.SIMCONNECT_UNUSED);
            this.hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.AVIONICS, "Nav Available:2", "bool", SIMCONNECT_DATATYPE.INT32, 0, SimConnect.SIMCONNECT_UNUSED);
            this.hSimConnect.RegisterDataDefineStruct<FSXObject.Avionics_Data>(FSXObject.DEFINITIONS.AVIONICS);

            
            // NavComm1 Frequencies
            this.hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.NAVCOM1_DATA, "Com Active Frequency:1", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
            this.hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.NAVCOM1_DATA, "Com Standby Frequency:1", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
            this.hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.NAVCOM1_DATA, "Nav Active Frequency:1", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
            this.hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.NAVCOM1_DATA, "Nav Standby Frequency:1", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
            this.hSimConnect.RegisterDataDefineStruct<FSXObject.NavCom_Data>(FSXObject.DEFINITIONS.NAVCOM1_DATA);

            // NavComm2 Frequencies
            this.hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.NAVCOM2_DATA, "Com Active Frequency:2", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
            this.hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.NAVCOM2_DATA, "Com Standby Frequency:2", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
            this.hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.NAVCOM2_DATA, "Nav Active Frequency:2", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
            this.hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.NAVCOM2_DATA, "Nav Standby Frequency:2", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
            this.hSimConnect.RegisterDataDefineStruct<FSXObject.NavCom_Data>(FSXObject.DEFINITIONS.NAVCOM2_DATA);
            
        }

        // Individual data requests
        public void requestNavComm1Data() {
            this.requestData(FSXObject.DATA_REQUESTS.NAVCOM1_REQ, FSXObject.DEFINITIONS.NAVCOM1_DATA);
        }

        public void requestNavComm2Data() {
            this.requestData(FSXObject.DATA_REQUESTS.NAVCOM2_REQ, FSXObject.DEFINITIONS.NAVCOM2_DATA);
        }

        public void requestAudioSelData() {
            this.requestData(FSXObject.DATA_REQUESTS.AUDIOSEL_REQ, FSXObject.DEFINITIONS.AUDIOSEL_DATA);
        }

        // AudioSel Events
        public void AudioSelButton(int button) {
            this.transmitEvent((FSXObject.EVENT_ID)button, FSXObject.GROUP_ID.AUDIOSEL);
        }


        // NavComm1 Events
        public void Comm1DecFract() {
            this.transmitEvent(FSXObject.EVENT_ID.COM_RADIO_FRACT_DEC, FSXObject.GROUP_ID.NAVCOM1);
        }

        public void Comm1DecWhole() {
            this.transmitEvent(FSXObject.EVENT_ID.COM_RADIO_WHOLE_DEC, FSXObject.GROUP_ID.NAVCOM1);
        }

        public void Comm1IncFract() {
            this.transmitEvent(FSXObject.EVENT_ID.COM_RADIO_FRACT_INC, FSXObject.GROUP_ID.NAVCOM1);
        }

        public void Comm1IncWhole() {
            this.transmitEvent(FSXObject.EVENT_ID.COM_RADIO_WHOLE_INC, FSXObject.GROUP_ID.NAVCOM1);
        }

        public void Comm1SwapFreq() {
            this.transmitEvent(FSXObject.EVENT_ID.COM1_SWAP_FREQ, FSXObject.GROUP_ID.NAVCOM1);
        }

        public void Nav1DecFract() {
            this.transmitEvent(FSXObject.EVENT_ID.NAV1_RADIO_FRACT_DEC, FSXObject.GROUP_ID.NAVCOM1);
        }

        public void Nav1DecWhole() {
            this.transmitEvent(FSXObject.EVENT_ID.NAV1_RADIO_WHOLE_DEC, FSXObject.GROUP_ID.NAVCOM1);
        }

        public void Nav1IncFract() {
            this.transmitEvent(FSXObject.EVENT_ID.NAV1_RADIO_FRACT_INC, FSXObject.GROUP_ID.NAVCOM1);
        }

        public void Nav1IncWhole() {
            this.transmitEvent(FSXObject.EVENT_ID.NAV1_RADIO_WHOLE_INC, FSXObject.GROUP_ID.NAVCOM1);
        }

        public void Nav1SwapFreq() {
            this.transmitEvent(FSXObject.EVENT_ID.NAV1_SWAP_FREQ, FSXObject.GROUP_ID.NAVCOM1);
        }


        // NavComm2 Events
        public void Comm2DecFract() {
            this.transmitEvent(FSXObject.EVENT_ID.COM2_RADIO_FRACT_DEC, FSXObject.GROUP_ID.NAVCOM2);
        }

        public void Comm2DecWhole() {
            this.transmitEvent(FSXObject.EVENT_ID.COM2_RADIO_WHOLE_DEC, FSXObject.GROUP_ID.NAVCOM2);
        }

        public void Comm2IncFract() {
            this.transmitEvent(FSXObject.EVENT_ID.COM2_RADIO_FRACT_INC, FSXObject.GROUP_ID.NAVCOM2);
        }

        public void Comm2IncWhole() {
            this.transmitEvent(FSXObject.EVENT_ID.COM2_RADIO_WHOLE_INC, FSXObject.GROUP_ID.NAVCOM2);
        }

        public void Comm2SwapFreq() {
            this.transmitEvent(FSXObject.EVENT_ID.COM2_SWAP_FREQ, FSXObject.GROUP_ID.NAVCOM2);
        }

        public void Nav2DecFract() {
            this.transmitEvent(FSXObject.EVENT_ID.NAV2_RADIO_FRACT_DEC, FSXObject.GROUP_ID.NAVCOM2);
        }

        public void Nav2DecWhole() {
            this.transmitEvent(FSXObject.EVENT_ID.NAV2_RADIO_WHOLE_DEC, FSXObject.GROUP_ID.NAVCOM2);
        }

        public void Nav2IncFract() {
            this.transmitEvent(FSXObject.EVENT_ID.NAV2_RADIO_FRACT_INC, FSXObject.GROUP_ID.NAVCOM2);
        }

        public void Nav2IncWhole() {
            this.transmitEvent(FSXObject.EVENT_ID.NAV2_RADIO_WHOLE_INC, FSXObject.GROUP_ID.NAVCOM2);
        }

        public void Nav2SwapFreq() {
            this.transmitEvent(FSXObject.EVENT_ID.NAV2_SWAP_FREQ, FSXObject.GROUP_ID.NAVCOM2);
        }



    }
}