mirror of
https://github.com/hybridgroup/gobot.git
synced 2025-04-26 13:48:49 +08:00
Refactor joystick to use new driver and adaptor interfaces
This commit is contained in:
parent
cca91da5ed
commit
c1ce92f582
@ -2,11 +2,12 @@ package joystick
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/hybridgroup/go-sdl2/sdl"
|
||||
"github.com/hybridgroup/gobot"
|
||||
)
|
||||
|
||||
var _ gobot.AdaptorInterface = (*JoystickAdaptor)(nil)
|
||||
var _ gobot.Adaptor = (*JoystickAdaptor)(nil)
|
||||
|
||||
type joystick interface {
|
||||
Close()
|
||||
@ -14,7 +15,7 @@ type joystick interface {
|
||||
}
|
||||
|
||||
type JoystickAdaptor struct {
|
||||
gobot.Adaptor
|
||||
name string
|
||||
joystick joystick
|
||||
connect func(*JoystickAdaptor) (err error)
|
||||
}
|
||||
@ -24,10 +25,7 @@ type JoystickAdaptor struct {
|
||||
// or panics if no joystick can be found
|
||||
func NewJoystickAdaptor(name string) *JoystickAdaptor {
|
||||
return &JoystickAdaptor{
|
||||
Adaptor: *gobot.NewAdaptor(
|
||||
name,
|
||||
"JoystickAdaptor",
|
||||
),
|
||||
name: name,
|
||||
connect: func(j *JoystickAdaptor) (err error) {
|
||||
sdl.Init(sdl.INIT_JOYSTICK)
|
||||
if sdl.NumJoysticks() > 0 {
|
||||
@ -39,6 +37,8 @@ func NewJoystickAdaptor(name string) *JoystickAdaptor {
|
||||
}
|
||||
}
|
||||
|
||||
func (j *JoystickAdaptor) Name() string { return j.name }
|
||||
|
||||
// Connect returns true if connection to device is succesfull
|
||||
func (j *JoystickAdaptor) Connect() (errs []error) {
|
||||
if err := j.connect(j); err != nil {
|
||||
|
@ -11,10 +11,13 @@ import (
|
||||
"github.com/hybridgroup/gobot"
|
||||
)
|
||||
|
||||
var _ gobot.DriverInterface = (*JoystickDriver)(nil)
|
||||
var _ gobot.Driver = (*JoystickDriver)(nil)
|
||||
|
||||
type JoystickDriver struct {
|
||||
gobot.Driver
|
||||
name string
|
||||
interval time.Duration
|
||||
connection gobot.Connection
|
||||
gobot.Eventer
|
||||
configPath string
|
||||
config joystickConfig
|
||||
poll func() sdl.Event
|
||||
@ -49,11 +52,9 @@ type joystickConfig struct {
|
||||
// (button)_release - triggered when (button) is released
|
||||
func NewJoystickDriver(a *JoystickAdaptor, name string, config string) *JoystickDriver {
|
||||
d := &JoystickDriver{
|
||||
Driver: *gobot.NewDriver(
|
||||
name,
|
||||
"JoystickDriver",
|
||||
a,
|
||||
),
|
||||
name: name,
|
||||
connection: a,
|
||||
Eventer: gobot.NewEventer(),
|
||||
configPath: config,
|
||||
poll: func() sdl.Event {
|
||||
return sdl.PollEvent()
|
||||
@ -63,10 +64,12 @@ func NewJoystickDriver(a *JoystickAdaptor, name string, config string) *Joystick
|
||||
d.AddEvent("error")
|
||||
return d
|
||||
}
|
||||
func (j *JoystickDriver) Name() string { return j.name }
|
||||
func (j *JoystickDriver) Connection() gobot.Connection { return j.connection }
|
||||
|
||||
// adaptor returns joystick adaptor
|
||||
func (j *JoystickDriver) adaptor() *JoystickAdaptor {
|
||||
return j.Adaptor().(*JoystickAdaptor)
|
||||
return j.Connection().(*JoystickAdaptor)
|
||||
}
|
||||
|
||||
// Start initiallizes event polling with defined interval
|
||||
@ -99,7 +102,7 @@ func (j *JoystickDriver) Start() (errs []error) {
|
||||
gobot.Publish(j.Event("error"), err)
|
||||
}
|
||||
}
|
||||
<-time.After(j.Interval())
|
||||
<-time.After(j.interval)
|
||||
}
|
||||
}()
|
||||
return
|
||||
|
@ -24,7 +24,7 @@ func initTestJoystickDriver() *JoystickDriver {
|
||||
|
||||
func TestJoystickDriverStart(t *testing.T) {
|
||||
d := initTestJoystickDriver()
|
||||
d.SetInterval(1 * time.Millisecond)
|
||||
d.interval = 1 * time.Millisecond
|
||||
gobot.Assert(t, len(d.Start()), 0)
|
||||
<-time.After(2 * time.Millisecond)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user