Skip to content
This repository has been archived by the owner on Mar 19, 2024. It is now read-only.

Some Fixes For Gamepad.go #41

Closed
wants to merge 4 commits into from
Closed
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
40 changes: 39 additions & 1 deletion gamepad.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ type Gamepad interface {
// HatRelease will issue a hat-release event in the given direction
HatRelease(direction HatDirection) error

// LeftTriggerForce performs a trigger-axis-z event with a given force
LeftTriggerForce(value float32) error
// RightTriggerForce performs a trigger-axis-rz event with a given force
RightTriggerForce(value float32) error

io.Closer
}

Expand Down Expand Up @@ -125,6 +130,14 @@ func (vg vGamepad) RightStickMoveY(value float32) error {
return vg.sendStickAxisEvent(absRY, value)
}

func (vg vGamepad) LeftTriggerForce(value float32) error {
return vg.sendStickAxisEvent(absZ, value)
}

func (vg vGamepad) RightTriggerForce(value float32) error {
return vg.sendStickAxisEvent(absRZ, value)
}

func (vg vGamepad) RightStickMove(x, y float32) error {
values := map[uint16]float32{}
values[absRX] = x
Expand Down Expand Up @@ -289,6 +302,27 @@ func createVGamepadDevice(path string, name []byte, vendor uint16, product uint1
absHat0Y,
}

//tell uinput what the minimum/maximum abs value is
var absMin [absSize]int32
absMin[absX] = -MaximumAxisValue
absMin[absY] = -MaximumAxisValue
absMin[absZ] = -MaximumAxisValue
absMin[absRX] = -MaximumAxisValue
absMin[absRY] = -MaximumAxisValue
absMin[absRZ] = -MaximumAxisValue
absMin[absHat0X] = -1
absMin[absHat0Y] = -1

var absMax [absSize]int32
absMax[absX] = MaximumAxisValue
absMax[absY] = MaximumAxisValue
absMax[absZ] = MaximumAxisValue
absMax[absRX] = MaximumAxisValue
absMax[absRY] = MaximumAxisValue
absMax[absRZ] = MaximumAxisValue
absMax[absHat0X] = 1
absMax[absHat0Y] = 1

deviceFile, err := createDeviceFile(path)
if err != nil {
return nil, fmt.Errorf("failed to create virtual gamepad device: %v", err)
Expand Down Expand Up @@ -331,7 +365,11 @@ func createVGamepadDevice(path string, name []byte, vendor uint16, product uint1
Bustype: busUsb,
Vendor: vendor,
Product: product,
Version: 1}})
Version: 1,
},
Absmin: absMin,
Absmax: absMax,
})
}

// Takes in a normalized value (-1.0:1.0) and return an event value
Expand Down
28 changes: 28 additions & 0 deletions gamepad_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,34 @@ func TestAxisMovement(t *testing.T) {
}
}

//This test press the triggers with some amount of force
func TestTriggerAxis(t *testing.T) {
vg, err := CreateGamepad("/dev/uinput", []byte("Hot gophers in your area"), 0xDEAD, 0xBEEF)
if err != nil {
t.Fatalf("Failed to create the virtual gamepad. Last error was: %s\n", err)
}

err = vg.LeftTriggerForce(1.0)
if err != nil {
t.Fatalf("Failed to send trigger axis event. Last error was: %s\n", err)
}

err = vg.RightTriggerForce(-1.0)
if err != nil {
t.Fatalf("Failed to send trigger axis event. Last error was: %s\n", err)
}

err = vg.LeftTriggerForce(-0.2)
if err != nil {
t.Fatalf("Failed to send trigger axis event. Last error was: %s\n", err)
}

err = vg.RightTriggerForce(0.2)
if err != nil {
t.Fatalf("Failed to send trigger axis event. Last error was: %s\n", err)
}
}

func TestHatMovement(t *testing.T) {
vg, err := CreateGamepad("/dev/uinput", []byte("Hot gophers in your area"), 0xDEAD, 0xBEEF)
if err != nil {
Expand Down