1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-26 13:48:49 +08:00

Update joystick package

This commit is contained in:
Adrian Zankich 2014-04-27 18:02:39 -07:00
parent 35437065f6
commit 96cb031261
9 changed files with 174 additions and 180 deletions

53
examples/joystick_ps3.go Normal file
View File

@ -0,0 +1,53 @@
package main
import (
"fmt"
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/joystick"
)
func main() {
joystickAdaptor := joystick.NewJoystickAdaptor()
joystickAdaptor.Name = "ps3"
joystickAdaptor.Params = map[string]interface{}{
"config": "../joystick/configs/dualshock3.json",
}
joystickDriver := joystick.NewJoystickDriver(joystickAdaptor)
joystickDriver.Name = "ps3"
work := func() {
gobot.On(joystickDriver.Events["square_press"], func(data interface{}) {
fmt.Println("square_press")
})
gobot.On(joystickDriver.Events["square_release"], func(data interface{}) {
fmt.Println("square_release")
})
gobot.On(joystickDriver.Events["triangle_press"], func(data interface{}) {
fmt.Println("triangle_press")
})
gobot.On(joystickDriver.Events["triangle_release"], func(data interface{}) {
fmt.Println("triangle_release")
})
gobot.On(joystickDriver.Events["left_x"], func(data interface{}) {
fmt.Println("left_x", data)
})
gobot.On(joystickDriver.Events["left_y"], func(data interface{}) {
fmt.Println("left_y", data)
})
gobot.On(joystickDriver.Events["right_x"], func(data interface{}) {
fmt.Println("right_x", data)
})
gobot.On(joystickDriver.Events["right_y"], func(data interface{}) {
fmt.Println("right_y", data)
})
}
robot := gobot.Robot{
Connections: []gobot.Connection{joystickAdaptor},
Devices: []gobot.Device{joystickDriver},
Work: work,
}
robot.Start()
}

View File

@ -0,0 +1,65 @@
package main
import (
"fmt"
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/joystick"
)
func main() {
joystickAdaptor := joystick.NewJoystickAdaptor()
joystickAdaptor.Name = "xbox360"
joystickAdaptor.Params = map[string]interface{}{
"config": "../joystick/configs/xbox360_power_a_mini_proex.json",
}
joystickDriver := joystick.NewJoystickDriver(joystickAdaptor)
joystickDriver.Name = "xbox360"
work := func() {
gobot.On(joystickDriver.Events["a_press"], func(data interface{}) {
fmt.Println("a_press")
})
gobot.On(joystickDriver.Events["a_release"], func(data interface{}) {
fmt.Println("a_release")
})
gobot.On(joystickDriver.Events["b_press"], func(data interface{}) {
fmt.Println("b_press")
})
gobot.On(joystickDriver.Events["b_release"], func(data interface{}) {
fmt.Println("b_release")
})
gobot.On(joystickDriver.Events["up"], func(data interface{}) {
fmt.Println("up", data)
})
gobot.On(joystickDriver.Events["down"], func(data interface{}) {
fmt.Println("down", data)
})
gobot.On(joystickDriver.Events["left"], func(data interface{}) {
fmt.Println("left", data)
})
gobot.On(joystickDriver.Events["right"], func(data interface{}) {
fmt.Println("right", data)
})
gobot.On(joystickDriver.Events["left_x"], func(data interface{}) {
fmt.Println("left_x", data)
})
gobot.On(joystickDriver.Events["left_y"], func(data interface{}) {
fmt.Println("left_y", data)
})
gobot.On(joystickDriver.Events["right_x"], func(data interface{}) {
fmt.Println("right_x", data)
})
gobot.On(joystickDriver.Events["right_y"], func(data interface{}) {
fmt.Println("right_y", data)
})
}
robot := gobot.Robot{
Connections: []gobot.Connection{joystickAdaptor},
Devices: []gobot.Device{joystickDriver},
Work: work,
}
robot.Start()
}

View File

@ -1,53 +0,0 @@
package main
import (
"fmt"
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot-joystick"
)
func main() {
joystickAdaptor := new(gobotJoystick.JoystickAdaptor)
joystickAdaptor.Name = "ps3"
joystickAdaptor.Params = map[string]interface{}{
"config": "./configs/dualshock3.json",
}
joystick := gobotJoystick.NewJoystick(joystickAdaptor)
joystick.Name = "ps3"
work := func() {
gobot.On(joystick.Events["square_press"], func(data interface{}) {
fmt.Println("square_press")
})
gobot.On(joystick.Events["square_release"], func(data interface{}) {
fmt.Println("square_release")
})
gobot.On(joystick.Events["triangle_press"], func(data interface{}) {
fmt.Println("triangle_press")
})
gobot.On(joystick.Events["triangle_release"], func(data interface{}) {
fmt.Println("triangle_release")
})
gobot.On(joystick.Events["left_x"], func(data interface{}) {
fmt.Println("left_x", data)
})
gobot.On(joystick.Events["left_y"], func(data interface{}) {
fmt.Println("left_y", data)
})
gobot.On(joystick.Events["right_x"], func(data interface{}) {
fmt.Println("right_x", data)
})
gobot.On(joystick.Events["right_y"], func(data interface{}) {
fmt.Println("right_y", data)
})
}
robot := gobot.Robot{
Connections: []gobot.Connection{joystickAdaptor},
Devices: []gobot.Device{joystick},
Work: work,
}
robot.Start()
}

View File

@ -1,65 +0,0 @@
package main
import (
"fmt"
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot-joystick"
)
func main() {
joystickAdaptor := new(gobotJoystick.JoystickAdaptor)
joystickAdaptor.Name = "xbox360"
joystickAdaptor.Params = map[string]interface{}{
"config": "./configs/xbox360_power_a_mini_proex.json",
}
joystick := gobotJoystick.NewJoystick(joystickAdaptor)
joystick.Name = "xbox360"
work := func() {
gobot.On(joystick.Events["a_press"], func(data interface{}) {
fmt.Println("a_press")
})
gobot.On(joystick.Events["a_release"], func(data interface{}) {
fmt.Println("a_release")
})
gobot.On(joystick.Events["b_press"], func(data interface{}) {
fmt.Println("b_press")
})
gobot.On(joystick.Events["b_release"], func(data interface{}) {
fmt.Println("b_release")
})
gobot.On(joystick.Events["up"], func(data interface{}) {
fmt.Println("up", data)
})
gobot.On(joystick.Events["down"], func(data interface{}) {
fmt.Println("down", data)
})
gobot.On(joystick.Events["left"], func(data interface{}) {
fmt.Println("left", data)
})
gobot.On(joystick.Events["right"], func(data interface{}) {
fmt.Println("right", data)
})
gobot.On(joystick.Events["left_x"], func(data interface{}) {
fmt.Println("left_x", data)
})
gobot.On(joystick.Events["left_y"], func(data interface{}) {
fmt.Println("left_y", data)
})
gobot.On(joystick.Events["right_x"], func(data interface{}) {
fmt.Println("right_x", data)
})
gobot.On(joystick.Events["right_y"], func(data interface{}) {
fmt.Println("right_y", data)
})
}
robot := gobot.Robot{
Connections: []gobot.Connection{joystickAdaptor},
Devices: []gobot.Device{joystick},
Work: work,
}
robot.Start()
}

View File

