Subversion Repositories group.electronics

Rev

Rev 156 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 156 Rev 157
Line 12... Line 12...
12
using LibUsbDotNet.DeviceNotify;
12
using LibUsbDotNet.DeviceNotify;
13
 
13
 
14
 
14
 
15
namespace NITNavComm {
15
namespace NITNavComm {
16
 
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
 
-
 
30
    public abstract class NITDevice : Device {
-
 
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; }
-
 
36
        public int assigned { get; set; }
-
 
37
 
-
 
38
        public FSXObject fsx { get; set; }
-
 
39
 
-
 
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
 
-
 
65
        public virtual bool Close() {
-
 
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;
-
 
78
            
-
 
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
 
-
 
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
 
-
 
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
 
-
 
114
    }
-
 
115
 
-
 
116
    class NITPanels {
17
    public class NITPanels {
117
 
18
 
118
        public static int CFG_BUTTON_DEBOUNCE_TIME = 500;
19
        public static int CFG_BUTTON_DEBOUNCE_TIME = 500;
119
 
20
 
120
        public List<NITDevice> devices { get; set; }
21
        public List<NITDevice> devices { get; set; }
121
 
22
 
Line 134... Line 35...
134
            devices = new List<NITDevice>();
35
            devices = new List<NITDevice>();
135
 
36
 
136
            UsbRegDeviceList allDevices = UsbDevice.AllLibUsbDevices;
37
            UsbRegDeviceList allDevices = UsbDevice.AllLibUsbDevices;
137
            foreach (UsbRegistry usbReg in allDevices) {
38
            foreach (UsbRegistry usbReg in allDevices) {
138
                foreach (Device device in regDevices) {
39
                foreach (Device device in regDevices) {
139
                    if (usbReg.Vid == device.vid 
-
 
140
                        && usbReg.Pid == device.pid) {
40
                    if (usbReg.Vid == device.vid && usbReg.Pid == device.pid) {
-
 
41
 
141
                            NITDevice found = null;
42
                            NITDevice found = null;
142
                            switch (device.type) {
43
                            switch (device.type) {
143
                                case ("NITNavComm"): {
44
                                case ("NITNavComm"): {
144
                                    found = new NITNavCommDevice(usbReg, device.type, device.vid, device.pid);
45
                                    found = new NITNavCommDevice(usbReg, device.type, device.vid, device.pid);
145
                                    break;
46
                                    break;
146
                                }
47
                                }
147
                                case ("NITAudioSel"): {
48
                                case ("NITAudioSel"): {
148
                                    found = new NITAudioSelDevice(usbReg, device.type, device.vid, device.pid);
49
                                    found = new NITAudioSelDevice(usbReg, device.type, device.vid, device.pid);
149
                                    break;
50
                                    break;
150
                                }
51
                                }
-
 
52
                                default: continue;
151
                            }
53
                            }
152
 
54
 
-
 
55
                            found.assigned = this.getNextDeviceAssignNumber(found);
153
                            found.id = idCount;
56
                            found.id = idCount;
154
                            found.status = "Found";
57
                            found.status = "Found";
155
                            
58
                            
156
                            found.usbRegistry = usbReg;
59
                            found.usbRegistry = usbReg;
157
                            UsbDevice dev; 
60
                            UsbDevice dev; 
Line 174... Line 77...
174
 
77
 
175
            return idCount;
78
            return idCount;
176
 
79
 
177
        }
80
        }
178
 
81
 
-
 
82
        /*
-
 
83
         * Incrementally add the assignment number for the newest found device
-
 
84
         *  This should only happen once, so it can be changed later on
-
 
85
         *  @TODO: Remember last assignment, and save it along side the serial number
-
 
86
         *
-
 
87
         */
-
 
88
        private int getNextDeviceAssignNumber(NITDevice newDevice) {
-
 
89
            int counter = 1;
-
 
90
            foreach (NITDevice device in this.devices) {
-
 
91
                if (device.type == newDevice.type) {
-
 
92
                    if (device.assigned > 0)
-
 
93
                        counter++;
-
 
94
                }
-
 
95
            }
-
 
96
            return counter;
-
 
97
        }
-
 
98
 
179
        public static bool readBit(byte data, int bitNum) {
99
        public static bool readBit(byte data, int bitNum) {
180
            return (data & (1 << bitNum)) != 0;
100
            return (data & (1 << bitNum)) != 0;
181
        }
101
        }
182
 
102
 
183
        public static byte setBit(byte data, int bitNum) {
103
        public static byte setBit(byte data, int bitNum) {