Ejemplo n.º 1
0
        /// <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());
            }
        }
        // ==========================================================================================
        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)
                                        { }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }