Subversion Repositories group.NITPanels

Rev

Rev 3 | Rev 11 | Go to most recent revision | Details | Compare with Previous | 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;
5 pfowler 20
        public static int CFG_INPUT_TIMER_INTERVAL = 5;
3 pfowler 21
 
22
        public List<NITDevice> devices { get; set; }
23
 
24
        public NITPanels() {
25
            devices = new List<NITDevice>();
26
        }
27
 
28
        public int UsbScan() {
29
 
30
            List<Device> regDevices = new List<Device>();
31
            regDevices.Add(new Device("NITNavComm", 0x20a0, 0x4236));
32
            regDevices.Add(new Device("NITAudioSel", 0x4242, 0xe231));
33
            regDevices.Add(new Device("NITAdf", 0x20a0, 0x4238));
34
 
35
            int idCount = 0;
36
            devices = new List<NITDevice>();
37
 
38
            UsbRegDeviceList allDevices = UsbDevice.AllLibUsbDevices;
39
            foreach (UsbRegistry usbReg in allDevices) {
40
                foreach (Device device in regDevices) {
41
                    if (usbReg.Vid == device.vid && usbReg.Pid == device.pid) {
42
 
43
                            NITDevice found = null;
44
                            switch (device.type) {
45
                                case ("NITNavComm"): {
46
                                    found = new NITNavCommDevice(usbReg, device.type, device.vid, device.pid);
47
                                    break;
48
                                }
49
                                case ("NITAudioSel"): {
50
                                    found = new NITAudioSelDevice(usbReg, device.type, device.vid, device.pid);
51
                                    break;
52
                                }
53
                                default: continue;
54
                            }
55
 
56
                            found.assigned = this.getNextDeviceAssignNumber(found);
57
                            found.id = idCount;
58
                            found.status = "Found";
59
 
60
                            found.usbRegistry = usbReg;
61
                            UsbDevice dev; 
62
                            if (usbReg.Open(out dev)) {
63
                                found.status = "Available";
64
                                dev.Close();
65
                            }
66
 
67
                            if (found.Open()) {
68
                                found.status = "Connected";
69
                                found.GetDeviceVersion();
70
                            }  else
71
                                found.status = "Failed";
72
 
73
                            idCount++;
74
                            devices.Add(found);
75
                    }
76
                }
77
            }
78
 
79
            return idCount;
80
 
81
        }
82
 
83
        /*
84
         * Incrementally add the assignment number for the newest found device
85
         *  This should only happen once, so it can be changed later on
86
         *  @TODO: Remember last assignment, and save it along side the serial number
87
         *
88
         */
89
        private int getNextDeviceAssignNumber(NITDevice newDevice) {
90
            int counter = 1;
91
            foreach (NITDevice device in this.devices) {
92
                if (device.type == newDevice.type) {
93
                    if (device.assigned > 0)
94
                        counter++;
95
                }
96
            }
97
            return counter;
98
        }
99
 
100
        public static bool readBit(byte data, int bitNum) {
101
            return (data & (1 << bitNum)) != 0;
102
        }
103
 
104
        public static byte setBit(byte data, int bitNum) {
105
            return (byte)(data | (1 << bitNum));
106
        }
107
 
108
        public static byte clearBit(byte data, int bitNum) {
109
            return (byte)(data & ~(1 << bitNum));
110
        }
111
 
112
    }
113
}