Subversion Repositories group.NITPanels

Rev

Rev 16 | Rev 22 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
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
 
24
        private int simStatus { get; set; }
25
        public byte ledStatus { get; set; }
26
        public byte buttonStatus { get; set; }
19 pfowler 27
        public byte analogValue { get; set; }
28
        public byte powerLedStatus { get; set; }
3 pfowler 29
 
30
        public bool avionicsMaster { get; set; }
31
 
32
        private const int TIMER_COUNT = 8;
33
        Timer[] timers = new Timer[TIMER_COUNT];
5 pfowler 34
        Timer inputTimer = new Timer();
3 pfowler 35
 
36
        public NITAudioSelDevice(NITDevice nitDevice) :
37
            base(nitDevice.usbRegistry, "NITAudioSel", nitDevice.vid, nitDevice.pid) {
38
            this.init();
39
        }
40
 
41
        public NITAudioSelDevice(UsbRegistry usbRegistry, string type, int vid, int pid) :
42
            base(usbRegistry, "NITAudioSel", 0x4242, 0xe231) {
43
            this.init();
44
        }
45
 
46
        private void init() {
47
            base.Open();
48
 
49
            this.simStatus = 0;
50
            this.ledStatus = 0x00;
51
            this.buttonStatus = 0x00;
52
 
19 pfowler 53
            this.analogValue = 0x00;
54
            this.powerLedStatus = 0;
55
 
3 pfowler 56
            for (int i = 0; i < TIMER_COUNT; i++) {
57
                Timer timer = new Timer();
58
                timer.Enabled = false;
59
                timer.AutoReset = false;
60
                timer.Interval = NITPanels.CFG_BUTTON_DEBOUNCE_TIME;
61
                timer.Elapsed += OnDebounceTimer;
62
                timers[i] = timer;
63
            }
5 pfowler 64
 
65
            inputTimer.Enabled = false;
66
            inputTimer.AutoReset = true;
67
            inputTimer.Interval = NITPanels.CFG_INPUT_TIMER_INTERVAL;
68
            inputTimer.Elapsed += inputTimer_Elapsed;
3 pfowler 69
        }
70
 
5 pfowler 71
        void inputTimer_Elapsed(object sender, ElapsedEventArgs e) {
72
            this.SimButtons();
73
        }
74
 
3 pfowler 75
        private static void OnDebounceTimer(Object source, ElapsedEventArgs e) {
76
            Timer timer = (Timer)source;
77
            timer.Enabled = false;
78
        }
79
 
80
        public override bool Close() {
5 pfowler 81
            this.powerDown();
3 pfowler 82
            return base.Close();
83
        }
84
 
85
        public void powerDown() {
5 pfowler 86
            this.inputTimer.Enabled = false;
3 pfowler 87
            this.ClearDisplay();
88
            this.simStatus = 2;
89
        }
90
 
91
        public void powerUp() {
92
            this.simStatus = 0;
93
            this.fsx.requestAudioSelData();
5 pfowler 94
            this.inputTimer.Enabled = true;
3 pfowler 95
        }
96
 
5 pfowler 97
        public override void FsxReady() {
98
            this.powerUp();
3 pfowler 99
        }
100
 
7 pfowler 101
        public override void reAssign() {
102
            if (this.simStatus != 0)
103
                return;
5 pfowler 104
 
7 pfowler 105
            this.fsx.requestAudioSelData();
106
        }
107
 
3 pfowler 108
        public override void FsxEvent(SIMCONNECT_RECV_SIMOBJECT_DATA data) {
109
 
110
            if (data.dwRequestID == (uint)FSXObject.DATA_REQUESTS.AVIONICS) {
111
                FSXObject.Avionics_Data avionics = (FSXObject.Avionics_Data)data.dwData[0];
112
                this.avionicsMaster = avionics.avionics_master;
113
                if (!this.avionicsMaster) {
114
                    this.powerDown();
115
                    return;
116
                }
117
 
118
                if (this.avionicsMaster && this.simStatus != 0) {
119
                    this.powerUp();
120
                    return;
121
                }
122
            }
123
 
124
            if (this.simStatus != 0)
125
                return;
126
 
127
            if (data.dwRequestID != (uint)FSXObject.DATA_REQUESTS.AUDIOSEL_REQ)
128
                return;
129
 
130
 
131
            FSXObject.AudioSel_Data audioseldata = (FSXObject.AudioSel_Data)data.dwData[0];
132
            this.setLed(0, audioseldata.com1);
133
            this.setLed(1, audioseldata.com2);
134
            this.setLed(2, audioseldata.comb);
135
            this.setLed(3, audioseldata.vor1);
136
            this.setLed(4, audioseldata.vor2);
137
            this.setLed(5, audioseldata.mkr);
138
            this.setLed(6, audioseldata.dme);
139
            this.setLed(7, audioseldata.adf);
140
 
141
            this.UpdateDisplay();
142
        }
143
 
144
        public override void SimButtons() {
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
                    this.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];
19 pfowler 166
 
167
            base.SendCommand(NITAudioSelDevice.USBCMD_GET_ANALOG, 0, 0, data, out transferred);
168
            this.analogValue = data[0];
3 pfowler 169
        }
170
 
171
        public void UpdateDisplay() {
172
            base.SendCommand(NITAudioSelDevice.USBCMD_LEDS_SET, this.ledStatus, 0);
19 pfowler 173
            base.SendCommand(NITAudioSelDevice.USBCMD_SET_POWER_LED, this.powerLedStatus, 0);
3 pfowler 174
        }
175
 
176
        public void ClearDisplay() {
177
            this.ledStatus = 0x00;
19 pfowler 178
            this.powerLedStatus = 0x00;
3 pfowler 179
            this.UpdateDisplay();
180
        }
181
 
182
        public void setLed(int ledNum, double status) {
183
            if (status == 1)
184
                this.ledStatus = NITPanels.setBit(this.ledStatus, ledNum);
185
            else
186
                this.ledStatus = NITPanels.clearBit(this.ledStatus, ledNum);
187
        }
188
 
189
        public void setLed(int ledNum, bool status) {
190
            if (status)
191
                this.ledStatus = NITPanels.setBit(this.ledStatus, ledNum);
192
            else
193
                this.ledStatus = NITPanels.clearBit(this.ledStatus, ledNum);
194
        }
195
 
196
        public bool isLedSet(int ledNum) {
197
            return NITPanels.readBit(this.ledStatus, ledNum);
198
        }
199
 
19 pfowler 200
        public bool isLedPwrSet() {
201
            if (this.powerLedStatus>0)
202
                return true;
203
            return false;
204
        }
205
 
206
        public void setLedPwr(bool status) {
207
            if (status)
208
                this.powerLedStatus = 1;
209
            else
210
                this.powerLedStatus = 0;
211
        }
212
 
3 pfowler 213
        public bool isButtonSet(int buttonNum) {
214
            return NITPanels.readBit(this.buttonStatus, buttonNum);
215
        }
216
    }
217
}