I have been using the joystick library in wx.adv.Joystick for menu navigation in an emulator front-end I have which I believe is a wx widgets wrapper ?? as there isn’t anything within it that does any real work. Anyway, just invoking the below causes around 15% CPU usage until closed. I’m assuming whatever library this is calling is the problem.
joystick = wx.adv.Joystick(joystick=wx.JOYSTICK1)
This isn’t great at all and I’d assume a real problem for low spec machines
I cant use the alternate ‘inputs’ library as it has serious blocking issues - I did try to use that from a thread which is fine but it still blocks that thread from closing while it waits for a joystick event. Some people have tried to work on this issue and there is a ‘fix’ for WIN but not for LINUX and as my application is cross-platform I really need a better solution.
Has anyone come across this and / or have any suggestions
This does seem to be a Windows related issue. The demo in Windows is definitely pulling between 15 and 19% CPU load. Just creating a very basic wx window and adding the lines below is enough to cause the problem. Joystick is working just fine but the CPU load is very strange.
self.joystick = wx.adv.Joystick()
if self.joystick.IsOk():
self.joystick.SetCapture(self)
Does changing the value of the SetCapture() method’s pollingFreq argument have any effect?
Edit: Looking in the wxWidgets C++ source code, both the Unix version and the Windows version use a separate thread to read the joystick’s values in the background.
The Unix version (src/unix/joystick.cpp) uses the select() function to wait for changes to the joystick’s values. The polling frequency is used to set the select() function’s timeout parameter.
The Window’s version (src/msw/joystick.cpp) simply uses the polling frequency as the duration of a Sleep() function call in a while-loop. If the polling frequency is set to the default value of zero, it means the while-loop in the thread just keeps looping without pausing.
Yup, well done - that makes a massive difference. The other library I tried (inputs) does it in a pretty similar way and they also had an issue with CPU load until someone injected a 1ms sleep in the loop just to slow it down a tad. I never realised wx.adv.joystick had this built-in.