Subversion Repositories group.NITPanels

Rev

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