Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Joystick needs a way to handle multiple joysticks #564

Open
deadprogram opened this issue Jun 10, 2018 · 4 comments
Open

Joystick needs a way to handle multiple joysticks #564

deadprogram opened this issue Jun 10, 2018 · 4 comments

Comments

@deadprogram
Copy link
Member

Right now, the code only connects to the first joystick i.e. joystick 0:

https://github.com/hybridgroup/gobot/blob/master/platforms/joystick/joystick_adaptor.go#L30

We need the ability to specify a particular joystick to handle case that more than one joystick is connected ta the same time.

@carl-ranson
Copy link

I did some tinkering on this today and managed to come up with the following joystick adaptor newAdaptor function. You pass in the joystick name like NewAdaptor("Magicsee R1").

hope that helps with resolving this ticket.

func NewAdaptor(targetName string) *Adaptor {
return &Adaptor{
name: gobot.DefaultName("Joystick"),
connect: func(j *Adaptor) (err error) {
sdl.Init(sdl.INIT_JOYSTICK)
foundIndex := -1
for i := 0; i<sdl.NumJoysticks(); i++ {
if strings.TrimSpace(targetName) == strings.TrimSpace(sdl.JoystickNameForIndex(i)) {
foundIndex = i
}
}
if foundIndex > -1 {
j.joystick = sdl.JoystickOpen(foundIndex)
return
}
return errors.New("No joystick available")
},
}
}

@theunkn0wn1
Copy link

Greetings, is there any progress on this?

I have a project that needs to be able to handle multiple joysticks, and this only appears to work when @carl-ranson 's patch is applied.

theunkn0wn1 added a commit to theunkn0wn1/gobot that referenced this issue Mar 6, 2020
 - based on hybridgroup#564 (comment)

Signed-off-by: joshua salzedo <thHunkn0WNd@gmail.com>
@gen2thomas
Copy link
Collaborator

there are two implementations available for reference:

@gen2thomas
Copy link
Collaborator

To earn the advantages of both implementations we could do the following:

  • use a string for the identifier
  • try to parse the number from string - if ok, try to use it -> (*)
  • otherwise try to find the number by name (use first finding)
  • optional: log a warning or return an error, if there are more than one devices with the same name

(*) If this fails, maybe the name is a number? If this can occur, try to find by name.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants