1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-27 13:48:56 +08:00
hybridgroup.gobot/platforms/joystick/joystick_driver.go

132 lines
3.1 KiB
Go
Raw Normal View History

2014-04-27 18:02:39 -07:00
package joystick
import (
"encoding/json"
"fmt"
"github.com/hybridgroup/go-sdl2/sdl"
"github.com/hybridgroup/gobot"
"io/ioutil"
"time"
)
type JoystickDriver struct {
gobot.Driver
config joystickConfig
}
type pair struct {
Name string `json:"name"`
2014-06-10 15:16:11 -07:00
ID int `json:"id"`
}
type hat struct {
Hat int `json:"hat"`
Name string `json:"name"`
2014-06-10 15:16:11 -07:00
ID int `json:"id"`
}
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{
Driver: gobot.Driver{
Events: make(map[string]*gobot.Event),
Adaptor: a,
2014-04-27 18:02:39 -07:00
},
}
2014-05-22 20:28:26 -07:00
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 {
2014-06-11 11:37:20 -07:00
d.Events[fmt.Sprintf("%s_press", value.Name)] = gobot.NewEvent()
d.Events[fmt.Sprintf("%s_release", value.Name)] = gobot.NewEvent()
}
for _, value := range d.config.Axis {
2014-06-11 11:37:20 -07:00
d.Events[value.Name] = gobot.NewEvent()
}
for _, value := range d.config.Hats {
2014-06-11 11:37:20 -07:00
d.Events[value.Name] = gobot.NewEvent()
}
return d
}
func (j *JoystickDriver) adaptor() *JoystickAdaptor {
return j.Driver.Adaptor.(*JoystickAdaptor)
}
2014-04-27 18:02:39 -07:00
func (j *JoystickDriver) Start() bool {
go func() {
var event sdl.Event
for {
for event = sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
switch data := event.(type) {
case *sdl.JoyAxisEvent:
if data.Which == j.adaptor().joystick.InstanceID() {
2014-04-27 18:02:39 -07:00
axis := j.findName(data.Axis, j.config.Axis)
if axis == "" {
fmt.Println("Unknown Axis:", data.Axis)
} else {
2014-04-27 18:02:39 -07:00
gobot.Publish(j.Events[axis], data.Value)
}
}
case *sdl.JoyButtonEvent:
if data.Which == j.adaptor().joystick.InstanceID() {
2014-04-27 18:02:39 -07:00
button := j.findName(data.Button, j.config.Buttons)
if button == "" {
fmt.Println("Unknown Button:", data.Button)
} else {
if data.State == 1 {
2014-04-27 18:02:39 -07:00
gobot.Publish(j.Events[fmt.Sprintf("%s_press", button)], nil)
} else {
2014-04-27 18:02:39 -07:00
gobot.Publish(j.Events[fmt.Sprintf("%s_release", button)], nil)
}
}
}
case *sdl.JoyHatEvent:
if data.Which == j.adaptor().joystick.InstanceID() {
2014-04-27 18:02:39 -07:00
hat := j.findHatName(data.Value, data.Hat, j.config.Hats)
if hat == "" {
fmt.Println("Unknown Hat:", data.Hat, data.Value)
} else {
2014-04-27 18:02:39 -07:00
gobot.Publish(j.Events[hat], true)
}
}
}
}
time.Sleep(10 * time.Millisecond)
}
}()
return true
}
2014-04-27 18:02:39 -07:00
func (j *JoystickDriver) Init() bool { return true }
func (j *JoystickDriver) Halt() bool { return true }
2014-04-27 18:02:39 -07:00
func (j *JoystickDriver) findName(id uint8, list []pair) string {
for _, value := range list {
2014-06-10 15:16:11 -07:00
if int(id) == value.ID {
return value.Name
}
}
return ""
}
2014-04-27 18:02:39 -07:00
func (j *JoystickDriver) findHatName(id uint8, hat uint8, list []hat) string {
for _, value := range list {
2014-06-10 15:16:11 -07:00
if int(id) == value.ID && int(hat) == value.Hat {
return value.Name
}
}
return ""
}