Skip to content

Commit

Permalink
joystick(core): replace sdl with 0xcafed00d/joystick package (#988)
Browse files Browse the repository at this point in the history
  • Loading branch information
deadprogram committed Sep 23, 2023
1 parent beaefb7 commit cd653e9
Show file tree
Hide file tree
Showing 41 changed files with 839 additions and 464 deletions.
12 changes: 6 additions & 6 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ jobs:
name: Debug version
command: go version
- run:
# digispark needs libusb, joystick needs sdl2, opencv needs opencv
name: Platform tests (except digispark, joystick, opencv)
# digispark needs libusb, opencv needs opencv
name: Platform tests (except digispark and opencv)
command: |
go test -v $(go list ./platforms/... | grep -v platforms/digispark | grep -v platforms/joystick | grep -v platforms/opencv)
go test -v $(go list ./platforms/... | grep -v platforms/digispark | grep -v platforms/opencv)
"check_examples":
docker:
Expand All @@ -52,11 +52,11 @@ jobs:
name: Debug version
command: go version
- run:
# digispark needs libusb, joystick needs sdl2, opencv needs opencv
name: Check examples (except digispark, joystick, opencv)
# digispark needs libusb, opencv needs opencv
name: Check examples (except digispark, opencv)
command: |
ALL=$(grep -l -r --include "*.go" 'build example' ./)
SOME=$(grep -L 'digispark' $(grep -L 'joystick' $(grep -L 'gocv' ${ALL})))
SOME=$(grep -L 'digispark' $(grep -L 'gocv' ${ALL}))
for e in ${SOME} ; do go vet "${e}" ; done
workflows:
Expand Down
6 changes: 1 addition & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@
ALL_EXAMPLES := $(shell grep -l -r --include "*.go" 'build example' ./)
# prevent examples with gocv (opencv) dependencies
EXAMPLES_NO_GOCV := $(shell grep -L 'gocv' $(ALL_EXAMPLES))
# prevent examples with joystick (sdl2) dependencies
EXAMPLES_NO_JOYSTICK := $(shell grep -L 'joystick' $(ALL_EXAMPLES))
# prevent examples with joystick (sdl2) and gocv (opencv) dependencies
EXAMPLES_NO_GOCV_JOYSTICK := $(shell grep -L 'joystick' $$(grep -L 'gocv' $(EXAMPLES_NO_GOCV)))
# used examples
EXAMPLES := $(EXAMPLES_NO_GOCV_JOYSTICK)
EXAMPLES := $(EXAMPLES_NO_GOCV)

.PHONY: test test_race test_cover robeaux version_check fmt_check fmt_fix examples examples_check $(EXAMPLES)

Expand Down
4 changes: 4 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ build_script:
- go test -v -cpu=2 .
- go test -v -cpu=2 ./drivers/aio/...
- go test -v -cpu=2 ./drivers/i2c/...
- go test -v -cpu=2 ./platforms/dji/...
- go test -v -cpu=2 ./platforms/firmata/...
- go test -v -cpu=2 ./platforms/ble/...
- go test -v -cpu=2 ./platforms/joystick/...
- go test -v -cpu=2 ./platforms/parrot/...
- go test -v -cpu=2 ./platforms/sphero/...
- cd ..
2 changes: 1 addition & 1 deletion examples/ardrone_ps3.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var leftX, leftY, rightX, rightY atomic.Value
const offset = 32767.0

func main() {
joystickAdaptor := joystick.NewAdaptor()
joystickAdaptor := joystick.NewAdaptor("0")
stick := joystick.NewDriver(joystickAdaptor, "dualshock3")

ardroneAdaptor := ardrone.NewAdaptor()
Expand Down
2 changes: 1 addition & 1 deletion examples/bebop_ps3.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var leftX, leftY, rightX, rightY atomic.Value
const offset = 32767.0

func main() {
joystickAdaptor := joystick.NewAdaptor()
joystickAdaptor := joystick.NewAdaptor("0")
stick := joystick.NewDriver(joystickAdaptor, "dualshock3")

bebopAdaptor := bebop.NewAdaptor()
Expand Down
2 changes: 1 addition & 1 deletion examples/bebop_ps3_video.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func ffmpeg() (stdin io.WriteCloser, stderr io.ReadCloser, err error) {
}

func main() {
joystickAdaptor := joystick.NewAdaptor()
joystickAdaptor := joystick.NewAdaptor("0")
stick := joystick.NewDriver(joystickAdaptor, "dualshock3")

bebopAdaptor := bebop.NewAdaptor()
Expand Down
26 changes: 25 additions & 1 deletion examples/joystick_ps3.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

func main() {
joystickAdaptor := joystick.NewAdaptor()
joystickAdaptor := joystick.NewAdaptor("0")
stick := joystick.NewDriver(joystickAdaptor, joystick.Dualshock3)

work := func() {
Expand Down Expand Up @@ -64,15 +64,27 @@ func main() {
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.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")
})

// joysticks
stick.On(joystick.LeftX, func(data interface{}) {
Expand All @@ -92,15 +104,27 @@ func main() {
stick.On(joystick.R1Press, func(data interface{}) {
fmt.Println("R1Press", data)
})
stick.On(joystick.R1Release, func(data interface{}) {
fmt.Println("R1Release", data)
})
stick.On(joystick.R2Press, func(data interface{}) {
fmt.Println("R2Press", data)
})
stick.On(joystick.R2Release, func(data interface{}) {
fmt.Println("R2Release", data)
})
stick.On(joystick.L1Press, func(data interface{}) {
fmt.Println("L1Press", data)
})
stick.On(joystick.L1Release, func(data interface{}) {
fmt.Println("L1Release", data)
})
stick.On(joystick.L2Press, func(data interface{}) {
fmt.Println("L2Press", data)
})
stick.On(joystick.L2Release, func(data interface{}) {
fmt.Println("L2Release", data)
})
}

robot := gobot.NewRobot("joystickBot",
Expand Down
2 changes: 1 addition & 1 deletion examples/joystick_ps4.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

func main() {
joystickAdaptor := joystick.NewAdaptor()
joystickAdaptor := joystick.NewAdaptor("0")
stick := joystick.NewDriver(joystickAdaptor, joystick.Dualshock4)

work := func() {
Expand Down
2 changes: 1 addition & 1 deletion examples/joystick_ps5.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

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

work := func() {
Expand Down
2 changes: 1 addition & 1 deletion examples/joystick_xbox360.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

func main() {
joystickAdaptor := joystick.NewAdaptor()
joystickAdaptor := joystick.NewAdaptor("0")
stick := joystick.NewDriver(joystickAdaptor, joystick.Xbox360)

work := func() {
Expand Down
2 changes: 1 addition & 1 deletion examples/joystick_xbox360_rock_band_drums.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

func main() {
joystickAdaptor := joystick.NewAdaptor()
joystickAdaptor := joystick.NewAdaptor("0")
stick := joystick.NewDriver(joystickAdaptor, joystick.Xbox360RockBandDrums)

work := func() {
Expand Down
2 changes: 1 addition & 1 deletion examples/joystick_xboxone.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

func main() {
joystickAdaptor := joystick.NewAdaptor()
joystickAdaptor := joystick.NewAdaptor("0")
joystick := joystick.NewDriver(joystickAdaptor, joystick.XboxOne)

work := func() {
Expand Down
2 changes: 1 addition & 1 deletion examples/minidrone_mambo_ps3.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var leftX, leftY, rightX, rightY atomic.Value
const offset = 32767.0

func main() {
joystickAdaptor := joystick.NewAdaptor()
joystickAdaptor := joystick.NewAdaptor("0")
stick := joystick.NewDriver(joystickAdaptor,
"./platforms/joystick/configs/dualshock3.json",
)
Expand Down
2 changes: 1 addition & 1 deletion examples/minidrone_ps3.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var leftX, leftY, rightX, rightY atomic.Value
const offset = 32767.0

func main() {
joystickAdaptor := joystick.NewAdaptor()
joystickAdaptor := joystick.NewAdaptor("0")
stick := joystick.NewDriver(joystickAdaptor, "dualshock3")

droneAdaptor := ble.NewClientAdaptor(os.Args[1])
Expand Down
2 changes: 1 addition & 1 deletion examples/tello_facetracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ var (
flightData *tello.FlightData

// joystick
joyAdaptor = joystick.NewAdaptor()
joyAdaptor = joystick.NewAdaptor("0")
stick = joystick.NewDriver(joyAdaptor, "dualshock4")
leftX, leftY, rightX, rightY atomic.Value
)
Expand Down
2 changes: 1 addition & 1 deletion examples/tello_ps3.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var leftX, leftY, rightX, rightY atomic.Value
const offset = 32767.0

func main() {
joystickAdaptor := joystick.NewAdaptor()
joystickAdaptor := joystick.NewAdaptor("0")
stick := joystick.NewDriver(joystickAdaptor, "dualshock3")

drone := tello.NewDriver("8888")
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module gobot.io/x/gobot/v2
go 1.18

require (
github.com/0xcafed00d/joystick v1.0.1
github.com/bmizerany/pat v0.0.0-20210406213842-e4b6760bdd6f
github.com/donovanhide/eventsource v0.0.0-20210830082556-c59027999da0
github.com/eclipse/paho.mqtt.golang v1.4.2
Expand All @@ -11,9 +12,9 @@ require (
github.com/hybridgroup/go-ardrone v0.0.0-20140402002621-b9750d8d7b78
github.com/hybridgroup/mjpeg v0.0.0-20140228234708-4680f319790e
github.com/nats-io/nats.go v1.27.1
github.com/nsf/termbox-go v1.1.1
github.com/sigurn/crc8 v0.0.0-20220107193325-2243fe600f9f
github.com/stretchr/testify v1.8.4
github.com/veandco/go-sdl2 v0.4.35
github.com/warthog618/gpiod v0.8.1
go.bug.st/serial v1.5.0
gocv.io/x/gocv v0.33.0
Expand All @@ -34,6 +35,7 @@ require (
github.com/gorilla/websocket v1.5.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/klauspost/compress v1.16.7 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/muka/go-bluetooth v0.0.0-20221213043340-85dc80edc4e1 // indirect
github.com/nats-io/nats-server/v2 v2.7.4 // indirect
github.com/nats-io/nkeys v0.4.4 // indirect
Expand Down
8 changes: 6 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/0xcafed00d/joystick v1.0.1 h1:r4p2cRp4MHJWu1gArhGtumbkPxmr3tcOUTFqybEhplM=
github.com/0xcafed00d/joystick v1.0.1/go.mod h1:gzszjNgzP6jtCAeSdC9OqPVO5rO7TJuaw4P7eAjNzx8=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/bgould/http v0.0.0-20190627042742-d268792bdee7/go.mod h1:BTqvVegvwifopl4KTEDth6Zezs9eR+lCWhvGKvkxJHE=
github.com/bmizerany/pat v0.0.0-20210406213842-e4b6760bdd6f h1:gOO/tNZMjjvTKZWpY7YnXC72ULNLErRtp94LountVE8=
Expand Down Expand Up @@ -52,6 +54,8 @@ github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfn
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/minio/highwayhash v1.0.2 h1:Aak5U0nElisjDCfPSG79Tgzkn2gl66NxOMspRrKnA/g=
github.com/muka/go-bluetooth v0.0.0-20220830075246-0746e3a1ea53/go.mod h1:dMCjicU6vRBk34dqOmIZm0aod6gUwZXOXzBROqGous0=
github.com/muka/go-bluetooth v0.0.0-20221213043340-85dc80edc4e1 h1:BuVRHr4HHJbk1DHyWkArJ7E8J/VA8ncCr/VLnQFazBo=
Expand All @@ -67,6 +71,8 @@ github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw=
github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/nsf/termbox-go v1.1.1 h1:nksUPLCb73Q++DwbYUBEglYBRPZyoXJdrj5L+TkjyZY=
github.com/nsf/termbox-go v1.1.1/go.mod h1:T0cTdVuOwf7pHQNtfhnEbzHbcNyCEcVU4YPpouCbVxo=
github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/paypal/gatt v0.0.0-20151011220935-4ae819d591cf/go.mod h1:+AwQL2mK3Pd3S+TUwg0tYQjid0q1txyNUJuuSmz8Kdk=
github.com/pelletier/go-toml v1.6.0/go.mod h1:5N711Q9dKgbdkxHL+MEfF31hpT7l0S0s/t2kKREewys=
Expand Down Expand Up @@ -100,8 +106,6 @@ github.com/tdakkota/win32metadata v0.1.0/go.mod h1:77e6YvX0LIVW+O81fhWLnXAxxcyu/
github.com/tinygo-org/cbgo v0.0.4 h1:3D76CRYbH03Rudi8sEgs/YO0x3JIMdyq8jlQtk/44fU=
github.com/tinygo-org/cbgo v0.0.4/go.mod h1:7+HgWIHd4nbAz0ESjGlJ1/v9LDU1Ox8MGzP9mah/fLk=
github.com/valyala/fastjson v1.6.3/go.mod h1:CLCAqky6SMuOcxStkYQvblddUtoRxhYMGLrsQns1aXY=
github.com/veandco/go-sdl2 v0.4.35 h1:NohzsfageDWGtCd9nf7Pc3sokMK/MOK+UA2QMJARWzQ=
github.com/veandco/go-sdl2 v0.4.35/go.mod h1:OROqMhHD43nT4/i9crJukyVecjPNYYuCofep6SNiAjY=
github.com/warthog618/gpiod v0.8.1 h1:+8iHpHd3fljAd6l4AT8jPbMDQNKdvBIpW/hmLgAcHiM=
github.com/warthog618/gpiod v0.8.1/go.mod h1:A7v1hGR2eTsnkN+e9RoAPYgJG9bLJWtwyIIK+pgqC7s=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
2 changes: 1 addition & 1 deletion platforms/joystick/LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2014-2018 The Hybrid Group
Copyright (c) 2014-2023 The Hybrid Group

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
52 changes: 7 additions & 45 deletions platforms/joystick/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Joystick

You can use Gobot with any USB joystick or game controller that is compatible with [Simple DirectMedia Layer](http://www.libsdl.org/).
You can use Gobot with many USB joysticks and game controllers.

Current configurations included:

Expand All @@ -14,41 +14,24 @@ Current configurations included:

## How to Install

This package requires `sdl2` to be installed on your system
Any platform specific info here...

### macOS

To install `sdl2` on macOS using Homebrew:

```sh
brew install sdl2
```

To use an XBox360 controller on macOS, you will most likely need to install additional software such as [https://github.com/360Controller/360Controller](https://github.com/360Controller/360Controller).

### Linux (Ubuntu and Raspbian)

Please refer to the main [README.md](https://github.com/hybridgroup/gobot/blob/release/README.md)

You must be running a Linux kernel that is v4.14+ in order for the various controller mappings to work as expected.
### Windows

Then you must install the latest SDL2 v2.0.8 or greater:

```sh
wget https://www.libsdl.org/release/SDL2-2.0.8.tar.gz
tar -zxvf SDL2-2.0.8.tar.gz
cd SDL2-2.0.8/
./configure && make && sudo make install
```

## How to Use

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.
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.

## How to Connect

Plug your USB joystick or game controller into your USB port. If your device is supported by SDL, you are now ready.
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.

For the Dualshock4, you must pair the device with your computers Bluetooth interface first, before running your Gobot program.

Expand All @@ -67,7 +50,7 @@ import (
)

func main() {
joystickAdaptor := joystick.NewAdaptor()
joystickAdaptor := joystick.NewAdaptor("0")
stick := joystick.NewDriver(joystickAdaptor, "dualshock3",
)

Expand Down Expand Up @@ -151,25 +134,4 @@ func main() {

## How to Add A New Joystick

In the `bin` directory for this package is a CLI utility program that scans for SDL joystick events, and displays the ID
and value:

```sh
$ go run ./platforms/joystick/bin/scanner.go
Joystick 0 connected
[6625 ms] Axis: 1 value:-22686
[6641 ms] Axis: 1 value:-32768
[6836 ms] Axis: 1 value:-18317
[6852 ms] Axis: 1 value:0
[8663 ms] Axis: 3 value:-32768
[8873 ms] Axis: 3 value:0
[10183 ms] Axis: 0 value:-24703
[10183 ms] Axis: 0 value:-32768
[10313 ms] Axis: 1 value:-3193
[10329 ms] Axis: 1 value:0
[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 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.
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.
Loading

0 comments on commit cd653e9

Please sign in to comment.