diff --git a/platforms/joystick/doc.go b/platforms/joystick/doc.go index a8997956..810a0034 100644 --- a/platforms/joystick/doc.go +++ b/platforms/joystick/doc.go @@ -3,9 +3,7 @@ Package joystick provides the Gobot adaptor and drivers for game controllers tha Installing: - This package requires `sdl2` to be installed on your system - - Please refer to the main [README.md](https://github.com/hybridgroup/gobot/blob/release/README.md) + Please refer to the main [README.md](https://github.com/hybridgroup/gobot/blob/release/README.md) Example: @@ -20,9 +18,7 @@ Example: func main() { joystickAdaptor := joystick.NewAdaptor(0) - joystick := joystick.NewDriver(joystickAdaptor, - "./platforms/joystick/configs/dualshock3.json", - ) + joystick := joystick.NewDriver(joystickAdaptor, "dualshock3") work := func() { joystick.On(joystick.Event("square_press"), func(data interface{}) { diff --git a/platforms/joystick/joystick_adaptor.go b/platforms/joystick/joystick_adaptor.go index 743588d0..0015f101 100644 --- a/platforms/joystick/joystick_adaptor.go +++ b/platforms/joystick/joystick_adaptor.go @@ -8,15 +8,10 @@ import ( js "github.com/0xcafed00d/joystick" ) -type joystick interface { - Close() - ID() int -} - // Adaptor represents a connection to a joystick type Adaptor struct { name string - id int + id int joystick js.Joystick connect func(*Adaptor) error } diff --git a/platforms/joystick/joystick_driver.go b/platforms/joystick/joystick_driver.go index da0e9672..224d37f9 100644 --- a/platforms/joystick/joystick_driver.go +++ b/platforms/joystick/joystick_driver.go @@ -6,8 +6,8 @@ import ( "os" "time" - "gobot.io/x/gobot/v2" js "github.com/0xcafed00d/joystick" + "gobot.io/x/gobot/v2" ) const ( @@ -41,15 +41,15 @@ const ( // Driver represents a joystick type Driver struct { - name string - interval time.Duration - connection gobot.Connection - configPath string - config joystickConfig + name string + interval time.Duration + connection gobot.Connection + configPath string + config joystickConfig buttonState map[int]bool - axisState map[int]int + axisState map[int]int - halt chan bool + halt chan bool gobot.Eventer } @@ -76,12 +76,12 @@ type joystickConfig struct { // time.Duration: Interval at which the Driver is polled for new information func NewDriver(a *Adaptor, config string, v ...time.Duration) *Driver { d := &Driver{ - name: gobot.DefaultName("Joystick"), - connection: a, - Eventer: gobot.NewEventer(), - configPath: config, + name: gobot.DefaultName("Joystick"), + connection: a, + Eventer: gobot.NewEventer(), + configPath: config, buttonState: make(map[int]bool), - axisState: make(map[int]int), + axisState: make(map[int]int), interval: 10 * time.Millisecond, halt: make(chan bool), @@ -161,8 +161,16 @@ func (j *Driver) Start() (err error) { j.Publish(j.Event("error"), err) break } - j.handleButtons(state) - j.handleAxes(state) + + // might just be missing a button definition, so keep going + if err := j.handleButtons(state); err != nil { + j.Publish(j.Event("error"), err) + } + + // might just be missing an axis definition, so keep going + if err := j.handleAxes(state); err != nil { + j.Publish(j.Event("error"), err) + } select { case <-time.After(j.interval): diff --git a/platforms/joystick/test_helper.go b/platforms/joystick/test_helper.go index f0ad6fdc..4fa17da1 100644 --- a/platforms/joystick/test_helper.go +++ b/platforms/joystick/test_helper.go @@ -4,9 +4,9 @@ import js "github.com/0xcafed00d/joystick" type testJoystick struct{} -func (t *testJoystick) Close() {} -func (t *testJoystick) ID() int { return 0 } -func (t *testJoystick) ButtonCount() int { return 0 } -func (t *testJoystick) AxisCount() int { return 0 } -func (t *testJoystick) Name() string { return "test-joy" } +func (t *testJoystick) Close() {} +func (t *testJoystick) ID() int { return 0 } +func (t *testJoystick) ButtonCount() int { return 0 } +func (t *testJoystick) AxisCount() int { return 0 } +func (t *testJoystick) Name() string { return "test-joy" } func (t *testJoystick) Read() (js.State, error) { return js.State{}, nil }