Skip to content

Commit

Permalink
joystick: able to configure joysticks without external json file
Browse files Browse the repository at this point in the history
Signed-off-by: Ron Evans <[email protected]>
  • Loading branch information
deadprogram committed Apr 14, 2018
1 parent 871b299 commit f9bb5a3
Show file tree
Hide file tree
Showing 13 changed files with 329 additions and 130 deletions.
4 changes: 1 addition & 3 deletions examples/ardrone_ps3.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ const offset = 32767.0

func main() {
joystickAdaptor := joystick.NewAdaptor()
stick := joystick.NewDriver(joystickAdaptor,
"./platforms/joystick/configs/dualshock3.json",
)
stick := joystick.NewDriver(joystickAdaptor, "dualshock3")

ardroneAdaptor := ardrone.NewAdaptor()
drone := ardrone.NewDriver(ardroneAdaptor)
Expand Down
4 changes: 1 addition & 3 deletions examples/bebop_ps3.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ const offset = 32767.0

func main() {
joystickAdaptor := joystick.NewAdaptor()
stick := joystick.NewDriver(joystickAdaptor,
"./platforms/joystick/configs/dualshock3.json",
)
stick := joystick.NewDriver(joystickAdaptor, "dualshock3")

bebopAdaptor := bebop.NewAdaptor()
drone := bebop.NewDriver(bebopAdaptor)
Expand Down
4 changes: 1 addition & 3 deletions examples/bebop_ps3_video.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ func ffmpeg() (stdin io.WriteCloser, stderr io.ReadCloser, err error) {

func main() {
joystickAdaptor := joystick.NewAdaptor()
stick := joystick.NewDriver(joystickAdaptor,
"./platforms/joystick/configs/dualshock3.json",
)
stick := joystick.NewDriver(joystickAdaptor, "dualshock3")

bebopAdaptor := bebop.NewAdaptor()
drone := bebop.NewDriver(bebopAdaptor)
Expand Down
4 changes: 1 addition & 3 deletions examples/joystick_ps3.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ import (

func main() {
joystickAdaptor := joystick.NewAdaptor()
stick := joystick.NewDriver(joystickAdaptor,
"./platforms/joystick/configs/dualshock3.json",
)
stick := joystick.NewDriver(joystickAdaptor, "dualshock3")

work := func() {
// buttons
Expand Down
4 changes: 1 addition & 3 deletions examples/joystick_ps4.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ import (

func main() {
joystickAdaptor := joystick.NewAdaptor()
stick := joystick.NewDriver(joystickAdaptor,
"./platforms/joystick/configs/dualshock4.json",
)
stick := joystick.NewDriver(joystickAdaptor, "dualshock4")

work := func() {
stick.On(joystick.SquarePress, func(data interface{}) {
Expand Down
4 changes: 1 addition & 3 deletions examples/joystick_xbox360.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ import (

func main() {
joystickAdaptor := joystick.NewAdaptor()
stick := joystick.NewDriver(joystickAdaptor,
"./platforms/joystick/configs/joystick/configs/xbox360_power_a_mini_proex.json",
)
stick := joystick.NewDriver(joystickAdaptor, "xbox360")

work := func() {
stick.On(joystick.APress, func(data interface{}) {
Expand Down
4 changes: 1 addition & 3 deletions examples/minidrone_ps3.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ const offset = 32767.0

func main() {
joystickAdaptor := joystick.NewAdaptor()
stick := joystick.NewDriver(joystickAdaptor,
"./platforms/joystick/configs/dualshock3.json",
)
stick := joystick.NewDriver(joystickAdaptor, "dualshock3")

droneAdaptor := ble.NewClientAdaptor(os.Args[1])
drone := minidrone.NewDriver(droneAdaptor)
Expand Down
4 changes: 1 addition & 3 deletions examples/tello_ps3.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ const offset = 32767.0

func main() {
joystickAdaptor := joystick.NewAdaptor()
stick := joystick.NewDriver(joystickAdaptor,
"./platforms/joystick/configs/dualshock3.json",
)
stick := joystick.NewDriver(joystickAdaptor, "dualshock3")

drone := tello.NewDriver("8888")

Expand Down
103 changes: 4 additions & 99 deletions platforms/joystick/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,102 +35,8 @@ go get -d -u gobot.io/x/gobot/...

## How to Use

Controller configurations are stored in JSON format. Here's an example configuration file for the Dualshock 3 controller

```json
{
"name": "Sony PLAYSTATION(R)3 Controller",
"guid": "030000004c0500006802000011010000",
"axis": [
{
"name": "left_x",
"id": 0
},
{
"name": "left_y",
"id": 1
},
{
"name": "right_x",
"id": 2
},
{
"name": "right_y",
"id": 3
}
],
"buttons": [
{
"name": "square",
"id": 15
},
{
"name": "triangle",
"id": 12
},
{
"name": "circle",
"id": 13
},
{
"name": "x",
"id": 14
},
{
"name": "up",
"id": 4
},
{
"name": "down",
"id": 6
},
{
"name": "left",
"id": 7
},
{
"name": "right",
"id": 5
},
{
"name": "left_stick",
"id": 1
},
{
"name": "right_stick",
"id": 2
},
{
"name": "l1",
"id": 10
},
{
"name": "l2",
"id": 8
},
{
"name": "r1",
"id": 11
},
{
"name": "r2",
"id": 9
},
{
"name": "start",
"id": 3
},
{
"name": "select",
"id": 0
},
{
"name": "home",
"id": 16
}
]
}
```
Controller configurations are stored in Gobot it, but you can also use external file in JSON format. Take a look at the `configs` directory for examples.


## How to Connect

Expand All @@ -154,8 +60,7 @@ import (

func main() {
joystickAdaptor := joystick.NewAdaptor()
stick := joystick.NewDriver(joystickAdaptor,
"./platforms/joystick/configs/dualshock3.json",
stick := joystick.NewDriver(joystickAdaptor, "dualshock3",
)

work := func() {
Expand Down Expand Up @@ -256,4 +161,4 @@ Joystick 0 connected
[10345 ms] Axis: 0 value:0
```

You can use the output from this program to create a JSON file for the various buttons and axes on your joystick/gamepad.
You can use the output from this program to create a JSON file for the various buttons and axes on your joystick/gamepad. You could also create a file similar to `joystick_dualshock3.go` and submit a pull request with the new configuration so others can use it as well.
29 changes: 22 additions & 7 deletions platforms/joystick/joystick_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,17 @@ func (j *Driver) adaptor() *Adaptor {
// [button]_release
// [axis]
func (j *Driver) Start() (err error) {
file, e := ioutil.ReadFile(j.configPath)
if e != nil {
return e
switch j.configPath {
case "dualshock3":
j.config = dualshock3Config
case "dualshock4":
j.config = dualshock4Config
case "xbox360":
j.config = xbox360Config
default:
j.loadFile()
}

var jsontype joystickConfig
json.Unmarshal(file, &jsontype)
j.config = jsontype

for _, value := range j.config.Buttons {
j.AddEvent(fmt.Sprintf("%s_press", value.Name))
j.AddEvent(fmt.Sprintf("%s_release", value.Name))
Expand Down Expand Up @@ -190,3 +192,16 @@ func (j *Driver) findHatName(id uint8, hat uint8, list []hat) string {
}
return ""
}

// loadFile load the joystick config from a .json file
func (j *Driver) loadFile() error {
file, e := ioutil.ReadFile(j.configPath)
if e != nil {
return e
}

var jsontype joystickConfig
json.Unmarshal(file, &jsontype)
j.config = jsontype
return nil
}
95 changes: 95 additions & 0 deletions platforms/joystick/joystick_dualshock3.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package joystick

var dualshock3Config = joystickConfig{
Name: "Dualshock3 Controller",
GUID: "1111",
Axis: []pair{
pair{
Name: "left_x",
ID: 0,
},
pair{
Name: "left_y",
ID: 1,
},
pair{
Name: "right_x",
ID: 2,
},
pair{
Name: "right_y",
ID: 3,
},
},
Buttons: []pair{
pair{
Name: "square",
ID: 15,
},
pair{
Name: "triangle",
ID: 12,
},
pair{
Name: "circle",
ID: 13,
},
pair{
Name: "x",
ID: 14,
},
pair{
Name: "up",
ID: 4,
},
pair{
Name: "down",
ID: 6,
},
pair{
Name: "left",
ID: 7,
},
pair{
Name: "right",
ID: 5,
},
pair{
Name: "left_stick",
ID: 1,
},
pair{
Name: "right_stick",
ID: 2,
},
pair{
Name: "l1",
ID: 10,
},
{
Name: "l2",
ID: 8,
},
pair{
Name: "r1",
ID: 11,
},
pair{
Name: "r2",
ID: 9,
},
pair{
Name: "start",
ID: 3,
},
pair{
Name: "select",
ID: 0,
},
pair{
Name: "home",
ID: 16,
},
},
Hats: []hat{},
}
Loading

0 comments on commit f9bb5a3

Please sign in to comment.