Subversion Repositories group.electronics

Rev

Rev 153 | Rev 156 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

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

namespace NITNavComm
{
    public partial class MainForm : Form
    {

        private NITPanels panels = new NITPanels();
        private SimConnect hSimConnect = null;

        private FSXObject fsx = new FSXObject();

        private NITNavCommDevice comm1;
        private NITNavCommDevice comm2;

        public MainForm() {
            InitializeComponent();

            Devices_Rescan();
            txtStatus.Text = "Running";

        }


        private void SimConnect_Connect() {
            if (hSimConnect == null) {
                try {
                    hSimConnect = new SimConnect("Managed Data Request", base.Handle, 0x402, null, 0);
                    fsx.hSimConnect = this.hSimConnect;
                    txtSimConnect.Text = "Available";
                    this.SimConnect_InitDataRequest();
                } catch (COMException) {
                    Log("Could not connect to FSX");
                    txtSimConnect.Text = "Failed";
                }
            }
        }

        private void SimConnect_Disconnect() {
            this.InitFsxClosed();
        }

        protected override void DefWndProc(ref Message m) {
            if (m.Msg == 0x402) {
                if (hSimConnect != null) {
                    hSimConnect.ReceiveMessage();
                }
            } else {
                base.DefWndProc(ref m);
            }
        }

        private void SimConnect_InitDataRequest() {
            try {

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

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

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

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

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

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

                hSimConnect.OnRecvOpen += new SimConnect.RecvOpenEventHandler(SimConnect_OnRecvOpen);
                hSimConnect.OnRecvQuit += new SimConnect.RecvQuitEventHandler(SimConnect_OnRecvQuit);
                hSimConnect.OnRecvException += new SimConnect.RecvExceptionEventHandler(SimConnect_OnRecvException);
                hSimConnect.OnRecvSimobjectData += new SimConnect.RecvSimobjectDataEventHandler(SimConnect_OnRecvSimObjectData);
                hSimConnect.OnRecvSimobjectDataBytype += new SimConnect.RecvSimobjectDataBytypeEventHandler(SimConnect_OnRecvSimObjectDataByType);

                // Request for aircrat data. This will also configure auto-updates for other required data.
                hSimConnect.RequestDataOnSimObjectType(FSXObject.DATA_REQUESTS.AIRCRAFT, FSXObject.DEFINITIONS.AIRCRAFT, 0, SIMCONNECT_SIMOBJECT_TYPE.USER);

            } catch (COMException e) {
                Log(e.Message);
            }
        }

        private void SimConnect_OnRecvOpen(SimConnect sender, SIMCONNECT_RECV_OPEN data) {
            Log("Connected to FSX.");
            txtSimConnect.Text = "Connected";
        }

        private void SimConnect_OnRecvQuit(SimConnect sender, SIMCONNECT_RECV data) {
            Log("FSX has exited.");
            this.SimConnect_Disconnect();
        }

        private void SimConnect_OnRecvException(SimConnect sender, SIMCONNECT_RECV_EXCEPTION data) {
            Log("SimConnect Exception: " + (uint)(data.dwException));
        }

