diff --git a/Makefile b/Makefile index 8e877783..7dba1b05 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,11 @@ # include also examples in other than ./examples folder 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 -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) diff --git a/examples/joystick_xboxone.go b/examples/joystick_xboxone.go new file mode 100644 index 00000000..2133fe59 --- /dev/null +++ b/examples/joystick_xboxone.go @@ -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() +} diff --git a/platforms/joystick/joystick_driver.go b/platforms/joystick/joystick_driver.go index 0ad54b7e..08bb9951 100644 --- a/platforms/joystick/joystick_driver.go +++ b/platforms/joystick/joystick_driver.go @@ -20,14 +20,14 @@ const ( // TFlightHotasX flight stick configuration. TFlightHotasX = "tflightHotasX" - // Xbox360 joystick configuration. + // Configuration for Xbox 360 controller. Xbox360 = "xbox360" // Xbox360RockBandDrums controller configuration. Xbox360RockBandDrums = "xbox360RockBandDrums" - // Nvidia Shield TV Controller - Shield = "shield" + // Configuration for the Xbox One controller. + XboxOne = "xboxOne" ) // Driver represents a joystick @@ -116,18 +116,18 @@ func (j *Driver) adaptor() *Adaptor { // [axis] func (j *Driver) Start() (err error) { switch j.configPath { - case "dualshock3": + case Dualshock3: j.config = dualshock3Config - case "dualshock4": + case Dualshock4: j.config = dualshock4Config - case "tflightHotasX": + case TFlightHotasX: j.config = tflightHotasXConfig - case "xbox360": + case Xbox360: j.config = xbox360Config - case "xbox360RockBandDrums": + case Xbox360RockBandDrums: j.config = xbox360RockBandDrumsConfig - case "shield": - j.config = shieldConfig + case XboxOne: + j.config = xboxOneConfig default: err := j.loadFile() if err != nil { diff --git a/platforms/joystick/joystick_xboxone.go b/platforms/joystick/joystick_xboxone.go new file mode 100644 index 00000000..ea2a244a --- /dev/null +++ b/platforms/joystick/joystick_xboxone.go @@ -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, + }, + }, +}