Subversion Repositories group.NITPanels

Rev

Rev 5 | Rev 11 | 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
 
88
        public override void FsxInit() { }
89
 
5 pfowler 90
        public override void MapEvents() { }
3 pfowler 91
 
5 pfowler 92
        public override void FsxReady() {
93
            this.powerUp();
3 pfowler 94
        }
95
 
7 pfowler 96
        public override void reAssign() {
97
            if (this.simStatus != 0)
98
                return;
5 pfowler 99
 
7 pfowler 100
            this.fsx.requestAudioSelData();
101
        }
102
 
3 pfowler 103
        public override void FsxEvent(SIMCONNECT_RECV_SIMOBJECT_DATA data) {
104
 
105
            if (data.dwRequestID == (uint)FSXObject.DATA_REQUESTS.AVIONICS) {
106
                FSXObject.Avionics_Data avionics = (FSXObject.Avionics_Data)data.dwData[0];
107
                this.avionicsMaster = avionics.avionics_master;
108
                if (!this.avionicsMaster) {
109
                    this.powerDown();
110
                    return;
111
                }
112
 
113
                if (this.avionicsMaster && this.simStatus != 0) {
114
                    this.powerUp();
115
                    return;
116
                }
117
            }
118
 
119
            if (this.simStatus != 0)
120
                return;
121
 
122
            if (data.dwRequestID != (uint)FSXObject.DATA_REQUESTS.AUDIOSEL_REQ)
123
                return;
124
 
125
 
126
            FSXObject.AudioSel_Data audioseldata = (FSXObject.AudioSel_Data)data.dwData[0];
127
            this.setLed(0, audioseldata.com1);
128
            this.setLed(1, audioseldata.com2);
129
            this.setLed(2, audioseldata.comb);
130
            this.setLed(3, audioseldata.vor1);
131
            this.setLed(4, audioseldata.vor2);
132
            this.setLed(5, audioseldata.mkr);
133
            this.setLed(6, audioseldata.dme);
134
            this.setLed(7, audioseldata.adf);
135
 
136
            this.UpdateDisplay();
137
        }
138
 
139
        public override void SimButtons() {
140
            if (this.simStatus != 0)
141
                return;
142
 
143
            this.updateInput();
144
 
145
            for (int i = 0; i <= 7; i++) {
146
                if (timers[i].Enabled)                              // Wait for the debounce before processing again
147
                    continue;
148
 
149
                if (NITPanels.readBit(this.buttonStatus, i)) {      // Check an audio button is pressed
150
                    this.fsx.AudioSelButton(i);                          //  and if so, send of the audio select event
151
                    timers[i].Enabled = true;                       //  and start up a timer to debounce the button
152
                }
153
            }
154
        }
155
 
156
        public void updateInput() {
157
            byte[] data = new byte[8];
158
            int transferred;
159
            base.SendCommand(NITAudioSelDevice.USBCMD_READ_BUTTONS, 0, 0, data, out transferred);
160
            this.buttonStatus = data[1];
161
        }
162
 
163
        public void UpdateDisplay() {
164
            base.SendCommand(NITAudioSelDevice.USBCMD_LEDS_SET, this.ledStatus, 0);
165
        }
166
 
167
        public void ClearDisplay() {
168
            this.ledStatus = 0x00;
169
            this.UpdateDisplay();
170
        }
171
 
172
        public void setLed(int ledNum, double status) {
173
            if (status == 1)
174
                this.ledStatus = NITPanels.setBit(this.ledStatus, ledNum);
175
            else
176
                this.ledStatus = NITPanels.clearBit(this.ledStatus, ledNum);
177
        }
178
 
179
        public void setLed(int ledNum, bool status) {
180
            if (status)
181
                this.ledStatus = NITPanels.setBit(this.ledStatus, ledNum);
182
            else
183
                this.ledStatus = NITPanels.clearBit(this.ledStatus, ledNum);
184
        }
185
 
186
        public bool isLedSet(int ledNum) {
187
            return NITPanels.readBit(this.ledStatus, ledNum);
188
        }
189
 
190
        public bool isButtonSet(int buttonNum) {
191
            return NITPanels.readBit(this.buttonStatus, buttonNum);
192
        }
193
    }
194
}