@ -1,4 +1,4 @@
package gobotJoystick
package joystick
import (
. "github.com/onsi/ginkgo"

View File

@ -1,4 +1,4 @@
package gobotJoystick
package joystick
import (
"github.com/hybridgroup/go-sdl2/sdl"
@ -8,27 +8,28 @@ import (
type JoystickAdaptor struct {
gobot.Adaptor
joystick *sdl.Joystick
connect func(*JoystickAdaptor)
}
func (me *JoystickAdaptor) Connect() bool {
sdl.Init(sdl.INIT_JOYSTICK)
if sdl.NumJoysticks() > 0 {
me.joystick = sdl.JoystickOpen(0)
} else {
panic("No joystick available")
func NewJoystickAdaptor() *JoystickAdaptor {
return &JoystickAdaptor{
connect: func(j *JoystickAdaptor) {
sdl.Init(sdl.INIT_JOYSTICK)
if sdl.NumJoysticks() > 0 {
j.joystick = sdl.JoystickOpen(0)
} else {
panic("No joystick available")
}
},
}
}
func (j *JoystickAdaptor) Connect() bool {
j.connect(j)
return true
}
func (me *JoystickAdaptor) Reconnect() bool {
return true
}
func (me *JoystickAdaptor) Disconnect() bool {
me.joystick.Close()
return true
}
func (me *JoystickAdaptor) Finalize() bool {
func (j *JoystickAdaptor) Finalize() bool {
j.joystick.Close()
return true
}

View File

@ -1,4 +1,4 @@
package gobotJoystick
package joystick
import (
. "github.com/onsi/ginkgo"
@ -7,23 +7,17 @@ import (
var _ = Describe("JoystickAdaptor", func() {
var (
adaptor *JoystickAdaptor
j *JoystickAdaptor
)
BeforeEach(func() {
adaptor = new(JoystickAdaptor)
j = NewJoystickAdaptor()
})
PIt("Must be able to Finalize", func() {
Expect(adaptor.Finalize()).To(Equal(true))
Expect(j.Finalize()).To(Equal(true))
})
PIt("Must be able to Connect", func() {
Expect(adaptor.Connect()).To(Equal(true))
})
PIt("Must be able to Disconnect", func() {
Expect(adaptor.Disconnect()).To(Equal(true))
})
PIt("Must be able to Reconnect", func() {
Expect(adaptor.Reconnect()).To(Equal(true))
Expect(j.Connect()).To(Equal(true))
})
})

View File

@ -1,4 +1,4 @@
package gobotJoystick
package joystick
import (
"encoding/json"
@ -11,8 +11,8 @@ import (
type JoystickDriver struct {
gobot.Driver
JoystickAdaptor *JoystickAdaptor
config joystickConfig
Adaptor *JoystickAdaptor
config joystickConfig
}
type pair struct {
@ -34,17 +34,16 @@ type joystickConfig struct {
Hats []hat `json:"Hats"`
}
type JoystickInterface interface {
}
func NewJoystick(adaptor *JoystickAdaptor) *JoystickDriver {
d := new(JoystickDriver)
d.Events = make(map[string]chan interface{})
d.JoystickAdaptor = adaptor
d.Commands = []string{}
func NewJoystickDriver(a *JoystickAdaptor) *JoystickDriver {
d := &JoystickDriver{
Driver: gobot.Driver{
Events: make(map[string]chan interface{}),
},
Adaptor: a,
}
var configFile string
if value, ok := d.JoystickAdaptor.Params["config"]; ok {
if value, ok := d.Adaptor.Params["config"]; ok {
configFile = value.(string)
} else {
panic("No joystick config specified")
@ -70,41 +69,41 @@ func NewJoystick(adaptor *JoystickAdaptor) *JoystickDriver {
return d
}
func (me *JoystickDriver) Start() bool {
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 == me.JoystickAdaptor.joystick.InstanceID() {
axis := me.findName(data.Axis, me.config.Axis)
if data.Which == j.Adaptor.joystick.InstanceID() {
axis := j.findName(data.Axis, j.config.Axis)
if axis == "" {
fmt.Println("Unknown Axis:", data.Axis)
} else {
gobot.Publish(me.Events[axis], data.Value)
gobot.Publish(j.Events[axis], data.Value)
}
}
case *sdl.JoyButtonEvent:
if data.Which == me.JoystickAdaptor.joystick.InstanceID() {
button := me.findName(data.Button, me.config.Buttons)
if data.Which == j.Adaptor.joystick.InstanceID() {
button := j.findName(data.Button, j.config.Buttons)
if button == "" {
fmt.Println("Unknown Button:", data.Button)
} else {
if data.State == 1 {
gobot.Publish(me.Events[fmt.Sprintf("%s_press", button)], nil)
gobot.Publish(j.Events[fmt.Sprintf("%s_press", button)], nil)
} else {
gobot.Publish(me.Events[fmt.Sprintf("%s_release", button)], nil)
gobot.Publish(j.Events[fmt.Sprintf("%s_release", button)], nil)
}
}
}
case *sdl.JoyHatEvent:
if data.Which == me.JoystickAdaptor.joystick.InstanceID() {
hat := me.findHatName(data.Value, data.Hat, me.config.Hats)
if data.Which == j.Adaptor.joystick.InstanceID() {
hat := j.findHatName(data.Value, data.Hat, j.config.Hats)
if hat == "" {
fmt.Println("Unknown Hat:", data.Hat, data.Value)
} else {
gobot.Publish(me.Events[hat], true)
gobot.Publish(j.Events[hat], true)
}
}
}
@ -114,10 +113,10 @@ func (me *JoystickDriver) Start() bool {
}()
return true
}
func (me *JoystickDriver) Init() bool { return true }
func (me *JoystickDriver) Halt() bool { return true }
func (j *JoystickDriver) Init() bool { return true }
func (j *JoystickDriver) Halt() bool { return true }
func (me *JoystickDriver) findName(id uint8, list []pair) string {
func (j *JoystickDriver) findName(id uint8, list []pair) string {
for _, value := range list {
if int(id) == value.Id {
return value.Name
@ -126,7 +125,7 @@ func (me *JoystickDriver) findName(id uint8, list []pair) string {
return ""
}
func (me *JoystickDriver) findHatName(id uint8, hat uint8, list []hat) string {
func (j *JoystickDriver) findHatName(id uint8, hat uint8, list []hat) string {
for _, value := range list {
if int(id) == value.Id && int(hat) == value.Hat {
return value.Name

View File

@ -1,4 +1,4 @@
package gobotJoystick
package joystick
import (
. "github.com/onsi/ginkgo"
@ -7,20 +7,20 @@ import (
var _ = Describe("JoystickDriver", func() {
var (
driver *JoystickDriver
d *JoystickDriver
)
BeforeEach(func() {
driver = NewJoystick(new(JoystickAdaptor))
d = NewJoystickDriver(NewJoystickAdaptor())
})
PIt("Must be able to Start", func() {
Expect(driver.Start()).To(Equal(true))
Expect(d.Start()).To(Equal(true))
})
PIt("Must be able to Init", func() {
Expect(driver.Init()).To(Equal(true))
Expect(d.Init()).To(Equal(true))
})
PIt("Must be able to Halt", func() {
Expect(driver.Halt()).To(Equal(true))
Expect(d.Halt()).To(Equal(true))
})
})