Subversion Repositories group.NITPanels

Rev

Rev 5 | 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 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;
                                }
                                default: continue;
                            }

                            found.assigned = this.getNextDeviceAssignNumber(found);
                            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();
                            }  else
                                found.status = "Failed";
                            
                            idCount++;
                            devices.Add(found);
                    }
                }
            }

            return idCount;

        }

        /*
         * Incrementally add the assignment number for the newest found device
         *  This should only happen once, so it can be changed later on
         *  @TODO: Remember last assignment, and save it along side the serial number
         *
         */
        private int getNextDeviceAssignNumber(NITDevice newDevice) {
            int counter = 1;
            foreach (NITDevice device in this.devices) {
                if (device.type == newDevice.type) {
                    if (device.assigned > 0)
                        counter++;
                }
            }
            return counter;
        }

        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));
        }

    }
}