155 |
pfowler |
1 |
using System;
|
|
|
2 |
using System.Collections.Generic;
|
|
|
3 |
using System.Linq;
|
|
|
4 |
using System.Text;
|
|
|
5 |
using System.Threading.Tasks;
|
156 |
pfowler |
6 |
using System.Timers;
|
155 |
pfowler |
7 |
|
156 |
pfowler |
8 |
using Microsoft.FlightSimulator.SimConnect;
|
|
|
9 |
|
|
|
10 |
using LibUsbDotNet;
|
|
|
11 |
using LibUsbDotNet.Main;
|
|
|
12 |
using LibUsbDotNet.DeviceNotify;
|
|
|
13 |
|
155 |
pfowler |
14 |
namespace NITNavComm {
|
156 |
pfowler |
15 |
public class NITAudioSelDevice : NITDevice {
|
|
|
16 |
|
|
|
17 |
public const byte USBCMD_READ_BUTTONS = 100;
|
|
|
18 |
public const byte USBCMD_LEDS_SET = 101;
|
|
|
19 |
|
|
|
20 |
private int simStatus { get; set; }
|
|
|
21 |
public byte ledStatus { get; set; }
|
|
|
22 |
public byte buttonStatus { get; set; }
|
|
|
23 |
|
|
|
24 |
public bool avionicsMaster { get; set; }
|
|
|
25 |
|
|
|
26 |
private const int TIMER_COUNT = 8;
|
|
|
27 |
Timer[] timers = new Timer[TIMER_COUNT];
|
|
|
28 |
|
|
|
29 |
public NITAudioSelDevice(NITDevice nitDevice) :
|
|
|
30 |
base(nitDevice.usbRegistry, "NITAudioSel", nitDevice.vid, nitDevice.pid) {
|
|
|
31 |
this.init();
|
|
|
32 |
}
|
|
|
33 |
|
|
|
34 |
public NITAudioSelDevice(UsbRegistry usbRegistry, string type, int vid, int pid) :
|
|
|
35 |
base(usbRegistry, "NITAudioSel", 0x4242, 0xe231) {
|
|
|
36 |
this.init();
|
|
|
37 |
}
|
|
|
38 |
|
|
|
39 |
private void init() {
|
|
|
40 |
base.Open();
|
|
|
41 |
|
|
|
42 |
this.simStatus = 0;
|
|
|
43 |
this.ledStatus = 0x00;
|
|
|
44 |
this.buttonStatus = 0x00;
|
|
|
45 |
|
|
|
46 |
for (int i = 0; i < TIMER_COUNT; i++) {
|
|
|
47 |
Timer timer = new Timer();
|
|
|
48 |
timer.Enabled = false;
|
|
|
49 |
timer.AutoReset = false;
|
|
|
50 |
timer.Interval = NITPanels.CFG_BUTTON_DEBOUNCE_TIME;
|
|
|
51 |
timer.Elapsed += OnDebounceTimer;
|
|
|
52 |
timers[i] = timer;
|
|
|
53 |
}
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
private static void OnDebounceTimer(Object source, ElapsedEventArgs e) {
|
|
|
57 |
Timer timer = (Timer)source;
|
|
|
58 |
timer.Enabled = false;
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
public override bool Close() {
|
|
|
62 |
this.ClearDisplay();
|
|
|
63 |
return base.Close();
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
public void powerDown() {
|
|
|
67 |
this.ClearDisplay();
|
|
|
68 |
this.simStatus = 2;
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
public void powerUp() {
|
|
|
72 |
this.simStatus = 0;
|
|
|
73 |
this.fsx.requestAudioSelData();
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
public override void MapEvents(FSXObject fsx) {
|
|
|
77 |
|
|
|
78 |
fsx.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.COM1_TRANSMIT_SELECT, "COM1_TRANSMIT_SELECT");
|
|
|
79 |
fsx.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.COM2_TRANSMIT_SELECT, "COM2_TRANSMIT_SELECT");
|
|
|
80 |
fsx.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.COM_RECEIVE_ALL_TOGGLE, "COM_RECEIVE_ALL_TOGGLE");
|
|
|
81 |
fsx.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.RADIO_VOR1_IDENT_TOGGLE, "RADIO_VOR1_IDENT_TOGGLE");
|
|
|
82 |
fsx.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.RADIO_VOR2_IDENT_TOGGLE, "RADIO_VOR2_IDENT_TOGGLE");
|
|
|
83 |
fsx.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.MARKER_SOUND_TOGGLE, "MARKER_SOUND_TOGGLE");
|
|
|
84 |
fsx.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.RADIO_DME1_IDENT_TOGGLE, "RADIO_DME1_IDENT_TOGGLE");
|
|
|
85 |
fsx.hSimConnect.MapClientEventToSimEvent(FSXObject.EVENT_ID.RADIO_ADF_IDENT_TOGGLE, "RADIO_ADF_IDENT_TOGGLE");
|
|
|
86 |
|
|
|
87 |
fsx.hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.AUDIOSEL_DATA, "COM TRANSMIT:1", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
|
|
|
88 |
fsx.hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.AUDIOSEL_DATA, "COM TRANSMIT:2", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
|
|
|
89 |
fsx.hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.AUDIOSEL_DATA, "COM RECIEVE ALL", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
|
|
|
90 |
fsx.hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.AUDIOSEL_DATA, "NAV SOUND:1", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
|
|
|
91 |
fsx.hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.AUDIOSEL_DATA, "NAV SOUND:2", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
|
|
|
92 |
fsx.hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.AUDIOSEL_DATA, "MARKER SOUND", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
|
|
|
93 |
fsx.hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.AUDIOSEL_DATA, "DME SOUND", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
|
|
|
94 |
fsx.hSimConnect.AddToDataDefinition(FSXObject.DEFINITIONS.AUDIOSEL_DATA, "ADF SOUND:1", "Number", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
|
|
|
95 |
fsx.hSimConnect.RegisterDataDefineStruct<FSXObject.AudioSel_Data>(FSXObject.DEFINITIONS.AUDIOSEL_DATA);
|
|
|
96 |
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
public override void FsxEvent(FSXObject fsx, SIMCONNECT_RECV_SIMOBJECT_DATA data) {
|
|
|
100 |
|
|
|
101 |
if (data.dwRequestID == (uint)FSXObject.DATA_REQUESTS.AVIONICS) {
|
|
|
102 |
FSXObject.Avionics_Data avionics = (FSXObject.Avionics_Data)data.dwData[0];
|
|
|
103 |
this.avionicsMaster = avionics.avionics_master;
|
|
|
104 |
if (!this.avionicsMaster) {
|
|
|
105 |
this.powerDown();
|
|
|
106 |
return;
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
if (this.avionicsMaster && this.simStatus != 0) {
|
|
|
110 |
this.powerUp();
|
|
|
111 |
return;
|
|
|
112 |
}
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
if (this.simStatus != 0)
|
|
|
116 |
return;
|
|
|
117 |
|
|
|
118 |
if (data.dwRequestID != (uint)FSXObject.DATA_REQUESTS.AUDIOSEL_REQ)
|
|
|
119 |
return;
|
|
|
120 |
|
|
|
121 |
FSXObject.AudioSel_Data audioseldata = (FSXObject.AudioSel_Data)data.dwData[0];
|
|
|
122 |
this.setLed(0, audioseldata.com1);
|
|
|
123 |
this.setLed(1, audioseldata.com2);
|
|
|
124 |
this.setLed(2, audioseldata.comb);
|
|
|
125 |
this.setLed(3, audioseldata.vor1);
|
|
|
126 |
this.setLed(4, audioseldata.vor2);
|
|
|
127 |
this.setLed(5, audioseldata.mkr);
|
|
|
128 |
this.setLed(6, audioseldata.dme);
|
|
|
129 |
this.setLed(7, audioseldata.adf);
|
|
|
130 |
|
|
|
131 |
this.UpdateDisplay();
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
public override void FsxReady(FSXObject fsx) {
|
|
|
135 |
fsx.hSimConnect.RequestDataOnSimObject(
|
|
|
136 |
FSXObject.DATA_REQUESTS.AUDIOSEL_REQ,
|
|
|
137 |
FSXObject.DEFINITIONS.AUDIOSEL_DATA,
|
|
|
138 |
fsx.simdata.objectid,
|
|
|
139 |
SIMCONNECT_PERIOD.SIM_FRAME,
|
|
|
140 |
SIMCONNECT_DATA_REQUEST_FLAG.CHANGED,
|
|
|
141 |
0, 0, 0);
|
|
|
142 |
}
|
|
|
143 |
|
|
|
144 |
public override void SimButtons(FSXObject fsx) {
|
|
|
145 |
if (this.simStatus != 0)
|
|
|
146 |
return;
|
|
|
147 |
|
|
|
148 |
this.updateInput();
|
|
|
149 |
|
|
|
150 |
for (int i = 0; i <= 7; i++) {
|
|
|
151 |
if (timers[i].Enabled) // Wait for the debounce before processing again
|
|
|
152 |
continue;
|
|
|
153 |
|
|
|
154 |
if (NITPanels.readBit(this.buttonStatus, i)) { // Check an audio button is pressed
|
|
|
155 |
fsx.AudioSelButton(i); // and if so, send of the audio select event
|
|
|
156 |
timers[i].Enabled = true; // and start up a timer to debounce the button
|
|
|
157 |
}
|
|
|
158 |
}
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
public void updateInput() {
|
|
|
162 |
byte[] data = new byte[8];
|
|
|
163 |
int transferred;
|
|
|
164 |
base.SendCommand(NITAudioSelDevice.USBCMD_READ_BUTTONS, 0, 0, data, out transferred);
|
|
|
165 |
this.buttonStatus = data[1];
|
|
|
166 |
}
|
|
|
167 |
|
|
|
168 |
public void UpdateDisplay() {
|
|
|
169 |
base.SendCommand(NITAudioSelDevice.USBCMD_LEDS_SET, this.ledStatus, 0);
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
public void ClearDisplay() {
|
|
|
173 |
this.ledStatus = 0x00;
|
|
|
174 |
this.UpdateDisplay();
|
|
|
175 |
}
|
|
|
176 |
|
|
|
177 |
public void setLed(int ledNum, double status) {
|
|
|
178 |
if (status == 1)
|
|
|
179 |
this.ledStatus = NITPanels.setBit(this.ledStatus, ledNum);
|
|
|
180 |
else
|
|
|
181 |
this.ledStatus = NITPanels.clearBit(this.ledStatus, ledNum);
|
|
|
182 |
}
|
|
|
183 |
|
|
|
184 |
public void setLed(int ledNum, bool status) {
|
|
|
185 |
if (status)
|
|
|
186 |
this.ledStatus = NITPanels.setBit(this.ledStatus, ledNum);
|
|
|
187 |
else
|
|
|
188 |
this.ledStatus = NITPanels.clearBit(this.ledStatus, ledNum);
|
|
|
189 |
}
|
|
|
190 |
|
|
|
191 |
public bool isLedSet(int ledNum) {
|
|
|
192 |
return NITPanels.readBit(this.ledStatus, ledNum);
|
|
|
193 |
}
|
|
|
194 |
|
|
|
195 |
public bool isButtonSet(int buttonNum) {
|
|
|
196 |
return NITPanels.readBit(this.buttonStatus, buttonNum);
|
|
|
197 |
}
|
155 |
pfowler |
198 |
}
|
|
|
199 |
}
|