| 3 |
pfowler |
1 |
using System;
|
|
|
2 |
using System.Collections.Generic;
|
|
|
3 |
using System.Linq;
|
|
|
4 |
using System.Text;
|
|
|
5 |
using System.Threading.Tasks;
|
|
|
6 |
using System.Timers;
|
|
|
7 |
|
|
|
8 |
using Microsoft.FlightSimulator.SimConnect;
|
|
|
9 |
|
|
|
10 |
using LibUsbDotNet;
|
|
|
11 |
using LibUsbDotNet.Main;
|
|
|
12 |
using LibUsbDotNet.DeviceNotify;
|
|
|
13 |
|
|
|
14 |
namespace NITNavComm {
|
|
|
15 |
public class NITAudioSelDevice : NITDevice {
|
|
|
16 |
|
| 16 |
pfowler |
17 |
public VolumeControl volcontrol = new VolumeControl();
|
|
|
18 |
|
| 3 |
pfowler |
19 |
public const byte USBCMD_READ_BUTTONS = 100;
|
|
|
20 |
public const byte USBCMD_LEDS_SET = 101;
|
| 19 |
pfowler |
21 |
public const byte USBCMD_GET_ANALOG = 102;
|
|
|
22 |
public const byte USBCMD_SET_POWER_LED = 103;
|
| 3 |
pfowler |
23 |
|
| 23 |
pfowler |
24 |
// The min analog value we'll ever read from the pot
|
| 22 |
pfowler |
25 |
// If the analog val ever drops below this, a uint to int
|
|
|
26 |
// conversion will make the number >17k. If its not low enough
|
|
|
27 |
// the volume will never be able to be musted. Should probably
|
|
|
28 |
// be calibrated or the protection resistor removed from the
|
|
|
29 |
// analog pot ground line
|
|
|
30 |
public const uint DEV_ANALOG_MIN = 4;
|
|
|
31 |
|
| 3 |
pfowler |
32 |
private int simStatus { get; set; }
|
|
|
33 |
public byte ledStatus { get; set; }
|
|
|
34 |
public byte buttonStatus { get; set; }
|
| 19 |
pfowler |
35 |
public byte analogValue { get; set; }
|
|
|
36 |
public byte powerLedStatus { get; set; }
|
| 3 |
pfowler |
37 |
|
|
|
38 |
public bool avionicsMaster { get; set; }
|
|
|
39 |
|
|
|
40 |
private const int TIMER_COUNT = 8;
|
|
|
41 |
Timer[] timers = new Timer[TIMER_COUNT];
|
| 5 |
pfowler |
42 |
Timer inputTimer = new Timer();
|
| 3 |
pfowler |
43 |
|
|
|
44 |
public NITAudioSelDevice(NITDevice nitDevice) :
|
|
|
45 |
base(nitDevice.usbRegistry, "NITAudioSel", nitDevice.vid, nitDevice.pid) {
|
|
|
46 |
this.init();
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
public NITAudioSelDevice(UsbRegistry usbRegistry, string type, int vid, int pid) :
|
|
|
50 |
base(usbRegistry, "NITAudioSel", 0x4242, 0xe231) {
|
|
|
51 |
this.init();
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
private void init() {
|
|
|
55 |
base.Open();
|
|
|
56 |
|
|
|
57 |
this.simStatus = 0;
|
|
|
58 |
this.ledStatus = 0x00;
|
|
|
59 |
this.buttonStatus = 0x00;
|
|
|
60 |
|
| 19 |
pfowler |
61 |
this.analogValue = 0x00;
|
|
|
62 |
this.powerLedStatus = 0;
|
|
|
63 |
|
| 3 |
pfowler |
64 |
for (int i = 0; i < TIMER_COUNT; i++) {
|
|
|
65 |
Timer timer = new Timer();
|
|
|
66 |
timer.Enabled = false;
|
|
|
67 |
timer.AutoReset = false;
|
|
|
68 |
timer.Interval = NITPanels.CFG_BUTTON_DEBOUNCE_TIME;
|
|
|
69 |
timer.Elapsed += OnDebounceTimer;
|
|
|
70 |
timers[i] = timer;
|
|
|
71 |
}
|
| 5 |
pfowler |
72 |
|
|
|
73 |
inputTimer.Enabled = false;
|
|
|
74 |
inputTimer.AutoReset = true;
|
|
|
75 |
inputTimer.Interval = NITPanels.CFG_INPUT_TIMER_INTERVAL;
|
|
|
76 |
inputTimer.Elapsed += inputTimer_Elapsed;
|
| 22 |
pfowler |
77 |
|
|
|
78 |
// VolControl Stuff
|
|
|
79 |
// Default the user setting to the first device it not already set
|
| 23 |
pfowler |
80 |
if (Properties.Settings.Default.volcontroldevice == "" && volcontrol.deviceCount > 0) {
|
| 22 |
pfowler |
81 |
Properties.Settings.Default.volcontroldevice = volcontrol.devices.ElementAt(0);
|
|
|
82 |
Properties.Settings.Default.Save();
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
// Retrieve the user setting for the selected device, find its index
|
|
|
86 |
if (volcontrol.deviceCount > 0) {
|
|
|
87 |
string selectedDevice = Properties.Settings.Default.volcontroldevice;
|
|
|
88 |
for (int i = 0; i < volcontrol.deviceCount; i++) {
|
|
|
89 |
if (selectedDevice == volcontrol.devices.ElementAt(i)) {
|
|
|
90 |
volcontrol.selectedDeviceIndex = (uint)i;
|
|
|
91 |
break;
|
|
|
92 |
}
|
|
|
93 |
}
|
|
|
94 |
}
|
| 3 |
pfowler |
95 |
}
|
|
|
96 |
|
| 5 |
pfowler |
97 |
void inputTimer_Elapsed(object sender, ElapsedEventArgs e) {
|
|
|
98 |
this.SimButtons();
|
|
|
99 |
}
|
|
|
100 |
|
| 3 |
pfowler |
101 |
private static void OnDebounceTimer(Object source, ElapsedEventArgs e) {
|
|
|
102 |
Timer timer = (Timer)source;
|
|
|
103 |
timer.Enabled = false;
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
public override bool Close() {
|
| 5 |
pfowler |
107 |
this.powerDown();
|
| 3 |
pfowler |
108 |
return base.Close();
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
public void powerDown() {
|
| 5 |
pfowler |
112 |
this.inputTimer.Enabled = false;
|
| 3 |
pfowler |
113 |
this.ClearDisplay();
|
|
|
114 |
this.simStatus = 2;
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
public void powerUp() {
|
|
|
118 |
this.simStatus = 0;
|
| 23 |
pfowler |
119 |
this.setLedPwr(true);
|
| 3 |
pfowler |
120 |
this.fsx.requestAudioSelData();
|
| 5 |
pfowler |
121 |
this.inputTimer.Enabled = true;
|
| 3 |
pfowler |
122 |
}
|
|
|
123 |
|
| 5 |
pfowler |
124 |
public override void FsxReady() {
|
|
|
125 |
this.powerUp();
|
| 3 |
pfowler |
126 |
}
|
|
|
127 |
|
| 7 |
pfowler |
128 |
public override void reAssign() {
|
| 22 |
pfowler |
129 |
if (this.simStatus != 0 && this.fsx != null)
|
| 7 |
pfowler |
130 |
return;
|
| 5 |
pfowler |
131 |
|
| 7 |
pfowler |
132 |
this.fsx.requestAudioSelData();
|
|
|
133 |
}
|
|
|
134 |
|
| 3 |
pfowler |
135 |
public override void FsxEvent(SIMCONNECT_RECV_SIMOBJECT_DATA data) {
|
|
|
136 |
|
|
|
137 |
if (data.dwRequestID == (uint)FSXObject.DATA_REQUESTS.AVIONICS) {
|
|
|
138 |
FSXObject.Avionics_Data avionics = (FSXObject.Avionics_Data)data.dwData[0];
|
|
|
139 |
this.avionicsMaster = avionics.avionics_master;
|
|
|
140 |
if (!this.avionicsMaster) {
|
|
|
141 |
this.powerDown();
|
|
|
142 |
return;
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
if (this.avionicsMaster && this.simStatus != 0) {
|
|
|
146 |
this.powerUp();
|
|
|
147 |
return;
|
|
|
148 |
}
|
|
|
149 |
}
|
|
|
150 |
|
|
|
151 |
if (this.simStatus != 0)
|
|
|
152 |
return;
|
|
|
153 |
|
|
|
154 |
if (data.dwRequestID != (uint)FSXObject.DATA_REQUESTS.AUDIOSEL_REQ)
|
|
|
155 |
return;
|
|
|
156 |
|
|
|
157 |
FSXObject.AudioSel_Data audioseldata = (FSXObject.AudioSel_Data)data.dwData[0];
|
|
|
158 |
this.setLed(0, audioseldata.com1);
|
|
|
159 |
this.setLed(1, audioseldata.com2);
|
|
|
160 |
this.setLed(2, audioseldata.comb);
|
|
|
161 |
this.setLed(3, audioseldata.vor1);
|
|
|
162 |
this.setLed(4, audioseldata.vor2);
|
|
|
163 |
this.setLed(5, audioseldata.mkr);
|
|
|
164 |
this.setLed(6, audioseldata.dme);
|
|
|
165 |
this.setLed(7, audioseldata.adf);
|
|
|
166 |
|
|
|
167 |
this.UpdateDisplay();
|
|
|
168 |
}
|
|
|
169 |
|
|
|
170 |
public override void SimButtons() {
|
|
|
171 |
if (this.simStatus != 0)
|
|
|
172 |
return;
|
|
|
173 |
|
|
|
174 |
this.updateInput();
|
|
|
175 |
|
|
|
176 |
for (int i = 0; i <= 7; i++) {
|
|
|
177 |
if (timers[i].Enabled) // Wait for the debounce before processing again
|
|
|
178 |
continue;
|
|
|
179 |
|
|
|
180 |
if (NITPanels.readBit(this.buttonStatus, i)) { // Check an audio button is pressed
|
|
|
181 |
this.fsx.AudioSelButton(i); // and if so, send of the audio select event
|
|
|
182 |
timers[i].Enabled = true; // and start up a timer to debounce the button
|
|
|
183 |
}
|
|
|
184 |
}
|
| 22 |
pfowler |
185 |
|
|
|
186 |
|
|
|
187 |
// VolControl Stuff
|
|
|
188 |
float setVolume = NITPanels.map_f(this.analogValue, DEV_ANALOG_MIN, 255, 0.00F, 1.00F);
|
|
|
189 |
float devVolume = this.volcontrol.getDeviceVolume();
|
|
|
190 |
if (setVolume != devVolume)
|
|
|
191 |
this.volcontrol.setDeviceVolume(setVolume);
|
|
|
192 |
|
| 3 |
pfowler |
193 |
}
|
|
|
194 |
|
|
|
195 |
public void updateInput() {
|
|
|
196 |
byte[] data = new byte[8];
|
|
|
197 |
int transferred;
|
|
|
198 |
base.SendCommand(NITAudioSelDevice.USBCMD_READ_BUTTONS, 0, 0, data, out transferred);
|
|
|
199 |
this.buttonStatus = data[1];
|
| 19 |
pfowler |
200 |
|
|
|
201 |
base.SendCommand(NITAudioSelDevice.USBCMD_GET_ANALOG, 0, 0, data, out transferred);
|
|
|
202 |
this.analogValue = data[0];
|
| 3 |
pfowler |
203 |
}
|
|
|
204 |
|
|
|
205 |
public void UpdateDisplay() {
|
|
|
206 |
base.SendCommand(NITAudioSelDevice.USBCMD_LEDS_SET, this.ledStatus, 0);
|
| 19 |
pfowler |
207 |
base.SendCommand(NITAudioSelDevice.USBCMD_SET_POWER_LED, this.powerLedStatus, 0);
|
| 3 |
pfowler |
208 |
}
|
|
|
209 |
|
|
|
210 |
public void ClearDisplay() {
|
|
|
211 |
this.ledStatus = 0x00;
|
| 23 |
pfowler |
212 |
this.setLedPwr(false);
|
| 3 |
pfowler |
213 |
this.UpdateDisplay();
|
|
|
214 |
}
|
|
|
215 |
|
|
|
216 |
public void setLed(int ledNum, double status) {
|
|
|
217 |
if (status == 1)
|
|
|
218 |
this.ledStatus = NITPanels.setBit(this.ledStatus, ledNum);
|
|
|
219 |
else
|
|
|
220 |
this.ledStatus = NITPanels.clearBit(this.ledStatus, ledNum);
|
|
|
221 |
}
|
|
|
222 |
|
|
|
223 |
public void setLed(int ledNum, bool status) {
|
|
|
224 |
if (status)
|
|
|
225 |
this.ledStatus = NITPanels.setBit(this.ledStatus, ledNum);
|
|
|
226 |
else
|
|
|
227 |
this.ledStatus = NITPanels.clearBit(this.ledStatus, ledNum);
|
|
|
228 |
}
|
|
|
229 |
|
|
|
230 |
public bool isLedSet(int ledNum) {
|
|
|
231 |
return NITPanels.readBit(this.ledStatus, ledNum);
|
|
|
232 |
}
|
|
|
233 |
|
| 19 |
pfowler |
234 |
public bool isLedPwrSet() {
|
|
|
235 |
if (this.powerLedStatus>0)
|
|
|
236 |
return true;
|
|
|
237 |
return false;
|
|
|
238 |
}
|
|
|
239 |
|
|
|
240 |
public void setLedPwr(bool status) {
|
|
|
241 |
if (status)
|
|
|
242 |
this.powerLedStatus = 1;
|
|
|
243 |
else
|
|
|
244 |
this.powerLedStatus = 0;
|
|
|
245 |
}
|
|
|
246 |
|
| 3 |
pfowler |
247 |
public bool isButtonSet(int buttonNum) {
|
|
|
248 |
return NITPanels.readBit(this.buttonStatus, buttonNum);
|
|
|
249 |
}
|
|
|
250 |
}
|
|
|
251 |
}
|