Rev 4 | 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
{
public NITPanels panels = new NITPanels();
public FSXObject fsx = new FSXObject();
public MainForm() {
InitializeComponent();
Devices_Rescan();
txtStatus.Text = "Running";
}
private void SimConnect_Connect() {
if (fsx.hSimConnect == null) {
try {
fsx.hSimConnect = new SimConnect("Managed Data Request", base.Handle, 0x402, null, 0);
txtSimConnect.Text = "Available";
fsx.FsxInit();
// Make our devices aware of the fsx object
// Let the device to any Fsx initilization
foreach (NITDevice device in panels.devices) {
if (device.isOpen()) {
device.fsx = this.fsx;
}
}
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 (fsx.hSimConnect != null) {
fsx.hSimConnect.ReceiveMessage();
}
} else {
base.DefWndProc(ref m);
}
}
private void SimConnect_InitDataRequest() {
try {
foreach (NITDevice device in panels.devices) {
if (device.isOpen())
device.MapEvents();
}
fsx.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.COM1_SWAP_FREQ, "COM_STBY_RADIO_SWAP");
fsx.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.NAV1_SWAP_FREQ, "NAV1_RADIO_SWAP");
fsx.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.COM_RADIO_WHOLE_INC, "COM_RADIO_WHOLE_INC");
fsx.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.COM_RADIO_WHOLE_DEC, "COM_RADIO_WHOLE_DEC");
fsx.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.COM_RADIO_FRACT_INC, "COM_RADIO_FRACT_INC");
fsx.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.COM_RADIO_FRACT_DEC, "COM_RADIO_FRACT_DEC");
fsx.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.NAV1_RADIO_WHOLE_INC, "NAV1_RADIO_WHOLE_INC");
fsx.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.NAV1_RADIO_WHOLE_DEC, "NAV1_RADIO_WHOLE_DEC");
fsx.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.NAV1_RADIO_FRACT_INC, "NAV1_RADIO_FRACT_INC");
fsx.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.NAV1_RADIO_FRACT_DEC, "NAV1_RADIO_FRACT_DEC");
fsx.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.COM2_SWAP_FREQ, "COM2_RADIO_SWAP");
fsx.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.NAV2_SWAP_FREQ, "NAV2_RADIO_SWAP");
fsx.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.COM2_RADIO_WHOLE_INC, "COM2_RADIO_WHOLE_INC");
fsx.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.COM2_RADIO_WHOLE_DEC, "COM2_RADIO_WHOLE_DEC");
fsx.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.COM2_RADIO_FRACT_INC, "COM2_RADIO_FRACT_INC");
fsx.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.COM2_RADIO_FRACT_DEC, "COM2_RADIO_FRACT_DEC");
fsx.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.NAV2_RADIO_WHOLE_INC, "NAV2_RADIO_WHOLE_INC");
fsx.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.NAV2_RADIO_WHOLE_DEC, "NAV2_RADIO_WHOLE_DEC");
fsx.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.NAV2_RADIO_FRACT_INC, "NAV2_RADIO_FRACT_INC");
fsx.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.NAV2_RADIO_FRACT_DEC, "NAV2_RADIO_FRACT_DEC");
fsx.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.COM1_TRANSMIT_SELECT, "COM1_TRANSMIT_SELECT");
fsx.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.COM2_TRANSMIT_SELECT, "COM2_TRANSMIT_SELECT");
fsx.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.COM_RECEIVE_ALL_TOGGLE, "COM_RECEIVE_ALL_TOGGLE");
fsx.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.RADIO_VOR1_IDENT_TOGGLE, "RADIO_VOR1_IDENT_TOGGLE");
fsx.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.RADIO_VOR2_IDENT_TOGGLE, "RADIO_VOR2_IDENT_TOGGLE");
fsx.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.MARKER_SOUND_TOGGLE, "MARKER_SOUND_TOGGLE");
fsx.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.RADIO_DME1_IDENT_TOGGLE, "RADIO_DME1_IDENT_TOGGLE");
fsx.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.RADIO_ADF_IDENT_TOGGLE, "RADIO_ADF_IDENT_TOGGLE");
fsx.hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.AUDIOSEL_DATA, "COM TRANSMIT:1", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
fsx.hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.AUDIOSEL_DATA, "COM TRANSMIT:2", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
fsx.hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.AUDIOSEL_DATA, "COM RECIEVE ALL", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
fsx.hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.AUDIOSEL_DATA, "NAV SOUND:1", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
fsx.hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.AUDIOSEL_DATA, "NAV SOUND:2", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
fsx.hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.AUDIOSEL_DATA, "MARKER SOUND", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
fsx.hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.AUDIOSEL_DATA, "DME SOUND", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
fsx.hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.AUDIOSEL_DATA, "ADF SOUND:1", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
fsx.hSimConnect.RegisterDataDefineStruct<FSXObject.AudioSel_Data>(FSXObject.DEFINITIONS.AUDIOSEL_DATA);
fsx.hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.AIRCRAFT, "Atc Type", "", SIMCONNECT_DATATYPE.STRING32, 0.0f, SimConnect.SIMCONNECT_UNUSED);
fsx.hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.AIRCRAFT, "ATC Model", "", SIMCONNECT_DATATYPE.STRING32, 0.0f, SimConnect.SIMCONNECT_UNUSED);
fsx.hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.AIRCRAFT, "ATC ID", "", SIMCONNECT_DATATYPE.STRING32, 0.0f, SimConnect.SIMCONNECT_UNUSED);
fsx.hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.AIRCRAFT, "Title", "", SIMCONNECT_DATATYPE.STRING128, 0.0f, SimConnect.SIMCONNECT_UNUSED);
fsx.hSimConnect.RegisterDataDefineStruct<FSXObject.Aircraft_Data>(FSXObject.DEFINITIONS.AIRCRAFT);
fsx.hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.AVIONICS, "Avionics Master Switch", "bool", SIMCONNECT_DATATYPE.INT32, 0, SimConnect.SIMCONNECT_UNUSED);
fsx.hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.AVIONICS, "Com Status:1", "Enum", SIMCONNECT_DATATYPE.FLOAT64, 0, SimConnect.SIMCONNECT_UNUSED);
fsx.hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.AVIONICS, "Com Status:2", "Enum", SIMCONNECT_DATATYPE.FLOAT64, 0, SimConnect.SIMCONNECT_UNUSED);
fsx.hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.AVIONICS, "Nav Available:1", "bool", SIMCONNECT_DATATYPE.INT32, 0, SimConnect.SIMCONNECT_UNUSED);
fsx.hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.AVIONICS, "Nav Available:2", "bool", SIMCONNECT_DATATYPE.INT32, 0, SimConnect.SIMCONNECT_UNUSED);
fsx.hSimConnect.RegisterDataDefineStruct<FSXObject.Avionics_Data>(FSXObject.DEFINITIONS.AVIONICS);
// NavComm1 Frequencies
fsx.hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.NAVCOM1_DATA, "Com Active Frequency:1", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
fsx.hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.NAVCOM1_DATA, "Com Standby Frequency:1", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
fsx.hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.NAVCOM1_DATA, "Nav Active Frequency:1", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
fsx.hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.NAVCOM1_DATA, "Nav Standby Frequency:1", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
fsx.hSimConnect.RegisterDataDefineStruct<FSXObject.NavCom_Data>(FSXObject.DEFINITIONS.NAVCOM1_DATA);
// NavComm2 Frequencies
fsx.hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.NAVCOM2_DATA, "Com Active Frequency:2", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
fsx.hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.NAVCOM2_DATA, "Com Standby Frequency:2", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
fsx.hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.NAVCOM2_DATA, "Nav Active Frequency:2", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
fsx.hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.NAVCOM2_DATA, "Nav Standby Frequency:2", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
fsx.hSimConnect.RegisterDataDefineStruct<FSXObject.NavCom_Data>(FSXObject.DEFINITIONS.NAVCOM2_DATA);
fsx.hSimConnect.OnRecvOpen += new SimConnect.RecvOpenEventHandler(SimConnect_OnRecvOpen);
fsx.hSimConnect.OnRecvQuit += new SimConnect.RecvQuitEventHandler(SimConnect_OnRecvQuit);
fsx.hSimConnect.OnRecvException += new SimConnect.RecvExceptionEventHandler(SimConnect_OnRecvException);
fsx.hSimConnect.OnRecvSimobjectData += new SimConnect.RecvSimobjectDataEventHandler(SimConnect_OnRecvSimObjectData);
fsx.hSimConnect.OnRecvSimobjectDataBytype += new SimConnect.RecvSimobjectDataBytypeEventHandler(SimConnect_OnRecvSimObjectDataByType);
// Request for aircrat data. This will also configure auto-updates for other required data.
fsx.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) {
if (data.dwRequestID == (uint)FSXObject.DATA_REQUESTS.AUDIOSEL_REQ)
MainForm.NITMessageBox("Debug", "AudioSel data");
foreach (NITDevice device in panels.devices) {
if (device.isOpen()) {
device.FsxEvent(data);
}
}
}
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.fsx.FsxReady();
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
);
inputTimer.Enabled = true;
}
private void InitFsxClosed() {
if (fsx.hSimConnect != null) {
fsx.hSimConnect.Dispose();
txtSimConnect.Text = "Available";
} else {
txtSimConnect.Text = "Failed";
fsx.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.");
} 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") {
NITAudioSelForm form = new NITAudioSelForm();
form.setDevice((NITAudioSelDevice)device);
form.Show(this);
} 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() {
inputTimer.Enabled = false;
foreach (NITDevice device in panels.devices) {
if (device.isOpen())
device.Close();
}
Application.Exit();
}
private void quitToolStripMenuItem_Click(object sender, EventArgs e) {
if (MainForm.QuestionBox("Exit NIT Panels?", "Exiting will disable NIT Panels. Continue?")) {
this.quitNITPanels();
}
}
public static 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;
}
public static void NITMessageBox(string caption, string message) {
MessageBoxButtons buttons = MessageBoxButtons.OK;
MessageBox.Show(message, caption, buttons);
}
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 (fsx.hSimConnect != null)
this.SimConnect_Disconnect();
this.SimConnect_Connect();
}
private void cmdRequest_Click(object sender, EventArgs e) {
}
private void inputTimer_Tick(object sender, EventArgs e) {
foreach (NITDevice device in panels.devices) {
if (device.isOpen())
device.SimButtons();
}
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e) {
this.quitNITPanels();
}
private void debugTimer_Tick(object sender, EventArgs e) {
}
}
}