Subversion Repositories group.NITPanels

Rev

Rev 5 | Go to most recent revision | Details | 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.Data;
7
 
8
using Microsoft.FlightSimulator.SimConnect;
9
 
10
using LibUsbDotNet;
11
using LibUsbDotNet.Main;
12
using LibUsbDotNet.DeviceNotify;
13
 
14
 
15
namespace NITNavComm {
16
 
17
    public class NITPanels {
18
 
19
        public static int CFG_BUTTON_DEBOUNCE_TIME = 500;
20
 
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>();
30
            regDevices.Add(new Device("NITNavComm", 0x20a0, 0x4236));
31
            regDevices.Add(new Device("NITAudioSel", 0x4242, 0xe231));
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) {
40
                    if (usbReg.Vid == device.vid && usbReg.Pid == device.pid) {
41
 
42
                            NITDevice found = null;
43
                            switch (device.type) {
44
                                case ("NITNavComm"): {
45
                                    found = new NITNavCommDevice(usbReg, device.type, device.vid, device.pid);
46
                                    break;
47
                                }
48
                                case ("NITAudioSel"): {
49
                                    found = new NITAudioSelDevice(usbReg, device.type, device.vid, device.pid);
50
                                    break;
51
                                }
52
                                default: continue;
53
                            }
54
 
55
                            found.assigned = this.getNextDeviceAssignNumber(found);
56
                            found.id = idCount;
57
                            found.status = "Found";
58
 
59
                            found.usbRegistry = usbReg;
60
                            UsbDevice dev; 
61
                            if (usbReg.Open(out dev)) {
62
                                found.status = "Available";
63
                                dev.Close();
64
                            }
65
 
66
                            if (found.Open()) {
67
                                found.status = "Connected";
68
                                found.GetDeviceVersion();
69
                            }  else
70
                                found.status = "Failed";
71
 
72
                            idCount++;
73
                            devices.Add(found);
74
                    }
75
                }
76
            }
77
 
78
            return idCount;
79
 
80
        }
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
 
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
 
111
    }
112
}