Subversion Repositories group.NITPanels

Rev

Rev 11 | Rev 19 | 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;
21
 
22
        private int simStatus { get; set; }
23
        public byte ledStatus { get; set; }
24
        public byte buttonStatus { get; set; }
25
 
26
        public bool avionicsMaster { get; set; }
27
 
28
        private const int TIMER_COUNT = 8;
29
        Timer[] timers = new Timer[TIMER_COUNT];
5 pfowler 30
        Timer inputTimer = new Timer();
3 pfowler 31
 
32
        public NITAudioSelDevice(NITDevice nitDevice) :
33
            base(nitDevice.usbRegistry, "NITAudioSel", nitDevice.vid, nitDevice.pid) {
34
            this.init();
35
        }
36
 
37
        public NITAudioSelDevice(UsbRegistry usbRegistry, string type, int vid, int pid) :
38
            base(usbRegistry, "NITAudioSel", 0x4242, 0xe231) {
39
            this.init();
40
        }
41
 
42
        private void init() {
43
            base.Open();
44
 
45
            this.simStatus = 0;
46
            this.ledStatus = 0x00;
47
            this.buttonStatus = 0x00;
48
 
49
            for (int i = 0; i < TIMER_COUNT; i++) {
50
                Timer timer = new Timer();
51
                timer.Enabled = false;
52
                timer.AutoReset = false;
53
                timer.Interval = NITPanels.CFG_BUTTON_DEBOUNCE_TIME;
54
                timer.Elapsed += OnDebounceTimer;
55
                timers[i] = timer;
56
            }
5 pfowler 57
 
58
            inputTimer.Enabled = false;
59
            inputTimer.AutoReset = true;
60
            inputTimer.Interval = NITPanels.CFG_INPUT_TIMER_INTERVAL;
61
            inputTimer.Elapsed += inputTimer_Elapsed;
3 pfowler 62
        }
63
 
5 pfowler 64
        void inputTimer_Elapsed(object sender, ElapsedEventArgs e) {
65
            this.SimButtons();
66
        }
67
 
3 pfowler 68
        private static void OnDebounceTimer(Object source, ElapsedEventArgs e) {
69
            Timer timer = (Timer)source;
70
            timer.Enabled = false;
71
        }
72
 
73
        public override bool Close() {
5 pfowler 74
            this.powerDown();
3 pfowler 75
            return base.Close();
76
        }
77
 
78
        public void powerDown() {
5 pfowler 79
            this.inputTimer.Enabled = false;
3 pfowler 80
            this.ClearDisplay();
81
            this.simStatus = 2;
82
        }
83
 
84
        public void powerUp() {
85
            this.simStatus = 0;
86
            this.fsx.requestAudioSelData();
5 pfowler 87
            this.inputTimer.Enabled = true;
3 pfowler 88
        }
89
 
5 pfowler 90
        public override void FsxReady() {
91
            this.powerUp();
3 pfowler 92
        }
93
 
7 pfowler 94
        public override void reAssign() {
95
            if (this.simStatus != 0)
96
                return;
5 pfowler 97
 
7 pfowler 98
            this.fsx.requestAudioSelData();
99
        }
100
 
3 pfowler 101
        public override void FsxEvent(SIMCONNECT_RECV_SIMOBJECT_DATA data) {
102
 
103
            if (data.dwRequestID == (uint)FSXObject.DATA_REQUESTS.AVIONICS) {
104
                FSXObject.Avionics_Data avionics = (FSXObject.Avionics_Data)data.dwData[0];
105
                this.avionicsMaster = avionics.avionics_master;
106
                if (!this.avionicsMaster) {
107
                    this.powerDown();
108
                    return;
109
                }
110
 
111
                if (this.avionicsMaster && this.simStatus != 0) {
112
                    this.powerUp();
113
                    return;
114
                }
115
            }
116
 
117
            if (this.simStatus != 0)
118
                return;
119
 
120
            if (data.dwRequestID != (uint)FSXObject.DATA_REQUESTS.AUDIOSEL_REQ)
121
                return;
122
 
123
 
124
            FSXObject.AudioSel_Data audioseldata = (FSXObject.AudioSel_Data)data.dwData[0];
125
            this.setLed(0, audioseldata.com1);
126
            this.setLed(1, audioseldata.com2);
127
            this.setLed(2, audioseldata.comb);
128
            this.setLed(3, audioseldata.vor1);
129
            this.setLed(4, audioseldata.vor2);
130
            this.setLed(5, audioseldata.mkr);
131
            this.setLed(6, audioseldata.dme);
132
            this.setLed(7, audioseldata.adf);
133
 
134
            this.UpdateDisplay();
135
        }
136
 
137
        public override void SimButtons() {
138
            if (this.simStatus != 0)
139
                return;
140
 
141
            this.updateInput();
142
 
143
            for (int i = 0; i <= 7; i++) {
144
                if (timers[i].Enabled)                              // Wait for the debounce before processing again
145
                    continue;
146
 
147
                if (NITPanels.readBit(this.buttonStatus, i)) {      // Check an audio button is pressed
148
                    this.fsx.AudioSelButton(i);                          //  and if so, send of the audio select event
149
                    timers[i].Enabled = true;                       //  and start up a timer to debounce the button
150
                }
151
            }
152
        }
153
 
154
        public void updateInput() {
155
            byte[] data = new byte[8];
156
            int transferred;
157
            base.SendCommand(NITAudioSelDevice.USBCMD_READ_BUTTONS, 0, 0, data, out transferred);
158
            this.buttonStatus = data[1];
159
        }
160
 
161
        public void UpdateDisplay() {
162
            base.SendCommand(NITAudioSelDevice.USBCMD_LEDS_SET, this.ledStatus, 0);
163
        }
164
 
165
        public void ClearDisplay() {
166
            this.ledStatus = 0x00;
167
            this.UpdateDisplay();
168
        }
169
 
170
        public void setLed(int ledNum, double status) {
171
            if (status == 1)
172
                this.ledStatus = NITPanels.setBit(this.ledStatus, ledNum);
173
            else
174
                this.ledStatus = NITPanels.clearBit(this.ledStatus, ledNum);
175
        }
176
 
177
        public void setLed(int ledNum, bool status) {
178
            if (status)
179
                this.ledStatus = NITPanels.setBit(this.ledStatus, ledNum);
180
            else
181
                this.ledStatus = NITPanels.clearBit(this.ledStatus, ledNum);
182
        }
183
 
184
        public bool isLedSet(int ledNum) {
185
            return NITPanels.readBit(this.ledStatus, ledNum);
186
        }
187
 
188
        public bool isButtonSet(int buttonNum) {
189
            return NITPanels.readBit(this.buttonStatus, buttonNum);
190
        }
191
    }
192
}