Subversion Repositories group.electronics

Rev

Rev 155 | Go to most recent revision | 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 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);
        }

        // 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);
        }



    }
}