Subversion Repositories group.NITPanels

Rev

Rev 3 | 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
 
7
using Microsoft.FlightSimulator.SimConnect;
8
 
9
using LibUsbDotNet;
10
using LibUsbDotNet.Main;
11
using LibUsbDotNet.DeviceNotify;
12
 
13
namespace NITNavComm {
14
    public class Device {
15
        public int vid { get; set; }
16
        public int pid { get; set; }
17
        public string type { get; set; }
18
 
19
        public Device(string type, int vid, int pid) {
20
            this.type = type;
21
            this.vid = vid;
22
            this.pid = pid;
23
        }
24
 
25
    }
26
 
27
    public abstract class NITDevice : Device {
28
        public int id { get; set; }
29
        public string hw { get; set; }
30
        public string sw { get; set; }
31
        public string status { get; set; }
32
        public string serial { get; set; }
33
        public int assigned { get; set; }
34
 
35
        public FSXObject fsx { get; set; }
36
 
37
        protected UsbDevice usbDevice;
38
        public UsbRegistry usbRegistry { get; set; }
39
 
40
        public NITDevice(UsbRegistry usbRegistry, string type, int vid, int pid)
41
            : base(type, vid, pid) {
42
            this.id = -1;
43
            this.hw = "";
44
            this.sw = "";
45
            this.serial = "";
46
            this.status = "";
47
            this.assigned = 0;
48
            this.usbRegistry = usbRegistry;
49
 
50
        }
51
 
52
        public bool Open() {
53
            if (this.usbRegistry == null || !this.usbRegistry.IsAlive)
54
                return false;
55
 
56
            if (this.usbDevice == null)
57
                this.usbRegistry.Open(out this.usbDevice);
58
            else
59
                this.usbDevice.Open();
60
 
61
            return true;
62
        }
63
 
64
        public virtual bool Close() {
65
            if (this.usbDevice == null)
66
                return false;
67
 
68
            if (this.usbDevice.IsOpen)
69
                this.usbDevice.Close();
70
 
71
            return true;
72
        }
73
 
74
        public bool isOpen() {
75
            if (this.usbDevice == null || !usbDevice.IsOpen)
76
                return false;
77
 
78
 
79
            return true;
80
        }
81
 
82
        public bool SendCommand(byte command, short value, short index) {
83
            int transferred;
84
            byte[] buffer = new byte[8];
85
            return SendCommand(command, value, index, buffer, out transferred);
86
        }
87
 
88
        public bool SendCommand(byte command, short value, short index, object buffer, out int transferred) {
89
            if (!this.isOpen())
90
                throw new Exception("USB Device " + this.type + "(" + this.serial + ") not open/connected");
91
 
92
            UsbSetupPacket packet = new UsbSetupPacket((byte)UsbRequestType.TypeVendor
93
                | (byte)UsbEndpointDirection.EndpointIn, command, value, index, 0);
94
 
95
            return this.usbDevice.ControlTransfer(ref packet, buffer, 8, out transferred);
96
        }
97
 
98
        public bool GetDeviceVersion() {
99
            byte[] data = new byte[8];
100
            int transferred;
101
            bool success = this.SendCommand(01, 0, 0, data, out transferred);
102
            this.hw = data[0].ToString();
103
            this.sw = data[1].ToString();
104
            return success;
105
        }
106
 
107
        // Virtual Functions
108
        abstract public void MapEvents();
109
        abstract public void FsxEvent(SIMCONNECT_RECV_SIMOBJECT_DATA data);
110
        abstract public void FsxInit();
111
        abstract public void FsxReady();
112
        abstract public void SimButtons();
7 pfowler 113
        abstract public void reAssign();
3 pfowler 114
 
115
    }
116
}