2014-06-10 10:01:53 +08:00
# Joystick
2014-04-26 18:11:51 +08:00
2023-09-23 18:32:31 +08:00
You can use Gobot with many USB joysticks and game controllers.
2016-02-21 01:41:19 +08:00
Current configurations included:
2023-06-05 00:36:55 +08:00
2016-02-21 01:41:19 +08:00
- Dualshock3 game controller
- Dualshock4 game controller
2022-11-10 01:24:58 +08:00
- Dualsense game controller
2018-05-09 18:39:18 +08:00
- Thrustmaster T-Flight Hotas X Joystick
2016-02-21 01:41:19 +08:00
- XBox360 game controller
2018-05-09 18:39:18 +08:00
- XBox360 "Rock Band" drum controller
2023-03-24 02:14:02 +08:00
- Nintendo Switch Joy-Con controller pair
2014-04-26 18:11:51 +08:00
2014-11-29 07:34:42 +08:00
## How to Install
2014-04-26 18:11:51 +08:00
2023-09-23 18:32:31 +08:00
Any platform specific info here...
2014-04-26 18:11:51 +08:00
2018-08-17 16:37:05 +08:00
### macOS
2014-04-26 18:11:51 +08:00
2017-07-26 03:45:26 +08:00
2018-08-17 16:37:05 +08:00
### Linux (Ubuntu and Raspbian)
2023-06-05 00:36:55 +08:00
2023-09-23 18:32:31 +08:00
### Windows
2018-08-17 16:37:05 +08:00
2014-04-26 18:11:51 +08:00
2014-11-29 07:34:42 +08:00
## How to Use
2014-04-26 18:11:51 +08:00
2023-09-23 18:32:31 +08:00
Controller configurations are stored in Gobot, but you can also use external file in JSON format. Take a look at the `configs` directory for examples.
2014-04-26 18:11:51 +08:00
2014-11-29 07:34:42 +08:00
## How to Connect
2023-09-23 18:32:31 +08:00
Plug your USB joystick or game controller into your USB port. If your device is supported by your operating system, it might prompt you to install some system drivers.
2014-11-29 07:34:42 +08:00
2016-02-21 01:41:19 +08:00
For the Dualshock4, you must pair the device with your computers Bluetooth interface first, before running your Gobot program.
2014-04-26 18:11:51 +08:00
## Examples
2014-11-29 07:34:42 +08:00
This small program receives joystick and button press events from an PlayStation 3 game controller.
2014-04-26 18:11:51 +08:00
```go
package main
import (
2023-06-05 00:36:55 +08:00
"fmt"
2014-07-11 08:02:00 +08:00
2023-06-05 00:36:55 +08:00
"gobot.io/x/gobot/v2"
"gobot.io/x/gobot/v2/platforms/joystick"
2014-04-26 18:11:51 +08:00
)
func main() {
2023-09-23 18:32:31 +08:00
joystickAdaptor := joystick.NewAdaptor("0")
2023-06-05 00:36:55 +08:00
stick := joystick.NewDriver(joystickAdaptor, "dualshock3",
)
work := func() {
// buttons
2024-02-11 22:34:50 +08:00
_ = stick.On(joystick.SquarePress, func(data interface{}) {
2023-06-05 00:36:55 +08:00
fmt.Println("square_press")
})
2024-02-11 22:34:50 +08:00
_ = stick.On(joystick.SquareRelease, func(data interface{}) {
2023-06-05 00:36:55 +08:00
fmt.Println("square_release")
})
2024-02-11 22:34:50 +08:00
_ = stick.On(joystick.TrianglePress, func(data interface{}) {
2023-06-05 00:36:55 +08:00
fmt.Println("triangle_press")
})
2024-02-11 22:34:50 +08:00
_ = stick.On(joystick.TriangleRelease, func(data interface{}) {
2023-06-05 00:36:55 +08:00
fmt.Println("triangle_release")
})
2024-02-11 22:34:50 +08:00
_ = stick.On(joystick.CirclePress, func(data interface{}) {
2023-06-05 00:36:55 +08:00
fmt.Println("circle_press")
})
2024-02-11 22:34:50 +08:00
_ = stick.On(joystick.CircleRelease, func(data interface{}) {
2023-06-05 00:36:55 +08:00
fmt.Println("circle_release")
})
2024-02-11 22:34:50 +08:00
_ = stick.On(joystick.XPress, func(data interface{}) {
2023-06-05 00:36:55 +08:00
fmt.Println("x_press")
})
2024-02-11 22:34:50 +08:00
_ = stick.On(joystick.XRelease, func(data interface{}) {
2023-06-05 00:36:55 +08:00
fmt.Println("x_release")
})
2024-02-11 22:34:50 +08:00
_ = stick.On(joystick.StartPress, func(data interface{}) {
2023-06-05 00:36:55 +08:00
fmt.Println("start_press")
})
2024-02-11 22:34:50 +08:00
_ = stick.On(joystick.StartRelease, func(data interface{}) {
2023-06-05 00:36:55 +08:00
fmt.Println("start_release")
})
2024-02-11 22:34:50 +08:00
_ = stick.On(joystick.SelectPress, func(data interface{}) {
2023-06-05 00:36:55 +08:00
fmt.Println("select_press")
})
2024-02-11 22:34:50 +08:00
_ = stick.On(joystick.SelectRelease, func(data interface{}) {
2023-06-05 00:36:55 +08:00
fmt.Println("select_release")
})
// joysticks
2024-02-11 22:34:50 +08:00
_ = stick.On(joystick.LeftX, func(data interface{}) {
2023-06-05 00:36:55 +08:00
fmt.Println("left_x", data)
})
2024-02-11 22:34:50 +08:00
_ = stick.On(joystick.LeftY, func(data interface{}) {
2023-06-05 00:36:55 +08:00
fmt.Println("left_y", data)
})
2024-02-11 22:34:50 +08:00
_ = stick.On(joystick.RightX, func(data interface{}) {
2023-06-05 00:36:55 +08:00
fmt.Println("right_x", data)
})
2024-02-11 22:34:50 +08:00
_ = stick.On(joystick.RightY, func(data interface{}) {
2023-06-05 00:36:55 +08:00
fmt.Println("right_y", data)
})
// triggers
2024-02-11 22:34:50 +08:00
_ = stick.On(joystick.R1Press, func(data interface{}) {
2023-06-05 00:36:55 +08:00
fmt.Println("R1Press", data)
})
2024-02-11 22:34:50 +08:00
_ = stick.On(joystick.R2Press, func(data interface{}) {
2023-06-05 00:36:55 +08:00
fmt.Println("R2Press", data)
})
2024-02-11 22:34:50 +08:00
_ = stick.On(joystick.L1Press, func(data interface{}) {
2023-06-05 00:36:55 +08:00
fmt.Println("L1Press", data)
})
2024-02-11 22:34:50 +08:00
_ = stick.On(joystick.L2Press, func(data interface{}) {
2023-06-05 00:36:55 +08:00
fmt.Println("L2Press", data)
})
}
robot := gobot.NewRobot("joystickBot",
[]gobot.Connection{joystickAdaptor},
[]gobot.Device{stick},
work,
)
2024-02-11 22:34:50 +08:00
if err := robot.Start(); err != nil {
panic(err)
}
2014-04-26 18:11:51 +08:00
}
2016-02-21 01:41:19 +08:00
```
2017-09-13 03:07:53 +08:00
## How to Add A New Joystick
2023-09-23 18:32:31 +08:00
You can create a file similar to `joystick_dualshock3.go` and submit a pull request with the new configuration so others can use it as well.