dualshock4

package
v0.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 1, 2026 License: GPL-3.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultVID = 0x054C
	DefaultPID = 0x09CC
)
View Source
const (
	DefaultSerialString  = "1111020BF619A500"
	DefaultBoardString   = "JDM-055"
	DefaultTemperature   = 28.0
	DefaultVoltage       = 4.2
	DefaultBatteryStatus = BatteryChargingFlag | BatteryFullyCharged
)
View Source
const (
	EndpointIn  = 0x84
	EndpointOut = 0x03
)
View Source
const (
	ReportIDInput  = 0x01
	ReportIDOutput = 0x05
)
View Source
const (
	ButtonSquare   uint16 = 0x0010
	ButtonCross    uint16 = 0x0020
	ButtonCircle   uint16 = 0x0040
	ButtonTriangle uint16 = 0x0080

	DPadMask uint8 = 0x0F
)
View Source
const (
	ButtonL1      uint16 = 0x0100
	ButtonR1      uint16 = 0x0200
	ButtonL2      uint16 = 0x0400
	ButtonR2      uint16 = 0x0800
	ButtonShare   uint16 = 0x1000
	ButtonOptions uint16 = 0x2000
	ButtonL3      uint16 = 0x4000
	ButtonR3      uint16 = 0x8000

	ButtonPS            uint16 = 0x0001
	ButtonTouchpadClick uint16 = 0x0002
)
View Source
const (
	ButtonPSUSB            uint8 = 0x01
	ButtonTouchpadClickUSB uint8 = 0x02

	CounterShift = 2
)
View Source
const (
	DPadUSBUp        = 0x00
	DPadUSBUpRight   = 0x01
	DPadUSBRight     = 0x02
	DPadUSBDownRight = 0x03
	DPadUSBDown      = 0x04
	DPadUSBDownLeft  = 0x05
	DPadUSBLeft      = 0x06
	DPadUSBUpLeft    = 0x07
	DPadUSBNeutral   = 0x08
)
View Source
const (
	DPadUp    = 0x01
	DPadDown  = 0x02
	DPadLeft  = 0x04
	DPadRight = 0x08
)
View Source
const (
	// GyroCountsPerDps is the fixed-point scale factor for °/s.
	// resolution is 0.0625 °/s and range is about +-2048 °/s.
	GyroCountsPerDps = 16.0

	// AccelCountsPerMS2 is the fixed-point scale factor for m/s².
	// resolution is ~0.00195 m/s² and range is about +-64 m/s² (~+-6.5 g).
	AccelCountsPerMS2 = 512.0

	StandardGravityMS2 = 9.81
)

The DS4 USB input report carries gyro/accel as signed int16 values. VIIPER's wire protocol keeps them as int16, but interprets them as fixed-point physical units to avoid float serialization across clients.

Gyro fields (GyroX/Y/Z): °/s scaled by GyroCountsPerDps. Accel fields (AccelX/Y/Z): m/s² scaled by AccelCountsPerMS2.

View Source
const (
	DefaultAccelXRaw int16 = 0
	DefaultAccelYRaw int16 = 0
	// -StandardGravityMS2 * AccelCountsPerMS2 = (-9.81 * 512) = -5023
	DefaultAccelZRaw int16 = -5023
)

Default accelerometer raw values for a controller lying flat on a table.

View Source
const (
	DefaultLedRed   = 0x00
	DefaultLedGreen = 0x00
	DefaultLedBlue  = 0x40
)
View Source
const (
	TouchpadMinX uint16 = 0
	TouchpadMaxX uint16 = 1920
	TouchpadMinY uint16 = 0
	TouchpadMaxY uint16 = 942

	TouchInactiveMask uint8 = 0x80
)
View Source
const (
	BatteryLevelMask    = 0x0F
	BatteryChargingFlag = 0x10
	BatteryFullyCharged = 0x0A
)
View Source
const (
	HardwareVersionMajor uint16 = 0x0001
	HardwareVersionMinor uint16 = 0xB400
	SoftwareVersionMajor uint32 = 0x00000001
	SoftwareVersionMinor uint16 = 0xA00B
)
View Source
const (
	InputReportSize = 64
)

Variables

View Source
var DefaultBuildTime = time.Date(2021, time.September, 17, 11, 34, 0, 0, time.UTC)

Functions

func AccelMS2ToRaw

func AccelMS2ToRaw(ms2 float64) int16

