Categorygithub.com/kpeu3i/gods4
modulepackage
1.0.0
Repository: https://github.com/kpeu3i/gods4.git
Documentation: pkg.go.dev

# README

gods4

A userspace cross-platform driver for Sony DualShock 4 controller over HID. Works for Bluetooth and USB connections.

Features

  • Buttons: x, circle, square, triangle, share, options, touchpad
  • D-pad: up, down, left, right (also intermediate positions)
  • Analog sticks: left, right
  • Analog triggers: L2, R2
  • Touchpad: 2 touches and button
  • Battery
  • Gyroscope (absolute orientation)
  • Accelerometer (relative movement)
  • Activating the motors (rumble)
  • Setting the LED color

Install

Use go get to install the latest version of the library:

go get github.com/kpeu3i/[email protected]

Next, include ds4 in your application:

import "github.com/kpeu3i/gods4"

Usage

First, connect the controller to your computer using a micro-USB cable or Bluetooth.

package main

import (
	"log"
	"os"
	"os/signal"
	"syscall"

	"github.com/kpeu3i/gods4"
	"github.com/kpeu3i/gods4/led"
	"github.com/kpeu3i/gods4/rumble"
)

func main() {
	// Find all controllers connected to your machine via USB or Bluetooth
	controllers := gods4.Find()
	if len(controllers) == 0 {
		panic("No connected DS4 controllers found")
	}

	// Select first controller from the list
	controller := controllers[0]

	// Connect to the controller
	err := controller.Connect()
	if err != nil {
		panic(err)
	}

	log.Printf("* Controller #1 | %-10s | name: %s, connection: %s\n", "Connect", controller, controller.ConnectionType())

	// Disconnect controller when a program is terminated
	signals := make(chan os.Signal, 1)
	signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
	go func() {
		<-signals
		err := controller.Disconnect()
		if err != nil {
			panic(err)
		}
		log.Printf("* Controller #1 | %-10s | bye!\n", "Disconnect")
	}()

	// Register callback for "BatteryUpdate" event
	controller.On(gods4.EventBatteryUpdate, func(data interface{}) error {
		battery := data.(gods4.Battery)
		log.Printf("* Controller #1 | %-10s | capacity: %v%%, charging: %v, cable: %v\n",
			"Battery",
			battery.Capacity,
			battery.IsCharging,
			battery.IsCableConnected,
		)

		return nil
	})

	// Register callback for "CrossPress" event
	controller.On(gods4.EventCrossPress, func(data interface{}) error {
		log.Printf("* Controller #1 | %-10s | state: press\n", "Cross")

		return nil
	})

	// Register callback for "CrossRelease" event
	controller.On(gods4.EventCrossRelease, func(data interface{}) error {
		log.Printf("* Controller #1 | %-10s | state: release\n", "Cross")

		return nil
	})

	// Register callback for "RightStickMove" event
	controller.On(gods4.EventRightStickMove, func(data interface{}) error {
		stick := data.(gods4.Stick)
		log.Printf("* Controller #1 | %-10s | x: %v, y: %v\n", "RightStick", stick.X, stick.Y)

		return nil
	})

	// Enable left and right rumble motors
	err = controller.Rumble(rumble.Both())
	if err != nil {
		panic(err)
	}

	// Enable LED (yellow) with flash
	err = controller.Led(led.Yellow().Flash(50, 50))
	if err != nil {
		panic(err)
	}

	// Start listening for controller events
	err = controller.Listen()
	if err != nil {
		panic(err)
	}

	// Output:
	// 2019/02/16 17:00:23 * Controller #1 | Connect    | name: Wireless Controller (vendor: 1356, product: 2508), connection: BT
	// 2019/02/16 17:00:23 * Controller #1 | Battery    | capacity: 77%, charging: false, cable: false
	// 2019/02/16 17:00:34 * Controller #1 | Cross      | state: press
	// 2019/02/16 17:00:34 * Controller #1 | Cross      | state: release
	// 2019/02/16 17:00:39 * Controller #1 | RightStick | x: 187, y: 98
	// 2019/02/16 17:00:39 * Controller #1 | RightStick | x: 191, y: 94
	// 2019/02/16 17:00:39 * Controller #1 | RightStick | x: 196, y: 93
	// 2019/02/16 17:00:39 * Controller #1 | RightStick | x: 212, y: 88
	// 2019/02/16 17:00:39 * Controller #1 | RightStick | x: 228, y: 79
	// 2019/02/16 17:02:52 * Controller #1 | Disconnect | bye!
}

Events

Events on which you can subscribe are listed below:

NameData
EventCrossPressnil
EventCrossReleasenil
EventCirclePressnil
EventCircleReleasenil
EventSquarePressnil
EventSquareReleasenil
EventTrianglePressnil
EventTriangleReleasenil
EventL1Pressnil
EventL1Releasenil
EventL2Pressbyte
EventL2Releasebyte
EventL3Pressnil
EventL3Releasenil
EventR1Pressnil
EventR1Releasenil
EventR2Pressbyte
EventR2Releasebyte
EventR3Pressnil
EventR3Releasenil
EventDPadUpPressnil
EventDPadUpReleasenil
EventDPadDownPressnil
EventDPadDownReleasenil
EventDPadLeftPressnil
EventDPadLeftReleasenil
EventDPadRightPressnil
EventDPadRightReleasenil
EventSharePressnil
EventShareReleasenil
EventOptionsPressnil
EventOptionsReleasenil
EventTouchpadSwipeTouchpad
EventTouchpadPressTouchpad
EventTouchpadReleaseTouchpad
EventPSPressnil
EventPSReleasenil
EventLeftStickMoveStick
EventRightStickMoveStick
EventAccelerometerUpdateAccelerometer
EventGyroscopeUpdateGyroscope
EventBatteryUpdateBattery

TODO

  • Microphone/speaker
  • Tests

References

The DualShock 4 report format is not open and these resources have been very helpful when creating gods4:

# Packages

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Functions

No description provided by the author
No description provided by the author

# Constants

No description provided by the author
No description provided by the author
No description provided by the author
Accelerometer.
Battery.
Circle.
No description provided by the author
Cross.
No description provided by the author
D-pad down.
No description provided by the author
D-pad left.
No description provided by the author
D-pad right.
No description provided by the author
D-pad up.
No description provided by the author
Gyroscope.
L1.
No description provided by the author
L2.
No description provided by the author
L3.
No description provided by the author
Left stick.
Options.
No description provided by the author
PS.
No description provided by the author
R1.
No description provided by the author
R2.
No description provided by the author
R3.
No description provided by the author
Right stick.
Share.
No description provided by the author
Square.
No description provided by the author
No description provided by the author
No description provided by the author
Touchpad.
Triangle.
No description provided by the author

# Variables

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Structs

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Interfaces

No description provided by the author

# Type aliases

No description provided by the author
No description provided by the author
No description provided by the author