        // Automatic updates on various variables. Set through InitFsxReady
        private void SimConnect_OnRecvSimObjectData(SimConnect sender, SIMCONNECT_RECV_SIMOBJECT_DATA data) {
            
            switch (data.dwRequestID) {


                case ((uint)FSXObject.DATA_REQUESTS.NAVCOM1_REQ): {
                        FSXObject.NavCom_Data navcomdata = (FSXObject.NavCom_Data)data.dwData[0];
                        if (comm1 != null && comm1.isOpen())
                            comm1.setNavComFreqs(navcomdata);
                        break;
                }

                case ((uint)FSXObject.DATA_REQUESTS.NAVCOM2_REQ): {
                        FSXObject.NavCom_Data navcomdata = (FSXObject.NavCom_Data)data.dwData[0];
                        if (comm2 != null && comm2.isOpen())
                            comm2.setNavComFreqs(navcomdata);
                        break;
                }

                case ((uint)FSXObject.DATA_REQUESTS.AVIONICS): {
                    FSXObject.Avionics_Data avionics = (FSXObject.Avionics_Data)data.dwData[0];
                        /*
                        Log("Avionics: "
                            + avionics.avionics_master + ", "
                            + "Com1: " + avionics.com1_status + ", "
                            + "Com2: " + avionics.com2_status + ", "
                        );
                        */
                         
                        if (!avionics.avionics_master || avionics.com1_status != 0) {       // If avionics master off or com1 in shutdown
                            if (comm1 != null && comm1.isOpen() && comm1.simStatus == 0)    // and the device is connected
                                comm1.powerDown();                                          // Power down the device
                        }

                        if (comm1 != null && comm1.isOpen()         // If comm1 device is active
                                && comm1.simStatus != 0             // and connected to fsx
                                && avionics.avionics_master         // and avionics master is on
                                && avionics.com1_status == 0) {     // and comm1 functioning
                            comm1.powerUp();                        // Power it up

                            hSimConnect.RequestDataOnSimObject(
                                FSXObject.DATA_REQUESTS.NAVCOM1_REQ,
                                FSXObject.DEFINITIONS.NAVCOM1_DATA,
                                this.fsx.simdata.objectid,
                                SIMCONNECT_PERIOD.ONCE,
                                SIMCONNECT_DATA_REQUEST_FLAG.DEFAULT,
                                0, 0, 0);
                        }

                        if (!avionics.avionics_master || avionics.com2_status != 0) {       // If avionics master off or com1 in shutdown
                            if (comm2 != null && comm2.isOpen() && comm2.simStatus == 0)    // and the device is connected
                                comm2.powerDown();                                          // Power down the device
                        }

                        if (comm2 != null && comm2.isOpen()         // If comm1 device is active
                                && comm2.simStatus != 0             // and connected to fsx
                                && avionics.avionics_master         // and avionics master is on
                                && avionics.com2_status == 0) {     // and comm1 functioning
                            comm2.powerUp();                        // Power it up

                            hSimConnect.RequestDataOnSimObject(
                                FSXObject.DATA_REQUESTS.NAVCOM2_REQ,
                                FSXObject.DEFINITIONS.NAVCOM2_DATA,
                                this.fsx.simdata.objectid,
                                SIMCONNECT_PERIOD.ONCE,
                                SIMCONNECT_DATA_REQUEST_FLAG.DEFAULT,
                                0, 0, 0);
                        }

                    break;
                }
            }
        }

        private void SimConnect_OnRecvSimObjectDataByType(SimConnect sender, SIMCONNECT_RECV_SIMOBJECT_DATA_BYTYPE data) {
            switch (data.dwRequestID) {

                // Request basic aircraft data to get the object ID
                case ((uint)FSXObject.DATA_REQUESTS.AIRCRAFT): {
                        uint ObjectID = data.dwObjectID;
                        FSXObject.Aircraft_Data aircraftdata = (FSXObject.Aircraft_Data)data.dwData[0];

                        this.fsx.simdata.objectid = ObjectID;
                        this.fsx.simdata.aircraft = aircraftdata;

                        this.InitFsxReady();                      

                        break;
                    }
            }           
        }

        private void InitFsxReady() {


            Log("Aircraft: "
                + this.fsx.simdata.aircraft.atc_id + ", "
                + this.fsx.simdata.aircraft.atc_model + ", "
                + this.fsx.simdata.aircraft.atc_type + ", "
                + this.fsx.simdata.aircraft.title
            );

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

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

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

            inputTimer.Enabled = true;
        }

        private void InitFsxClosed() {

            if (comm1 != null && comm1.isOpen())
                comm1.blankDisplay();

            if (comm2 != null && comm2.isOpen())
                comm2.blankDisplay();

            if (hSimConnect != null) {
                hSimConnect.Dispose();
                txtSimConnect.Text = "Available";
            } else {
                txtSimConnect.Text = "Failed";
                hSimConnect = null;
            }

        }

