2023-05-20 20:25:21 +08:00
|
|
|
//go:build example
|
2022-10-22 00:57:27 +08:00
|
|
|
// +build example
|
2023-05-20 20:25:21 +08:00
|
|
|
|
2022-10-22 00:57:27 +08:00
|
|
|
//
|
|
|
|
// Do not build by default.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2023-05-20 20:25:21 +08:00
|
|
|
"gobot.io/x/gobot/v2"
|
|
|
|
"gobot.io/x/gobot/v2/platforms/joystick"
|
2022-10-22 00:57:27 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2023-09-23 18:32:31 +08:00
|
|
|
joystickAdaptor := joystick.NewAdaptor("0")
|
2022-10-22 00:57:27 +08:00
|
|
|
joystick := joystick.NewDriver(joystickAdaptor, joystick.XboxOne)
|
|
|
|
|
|
|
|
work := func() {
|
|
|
|
// start button
|
|
|
|
joystick.On(joystick.Event("start_press"), func(data interface{}) {
|
|
|
|
fmt.Println("start_press")
|
|
|
|
})
|
|
|
|
joystick.On(joystick.Event("start_release"), func(data interface{}) {
|
|
|
|
fmt.Println("start_release")
|
|
|
|
})
|
|
|
|
|
|
|
|
// back button
|
|
|
|
joystick.On(joystick.Event("back_press"), func(data interface{}) {
|
|
|
|
fmt.Println("back_press")
|
|
|
|
})
|
|
|
|
joystick.On(joystick.Event("back_release"), func(data interface{}) {
|
|
|
|
fmt.Println("back_release")
|
|
|
|
})
|
|
|
|
|
|
|
|
// a button
|
|
|
|
joystick.On(joystick.Event("a_press"), func(data interface{}) {
|
|
|
|
fmt.Println("a_press")
|
|
|
|
})
|
|
|
|
joystick.On(joystick.Event("a_release"), func(data interface{}) {
|
|
|
|
fmt.Println("a_release")
|
|
|
|
})
|
|
|
|
|
|
|
|
// b button
|
|
|
|
joystick.On(joystick.Event("b_press"), func(data interface{}) {
|
|
|
|
fmt.Println("b_press")
|
|
|
|
})
|
|
|
|
joystick.On(joystick.Event("b_release"), func(data interface{}) {
|
|
|
|
fmt.Println("b_release")
|
|
|
|
})
|
|
|
|
|
|
|
|
// x button
|
|
|
|
joystick.On(joystick.Event("x_press"), func(data interface{}) {
|
|
|
|
fmt.Println("x_press")
|
|
|
|
})
|
|
|
|
joystick.On(joystick.Event("x_release"), func(data interface{}) {
|
|
|
|
fmt.Println("x_release")
|
|
|
|
})
|
|
|
|
|
|
|
|
// y button
|
|
|
|
joystick.On(joystick.Event("y_press"), func(data interface{}) {
|
|
|
|
fmt.Println("y_press")
|
|
|
|
})
|
|
|
|
joystick.On(joystick.Event("y_release"), func(data interface{}) {
|
|
|
|
fmt.Println("y_release")
|
|
|
|
})
|
|
|
|
|
|
|
|
// up dpad
|
|
|
|
joystick.On(joystick.Event("up_press"), func(data interface{}) {
|
|
|
|
fmt.Println("up_press", data)
|
|
|
|
})
|
|
|
|
joystick.On(joystick.Event("up_release"), func(data interface{}) {
|
|
|
|
fmt.Println("up_release", data)
|
|
|
|
})
|
|
|
|
|
|
|
|
// down dpad
|
|
|
|
joystick.On(joystick.Event("down_press"), func(data interface{}) {
|
|
|
|
fmt.Println("down_press")
|
|
|
|
})
|
|
|
|
joystick.On(joystick.Event("down_release"), func(data interface{}) {
|
|
|
|
fmt.Println("down_release")
|
|
|
|
})
|
|
|
|
|
|
|
|
// left dpad
|
|
|
|
joystick.On(joystick.Event("left_press"), func(data interface{}) {
|
|
|
|
fmt.Println("left_press")
|
|
|
|
})
|
|
|
|
joystick.On(joystick.Event("left_release"), func(data interface{}) {
|
|
|
|
fmt.Println("left_release")
|
|
|
|
})
|
|
|
|
|
|
|
|
// right dpad
|
|
|
|
joystick.On(joystick.Event("right_press"), func(data interface{}) {
|
|
|
|
fmt.Println("right_press")
|
|
|
|
})
|
|
|
|
joystick.On(joystick.Event("right_release"), func(data interface{}) {
|
|
|
|
fmt.Println("right_release")
|
|
|
|
})
|
|
|
|
|
|
|
|
// rt trigger
|
|
|
|
joystick.On(joystick.Event("rt"), func(data interface{}) {
|
|
|
|
fmt.Println("rt", data)
|
|
|
|
})
|
|
|
|
|
|
|
|
// lt trigger
|
|
|
|
joystick.On(joystick.Event("lt"), func(data interface{}) {
|
|
|
|
fmt.Println("lt", data)
|
|
|
|
})
|
|
|
|
|
|
|
|
// lb button
|
|
|
|
joystick.On(joystick.Event("lb_press"), func(data interface{}) {
|
|
|
|
fmt.Println("lb_press")
|
|
|
|
})
|
|
|
|
joystick.On(joystick.Event("lb_release"), func(data interface{}) {
|
|
|
|
fmt.Println("lb_release")
|
|
|
|
})
|
|
|
|
|
2023-10-21 02:50:42 +08:00
|
|
|
// rb button
|
2022-10-22 00:57:27 +08:00
|
|
|
joystick.On(joystick.Event("rb_press"), func(data interface{}) {
|
|
|
|
fmt.Println("rb_press")
|
|
|
|
})
|
|
|
|
joystick.On(joystick.Event("rb_release"), func(data interface{}) {
|
|
|
|
fmt.Println("rb_release")
|
|
|
|
})
|
|
|
|
|
|
|
|
// rx stick
|
|
|
|
joystick.On(joystick.Event("right_x"), func(data interface{}) {
|
|
|
|
fmt.Println("right_x", data)
|
|
|
|
})
|
|
|
|
|
2023-10-21 02:50:42 +08:00
|
|
|
// ry stick
|
2022-10-22 00:57:27 +08:00
|
|
|
joystick.On(joystick.Event("right_y"), func(data interface{}) {
|
|
|
|
fmt.Println("right_y", data)
|
|
|
|
})
|
|
|
|
|
|
|
|
// right_stick button
|
|
|
|
joystick.On(joystick.Event("right_stick_press"), func(data interface{}) {
|
|
|
|
fmt.Println("right_stick_press")
|
|
|
|
})
|
|
|
|
joystick.On(joystick.Event("right_stick_release"), func(data interface{}) {
|
|
|
|
fmt.Println("right_stick_release")
|
|
|
|
})
|
|
|
|
|
2023-10-21 02:50:42 +08:00
|
|
|
// lx stick
|
2022-10-22 00:57:27 +08:00
|
|
|
joystick.On(joystick.Event("left_x"), func(data interface{}) {
|
|
|
|
fmt.Println("left_x", data)
|
|
|
|
})
|
|
|
|
|
2023-10-21 02:50:42 +08:00
|
|
|
// ly stick
|
2022-10-22 00:57:27 +08:00
|
|
|
joystick.On(joystick.Event("left_y"), func(data interface{}) {
|
|
|
|
fmt.Println("left_y", data)
|
|
|
|
})
|
|
|
|
|
|
|
|
// left_stick button
|
|
|
|
joystick.On(joystick.Event("left_stick_press"), func(data interface{}) {
|
|
|
|
fmt.Println("left_stick_press")
|
|
|
|
})
|
|
|
|
joystick.On(joystick.Event("left_stick_release"), func(data interface{}) {
|
|
|
|
fmt.Println("left_stick_release")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
robot := gobot.NewRobot("joystickBot",
|
|
|
|
[]gobot.Connection{joystickAdaptor},
|
|
|
|
[]gobot.Device{joystick},
|
|
|
|
work,
|
|
|
|
)
|
|
|
|
|
|
|
|
robot.Start()
|
|
|
|
}
|