Joystick add Xbox-One controller

Joystick add Xbox-One controller
This commit is contained in:
Thomas Kohler 2022-10-21 19:08:10 +02:00 committed by GitHub
commit d827c913c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 291 additions and 11 deletions

View File

@ -1,7 +1,11 @@
# include also examples in other than ./examples folder # include also examples in other than ./examples folder
ALL_EXAMPLES := $(shell grep -l -r --include "*.go" 'build example' ./) 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) and gocv (opencv) dependencies # prevent examples with joystick (sdl2) and gocv (opencv) dependencies
EXAMPLES := $(shell grep -L 'joystick' $$(grep -L 'gocv' $(ALL_EXAMPLES))) EXAMPLES_NO_GOCV_JOYSTICK := $(shell grep -L 'joystick' $$(grep -L 'gocv' $(EXAMPLES_NO_GOCV)))
# used examples
EXAMPLES := $(EXAMPLES_NO_GOCV)
.PHONY: test test_race test_cover robeaux version_check fmt_check fmt_fix examples examples_check $(EXAMPLES) .PHONY: test test_race test_cover robeaux version_check fmt_check fmt_fix examples examples_check $(EXAMPLES)

View File

@ -0,0 +1,171 @@
// +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()
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")
})
//rb button
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)
})
//ry stick
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")
})
//lx stick
joystick.On(joystick.Event("left_x"), func(data interface{}) {
fmt.Println("left_x", data)
})
//ly stick
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()
}

View File

@ -20,14 +20,14 @@ const (
// TFlightHotasX flight stick configuration. // TFlightHotasX flight stick configuration.
TFlightHotasX = "tflightHotasX" TFlightHotasX = "tflightHotasX"
// Xbox360 joystick configuration. // Configuration for Xbox 360 controller.
Xbox360 = "xbox360" Xbox360 = "xbox360"
// Xbox360RockBandDrums controller configuration. // Xbox360RockBandDrums controller configuration.
Xbox360RockBandDrums = "xbox360RockBandDrums" Xbox360RockBandDrums = "xbox360RockBandDrums"
// Nvidia Shield TV Controller // Configuration for the Xbox One controller.
Shield = "shield" XboxOne = "xboxOne"
) )
// Driver represents a joystick // Driver represents a joystick
@ -116,18 +116,18 @@ func (j *Driver) adaptor() *Adaptor {
// [axis] // [axis]
func (j *Driver) Start() (err error) { func (j *Driver) Start() (err error) {
switch j.configPath { switch j.configPath {
case "dualshock3": case Dualshock3:
j.config = dualshock3Config j.config = dualshock3Config
case "dualshock4": case Dualshock4:
j.config = dualshock4Config j.config = dualshock4Config
case "tflightHotasX": case TFlightHotasX:
j.config = tflightHotasXConfig j.config = tflightHotasXConfig
case "xbox360": case Xbox360:
j.config = xbox360Config j.config = xbox360Config
case "xbox360RockBandDrums": case Xbox360RockBandDrums:
j.config = xbox360RockBandDrumsConfig j.config = xbox360RockBandDrumsConfig
case "shield": case XboxOne:
j.config = shieldConfig j.config = xboxOneConfig
default: default:
err := j.loadFile() err := j.loadFile()
if err != nil { if err != nil {

View File

@ -0,0 +1,105 @@
package joystick
var xboxOneConfig = joystickConfig{
Name: "Xbox One Controller",
GUID: "30001983600083000100",
Axis: []pair{
pair{
Name: "left_x",
ID: 0,
},
pair{
Name: "left_y",
ID: 1,
},
pair{
Name: "right_x",
ID: 3,
},
pair{
Name: "right_y",
ID: 4,
},
pair{
Name: "rt",
ID: 5,
},
pair{
Name: "lt",
ID: 2,
},
},
Buttons: []pair{
pair{
Name: "x",
ID: 2,
},
pair{
Name: "a",
ID: 0,
},
pair{
Name: "b",
ID: 1,
},
pair{
Name: "y",
ID: 3,
},
pair{
Name: "lb",
ID: 4,
},
pair{
Name: "rb",
ID: 5,
},
pair{
Name: "back",
ID: 6,
},
pair{
Name: "start",
ID: 7,
},
pair{
Name: "home",
ID: 9,
},
pair{
Name: "right_stick",
ID: 10,
},
pair{
Name: "left_stick",
ID: 8,
},
},
Hats: []hat{
hat{
Hat: 0,
Name: "down",
ID: 4,
},
hat{
Hat: 0,
Name: "up",
ID: 1,
},
hat{
Hat: 0,
Name: "left",
ID: 8,
},
hat{
Hat: 0,
Name: "right",
ID: 2,
},
hat{
Hat: 0,
Name: "released",
ID: 0,
},
},
}