示例#1
0
        private bool CheckDeadzone(HidNumericControlDescription desc, HidNumericControl control)
        {
            var range            = desc.LogicalMaximum - desc.LogicalMinimum;
            var absoluteDeadzone = (int)(DEADZONE * range);
            var center           = NumericCenters[desc.UsagePage];
            var testValue        = Math.Abs(center - control.Value);

            if (desc.UsagePage == 0x01 && desc.UsageId == 0x39)
            {
                // dpad
                return((control.Value % 2 == 0) || !this.SetupMode);
            }
            return(testValue > absoluteDeadzone);
        }
示例#2
0
 private void ProcessInputForMap(HidInputReport report, HIDButtonMap buttonMap, ref ButtonStates newState)
 {
     foreach (var mappingItem in buttonMap)
     {
         HIDMapping mapping = mappingItem.Value;
         if (mapping.IsNumeric)
         {
             HidNumericControl control = report.GetNumericControl(mapping.UsagePage, mapping.UsageId);
             ushort            value   = (ushort)control.Value;
             if (!this.CheckDeadzone(control.ControlDescription, control))
             {
                 continue;
             }
             if (mapping.UsagePage == 0x01 && mapping.UsageId == 0x39)
             {
                 // dpad
                 if ((mapping.Direction == DPadDirection.Down && value >= 3 && value <= 5) ||
                     (mapping.Direction == DPadDirection.Left && value >= 5 && value <= 7) ||
                     (mapping.Direction == DPadDirection.Up && (value == 7 || value == 0 || value == 1)) ||
                     (mapping.Direction == DPadDirection.Right && value >= 1 && value <= 3)
                     )
                 {
                     this.setButtonState(mappingItem.Key, ref newState);
                 }
             }
             else
             {
                 // axis
                 ushort center = NumericCenters[mapping.UsagePage];
                 if ((value < center && mapping.Sign < 0) ||
                     (value > center && mapping.Sign > 0))
                 {
                     this.setButtonState(mappingItem.Key, ref newState);
                 }
             }
         }
         else
         {
             HidBooleanControl control = report.GetBooleanControl(mapping.UsagePage, mapping.UsageId);
             if (control.IsActive)
             {
                 this.setButtonState(mappingItem.Key, ref newState);
             }
         }
     }
 }
        private void Device_InputReportReceived(HidDevice sender, HidInputReportReceivedEventArgs args)
        {
            // Retrieve the sensor data
            HidInputReport inputReport = args.Report;
            IBuffer        buffer      = inputReport.Data;
            DataReader     dr          = DataReader.FromBuffer(buffer);

            byte[] bytes = new byte[inputReport.Data.Length];
            dr.ReadBytes(bytes);

            if (bytes.Length <= 0)
            {
                return;
            }

            lock (this)
            {
                // Clear the control states
                OnUpdate();

                // Report button controls
                foreach (var button in args.Report.ActivatedBooleanControls)
                {
                    ReportControl((int)button.Id, 1f);
                }

                // Report numeric controls
                int[] specialControls = GetNumericControls(gamepadType);
                for (int i = 0; i < specialControls.Length; ++i)
                {
                    HidNumericControl ctrl = args.Report.GetNumericControl(1, (ushort)specialControls[i]);
                    if (ctrl != null)
                    {
                        ReportControl(specialControls[i], ctrl.Value);
                    }
                }
            }
        }