jcpc

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2019 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package jcpc (Joy-Con PC) contains constants, interface definitions, and short utility functions for the joy-con driver.

Index

Constants

View Source
const (
	ButtonPushDown = 1 << iota
	ButtonPushRight
	ButtonPushLeft
	ButtonPushUp // remember to rotate these for L and R Joy-Cons
	ButtonPushSL
	ButtonPushSR

	ButtonPushMinus
	ButtonPushPlus
	ButtonPushLStick
	ButtonPushRStick
	ButtonPushHome
	ButtonPushCapture
	ButtonPushLR
	ButtonPushZLZR
)

Bits in the 0x3F "push" mode treated as a little-endian int16. See ConvertPushReport().

View Source
const (
	// Stick - uint8 from [0, 255]
	Axis_L_Horiz = iota
	Axis_L_Vertical
	Axis_R_Horiz
	Axis_R_Vertical
	// Gyro - int16 from [-0x10FF, +0x10FF]
	Axis_Yaw_X
	Axis_Yaw_Y
	Axis_Pitch_X
	Axis_Pitch_Y
	Axis_Roll_X
	Axis_Roll_Y

	Axis_Orientation_Min = Axis_Yaw_X
)
View Source
const (
	VENDOR_NINTENDO           = 0x057e
	JOYCON_PRODUCT_L          = 0x2006
	JOYCON_PRODUCT_R          = 0x2007
	JOYCON_PRODUCT_FAKE       = 0x2008 // TODO
	JOYCON_PRODUCT_PRO        = 0x2009
	JOYCON_PRODUCT_CHARGEGRIP = 0x200e
)
View Source
const (
	NotifyInput = 1 << iota
	NotifyConnection
	NotifyBattery
)
View Source
const (
	InputIRPolling        InputMode = 0
	InputIRPollingUnused            = 1
	InputIRPollingSpecial           = 2
	InputMCUUpdate                  = 0x23 // not fully known
	InputStandard                   = 0x30
	InputNFC                        = 0x31
	InputUnknown33                  = 0x33
	InputUnknown35                  = 0x35
	InputLazyButtons      InputMode = 0x3F
	InputActivePolling              = 0x13F // pseudo-mode, driver only
)
View Source
const SPIMaxData = 0x1C

Variables

View Source
var (
	// Side Joy-Con
	ButtonsSLSR_R = ButtonState{byte((Button_R_SL | Button_R_SR) & 0xFF), 0, 0}
	ButtonsSLSR_L = ButtonState{0, 0, byte((Button_L_SL | Button_L_SR) & 0xFF)}
	// Upright Pair
	ButtonsRZR = ButtonState{byte((Button_R_R | Button_R_ZR) & 0xFF), 0, 0}
	ButtonsLZL = ButtonState{0, 0, byte((Button_L_L | Button_L_ZL) & 0xFF)}
	// Pro Controller
	ButtonsLR   = ButtonState{byte(Button_R_R & 0xFF), 0, byte(Button_L_L & 0xFF)}
	ButtonsZLZR = ButtonState{byte(Button_R_ZR & 0xFF), 0, byte(Button_L_ZL & 0xFF)}

	ButtonsAnyLR = ButtonState{}.Union(ButtonsRZR).Union(ButtonsLZL).Union(ButtonsSLSR_L).Union(ButtonsSLSR_R)
)

All button combinations considered to be a L+R press or part of one

View Source
var GyroZero [3]GyroFrame
View Source
var RumbleDataNeutral = RumbleData{[8]byte{0, 1, 0x40, 0x40, 0, 1, 0x40, 0x40}, 8}

Functions

func SPIFlashRead

func SPIFlashRead(jc JoyCon, addr, size uint32) ([]byte, error)

func SPIFlashWrite

func SPIFlashWrite(jc JoyCon, addr uint16, p []byte) error

func SetHomeLightPulse

