Subversion Repositories group.NITPanels

Rev

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