コード例 #1
0
ファイル: HID.cs プロジェクト: sam210723/moRFctrl
        /// <summary>
        /// Begin communication with moRFeus
        /// </summary>
        private void ConfigDevice()
        {
            // Report stream opened
            if (moRFeusDevice.TryOpen(out moRFeusStream))
            {
                moRFeusStream.ReadTimeout = Timeout.Infinite;

                // HIDSharp setup
                reportBuffer = new byte[moRFeusDevice.GetMaxInputReportLength()];
                ReportDescriptor reportDescriptor = moRFeusDevice.GetReportDescriptor();
                reportReceiver = reportDescriptor.CreateHidDeviceInputReceiver();

                // Receive event handling
                //reportReceiver.Received += ReportReceived;
                reportReceiver.Start(moRFeusStream);

                // Disconnect event handling
                moRFeusStream.Closed += new EventHandler(Disconnected);

                // Indicate successful connection
                Tools.Debug("Connected to moRFeus");
                Program.MainClass.StatusMessage = "Connected to moRFeus";
                Program.MainClass.EnableUI();
                moRFeusOpen = true;

                // Initial polling of device values
                Task.Delay(10).ContinueWith(t => Program.MainClass.PollDevice());
            }
        }
コード例 #2
0
ファイル: HID.cs プロジェクト: N5FPP/moRFctrl
        /// <summary>
        /// Begin communication with moRFeus
        /// </summary>
        private void ConfigDevice()
        {
            HidStream hidStream;

            if (moRFeusDevice.TryOpen(out hidStream))
            {
                moRFeusStream = hidStream;
                Console.WriteLine("Connected to moRFeus");
                Program.MainClass.StatusMessage = "Connected to moRFeus";
                moRFeusStream.ReadTimeout       = Timeout.Infinite;


                byte[]           inputReportBuffer = new byte[moRFeusDevice.GetMaxInputReportLength()];
                ReportDescriptor reportDescriptor  = moRFeusDevice.GetReportDescriptor();
                HidSharp.Reports.Input.HidDeviceInputReceiver inputReceiver = reportDescriptor.CreateHidDeviceInputReceiver();

                inputReceiver.Received += (sender, e) =>
                {
                    Report report;
                    while (inputReceiver.TryRead(inputReportBuffer, 0, out report))
                    {
                        moRFeus.ParseReport(inputReportBuffer);
                    }
                };
                inputReceiver.Start(hidStream);

                moRFeusStream.Closed += new EventHandler(Disconnected);
                moRFeusOpen           = true;

                // Initial polling of device values
                Task.Delay(10).ContinueWith(t => Program.MainClass.PollDevice());
            }
        }
コード例 #3
0
 public void Start()
 {
     if (_digitizer.TryOpen(out _hidStream))
     {
         _hidDeviceInputReceiver           = _reportDescr.CreateHidDeviceInputReceiver();
         _hiddeviceInputParser             = _reportDescr.DeviceItems[0].CreateDeviceItemInputParser();
         _hidDeviceInputReceiver.Received += OnDigitizerInputReceived;
         _hidDeviceInputReceiver.Start(_hidStream);
     }
     else
     {
         throw new Exception("Failed to open iBridge HID digitizer");
     }
 }
コード例 #4
0
            public override void CommOpen(Bsl430NetDevice device)
            {
                try
                {
                    Bsl430NetDevice _device = device;

                    if (device == null)
                    {
                        _device = DefaultDevice;
                    }

                    if (_device == null || _device.Name == "")
                    {
                        throw new Bsl430NetException(461);
                    }

                    if (!devices.ContainsKey(_device.Name.ToLower()))
                    {
                        Status stat = Scan <USB_HID_Device>(out _);
                        if (!stat.OK)
                        {
                            throw new Bsl430NetException(stat.Error);
                        }
                    }

                    if (!devices.TryGetValue(_device.Name.ToLower(), out USB_HID_Device dev))
                    {
                        throw new Bsl430NetException(462);
                    }

                    DeviceList.Local.GetHidDeviceOrNull(dev.Vid,
                                                        dev.Pid)?.TryOpen(out usb);

                    if (usb == null)
                    {
                        throw new Bsl430NetException(610);
                    }
                    else
                    {
                        max_output_len   = usb.Device.GetMaxOutputReportLength();
                        max_input_len    = usb.Device.GetMaxInputReportLength();
                        usb.ReadTimeout  = Timeout.Infinite;
                        reportDescriptor = usb.Device.GetReportDescriptor();
                        inputReceiver    = reportDescriptor.CreateHidDeviceInputReceiver();
                        inputReceiver.Start(usb);
                    }
                }
                catch (Exception ex) { throw new Bsl430NetException(611, ex); }
            }
コード例 #5
0
 public RawInputDevice(HidDevice device, ReportDescriptor reportDescriptor, DeviceItem deviceItem, HidStream hidStream, string uniqueId)
 {
     this.device       = device;
     inputReportBuffer = new byte[device.GetMaxInputReportLength()];
     inputReceiver     = reportDescriptor.CreateHidDeviceInputReceiver();
     inputParser       = deviceItem.CreateDeviceItemInputParser();
     inputReceiver.Start(hidStream);
     DisplayName           = device.GetProductName();
     UniqueId              = uniqueId;
     InterfacePath         = device.DevicePath;
     HardwareID            = IdHelper.GetHardwareId(InterfacePath);
     inputChangedEventArgs = new DeviceInputChangedEventArgs(this);
     sources = reportDescriptor.InputReports.SelectMany(ir => ir.DataItems)
               .SelectMany(di => di.Usages.GetAllValues())
               .Select(u => (Usage)u)
               .SelectMany(u => RawInputSource.FromUsage(this, u))
               .ToArray();
     readThreadContext = ThreadCreator.CreateLoop($"{DisplayName} RawInput reader", ReadLoop, 1).Start();
 }
