Subversion Repositories group.NITPanels

Rev

Rev 15 | 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;
15 pfowler 20
        public static int CFG_INPUT_TIMER_INTERVAL = 40;
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
22 pfowler 86
         *  TODO: Remember last assignment, and save it along side the serial numberTODO In list
3 pfowler 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
 
22 pfowler 111
        public static float map_f(uint x, uint in_min, uint in_max, float out_min, float out_max) {
112
            float var = (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
113
            return var;
114
        }
115
 
116
        public static uint map_ui(uint x, uint in_min, uint in_max, uint out_min, uint out_max) {
117
            uint var = (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
118
            return var;
119
        }
120
 
3 pfowler 121
    }
122
}