func SetHomeLightPulse(jc JoyCon, pulseData []byte)

func SetPlayerLights

func SetPlayerLights(jc JoyCon, pattern byte)

Types

type AxisID

type AxisID int16

type BluetoothDeviceNotification

type BluetoothDeviceNotification struct {
	MAC       [6]byte
	MACString string
	// false if this is a disconnect event
	Connected bool
	NewDevice bool
}

type BluetoothManager

type BluetoothManager interface {
	// Call StartDiscovery when the UI enters a "change controller
	// order/layout" screen.
	StartDiscovery()
	// Call StopDiscovery when the UI exits a "change controller order/layout"
	// screen. Paired controllers set up for auto-reconnect will still generate
	// device connect events.
	StopDiscovery()

	// Check for devices already connected to the system, send notifications
	// on NotifyChannel, and subscribe to future changes.
	InitialScan()

	// The UI code should call this after a L+R press to ensure clean
	// auto-reconnect.
	SavePairingInfo(mac [6]byte)
	// The UI code must provide a way for the user to reset auto-reconnect
	// records, which (Linux) will occur whenever the Joy-Con is connected to a
	// different Switch.
	DeletePairingInfo()

	NotifyChannel() <-chan BluetoothDeviceNotification
}

BluetoothManager provides an interface to the OS bluetooth stack.

type ButtonID

type ButtonID int16
const (
	Button_R_Y ButtonID = 0x000 + (1 << iota)
	Button_R_X
	Button_R_B
	Button_R_A
	Button_R_SR
	Button_R_SL
	Button_R_R
	Button_R_ZR
)

First byte of ButtonState.

const (
	Button_Minus ButtonID = 0x100 + (1 << iota)
	Button_Plus
	Button_R_Stick
	Button_L_Stick
	Button_Home
	Button_Capture
	Button_Unused1
	Button_IsChargeGrip
)

Middle byte of ButtonState.

const (
	Button_L_Down ButtonID = 0x200 + (1 << iota)
	Button_L_Up
	Button_L_Right
	Button_L_Left
	Button_L_SR
	Button_L_SL
	Button_L_L
	Button_L_ZL
)

Last byte of ButtonState.

func (ButtonID) GetIndex

func (b ButtonID) GetIndex() int

func (ButtonID) String

func (b ButtonID) String() string

type ButtonState

type ButtonState [3]byte

func ButtonsFromSlice

func ButtonsFromSlice(b []byte) ButtonState

ButtonsFromSlice copies the provided slice from a standard input report into a ButtonState.

func ConvertPushReport

func ConvertPushReport(side JoyConType, buttons []byte) ButtonState

ConvertPushReport converts a 0x3F "push" button press report into a ButtonState. Only for Left and Right Joy-Cons (Pro Controllers use a different format).

The report ID (0x3F) should be removed from the 'buttons' slice.

func (ButtonState) DiffMask

func (b ButtonState) DiffMask(other ButtonState) ButtonState

DiffMask returns a ButtonState with a '1' bit everywhere that this state differs from `other`.

func (ButtonState) Get

func (b ButtonState) Get(i ButtonID) bool

Get the state of a single ButtonID.

func (ButtonState) HasAll

func (b ButtonState) HasAll(mask ButtonState) bool

func (ButtonState) HasAny

func (b ButtonState) HasAny(mask ButtonState) bool

func (ButtonState) PairCheckDouble

func (b ButtonState) PairCheckDouble(b2 ButtonState) bool

Check for a double Joy-Con L+R press. Make sure not to call this if either of the two controllers is a Pro Controller.

func (ButtonState) PairCheckSelf

func (b ButtonState) PairCheckSelf() (selfPair bool, maybeDouble bool)

Check for a L+R press. If this might be half of a double Joy-Con, the second return value (maybeDouble) will be true.

func (ButtonState) Remove

func (b ButtonState) Remove(side JoyConType) ButtonState

