1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-05-02 22:17:12 +08:00

Update joystick package error handling

This commit is contained in:
Adrian Zankich 2014-11-19 15:45:59 -08:00
parent 1666a85810
commit acbc388378
4 changed files with 51 additions and 53 deletions

View File

@ -1,6 +1,7 @@
package joystick package joystick
import ( import (
"errors"
"github.com/hybridgroup/go-sdl2/sdl" "github.com/hybridgroup/go-sdl2/sdl"
"github.com/hybridgroup/gobot" "github.com/hybridgroup/gobot"
) )
@ -15,7 +16,7 @@ type joystick interface {
type JoystickAdaptor struct { type JoystickAdaptor struct {
gobot.Adaptor gobot.Adaptor
joystick joystick joystick joystick
connect func(*JoystickAdaptor) connect func(*JoystickAdaptor) (err error)
} }
// NewJoysctickAdaptor creates a new adaptor with specified name. // NewJoysctickAdaptor creates a new adaptor with specified name.
@ -27,21 +28,20 @@ func NewJoystickAdaptor(name string) *JoystickAdaptor {
name, name,
"JoystickAdaptor", "JoystickAdaptor",
), ),
connect: func(j *JoystickAdaptor) { connect: func(j *JoystickAdaptor) (err error) {
sdl.Init(sdl.INIT_JOYSTICK) sdl.Init(sdl.INIT_JOYSTICK)
if sdl.NumJoysticks() > 0 { if sdl.NumJoysticks() > 0 {
j.joystick = sdl.JoystickOpen(0) j.joystick = sdl.JoystickOpen(0)
} else { return
panic("No joystick available")
} }
return errors.New("No joystick available")
}, },
} }
} }
// Connect returns true if connection to device is succesfull // Connect returns true if connection to device is succesfull
func (j *JoystickAdaptor) Connect() error { func (j *JoystickAdaptor) Connect() error {
j.connect(j) return j.connect(j)
return nil
} }
// Finalize closes connection to device // Finalize closes connection to device

View File

