M
Major_Major
Guest
I have a WPF dotnetCore3.1 Desktop application. In part, it uses a joystick to control an RS232 serial device. When a joystick is plugged in, the user can select it from a dropdown, then the software begins polling it, averaging it, and sending controls to the serial device. This use System.Windows.Gaming.Input.RawGameController
Selecting from the dropdown causes this initializer
private void UpdateController()
{
if (selectedController != null)
{
buttonStates = new bool[selectedController.ButtonCount];
axisPositions = new double[selectedController.AxisCount];
switchpositions = new GameControllerSwitchPosition[selectedController.SwitchCount];
selectedController.GetCurrentReading(buttonStates, switchpositions, axisPositions);
}
else
{
buttonStates = Array.Empty<bool>();
axisPositions = Array.Empty<double>();
switchpositions = Array.Empty<GameControllerSwitchPosition>();
}
}
The polling looks like this
private async void ControlAsync()
{
// If axiscount < 2 then don't bother
if (selectedController != null && selectedController.AxisCount > 1)
{
selectedController.GetCurrentReading(buttonStates, switchpositions, axisPositions);
controlBuffer[0] += axisPositions[0];
controlBuffer[1] += axisPositions[1];
// Debug.WriteLine($"Controller: {axisPositions[0]}, {axisPositions[1]}");
}
// 0.5 is the neutral position
else
{
controlBuffer[0] += 0.5;
controlBuffer[1] += 0.5;
}
if (++controlCounter == controlCount)
{
controlCounter = 0;
PositionerController.PanAxisPositon = controlBuffer[0] / controlCount;
PositionerController.TiltAxisPositon = controlBuffer[1] / controlCount;
controlBuffer[0] = 0;
controlBuffer[1] = 0;
await PositionerController.Control().ConfigureAwait(true);
}
}
The problem is many joysticks come up reporting (0.0, 0.0) (full down left), regardless of where they are, while the neutral position is (0.5, 0.5). This will not update correctly unless and until the joystick is moved, which means that the serial device, a physical device, will take off on the user.
I have not been able to come up with a reliable way to fix this. I had some catches that sort of worked, e.g., locking the input until something other than 0,0 is seen, but it seems like there should be a cleaner / more standard way to do this.
Continue reading...
Selecting from the dropdown causes this initializer
private void UpdateController()
{
if (selectedController != null)
{
buttonStates = new bool[selectedController.ButtonCount];
axisPositions = new double[selectedController.AxisCount];
switchpositions = new GameControllerSwitchPosition[selectedController.SwitchCount];
selectedController.GetCurrentReading(buttonStates, switchpositions, axisPositions);
}
else
{
buttonStates = Array.Empty<bool>();
axisPositions = Array.Empty<double>();
switchpositions = Array.Empty<GameControllerSwitchPosition>();
}
}
The polling looks like this
private async void ControlAsync()
{
// If axiscount < 2 then don't bother
if (selectedController != null && selectedController.AxisCount > 1)
{
selectedController.GetCurrentReading(buttonStates, switchpositions, axisPositions);
controlBuffer[0] += axisPositions[0];
controlBuffer[1] += axisPositions[1];
// Debug.WriteLine($"Controller: {axisPositions[0]}, {axisPositions[1]}");
}
// 0.5 is the neutral position
else
{
controlBuffer[0] += 0.5;
controlBuffer[1] += 0.5;
}
if (++controlCounter == controlCount)
{
controlCounter = 0;
PositionerController.PanAxisPositon = controlBuffer[0] / controlCount;
PositionerController.TiltAxisPositon = controlBuffer[1] / controlCount;
controlBuffer[0] = 0;
controlBuffer[1] = 0;
await PositionerController.Control().ConfigureAwait(true);
}
}
The problem is many joysticks come up reporting (0.0, 0.0) (full down left), regardless of where they are, while the neutral position is (0.5, 0.5). This will not update correctly unless and until the joystick is moved, which means that the serial device, a physical device, will take off on the user.
I have not been able to come up with a reliable way to fix this. I had some catches that sort of worked, e.g., locking the input until something other than 0,0 is seen, but it seems like there should be a cleaner / more standard way to do this.
Continue reading...