func (ButtonState) Set

func (b ButtonState) Set(i ButtonID, state bool) ButtonState

func (ButtonState) Union

func (b ButtonState) Union(other ButtonState) ButtonState

Union returns a ButtonState with all 'on' positions contained in either argument.

type CombinedState

type CombinedState struct {
	// 3 frames of 6 values
	Gyro [3]GyroFrame
	// [left, right][horizontal, vertical]
	// range is -0x7FF to +0x7FF
	AdjSticks [2][2]int16
	Buttons   ButtonState
}

type Controller

type Controller interface {
	JoyConNotify
	BindToOutput(Output)

	// forwards to each JoyCon
	Rumble(d []RumbleData)

	OnFrame()

	Close() error
}

type GyroFrame

type GyroFrame [6]int16

type InputMode

type InputMode int

func (InputMode) NeedsEmptyRumbles

func (i InputMode) NeedsEmptyRumbles() bool

type InputRemappingOptions

type InputRemappingOptions struct {
	InvertedAxes []AxisID
}

InputRemappingOptions specifies if and how Buttons or Axes should be remapped currently only Axis-Inversion is implemented

type Interface

type Interface interface {
	JoyConNotify
	RemoveController(c Controller)
}

type JoyCon

type JoyCon interface {
	BindToController(Controller)
	BindToInterface(Interface)
	Serial() string
	Type() JoyConType

	// Returns true if a reconnect is needed - a communication error has occurred, and
	// Close() / Shutdown() have not been called.
	WantsReconnect() bool
	// Returns true if Close() or Shutdown() have been called.
	IsStopping() bool
	// Ask the JoyCon to disconnect and stay disconnected.
	Shutdown()
	// Must be of type github.com/GeertJohan/go.hid#DeviceInfo
	Reconnect(hidDeviceInfo interface{})

	Buttons() ButtonState
	// Indexed by [left,right][x,y]
	// Prefer use of ReadInto() for stick data
	RawSticks() [2][2]uint16
	Battery() (int8, bool)
	ReadInto(out *CombinedState, includeGyro bool)

	ChangeInputMode(mode InputMode) bool // returns false if impossible
	EnableGyro(status bool)
	SPIRead(addr uint32, len byte) ([]byte, error)
	SPIWrite(addr uint32, p []byte) error

	// Valid returns have alpha=255. If alpha=0 the value is not yet available.
	CaseColor() color.RGBA
	ButtonColor() color.RGBA

	Rumble(d []RumbleData)
	SendCustomSubcommand(d []byte)

	OnFrame()

	Close() error
}

type JoyConNotify

type JoyConNotify interface {
	JoyConUpdate(jc JoyCon, flags int)
}

type JoyConType

type JoyConType int
const (
	TypeInvalid JoyConType = iota
	TypeLeft
	TypeRight
	TypeBoth
)

func (JoyConType) IsLeft

func (t JoyConType) IsLeft() bool

func (JoyConType) IsRight

func (t JoyConType) IsRight() bool

func (JoyConType) String

func (t JoyConType) String() string

type Options

type Options struct {
	InputRemapping InputRemappingOptions
}

Options specifies Options for changing the programms behavior (for example obtained via cli-flags)

type Output

type Output interface {
	BeginUpdate() error
	ButtonUpdate(b ButtonID, value bool)
	StickUpdate(axis AxisID, value int16)
	GyroUpdate(vals GyroFrame)
	FlushUpdate() error

	OnFrame()
	Close() error
}

Output represents an OS-level event sink for a Controller object. The Controller should call BeginUpdate(), then several *Update() methods, followed by FlushUpdate().

type OutputFactory

type OutputFactory func(t JoyConType, playerNum int, remap InputRemappingOptions) (Output, error)

type RumbleData

type RumbleData struct {
	Data [8]byte
	// number of frames that Data remains the same
	Time int
}

Jump to

Keyboard shortcuts

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