Bugs in SDL Joysticks?


ZXDunny

Deep avatar
Joined
Oct 12, 2010
Messages
2,585
I've tested this with the old SDL and with Notaz's new SDL.


If only one nub is set as a joystick, it appears as joystick 1 regardless of which nub it is. This confused me for a while :)


If you change nub mode from say, mouse to absolute while your SDL program is running, it cannot pick up the new joystick. Even closing the joystick subsystem and re-initialising fails. If the nub is set to absolute before the program is run, then changed to absolute (by a /proc/ write) - ie, no change in function - then re-initialising works. re-initialising fails only if the nub was not configured as absolute prior to the app starting.


Edit: If you change from absolute to mouse, and then back again while the app is running, then the joystick is lost.


Anyone any ideas about how these issues can be solved?


D.
 
Last edited by a moderator:
Do you realy need to have these nubs as mouse ? touchscreen isnt enough ?

Well, the point is that as the firmware provides this capability, PandaBAS should give it to the user too. And thanks to the efforts of those in the #openpandora channel on freenode.net, I've got it licked :)


The bugs were pretty much in my code and in my assumption as to how SDL works :)


D.
 
The bugs were pretty much in my code and in my assumption as to how SDL works :)


D.

is it now working as expected and if so could you tell us what it needs to get it right?


i'm just asking to not have another thread with "hey thanks it's solved" without mentioning how it was done ;)
 
The bugs were pretty much in my code and in my assumption as to how SDL works :)

Is it now working as expected and if so could you tell us what it needs to get it right?


i'm just asking to not have another thread with "hey thanks it's solved" without mentioning how it was done ;)

Indeed it is!


The aim was to have a command which switched nub modes, and two functions that return the nub x/y axes:


SETNUB nubid,mode


Where NubID is 1 or 2 (left or right), and mode is 0 to 3 (mouse, stick, scroll, buttons respectively).


LET x=NUBX nubid : LET y=NUBY nubid


If the nubs queried are in stick mode, then the axis queried returns the position, else returns 0.


GETNUB nubid


Returns the state of the nub specified, 0 to 3.


So for SDL:


Use the usual SDL_Init, with the SDL_INIT_JOYSTICK option.


Create two pSDL_Joystick vars, which I called Nub1 and Nub2, initialised to nil pointer.


My InitJoysticks() proc is then called at startup and whenever SETNUB is executed.


1. If either Nub1 or Nub2 <> nil pointer, release them with SDL_JoystickClose(Nub1 or Nub2), and set them to Nil pointer.


2. To be certain, I call SDL_CloseSubSystem(SDL_INIT_JOYSTICK) and then SDL_InitSubSystem(SDL_JOYSTICK) to make sure the joysticks internal functions are reset, but this may not be necessary.


3. Query the number of joysticks with SDL_NumJoysticks. The pandora presents three joysticks to SDL. The first (index 0) is "gpio-keys" and I assume that means the Pandora button/start/select etc. The next two are the nubs, indexes 1 and 2. You need to be careful though - if the left nub is set to mouse and the right nub is set to stick then only one stick will be visible (nub 2) and that will occupy index 1 rather than index 2. So: If only two sticks are available, you need to query their names with SDL_JoystickName - The left nub identifies as "nub0" and the right as "nub1". Using this information, I assign the Nub1 or Nub2 variable to the correct Stick index. Of course, if SDL reports that there are 3 sticks available then both nubs are assigned to stick mode :)


4. Having done all that, the Nub1 and Nub2 pointers are either nil or pointing to a SDL_Joystick ID. You just need to call SDL_JoystickUpdate


That should be it - when querying the axis later in your code, just ensure that the joystick variable isn't nil beforehand, and all should be well :) We don't need to enumerate axes or buttons, as the pandora hardware is set in stone and won't vary between devices.


If anyone wants to add this info to the wiki or whatever, feel free.


D.
 
If the nub status have been manually changed, It's conceivable that both might be in joystick mode but the joystick numbers are reversed. Do you still check the nub name when both are found?.


- Neelix
 
If the nub status have been manually changed, It's conceivable that both might be in joystick mode but the joystick numbers are reversed. Do you still check the nub name when both are found?.

- Neelix

No, there's no need as SDL re-discovers joysticks whenever a SETNUB command is executed and when PandaBAS is started up which means that the sticks are discovered in the correct order. The only time that I've found it to be an issue is in step 3 above, when only one nub is in stick mode.


However, you've got a good point and I'll be testing to make sure :)


D.
 
thanks ZXDunny - i'm sure it's useful for some others how to switch nub modes during runtime! great work :)
 
Back
Top