Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for Dualsense joystick (PlayStation 5) #880

Merged
merged 2 commits into from
Nov 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 140 additions & 0 deletions examples/joystick_ps5.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
//go:build example
// +build example

//
// Do not build by default.

package main

import (
"fmt"

"gobot.io/x/gobot"
"gobot.io/x/gobot/platforms/joystick"
)

func main() {
joystickAdaptor := joystick.NewAdaptor()
stick := joystick.NewDriver(joystickAdaptor, joystick.Dualsense)

work := func() {
stick.On(joystick.SquarePress, func(data interface{}) {
fmt.Println("square_press")
})
stick.On(joystick.SquareRelease, func(data interface{}) {
fmt.Println("square_release")
})
stick.On(joystick.TrianglePress, func(data interface{}) {
fmt.Println("triangle_press")
})
stick.On(joystick.TriangleRelease, func(data interface{}) {
fmt.Println("triangle_release")
})
stick.On(joystick.CirclePress, func(data interface{}) {
fmt.Println("circle_press")
})
stick.On(joystick.CircleRelease, func(data interface{}) {
fmt.Println("circle_release")
})
stick.On(joystick.XPress, func(data interface{}) {
fmt.Println("x_press")
})
stick.On(joystick.XRelease, func(data interface{}) {
fmt.Println("x_release")
})
stick.On(joystick.HomePress, func(data interface{}) {
fmt.Println("home_press")
})
stick.On(joystick.HomeRelease, func(data interface{}) {
fmt.Println("home_release")
})
stick.On(joystick.CreatePress, func(data interface{}) {
fmt.Println("create_press")
})
stick.On(joystick.CreateRelease, func(data interface{}) {
fmt.Println("create_release")
})
stick.On(joystick.OptionsPress, func(data interface{}) {
fmt.Println("options_press")
})
stick.On(joystick.OptionsRelease, func(data interface{}) {
fmt.Println("options_release")
})
stick.On(joystick.L1Press, func(data interface{}) {
fmt.Println("l1_press")
})
stick.On(joystick.L1Release, func(data interface{}) {
fmt.Println("l1_release")
})
stick.On(joystick.R1Press, func(data interface{}) {
fmt.Println("r1_press")
})
stick.On(joystick.R1Release, func(data interface{}) {
fmt.Println("r1_release")
})
stick.On(joystick.PSPress, func(data interface{}) {
fmt.Println("ps_press")
})
stick.On(joystick.PSRelease, func(data interface{}) {
fmt.Println("ps_release")
})
stick.On(joystick.TrackpadPress, func(data interface{}) {
fmt.Println("trackpad_press")
})
stick.On(joystick.TrackpadRelease, func(data interface{}) {
fmt.Println("trackpad_release")
})

stick.On(joystick.UpPress, func(data interface{}) {
fmt.Println("up_press")
})
stick.On(joystick.UpRelease, func(data interface{}) {
fmt.Println("up_release")
})
stick.On(joystick.DownPress, func(data interface{}) {
fmt.Println("down_press")
})
stick.On(joystick.DownRelease, func(data interface{}) {
fmt.Println("down_release")
})
stick.On(joystick.RightPress, func(data interface{}) {
fmt.Println("right_press")
})
stick.On(joystick.RightRelease, func(data interface{}) {
fmt.Println("right_release")
})
stick.On(joystick.LeftPress, func(data interface{}) {
fmt.Println("left_press")
})
stick.On(joystick.LeftRelease, func(data interface{}) {
fmt.Println("left_release")
})

// stick.On(joystick.LeftX, func(data interface{}) {
// fmt.Println("left_x", data)
// })
// stick.On(joystick.LeftY, func(data interface{}) {
// fmt.Println("left_y", data)
// })
// stick.On(joystick.RightX, func(data interface{}) {
// fmt.Println("right_x", data)
// })
// stick.On(joystick.RightY, func(data interface{}) {
// fmt.Println("right_y", data)
// })
// stick.On(joystick.L2, func(data interface{}) {
// fmt.Println("L2", data)
// })
// stick.On(joystick.R2, func(data interface{}) {
// fmt.Println("R2", data)
// })
}

robot := gobot.NewRobot("joystickBot",
[]gobot.Connection{joystickAdaptor},
[]gobot.Device{stick},
work,
)

robot.Start()
}
3 changes: 2 additions & 1 deletion platforms/joystick/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ You can use Gobot with any USB joystick or game controller that is compatible wi
Current configurations included:
- Dualshock3 game controller
- Dualshock4 game controller
- Dualsense game controller
- Thrustmaster T-Flight Hotas X Joystick
- XBox360 game controller
- XBox360 "Rock Band" drum controller
Expand Down Expand Up @@ -155,7 +156,7 @@ func main() {
In the `bin` directory for this package is a CLI utility program that scans for SDL joystick events, and displays the ID and value:

```
$ go run ./platforms/joystick/bin/scanner.go
$ go run ./platforms/joystick/bin/scanner.go
Joystick 0 connected
[6625 ms] Axis: 1 value:-22686
[6641 ms] Axis: 1 value:-32768
Expand Down
88 changes: 88 additions & 0 deletions platforms/joystick/configs/dualsense.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
{
"name": "Sony PLAYSTATION(R)5 Controller",
"guid": "E7D56FCA-A01F-4A14-B0D0-4FDAFD847E5E",
"axis": [
{
"name": "left_x",
"id": 0
},
{
"name": "left_y",
"id": 1
},
{
"name": "right_x",
"id": 2
},
{
"name": "right_y",
"id": 3
},
{
"name": "l2",
"id": 4
},
{
"name": "r2",
"id": 5
}
],
"buttons": [
{
"name": "x",
"id": 0
},
{
"name": "circle",
"id": 1
},
{
"name": "square",
"id": 2
},
{
"name": "triangle",
"id": 3
},
{
"name": "l1",
"id": 9
},
{
"name": "r1",
"id": 10
},
{
"name": "create",
"id": 4
},
{
"name": "options",
"id": 6
},
{
"name": "ps",
"id": 5
},
{
"name": "up",
"id": 11
},
{
"name": "down",
"id": 12
},
{
"name": "left",
"id": 13
},
{
"name": "right",
"id": 14
},
{
"name": "trackpad",
"id": 15
}
]
}
14 changes: 13 additions & 1 deletion platforms/joystick/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,20 @@ const (
XRelease = "x_release"
// share button press event
SharePress = "share_press"
// share button relase event
// share button release event
ShareRelease = "share_release"
// create button press event
CreatePress = "create_press"
// create button release event
CreateRelease = "create_release"
// ps button press event
PSPress = "ps_press"
// ps button release event
PSRelease = "ps_release"
// trackpad button press event
TrackpadPress = "trackpad_press"
// trackpad button release event
TrackpadRelease = "trackpad_release"
// options button press event
OptionsPress = "options_press"
// options button release event
Expand Down
9 changes: 8 additions & 1 deletion platforms/joystick/joystick_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ const (
// Dualshock4 joystick configuration.
Dualshock4 = "dualshock4"

// Dualsense joystick configuration.
Dualsense = "dualsense"

// TFlightHotasX flight stick configuration.
TFlightHotasX = "tflightHotasX"

Expand Down Expand Up @@ -69,7 +72,8 @@ type joystickConfig struct {
// file location.
//
// Optionally accepts:
// time.Duration: Interval at which the Driver is polled for new information
//
// time.Duration: Interval at which the Driver is polled for new information
func NewDriver(a *Adaptor, config string, v ...time.Duration) *Driver {
d := &Driver{
name: gobot.DefaultName("Joystick"),
Expand Down Expand Up @@ -108,6 +112,7 @@ func (j *Driver) adaptor() *Adaptor {
// Start and polls the state of the joystick at the given interval.
//
// Emits the Events:
//
// Error error - On button error
// Events defined in the json button configuration file.
// They will have the format:
Expand All @@ -120,6 +125,8 @@ func (j *Driver) Start() (err error) {
j.config = dualshock3Config
case Dualshock4:
j.config = dualshock4Config
case Dualsense:
j.config = dualsenseConfig
case TFlightHotasX:
j.config = tflightHotasXConfig
case Xbox360:
Expand Down
Loading