When polling my gamepad and it does nothing for a a minute or two the PoV control goes into some sort of sleep mode and returns nothing, but selected a button wakes it up. Is this normal and is there a way to keep the PoV from sleeping?
Ckeckbox activation....
private void CheckBoxJoystick_Checked(object sender, EventArgs e)
{
if (CheckboxJoystick.IsChecked.HasValue & CheckboxJoystick.IsChecked == true)
{
var windowHandle = Process.GetCurrentProcess().MainWindowHandle;
_gamepad = new Gamepad(windowHandle);
if (!_gamepad.IsAvailable) return;
ctsGamepad?.Cancel();
ctsGamepad = new CancellationTokenSource();
ThreadPool.QueueUserWorkItem(DoGamepadWork, ctsGamepad.Token);
}
}
Main loop to poll the gamepad...
private void DoGamepadWork(object obj)
{
if (!_gamepad.IsAvailable) return;
var token = (CancellationToken)obj;
var buttontocheck = -1;
var povtocheck = new PovPair(-1,0);
while (true)
{
if (token.IsCancellationRequested)
{
break;
}
_gamepad.Poll();
// Check buttons...
// Check PoVs...
Thread.Sleep(100);
}
}
Polling Gamepad....
public void Poll()
{
try
{
if (!IsAvailable) return;
joystick.Acquire();
joystick.Poll();
State = joystick.GetCurrentState();
Buttons = State.Buttons;
Povs = State.PointOfViewControllers;
Datas = joystick.GetBufferedData();
}
catch(Exception ex)
{ }
}
Finds attached Gamepads...
private void Find()
{
foreach (var deviceInstance in directInput.GetDevices(DeviceType.Gamepad, DeviceEnumerationFlags.AttachedOnly))
joystickGuid = deviceInstance.InstanceGuid;
// If Gamepad not found, look for a Joystick
if (joystickGuid == Guid.Empty)
foreach (var deviceInstance in directInput.GetDevices(DeviceType.Joystick, DeviceEnumerationFlags.AttachedOnly))
joystickGuid = deviceInstance.InstanceGuid;
// If Joystick not found
if (joystickGuid == Guid.Empty)
{
IsAvailable = false;
return;
}
// Instantiate the joystick
joystick = new Joystick(directInput, joystickGuid);
joystick.SetCooperativeLevel(hWnd, CooperativeLevel.Background | CooperativeLevel.NonExclusive);
// Set BufferSize in order to use buffered data.
joystick.Properties.BufferSize = 128;
// Acquire the joystick
joystick.Acquire();
IsAvailable = true;
Load_Settings();
}
its a functional example, i select the first stick... the button 0 stops the loop and i test the pov[0]
using SharpDX.DirectInput;
using System;
using System.Diagnostics;
using System.Threading.Tasks;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
var controller = new Controller();
}
}
public class Controller
{
private Task pollingTask;
private bool running;
private JoystickState state;
public JoystickState State => state ?? (state = controller.GetCurrentState());
public Joystick controller;
public Controller()
{
var directInput = new DirectInput();
var handle = Process.GetCurrentProcess().MainWindowHandle;
var diDevices = directInput.GetDevices(DeviceClass.GameControl, DeviceEnumerationFlags.AttachedOnly);
controller = new Joystick(directInput, diDevices[0].InstanceGuid);
controller.SetCooperativeLevel(handle, CooperativeLevel.Exclusive | CooperativeLevel.Background);
controller.Acquire();
running = true;
PollJoystick();
if (pollingTask != null)
{
pollingTask.Wait();
}
Console.WriteLine("fini");
Console.ReadKey();
}
TimeSpan period = TimeSpan.FromMilliseconds(30);
public int[] Pov => State.PointOfViewControllers;
public bool stop => State.Buttons[0];
public void PollJoystick()
{
pollingTask = Task.Factory.StartNew(() => {
while (running)
{
state = null;
running = !stop;
if (Pov[0] != -1)
Console.WriteLine(Pov[0]);
Task.Delay(period);
}
});
}
}
}