Subversion Repositories group.electronics

Rev

Rev 155 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
139 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.Data;
7
 
156 pfowler 8
using Microsoft.FlightSimulator.SimConnect;
9
 
139 pfowler 10
using LibUsbDotNet;
11
using LibUsbDotNet.Main;
12
using LibUsbDotNet.DeviceNotify;
13
 
14
 
15
namespace NITNavComm {
16
 
17
    public class Device {
18
        public int vid { get; set; }
19
        public int pid { get; set; }
20
        public string type { get; set; }
21
 
22
        public Device(string type, int vid, int pid) {
23
            this.type = type;
24
            this.vid = vid;
25
            this.pid = pid;
26
        }
27
 
28
    }
29
 
156 pfowler 30
    public abstract class NITDevice : Device {
139 pfowler 31
        public int id { get; set; }
32
        public string hw { get; set; }
33
        public string sw { get; set; }
34
        public string status { get; set; }
35
        public string serial { get; set; }
155 pfowler 36
        public int assigned { get; set; }
139 pfowler 37
 
156 pfowler 38
        public FSXObject fsx { get; set; }
39
 
139 pfowler 40
        protected UsbDevice usbDevice;
41
        public UsbRegistry usbRegistry { get; set; }
42
 
43
        public NITDevice(UsbRegistry usbRegistry, string type, int vid, int pid) : base(type, vid, pid) {
44
            this.id = -1;
45
            this.hw = "";
46
            this.sw = "";
47
            this.serial = "";
48
            this.status = "";
49
            this.usbRegistry = usbRegistry;
50
 
51
        }
52
 
53
        public bool Open() {
54
            if (this.usbRegistry == null || !this.usbRegistry.IsAlive)
55
                return false;
56
 
57
            if (this.usbDevice == null)
58
                this.usbRegistry.Open(out this.usbDevice);
59
            else
60
                this.usbDevice.Open();
61
 
62
            return true;
63
        }
64
 
156 pfowler 65
        public virtual bool Close() {
139 pfowler 66
            if (this.usbDevice == null)
67
                return false;
68
 
69
            if (this.usbDevice.IsOpen)
70
                this.usbDevice.Close();
71
 
72
            return true;
73
        }
74
 
75
        public bool isOpen() {
76
            if (this.usbDevice == null || ! usbDevice.IsOpen)
77
                return false;
155 pfowler 78
 
139 pfowler 79
 
80
            return true;
81
        }
82
 
83
        public bool SendCommand(byte command, short value, short index) {
84
            int transferred;
85
            byte[] buffer = new byte[8];
86
            return SendCommand(command, value, index, buffer, out transferred);
87
        }
88
 
89
        public bool SendCommand(byte command, short value, short index, object buffer, out int transferred) {
90
            if (!this.isOpen())
91
                throw new Exception("USB Device " + this.type + "(" + this.serial + ") not open/connected");
92
 
93
            UsbSetupPacket packet = new UsbSetupPacket((byte)UsbRequestType.TypeVendor 
94
                | (byte)UsbEndpointDirection.EndpointIn, command, value, index, 0);
95
 
96
            return this.usbDevice.ControlTransfer(ref packet, buffer, 8, out transferred);
97
        }
98
 
153 pfowler 99
        public bool GetDeviceVersion() {
100
            byte[] data = new byte[8];
101
            int transferred;
102
            bool success = this.SendCommand(01, 0, 0, data, out transferred);
103
            this.hw = data[0].ToString();
104
            this.sw = data[1].ToString();
105
            return success;
106
        }
107
 
156 pfowler 108
        // Virtual Functions
109
        abstract public void MapEvents(FSXObject fsx);
110
        abstract public void FsxEvent(FSXObject fsx, SIMCONNECT_RECV_SIMOBJECT_DATA data);
111
        abstract public void FsxReady(FSXObject fsx);
112
        abstract public void SimButtons(FSXObject fsx);
113
 
139 pfowler 114
    }
115
 
116
    class NITPanels {
117
 
156 pfowler 118
        public static int CFG_BUTTON_DEBOUNCE_TIME = 500;
119
 
139 pfowler 120
        public List<NITDevice> devices { get; set; }
121
 
122
        public NITPanels() {
123
            devices = new List<NITDevice>();
124
        }
125
 
126
        public int UsbScan() {
127
 
128
            List<Device> regDevices = new List<Device>();
155 pfowler 129
            regDevices.Add(new Device("NITNavComm", 0x20a0, 0x4236));
130
            regDevices.Add(new Device("NITAudioSel", 0x4242, 0xe231));
139 pfowler 131
            regDevices.Add(new Device("NITAdf", 0x20a0, 0x4238));
132
 
133
            int idCount = 0;
134
            devices = new List<NITDevice>();
135
 
136
            UsbRegDeviceList allDevices = UsbDevice.AllLibUsbDevices;
137
            foreach (UsbRegistry usbReg in allDevices) {
138
                foreach (Device device in regDevices) {
139
                    if (usbReg.Vid == device.vid 
140
                        && usbReg.Pid == device.pid) {
153 pfowler 141
                            NITDevice found = null;
142
                            switch (device.type) {
155 pfowler 143
                                case ("NITNavComm"): {
153 pfowler 144
                                    found = new NITNavCommDevice(usbReg, device.type, device.vid, device.pid);
145
                                    break;
146
                                }
155 pfowler 147
                                case ("NITAudioSel"): {
156 pfowler 148
                                    found = new NITAudioSelDevice(usbReg, device.type, device.vid, device.pid);
155 pfowler 149
                                    break;
150
                                }
153 pfowler 151
                            }
152
 
139 pfowler 153
                            found.id = idCount;
154
                            found.status = "Found";
155
 
156
                            found.usbRegistry = usbReg;
157
                            UsbDevice dev; 
158
                            if (usbReg.Open(out dev)) {
153 pfowler 159
                                found.status = "Available";
139 pfowler 160
                                dev.Close();
161
                            }
162
 
153 pfowler 163
                            if (found.Open()) {
164
                                found.status = "Connected";
165
                                found.GetDeviceVersion();
166
                            }  else
167
                                found.status = "Failed";
168
 
139 pfowler 169
                            idCount++;
170
                            devices.Add(found);
171
                    }
172
                }
173
            }
174
 
175
            return idCount;
176
 
177
        }
178
 
156 pfowler 179
        public static bool readBit(byte data, int bitNum) {
180
            return (data & (1 << bitNum)) != 0;
181
        }
182
 
183
        public static byte setBit(byte data, int bitNum) {
184
            return (byte)(data | (1 << bitNum));
185
        }
186
 
187
        public static byte clearBit(byte data, int bitNum) {
188
            return (byte)(data & ~(1 << bitNum));
189
        }
190
 
139 pfowler 191
    }
192
}