        private void Devices_Rescan() {
            Log("Scanning for devices...");

            panels.UsbScan();

            this.deviceGrid.DataSource = panels.devices;
            this.txtDevices.Text = panels.devices.Count.ToString();

            if (panels.devices.Count > 0) {
                this.cmDevTest.Enabled = true;
                Log(panels.devices.Count.ToString() + " devices found during scan.");

                foreach (NITDevice device in panels.devices) {
                    if (device.type == "NITNavComm") {
                        if (comm1 == null) {
                            comm1 = (NITNavCommDevice)device;
                            comm1.assigned = 1;
                        } else if (comm2 == null) {
                            comm2 = (NITNavCommDevice)device;
                            comm2.assigned = 2;
                        }
                    } else if (device.type == "NITAudioSel") {
                        // @TODO: Add other devices
                    }
                }

            } else {
                Log("No devices found, check connections and rescan.");
            }

            
        }

        private void cmdRescan_Click(object sender, EventArgs e) {
            this.Devices_Rescan();
        }

        private void cmdDevRescan_Click(object sender, EventArgs e) {
            this.Devices_Rescan();
        }

        private void cmDevTest_Click(object sender, EventArgs e) {
            NITDevice device = (NITDevice) this.deviceGrid.CurrentRow.DataBoundItem;

            if (!device.Open()) {
                Log("Could not open device " + device.type + "(" + device.serial + ").");
                return;
            }
            if (device.type == "NITNavComm") {
                NITCommNavForm form = new NITCommNavForm();
                form.setDevice((NITNavCommDevice)device);
                form.Show(this);
            } else if (device.type == "NITAudioSel") {
                Log("Not implemented for NITAudioSel");
            } else {
                Log("No device type for " + device.type + " (" + device.serial + ").");
            }
        }

        public void Log(string msg) {
            txtLog.AppendText(msg + "\r\n");
        }

        public void Log(byte[] buffer) {
            StringBuilder sb = new StringBuilder();
            foreach (byte data in buffer) {
                sb.Append(data.ToString("X") + " ");
            }
            Log(sb.ToString());
        }

        private void quitNITPanels() {        
            //this.InitFsxClosed();

            inputTimer.Enabled = false;

            if (comm1 != null && comm1.isOpen())
                comm1.Close();

            if (comm2 != null && comm2.isOpen())
                comm2.Close();

            Application.Exit();
        }

        private void quitToolStripMenuItem_Click(object sender, EventArgs e) {
            if (QuestionBox("Exit NIT Panels?", "Exiting will disable NIT Panels. Continue?")) {
                this.quitNITPanels();
            }
        }

        public bool QuestionBox(string caption, string message) {
            MessageBoxButtons buttons = MessageBoxButtons.YesNo;
            DialogResult result;

            result = MessageBox.Show(message, caption, buttons);

            if (result == System.Windows.Forms.DialogResult.Yes) {
                return true;
            }
            return false;
        }

        private void txtLog_VisibleChanged(object sender, EventArgs e) {
            if (txtLog.Visible) {
                txtLog.SelectionStart = txtLog.TextLength;
                txtLog.ScrollToCaret();
            }
        }

        private void cmdSimConnect_Click(object sender, EventArgs e) {
            if (hSimConnect != null)
                this.SimConnect_Disconnect();

            this.SimConnect_Connect();
        }

        private void cmdRequest_Click(object sender, EventArgs e) {

        }

        private void inputTimer_Tick(object sender, EventArgs e) {
            if (comm1 != null && comm1.isOpen()) {
                comm1.updateInput();
                comm1.simButtons(this.fsx);
            }

            if (comm2 != null && comm2.isOpen()) {
                comm2.updateInput();
                comm2.simButtons(this.fsx);
            }

        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e) {
            this.quitNITPanels();
        }

    }
}