2014-04-27 18:02:39 -07:00
|
|
|
package joystick
|
2014-04-26 03:11:51 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2014-07-22 13:55:19 -07:00
|
|
|
"errors"
|
2014-04-26 03:11:51 -07:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2014-07-09 18:32:27 -07:00
|
|
|
|
|
|
|
"github.com/hybridgroup/go-sdl2/sdl"
|
|
|
|
"github.com/hybridgroup/gobot"
|
2014-04-26 03:11:51 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type JoystickDriver struct {
|
|
|
|
gobot.Driver
|
2014-06-15 17:22:50 -07:00
|
|
|
config joystickConfig
|
2014-07-22 13:55:19 -07:00
|
|
|
poll func() sdl.Event
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type pair struct {
|
|
|
|
Name string `json:"name"`
|
2014-06-10 15:16:11 -07:00
|
|
|
ID int `json:"id"`
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type hat struct {
|
|
|
|
Hat int `json:"hat"`
|
|
|
|
Name string `json:"name"`
|
2014-06-10 15:16:11 -07:00
|
|
|
ID int `json:"id"`
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type joystickConfig struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Guid string `json:"guid"`
|
|
|
|
Axis []pair `json:"axis"`
|
|
|
|
Buttons []pair `json:"buttons"`
|
|
|
|
Hats []hat `json:"Hats"`
|
|
|
|
}
|
|
|
|
|
2014-05-22 20:28:26 -07:00
|
|
|
func NewJoystickDriver(a *JoystickAdaptor, name string, config string) *JoystickDriver {
|
2014-04-27 18:02:39 -07:00
|
|
|
d := &JoystickDriver{
|
2014-07-07 17:12:45 -07:00
|
|
|
Driver: *gobot.NewDriver(
|
|
|
|
name,
|
|
|
|
"JoystickDriver",
|
|
|
|
a,
|
|
|
|
),
|
2014-07-22 13:55:19 -07:00
|
|
|
poll: func() sdl.Event {
|
|
|
|
return sdl.PollEvent()
|
|
|
|
},
|
2014-04-27 18:02:39 -07:00
|
|
|
}
|
2014-04-26 03:11:51 -07:00
|
|
|
|
2014-05-22 20:28:26 -07:00
|
|
|
file, e := ioutil.ReadFile(config)
|
2014-04-26 03:11:51 -07:00
|
|
|
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 {
|
2014-07-07 17:12:45 -07:00
|
|
|
d.AddEvent(fmt.Sprintf("%s_press", value.Name))
|
|
|
|
d.AddEvent(fmt.Sprintf("%s_release", value.Name))
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
|
|
|
for _, value := range d.config.Axis {
|
2014-07-07 17:12:45 -07:00
|
|
|
d.AddEvent(value.Name)
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
|
|
|
for _, value := range d.config.Hats {
|
2014-07-07 17:12:45 -07:00
|
|
|
d.AddEvent(value.Name)
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
2014-06-15 17:22:50 -07:00
|
|
|
func (j *JoystickDriver) adaptor() *JoystickAdaptor {
|
2014-07-09 18:32:27 -07:00
|
|
|
return j.Adaptor().(*JoystickAdaptor)
|
2014-06-15 17:22:50 -07:00
|
|
|
}
|
|
|
|
|
2014-04-27 18:02:39 -07:00
|
|
|
func (j *JoystickDriver) Start() bool {
|
2014-07-22 13:55:19 -07:00
|
|
|
gobot.Every(j.Interval(), func() {
|
|
|
|
event := j.poll()
|
|
|
|
if event != nil {
|
|
|
|
j.handleEvent(event)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *JoystickDriver) handleEvent(event sdl.Event) error {
|
|
|
|
switch data := event.(type) {
|
|
|
|
case *sdl.JoyAxisEvent:
|
|
|
|
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
|
|
|
|
} else {
|
|
|
|
gobot.Publish(j.Event(axis), data.Value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case *sdl.JoyButtonEvent:
|
|
|
|
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
|
|
|
|
} else {
|
|
|
|
if data.State == 1 {
|
|
|
|
gobot.Publish(j.Event(fmt.Sprintf("%s_press", button)), nil)
|
|
|
|
} else {
|
|
|
|
gobot.Publish(j.Event(fmt.Sprintf("%s_release", button)), nil)
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-07-22 13:55:19 -07:00
|
|
|
case *sdl.JoyHatEvent:
|
|
|
|
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
|
|
|
|
} else {
|
|
|
|
gobot.Publish(j.Event(hat), true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
2014-07-22 13:55:19 -07:00
|
|
|
|
2014-04-27 18:02:39 -07:00
|
|
|
func (j *JoystickDriver) Halt() bool { return true }
|
2014-04-26 03:11:51 -07:00
|
|
|
|
2014-04-27 18:02:39 -07:00
|
|
|
func (j *JoystickDriver) findName(id uint8, list []pair) string {
|
2014-04-26 03:11:51 -07:00
|
|
|
for _, value := range list {
|
2014-06-10 15:16:11 -07:00
|
|
|
if int(id) == value.ID {
|
2014-04-26 03:11:51 -07:00
|
|
|
return value.Name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2014-04-27 18:02:39 -07:00
|
|
|
func (j *JoystickDriver) findHatName(id uint8, hat uint8, list []hat) string {
|
2014-04-26 03:11:51 -07:00
|
|
|
for _, value := range list {
|
2014-06-10 15:16:11 -07:00
|
|
|
if int(id) == value.ID && int(hat) == value.Hat {
|
2014-04-26 03:11:51 -07:00
|
|
|
return value.Name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|