Subversion Repositories group.NITPanels

Rev

Rev 3 | 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 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.assigned = 0;
            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);
            else
                this.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 Functions
        abstract public void MapEvents();
        abstract public void FsxEvent(SIMCONNECT_RECV_SIMOBJECT_DATA data);
        abstract public void FsxInit();
        abstract public void FsxReady();
        abstract public void SimButtons();
        abstract public void reAssign();

    }
}