Subversion Repositories group.electronics

Rev

Rev 156 | Details | Compare with Previous | Last modification | View Log | RSS feed

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