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

View File

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

View File

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

View File

@ -10,8 +10,9 @@ import (
func initTestJoystickDriver() *JoystickDriver {
a := NewJoystickAdaptor("bot")
a.connect = func(j *JoystickAdaptor) {
a.connect = func(j *JoystickAdaptor) (err error) {
j.joystick = &testJoystick{}
return nil
}
a.Connect()
d := NewJoystickDriver(a, "bot", "./configs/xbox360_power_a_mini_proex.json")
@ -21,17 +22,6 @@ func initTestJoystickDriver() *JoystickDriver {
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) {
d := initTestJoystickDriver()
d.SetInterval(1 * time.Millisecond)
@ -47,6 +37,7 @@ func TestJoystickDriverHalt(t *testing.T) {
func TestJoystickDriverHandleEvent(t *testing.T) {
sem := make(chan bool)
d := initTestJoystickDriver()
d.Start()
d.handleEvent(&sdl.JoyAxisEvent{
Which: 0,
Axis: 0,