hybridgroup.gobot/platforms/joystick/joystick_adaptor.go

55 lines
1.0 KiB
Go
Raw Permalink Normal View History

2014-04-28 09:02:39 +08:00
package joystick
import (
2014-11-20 07:45:59 +08:00
"errors"
"gobot.io/x/gobot"
"github.com/veandco/go-sdl2/sdl"
)
2014-07-23 04:55:19 +08:00
type joystick interface {
Close()
InstanceID() sdl.JoystickID
}
// Adaptor represents a connection to a joystick
type Adaptor struct {
name string
2014-07-23 04:55:19 +08:00
joystick joystick
connect func(*Adaptor) (err error)
}
// NewAdaptor returns a new Joystick Adaptor.
func NewAdaptor() *Adaptor {
return &Adaptor{
name: gobot.DefaultName("Joystick"),
connect: func(j *Adaptor) (err error) {
2014-04-28 09:02:39 +08:00
sdl.Init(sdl.INIT_JOYSTICK)
if sdl.NumJoysticks() > 0 {
j.joystick = sdl.JoystickOpen(0)
2014-11-20 07:45:59 +08:00
return
2014-04-28 09:02:39 +08:00
}
2014-11-20 07:45:59 +08:00
return errors.New("No joystick available")
2014-04-28 09:02:39 +08:00
},
}
}
// Name returns the Adaptors name
func (j *Adaptor) Name() string { return j.name }
// SetName sets the Adaptors name
func (j *Adaptor) SetName(n string) { j.name = n }
// Connect connects to the joystick
func (j *Adaptor) Connect() (err error) {
err = j.connect(j)
return
}
// Finalize closes connection to joystick
func (j *Adaptor) Finalize() (err error) {
2014-04-28 09:02:39 +08:00
j.joystick.Close()
return
}