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