@ -8,8 +8,9 @@ import (
func initTestJoystickAdaptor() *JoystickAdaptor { func initTestJoystickAdaptor() *JoystickAdaptor {
a := NewJoystickAdaptor("bot") a := NewJoystickAdaptor("bot")
a.connect = func(j *JoystickAdaptor) { a.connect = func(j *JoystickAdaptor) (err error) {
j.joystick = &testJoystick{} j.joystick = &testJoystick{}
return nil
} }
return a return a
} }

View File

@ -5,6 +5,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"time"
"github.com/hybridgroup/go-sdl2/sdl" "github.com/hybridgroup/go-sdl2/sdl"
"github.com/hybridgroup/gobot" "github.com/hybridgroup/gobot"
@ -14,8 +15,9 @@ var _ gobot.DriverInterface = (*JoystickDriver)(nil)
type JoystickDriver struct { type JoystickDriver struct {
gobot.Driver gobot.Driver
config joystickConfig configPath string
poll func() sdl.Event config joystickConfig
poll func() sdl.Event
} }
// pair is a JSON representation of name and id // pair is a JSON representation of name and id
@ -52,28 +54,13 @@ func NewJoystickDriver(a *JoystickAdaptor, name string, config string) *Joystick
"JoystickDriver", "JoystickDriver",
a, a,
), ),
configPath: config,
poll: func() sdl.Event { poll: func() sdl.Event {
return sdl.PollEvent() return sdl.PollEvent()
}, },
} }
file, e := ioutil.ReadFile(config) d.AddEvent("error")
if e != nil {
panic(fmt.Sprintf("File error: %v\n", e))
}
var jsontype joystickConfig
json.Unmarshal(file, &jsontype)
d.config = jsontype
for _, value := range d.config.Buttons {
d.AddEvent(fmt.Sprintf("%s_press", value.Name))
d.AddEvent(fmt.Sprintf("%s_release", value.Name))
}
for _, value := range d.config.Axis {
d.AddEvent(value.Name)
}
for _, value := range d.config.Hats {
d.AddEvent(value.Name)
}
return d return d
} }
@ -83,13 +70,38 @@ func (j *JoystickDriver) adaptor() *JoystickAdaptor {
} }
// Start initiallizes event polling with defined interval // Start initiallizes event polling with defined interval
func (j *JoystickDriver) Start() error { func (j *JoystickDriver) Start() (err error) {
gobot.Every(j.Interval(), func() { file, err := ioutil.ReadFile(j.configPath)
event := j.poll() if err != nil {
if event != nil { return err
j.handleEvent(event) }
var jsontype joystickConfig
json.Unmarshal(file, &jsontype)
j.config = jsontype
for _, value := range j.config.Buttons {
j.AddEvent(fmt.Sprintf("%s_press", value.Name))
j.AddEvent(fmt.Sprintf("%s_release", value.Name))
}
for _, value := range j.config.Axis {
j.AddEvent(value.Name)
}
for _, value := range j.config.Hats {
j.AddEvent(value.Name)
}
go func() {
for {
event := j.poll()
if event != nil {
if err = j.handleEvent(event); err != nil {
gobot.Publish(j.Event("error"), err)
}
}
<-time.After(j.Interval())
} }
}) }()
return nil return nil
} }
@ -100,9 +112,7 @@ func (j *JoystickDriver) handleEvent(event sdl.Event) error {
if data.Which == j.adaptor().joystick.InstanceID() { if data.Which == j.adaptor().joystick.InstanceID() {
axis := j.findName(data.Axis, j.config.Axis) axis := j.findName(data.Axis, j.config.Axis)
if axis == "" { if axis == "" {
e := errors.New(fmt.Sprintf("Unknown Axis: %v", data.Axis)) return errors.New(fmt.Sprintf("Unknown Axis: %v", data.Axis))
fmt.Println(e.Error())
return e
} else { } else {
gobot.Publish(j.Event(axis), data.Value) gobot.Publish(j.Event(axis), data.Value)
} }
@ -111,9 +121,7 @@ func (j *JoystickDriver) handleEvent(event sdl.Event) error {
if data.Which == j.adaptor().joystick.InstanceID() { if data.Which == j.adaptor().joystick.InstanceID() {
button := j.findName(data.Button, j.config.Buttons) button := j.findName(data.Button, j.config.Buttons)
if button == "" { if button == "" {
e := errors.New(fmt.Sprintf("Unknown Button: %v", data.Button)) return errors.New(fmt.Sprintf("Unknown Button: %v", data.Button))
fmt.Println(e.Error())
return e
} else { } else {
if data.State == 1 { if data.State == 1 {
gobot.Publish(j.Event(fmt.Sprintf("%s_press", button)), nil) gobot.Publish(j.Event(fmt.Sprintf("%s_press", button)), nil)
@ -126,9 +134,7 @@ func (j *JoystickDriver) handleEvent(event sdl.Event) error {
if data.Which == j.adaptor().joystick.InstanceID() { if data.Which == j.adaptor().joystick.InstanceID() {
hat := j.findHatName(data.Value, data.Hat, j.config.Hats) hat := j.findHatName(data.Value, data.Hat, j.config.Hats)
if hat == "" { if hat == "" {
e := errors.New(fmt.Sprintf("Unknown Hat: %v %v", data.Hat, data.Value)) return errors.New(fmt.Sprintf("Unknown Hat: %v %v", data.Hat, data.Value))
fmt.Println(e.Error())
return e
} else { } else {
gobot.Publish(j.Event(hat), true) gobot.Publish(j.Event(hat), true)
} }

View File

@ -10,8 +10,9 @@ import (
func initTestJoystickDriver() *JoystickDriver { func initTestJoystickDriver() *JoystickDriver {
a := NewJoystickAdaptor("bot") a := NewJoystickAdaptor("bot")
a.connect = func(j *JoystickAdaptor) { a.connect = func(j *JoystickAdaptor) (err error) {
j.joystick = &testJoystick{} j.joystick = &testJoystick{}
return nil
} }
a.Connect() a.Connect()
d := NewJoystickDriver(a, "bot", "./configs/xbox360_power_a_mini_proex.json") d := NewJoystickDriver(a, "bot", "./configs/xbox360_power_a_mini_proex.json")
@ -21,17 +22,6 @@ func initTestJoystickDriver() *JoystickDriver {
return d return d
} }
func TestJoystickDriver(t *testing.T) {
defer func() {
r := recover()
if r != nil {
gobot.Assert(t, "File error: open ./fake_config.json: no such file or directory\n", r)
} else {
t.Errorf("Did not return Unknown Event error")
}
}()
NewJoystickDriver(NewJoystickAdaptor("bot"), "bot", "./fake_config.json")
}
func TestJoystickDriverStart(t *testing.T) { func TestJoystickDriverStart(t *testing.T) {
d := initTestJoystickDriver() d := initTestJoystickDriver()
d.SetInterval(1 * time.Millisecond) d.SetInterval(1 * time.Millisecond)
@ -47,6 +37,7 @@ func TestJoystickDriverHalt(t *testing.T) {
func TestJoystickDriverHandleEvent(t *testing.T) { func TestJoystickDriverHandleEvent(t *testing.T) {
sem := make(chan bool) sem := make(chan bool)
d := initTestJoystickDriver() d := initTestJoystickDriver()
d.Start()
d.handleEvent(&sdl.JoyAxisEvent{ d.handleEvent(&sdl.JoyAxisEvent{
Which: 0, Which: 0,
Axis: 0, Axis: 0,