AccelMS2ToRaw converts an acceleration value in meters/second^2 (m/s²) into the fixed-point raw int16 wire/report representation.

func AccelRawToMS2

func AccelRawToMS2(raw int16) float64

AccelRawToMS2 converts a fixed-point raw accelerometer value into m/s².

func DefaultAccelRaw

func DefaultAccelRaw() (x, y, z int16)

DefaultAccelRaw returns the default ("neutral") accelerometer vector for a controller lying flat on a table.

func GyroDpsToRaw

func GyroDpsToRaw(dps float64) int16

GyroDpsToRaw converts a gyro angular velocity value in degrees/second (°/s) into the fixed-point raw int16 wire/report representation.

func GyroRawToDps

func GyroRawToDps(raw int16) float64

GyroRawToDps converts a fixed-point raw gyro value into degrees/second (°/s).

Types

type DualShock4

type DualShock4 struct {
	// contains filtered or unexported fields
}

func New

func (*DualShock4) GetDescriptor

func (d *DualShock4) GetDescriptor() *usb.Descriptor

func (*DualShock4) GetDeviceSpecificArgs added in v0.5.0

func (d *DualShock4) GetDeviceSpecificArgs() map[string]any

func (*DualShock4) HandleControl

func (d *DualShock4) HandleControl(bmRequestType, bRequest uint8, wValue, wIndex, wLength uint16, data []byte) ([]byte, bool)

func (*DualShock4) HandleTransfer

func (d *DualShock4) HandleTransfer(ep uint32, dir uint32, out []byte) []byte

func (*DualShock4) SetMetaState added in v0.7.0

func (d *DualShock4) SetMetaState(meta MetaState)

func (*DualShock4) SetOutputCallback

func (d *DualShock4) SetOutputCallback(f func(OutputState))

func (*DualShock4) UpdateInputState

func (d *DualShock4) UpdateInputState(state *InputState)

type InputState

type InputState struct {
	LX, LY  int8
	RX, RY  int8
	Buttons uint16
	DPad    uint8
	L2, R2  uint8

	Touch1X, Touch1Y uint16
	Touch1Active     bool
	Touch2X, Touch2Y uint16
	Touch2Active     bool

	GyroX, GyroY, GyroZ    int16
	AccelX, AccelY, AccelZ int16
}

nolint viiper:wire dualshock4 c2s stickLX:i8 stickLY:i8 stickRX:i8 stickRY:i8 buttons:u16 dpad:u8 triggerL2:u8 triggerR2:u8 touch1X:u16 touch1Y:u16 touch1Active:bool touch2X:u16 touch2Y:u16 touch2Active:bool gyroX:i16 gyroY:i16 gyroZ:i16 accelX:i16 accelY:i16 accelZ:i16

func (*InputState) MarshalBinary

func (s *InputState) MarshalBinary() ([]byte, error)

func (*InputState) UnmarshalBinary

func (s *InputState) UnmarshalBinary(data []byte) error

type MetaState added in v0.7.0

type MetaState struct {
	SerialNumber string    `json:"serial_number"`
	Board        string    `json:"board"`
	BuildTime    time.Time `json:"build_time"`

	BatteryStatus      byte    `json:"battery_status"`
	TemperatureCelsius float64 `json:"temperature_celsius"`
	BatteryVoltage     float64 `json:"battery_voltage"`
}

func (*MetaState) ToMap added in v0.7.0

func (m *MetaState) ToMap() map[string]any

func (*MetaState) UpdateFromMap added in v0.7.0

func (m *MetaState) UpdateFromMap(data map[string]any)

type OutputState

type OutputState struct {
	RumbleSmall uint8 // (0-255)
	RumbleLarge uint8 // (0-255)
	LedRed      uint8 // (0-255)
	LedGreen    uint8 // (0-255)
	LedBlue     uint8 // (0-255)
	FlashOn     uint8 // (units of 2.5ms)
	FlashOff    uint8 // (units of 2.5ms)
}

nolint viiper:wire dualshock4 s2c rumbleSmall:u8 rumbleLarge:u8 ledRed:u8 ledGreen:u8 ledBlue:u8 flashOn:u8 flashOff:u8

func (*OutputState) MarshalBinary

func (f *OutputState) MarshalBinary() ([]byte, error)

func (*OutputState) UnmarshalBinary

func (f *OutputState) UnmarshalBinary(data []byte) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL