Rev 155 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Data;using Microsoft.FlightSimulator.SimConnect;using LibUsbDotNet;using LibUsbDotNet.Main;using LibUsbDotNet.DeviceNotify;namespace NITNavComm {public class Device {public int vid { get; set; }public int pid { get; set; }public string type { get; set; }public Device(string type, int vid, int pid) {this.type = type;this.vid = vid;this.pid = pid;}}public abstract class NITDevice : Device {public int id { get; set; }public string hw { get; set; }public string sw { get; set; }public string status { get; set; }public string serial { get; set; }public int assigned { get; set; }public FSXObject fsx { get; set; }protected UsbDevice usbDevice;public UsbRegistry usbRegistry { get; set; }public NITDevice(UsbRegistry usbRegistry, string type, int vid, int pid) : base(type, vid, pid) {this.id = -1;this.hw = "";this.sw = "";this.serial = "";this.status = "";this.usbRegistry = usbRegistry;}public bool Open() {if (this.usbRegistry == null || !this.usbRegistry.IsAlive)return false;if (this.usbDevice == null)this.usbRegistry.Open(out this.usbDevice);elsethis.usbDevice.Open();return true;}public virtual bool Close() {if (this.usbDevice == null)return false;if (this.usbDevice.IsOpen)this.usbDevice.Close();return true;}public bool isOpen() {if (this.usbDevice == null || ! usbDevice.IsOpen)return false;return true;}public bool SendCommand(byte command, short value, short index) {int transferred;byte[] buffer = new byte[8];return SendCommand(command, value, index, buffer, out transferred);}public bool SendCommand(byte command, short value, short index, object buffer, out int transferred) {if (!this.isOpen())throw new Exception("USB Device " + this.type + "(" + this.serial + ") not open/connected");UsbSetupPacket packet = new UsbSetupPacket((byte)UsbRequestType.TypeVendor| (byte)UsbEndpointDirection.EndpointIn, command, value, index, 0);return this.usbDevice.ControlTransfer(ref packet, buffer, 8, out transferred);}public bool GetDeviceVersion() {byte[] data = new byte[8];int transferred;bool success = this.SendCommand(01, 0, 0, data, out transferred);this.hw = data[0].ToString();this.sw = data[1].ToString();return success;}// Virtual Functionsabstract public void MapEvents(FSXObject fsx);abstract public void FsxEvent(FSXObject fsx, SIMCONNECT_RECV_SIMOBJECT_DATA data);abstract public void FsxReady(FSXObject fsx);abstract public void SimButtons(FSXObject fsx);}class NITPanels {public static int CFG_BUTTON_DEBOUNCE_TIME = 500;public List<NITDevice> devices { get; set; }public NITPanels() {devices = new List<NITDevice>();}public int UsbScan() {List<Device> regDevices = new List<Device>();regDevices.Add(new Device("NITNavComm", 0x20a0, 0x4236));regDevices.Add(new Device("NITAudioSel", 0x4242, 0xe231));regDevices.Add(new Device("NITAdf", 0x20a0, 0x4238));int idCount = 0;devices = new List<NITDevice>();UsbRegDeviceList allDevices = UsbDevice.AllLibUsbDevices;foreach (UsbRegistry usbReg in allDevices) {foreach (Device device in regDevices) {if (usbReg.Vid == device.vid&& usbReg.Pid == device.pid) {NITDevice found = null;switch (device.type) {case ("NITNavComm"): {found = new NITNavCommDevice(usbReg, device.type, device.vid, device.pid);break;}case ("NITAudioSel"): {found = new NITAudioSelDevice(usbReg, device.type, device.vid, device.pid);break;}}found.id = idCount;found.status = "Found";found.usbRegistry = usbReg;UsbDevice dev;if (usbReg.Open(out dev)) {found.status = "Available";dev.Close();}if (found.Open()) {found.status = "Connected";found.GetDeviceVersion();} elsefound.status = "Failed";idCount++;devices.Add(found);}}}return idCount;}public static bool readBit(byte data, int bitNum) {return (data & (1 << bitNum)) != 0;}public static byte setBit(byte data, int bitNum) {return (byte)(data | (1 << bitNum));}public static byte clearBit(byte data, int bitNum) {return (byte)(data & ~(1 << bitNum));}}}