Subversion Repositories group.electronics

Rev

Rev 156 | 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
 
157 pfowler 17
    public class NITPanels {
139 pfowler 18
 
156 pfowler 19
        public static int CFG_BUTTON_DEBOUNCE_TIME = 500;
20
 
139 pfowler 21
        public List<NITDevice> devices { get; set; }
22
 
23
        public NITPanels() {
24
            devices = new List<NITDevice>();
25
        }
26
 
27
        public int UsbScan() {
28
 
29
            List<Device> regDevices = new List<Device>();
155 pfowler 30
            regDevices.Add(new Device("NITNavComm", 0x20a0, 0x4236));
31
            regDevices.Add(new Device("NITAudioSel", 0x4242, 0xe231));
139 pfowler 32
            regDevices.Add(new Device("NITAdf", 0x20a0, 0x4238));
33
 
34
            int idCount = 0;
35
            devices = new List<NITDevice>();
36
 
37
            UsbRegDeviceList allDevices = UsbDevice.AllLibUsbDevices;
38
            foreach (UsbRegistry usbReg in allDevices) {
39
                foreach (Device device in regDevices) {
157 pfowler 40
                    if (usbReg.Vid == device.vid && usbReg.Pid == device.pid) {
41
 
153 pfowler 42
                            NITDevice found = null;
43
                            switch (device.type) {
155 pfowler 44
                                case ("NITNavComm"): {
153 pfowler 45
                                    found = new NITNavCommDevice(usbReg, device.type, device.vid, device.pid);
46
                                    break;
47
                                }
155 pfowler 48
                                case ("NITAudioSel"): {
156 pfowler 49
                                    found = new NITAudioSelDevice(usbReg, device.type, device.vid, device.pid);
155 pfowler 50
                                    break;
51
                                }
157 pfowler 52
                                default: continue;
153 pfowler 53
                            }
54
 
157 pfowler 55
                            found.assigned = this.getNextDeviceAssignNumber(found);
139 pfowler 56
                            found.id = idCount;
57
                            found.status = "Found";
58
 
59
                            found.usbRegistry = usbReg;
60
                            UsbDevice dev; 
61
                            if (usbReg.Open(out dev)) {
153 pfowler 62
                                found.status = "Available";
139 pfowler 63
                                dev.Close();
64
                            }
65
 
153 pfowler 66
                            if (found.Open()) {
67
                                found.status = "Connected";
68
                                found.GetDeviceVersion();
69
                            }  else
70
                                found.status = "Failed";
71
 
139 pfowler 72
                            idCount++;
73
                            devices.Add(found);
74
                    }
75
                }
76
            }
77
 
78
            return idCount;
79
 
80
        }
81
 
157 pfowler 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
 
156 pfowler 99
        public static bool readBit(byte data, int bitNum) {
100
            return (data & (1 << bitNum)) != 0;
101
        }
102
 
103
        public static byte setBit(byte data, int bitNum) {
104
            return (byte)(data | (1 << bitNum));
105
        }
106
 
107
        public static byte clearBit(byte data, int bitNum) {
108
            return (byte)(data & ~(1 << bitNum));
109
        }
110
 
139 pfowler 111
    }
112
}