Rev 145 | 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.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 SimData simdata = new SimData();
private struct SimData {
public uint objectid;
public Aircraft_Data aircraft;
}
public MainForm() {
InitializeComponent();
Devices_Rescan();
txtStatus.Text = "Running";
}
private enum DATA_REQUESTS {
AIRCRAFT,
AVIONICS,
NAVCOM1_REQ,
NAVCOM2_REQ
}
private enum DEFINITIONS {
AIRCRAFT,
AVIONICS,
NAVCOM1_DATA,
NAVCOM2_DATA
}
private enum EVENT_ID {
COM1_SWAP_FREQ,
NAV1_SWAP_FREQ,
COM_RADIO_WHOLE_INC,
COM_RADIO_WHOLE_DEC,
}
private enum GROUP_ID {
NAVCOM1
}
[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 Avionics_Data {
public bool avionics_master;
public double com_status;
}
[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;
}
private void SimConnect_Connect() {
if (hSimConnect == null) {
try {
hSimConnect = new SimConnect("Managed Data Request", base.Handle, 0x402, null, 0);
txtSimConnect.Text = "Available";
this.SimConnect_InitDataRequest();
} catch (COMException) {
Log("Could not connect to FSX");
txtSimConnect.Text = "Failed";
}
}
}
private void SimConnect_Disconnect() {
this.InitFsxClosed();
if (hSimConnect != null) {
hSimConnect.Dispose();
txtSimConnect.Text = "Available";
} else {
txtSimConnect.Text = "Failed";
}
hSimConnect = null;
}
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(EVENT_ID.COM1_SWAP_FREQ, "COM_STBY_RADIO_SWAP");
hSimConnect.MapClientEventToSimEvent(EVENT_ID.NAV1_SWAP_FREQ, "NAV1_RADIO_SWAP");
hSimConnect.MapClientEventToSimEvent(EVENT_ID.COM_RADIO_WHOLE_INC, "COM_RADIO_WHOLE_INC");
hSimConnect.MapClientEventToSimEvent(EVENT_ID.COM_RADIO_WHOLE_DEC, "COM_RADIO_WHOLE_DEC");
hSimConnect.AddToDataDefinition(DEFINITIONS.AIRCRAFT, "Atc Type", "", SIMCONNECT_DATATYPE.STRING32, 0.0f, SimConnect.SIMCONNECT_UNUSED);
hSimConnect.AddToDataDefinition(DEFINITIONS.AIRCRAFT, "ATC Model", "", SIMCONNECT_DATATYPE.STRING32, 0.0f, SimConnect.SIMCONNECT_UNUSED);
hSimConnect.AddToDataDefinition(DEFINITIONS.AIRCRAFT, "ATC ID", "", SIMCONNECT_DATATYPE.STRING32, 0.0f, SimConnect.SIMCONNECT_UNUSED);
hSimConnect.AddToDataDefinition(DEFINITIONS.AIRCRAFT, "Title", "", SIMCONNECT_DATATYPE.STRING128, 0.0f, SimConnect.SIMCONNECT_UNUSED);
hSimConnect.RegisterDataDefineStruct<Aircraft_Data>(DEFINITIONS.AIRCRAFT);
hSimConnect.AddToDataDefinition(DEFINITIONS.AVIONICS, "Avionics Master Switch", "bool", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
hSimConnect.AddToDataDefinition(DEFINITIONS.AVIONICS, "Com Status", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
hSimConnect.RegisterDataDefineStruct<Avionics_Data>(DEFINITIONS.AVIONICS);
hSimConnect.AddToDataDefinition(DEFINITIONS.NAVCOM1_DATA, "Com Active Frequency:1", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
hSimConnect.AddToDataDefinition(DEFINITIONS.NAVCOM1_DATA, "Com Standby Frequency:1", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
hSimConnect.AddToDataDefinition(DEFINITIONS.NAVCOM1_DATA, "Nav Active Frequency:1", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
hSimConnect.AddToDataDefinition(DEFINITIONS.NAVCOM1_DATA, "Nav Standby Frequency:1", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
hSimConnect.RegisterDataDefineStruct<NavCom_Data>(DEFINITIONS.NAVCOM1_DATA);
hSimConnect.AddToDataDefinition(DEFINITIONS.NAVCOM2_DATA, "Com Active Frequency:2", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
hSimConnect.AddToDataDefinition(DEFINITIONS.NAVCOM2_DATA, "Com Standby Frequency:2", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
hSimConnect.AddToDataDefinition(DEFINITIONS.NAVCOM2_DATA, "Nav Active Frequency:2", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
hSimConnect.AddToDataDefinition(DEFINITIONS.NAVCOM2_DATA, "Nav Standby Frequency:2", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
hSimConnect.RegisterDataDefineStruct<NavCom_Data>(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(DATA_REQUESTS.AIRCRAFT, 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));
}
private void SimConnect_OnRecvSimObjectData(SimConnect sender, SIMCONNECT_RECV_SIMOBJECT_DATA data) {
switch (data.dwRequestID) {
// Automatically provided updates on NavCom1
case ((uint)DATA_REQUESTS.NAVCOM1_REQ): {
NavCom_Data navcomdata = (NavCom_Data)data.dwData[0];
NITNavCommDevice navcomm = (NITNavCommDevice)panels.devices[0];
navcomm.setNavComFreqs(navcomdata);
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)DATA_REQUESTS.AIRCRAFT): {
uint ObjectID = data.dwObjectID;
Aircraft_Data aircraftdata = (Aircraft_Data)data.dwData[0];
this.simdata.objectid = ObjectID;
this.simdata.aircraft = aircraftdata;
this.InitFsxReady();
break;
}
}
}
private void InitFsxReady() {
Log("Aircraft: "
+ this.simdata.aircraft.atc_id + ", "
+ this.simdata.aircraft.atc_model + ", "
+ this.simdata.aircraft.atc_type + ", "
+ this.simdata.aircraft.title
);
hSimConnect.RequestDataOnSimObject(
DATA_REQUESTS.NAVCOM1_REQ,
DEFINITIONS.NAVCOM1_DATA,
this.simdata.objectid,
SIMCONNECT_PERIOD.SIM_FRAME,
SIMCONNECT_DATA_REQUEST_FLAG.CHANGED,
0, 0, 0);
inputTimer.Enabled = true;
}
private void InitFsxClosed() {
inputTimer.Enabled = false;
}
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.");
}
NITNavCommDevice navcomm = (NITNavCommDevice)panels.devices[0];
navcomm.resetRotary(0, 0);
}
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;
NITNavCommDevice navcomm = (NITNavCommDevice)this.deviceGrid.CurrentRow.DataBoundItem;;
if (!navcomm.Open()) {
Log("Could not open device " + navcomm.type + "(" + navcomm.serial + ").");
return;
}
navcomm.Close();
NITCommNavForm form = new NITCommNavForm();
form.setDevice(navcomm);
form.Show(this);
}
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 quitToolStripMenuItem_Click(object sender, EventArgs e) {
if (QuestionBox("Exit NIT Panels?", "Exiting will disable NIT Panels. Continue?")) {
Application.Exit();
}
}
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) {
hSimConnect.TransmitClientEvent(
this.simdata.objectid,
EVENT_ID.COM1_SWAP_FREQ, 1,
GROUP_ID.NAVCOM1,
SIMCONNECT_EVENT_FLAG.GROUPID_IS_PRIORITY
);
}
private void inputTimer_Tick(object sender, EventArgs e) {
NITNavCommDevice navcomm = (NITNavCommDevice)panels.devices[0];
navcomm.updateInput();
if (navcomm.isSwapSet(0))
hSimConnect.TransmitClientEvent(
this.simdata.objectid,
EVENT_ID.COM1_SWAP_FREQ, 1,
GROUP_ID.NAVCOM1,
SIMCONNECT_EVENT_FLAG.GROUPID_IS_PRIORITY
);
if (navcomm.isSwapSet(1))
hSimConnect.TransmitClientEvent(
this.simdata.objectid,
EVENT_ID.NAV1_SWAP_FREQ, 1,
GROUP_ID.NAVCOM1,
SIMCONNECT_EVENT_FLAG.GROUPID_IS_PRIORITY
);
sbyte delta = navcomm.getRotary(0, 0);
navcomm.resetRotary(0, 0);
if (delta != 0) {
//sbyte start = 0;
//sbyte finish = 0;
if (delta < 0) {
//start = delta;
//for (sbyte i = start; start < finish; i++) {
delta = (sbyte)-delta;
hSimConnect.TransmitClientEvent(
this.simdata.objectid,
EVENT_ID.COM_RADIO_WHOLE_DEC, 1,
GROUP_ID.NAVCOM1,
SIMCONNECT_EVENT_FLAG.GROUPID_IS_PRIORITY
);
//}
} else {
//finish = delta;
//for (sbyte i = start; start < finish; i++) {
hSimConnect.TransmitClientEvent(
this.simdata.objectid,
EVENT_ID.COM_RADIO_WHOLE_INC, 1,
GROUP_ID.NAVCOM1,
SIMCONNECT_EVENT_FLAG.GROUPID_IS_PRIORITY
);
//}
}
}
}
}
}