コード例 #6
0
        private void HidHandler_DoWork(object sender, DoWorkEventArgs e)
        {
            Console.Write("Opening device...");

            ReportDescriptor reportDescriptor = selectedDevice.GetReportDescriptor();

            foreach (DeviceItem deviceItem in reportDescriptor.DeviceItems)
            {
                HidStream hidStream;
                if (selectedDevice.TryOpen(out hidStream))
                {
                    Console.WriteLine("OK");
                    hidStream.ReadTimeout = Timeout.Infinite;

                    using (hidStream) {
                        var inputReportBuffer = new byte[selectedDevice.GetMaxInputReportLength()];
                        var inputReceiver     = reportDescriptor.CreateHidDeviceInputReceiver();
                        var inputParser       = deviceItem.CreateDeviceItemInputParser();

                        inputReceiver.Start(hidStream);

                        while (!HidHandler.CancellationPending)
                        {
                            if (inputReceiver.WaitHandle.WaitOne(1))
                            {
                                if (!inputReceiver.IsRunning)
                                {
                                    Console.WriteLine("Device disconnected!");
                                    break;
                                }

                                Report report;
                                byte[] buffer = new byte[255];
                                while (inputReceiver.TryRead(inputReportBuffer, 0, out report))
                                {
                                    if (inputParser.TryParseReport(inputReportBuffer, 0, report))
                                    {
                                        AirFrame af = new AirFrame(inputReportBuffer);

                                        string address               = BitConverter.ToString(af.address).Replace("-", ":");
                                        string pid                   = af.pid.ToString();
                                        string ch                    = af.rf_channel.ToString();
                                        string len                   = af.payloadLength.ToString();
                                        string payload               = af.payloadLength != 0 ? BitConverter.ToString(af.payload) : "<EMPTY>";
                                        string decrpytedPayload      = "";
                                        byte[] decryptedPayloadBytes = null;

                                        try {
                                            if (autoDecrypt)
                                            {
                                                af.DecryptKeyboardFrame(deviceKey);
                                                decryptedPayloadBytes = af.decryptedPayload;
                                                decrpytedPayload      = BitConverter.ToString(decryptedPayloadBytes);
                                            }
                                        } catch {
                                            decrpytedPayload = "";
                                        }

                                        frameList.Add(af);

                                        try {
                                            if (autoDecrypt && decryptedPayloadBytes != null)
                                            {
                                                KeyboardLayout kl = new KeyboardLayout();

                                                textBoxKeys.Invoke(new Action(() => {
                                                    textBoxKeys.Text += kl.ToComboKeyPress(decryptedPayloadBytes);
                                                }));
                                            }
                                        } catch {
                                        }


                                        listView1.Invoke(new Action(() => {
                                            listView1.Items.Insert(0, new ListViewItem(new[] { address, pid, ch, len, payload, decrpytedPayload }));
                                        }));
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    Console.WriteLine("Fail");
                }
            }
        }
コード例 #7
0
        // ==========================================================================================
        public static void PollReports()
        {
            ReportDescriptor reportDescriptor = device.GetReportDescriptor();

            foreach (DeviceItem deviceItem in reportDescriptor.DeviceItems)
            {
                if (device.TryOpen(out HidStream hidStream))
                {
                    hidStream.ReadTimeout = Timeout.Infinite;

                    using (hidStream)
                    {
                        byte[] inputReportBuffer = new byte[device.GetMaxInputReportLength()];
                        HidSharp.Reports.Input.HidDeviceInputReceiver inputReceiver = reportDescriptor.CreateHidDeviceInputReceiver();
                        HidSharp.Reports.Input.DeviceItemInputParser  inputParser   = deviceItem.CreateDeviceItemInputParser();
                        inputReceiver.Start(hidStream);

                        int startTime = Environment.TickCount;
                        while (true)
                        {
                            if (!inputReceiver.IsRunning)
                            {
                                break;
                            }              // Disconnected?

                            Report report; // Periodically check if the receiver has any reports.
                            while (inputReceiver.TryRead(inputReportBuffer, 0, out report))
                            {
                                bool first = true;
                                // Parse the report if possible.
                                // This will return false if (for example) the report applies to a different DeviceItem.
                                if (inputParser.TryParseReport(inputReportBuffer, 0, report))
                                {
                                    while (inputParser.HasChanged || first)
                                    {
                                        first = false;
                                        int changedIndex = inputParser.GetNextChangedIndex();
                                        try
                                        {
                                            DataValue previousDataValue = inputParser.GetPreviousValue(changedIndex);

                                            DataValue dataValue = inputParser.GetValue(changedIndex);
                                            string    Type      = ((Usage)dataValue.Usages.FirstOrDefault()).ToString();
                                            status    _status   = new status((double)previousDataValue.GetPhysicalValue(),
                                                                             (double)dataValue.GetPhysicalValue());

                                            lock (EventBufferLock)
                                            {
                                                if (Type.Contains("Button"))
                                                {
                                                    int btn = Convert.ToByte(Type.Last()) - 49;
                                                    if (_status.newval == 1)
                                                    {
                                                        CurrentButtons = (byte)(CurrentButtons | (1 << btn));
                                                        lastbtn        = btn;
                                                    }
                                                    else
                                                    {
                                                        CurrentButtons = (byte)(CurrentButtons & ~(1 << btn));
                                                    }
                                                }
                                                try
                                                {
                                                    AxisAndButtons[Type] = _status;
                                                }
                                                catch (Exception)
                                                {
                                                    AxisAndButtons.Add(Type, _status);
                                                }
                                            }
                                        }
                                        catch (Exception)
                                        { }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }