コード例 #1
0
        private void ReadLoop()
        {
            if (!inputReceiver.IsRunning)
            {
                return;
            }

            Report report;
            Dictionary <Usage, DataValue> changedIndexes = new Dictionary <Usage, DataValue>();

            for (int i = 0; i < limit && inputReceiver.TryRead(inputReportBuffer, 0, out report); i++)
            {
                if (inputParser.TryParseReport(inputReportBuffer, 0, report))
                {
                    while (inputParser.HasChanged)
                    {
                        int changedIndex = inputParser.GetNextChangedIndex();
                        var dataValue    = inputParser.GetValue(changedIndex);
                        if (dataValue.Usages.Count() > 0)
                        {
                            changedIndexes[(Usage)dataValue.Usages.FirstOrDefault()] = dataValue;
                        }
                    }
                }
            }
            var changedSources = sources.Where(s => s.Refresh(changedIndexes)).ToArray();

            inputChangedEventArgs.Refresh(changedSources);
            if (inputChangedEventArgs.ChangedValues.Any())
            {
                InputChanged?.Invoke(this, inputChangedEventArgs);
            }
        }
コード例 #2
0
        private void OnDigitizerInputReceived(object sender, EventArgs e)
        {
            var inputReportBuffer = new byte[_digitizer.GetMaxInputReportLength()];

            while (_hidDeviceInputReceiver.TryRead(inputReportBuffer, 0, out Report report))
            {
                // Parse the report if possible.
                // This will return false if (for example) the report applies to a different DeviceItem.
                if (_hiddeviceInputParser.TryParseReport(inputReportBuffer, 0, report))
                {
                    BridgeFrameBufferPlatform.Threading.Send(() => ProcessEvent());
                }
            }
        }
コード例 #3
0
        private bool Parse(byte[] reportBuffer)
        {
            try {
                byte reportId = reportBuffer[0];
                if (!_reportCache.TryGetValue(reportId, out Report report))
                {
                    _reportCache[reportId] = report = HidDevice.GetReportDescriptor().GetReport(ReportType.Input, reportId);
                }

                // Parse the report if possible.
                if (_inputParser.TryParseReport(reportBuffer, 0, report))
                {
                    while (_inputParser.HasChanged)
                    {
                        int changedIndex = _inputParser.GetNextChangedIndex();
                        var dataValue    = _inputParser.GetValue(changedIndex);

                        Usage usage = (Usage)dataValue.Usages.FirstOrDefault();
                        if (Usage.Button1 <= usage && usage <= Usage.Button31)
                        {
                            int btnIdx = (int)(usage - (int)Usage.Button1);
                            _buttons.EnsureSize(btnIdx + 1);
                            _buttons[btnIdx] = dataValue.GetLogicalValue() != 0;
                            int val = dataValue.GetLogicalValue();
                        }
                        else if (usage == Usage.GenericDesktopHatSwitch)
                        {
                            // can only support 1 hat..
                            _hats.EnsureSize(1);
                            _hats[0] = ExtractHat(dataValue);
                        }
                        else if (Usage.GenericDesktopX <= usage && usage <= Usage.GenericDesktopRz)
                        {
                            int axisIdx = (int)(usage - (int)Usage.GenericDesktopX);
                            _axes.EnsureSize(axisIdx + 1);
                            _axes[axisIdx] = ScaleAxis(dataValue);
                        }
                        else
                        {
                            // unrecognized usage
                            Debug.WriteLine("Unrecognized usage: " + usage);
                        }
                    }

                    // for skinning simplicity sake, map hats also to buttons
                    int btn = _buttons.Count - 4 * _hats.Count;
                    for (int i = 0; i < _hats.Count; i++)
                    {
                        // UP DOWN LEFT RIGHT
                        _buttons[btn++] = _hats[i].HasFlag(Hat.Up);
                        _buttons[btn++] = _hats[i].HasFlag(Hat.Down);
                        _buttons[btn++] = _hats[i].HasFlag(Hat.Left);
                        _buttons[btn++] = _hats[i].HasFlag(Hat.Right);
                    }

                    return(true);
                }
            }
            catch { }
            return(false);
        }