x52

package
v0.0.0-...-52065a0 Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2025 License: BSD-3-Clause Imports: 9 Imported by: 0

README

Saitek X52/X52Pro Library

The Saitek X52/X52Pro library is used to interface with a supported Saitek X52/X52Pro joystick. The library uses the gousb library to communicate with the joystick.

API Overview

In order to use the library, you will need to create a connection. All joystick LED and MFD data is managed through the connection. You should close the connection when you no longer need it.

cnn := x52.NewConnection()
defer cnn.Close()

Unlike the corresponding C library, connection creation does not require you to have the joystick plugged in until you wish to actually update it. Therefore, you will need to call OpenDevice to actually connect to the device. The OpenDevice will return a nil error if it was successful in connecting to the joystick, and a corresponding error value if it failed for any reason.

err := cnn.OpenDevice()
if err != nil {
    // ...
}

The library also provides a corresponding CloseDevice method, which can be used when the application no longer needs access to the joystick. However, the library will automatically call CloseDevice when the connection is closed.

All the Set* methods set internal data structures within the connection, and don't actually update the joystick. In order to do so, the application must call the Update method of the connection.

err = cnn.SetLed(x52.LedFire, x52.LedOff)
if err != nil {
    // ...
}
// Write the updated state to the joystick
err = cnn.Update()

The library will also check and close the device automatically if updating it fails because the joystick was unplugged.

LED and MFD control

Currently, the library supports setting the state of all LEDs, the brightness of the LEDs and MFD, and the text on the MFD. The update API is still not working, so the sets won't take effect anyway. The API for these can be considered frozen. The library does NOT yet support setting the date and time display on the MFD, and the API for this is still a work in progress.

Limitations

The library can maintain a connection to only 1 supported device at a time. This means that if you, for some reason, have multiple X52 joysticks connected, only 1 of them will be controlled by the library. No guarantees are made about which of these will be selected, as this is a function of the order in which the devices are enumerated by the USB subsystem.

Documentation

Overview

SPDX-FileCopyrightText: 2025 Peter Magnusson <me@kmpm.se>

SPDX-License-Identifier: BSD-3-Clause

SPDX-FileCopyrightText: 2020-2021 Nirenjan Krishnan

SPDX-License-Identifier: BSD-3-Clause

SPDX-FileCopyrightText: 2020-2021 Nirenjan Krishnan SPDX-FileCopyrightText: 2025 Peter Magnusson

SPDX-License-Identifier: BSD-3-Clause

SPDX-FileCopyrightText: 2020-2021 Nirenjan Krishnan SPDX-FileCopyrightText: 2025 Peter Magnusson

SPDX-License-Identifier: BSD-3-Clause

SPDX-FileCopyrightText: 2025 Peter Magnusson

SPDX-License-Identifier: BSD-3-Clause

SPDX-FileCopyrightText: 2020-2021 Nirenjan Krishnan SPDX-FileCopyrightText: 2025 Peter Magnusson

SPDX-License-Identifier: BSD-3-Clause

Index

Constants

View Source
const (
	LedFire     = Led(0x01)
	LedA        = Led(0x02)
	LedB        = Led(0x04)
	LedD        = Led(0x06)
	LedE        = Led(0x08)
	LedT1       = Led(0x0a)
	LedT2       = Led(0x0c)
	LedT3       = Led(0x0e)
	LedPOV      = Led(0x10)
	LedClutch   = Led(0x12)
	LedThrottle = Led(0x14)
)

LED Identifiers

View Source
const (
	FeatureLed uint32 = iota
)

Feature flags

Variables

View Source
var ErrInvalidAlignment = errors.New("invalid alignment")
View Source
var ErrInvalidLine = errors.New("invalid line")

Functions

This section is empty.

Types

type Action

type Action interface {
	SetMfdBrightness(v uint16) Action
	SetMfdText(line uint8, s string) Action
	SetMfdTextAlign(line uint8, s string, align TextAlignment) Action
	SetMfdShift(state bool) Action
	SetLed(led Led, state LedState) Action
	SetLedBrightness(v uint16) Action
	Execute() error
}

Action represents chainable actions to be executed on a Saitek X52 Pro joystick. Always call Execute() to execute the actions.

func Do

func Do(xcn *Context) Action

Do creates a new Action instance for the given Saitek X52 Pro joystick connection.

type Button

type Button int

Button definitions

const (
	BtnTrigger Button = iota
	BtnTrigger2
	BtnFire
	BtnPinky
	BtnA
	BtnB
	BtnC
	BtnD
	BtnE
	BtnT1Up
	BtnT1Dn
	BtnT2Up
	BtnT2Dn
	BtnT3Up
	BtnT3Dn
	BtnPOV1N // Stick POV North
	BtnPOV1E // Stick POV East
	BtnPOV1S // Stick POV South
	BtnPOV1W // Stick POV West
	BtnPOV2N // Throttle POV North
	BtnPOV2E // Throttle POV East
	BtnPOV2S // Throttle POV South
	BtnPOV2W // Throttle POV West
	BtnClutch
	BtnMousePrimary
	BtnMouseSecondary
	BtnMouseScrollUp
	BtnMouseScrollDn
	BtnFunction
	BtnStartStop
	BtnReset
	BtnPgUp   // X52 Pro only
	BtnPgDn   // X52 Pro only
	BtnUp     // X52 Pro only
	BtnDn     // X52 Pro only
	BtnSelect // X52 Pro only
	BtnMode1
	BtnMode2
	BtnMode3
	ButtonMax
)

func (Button) String

func (b Button) String() string

type ClockFormat

type ClockFormat uint

ClockFormat describes the time format on the MFD

const (
	ClockFormat12Hr ClockFormat = iota
	ClockFormat24Hr
)

Clock Format Identifiers

type ClockID

type ClockID uint

ClockID identifies the clock on the X52 multifunction display

const (
	Clock1 ClockID = iota
	Clock2
	Clock3
)

Clock Identifiers

type Context

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

Context manages all resources related to device handling

func NewContext

func NewContext() *Context

NewContext returns a new Connection against which to run device operations

func (*Context) Close

func (ctx *Context) Close() error

Close closes the connection, and any devices that may have been opened will also be closed

func (*Context) Debug

func (ctx *Context) Debug(level LogLevel)

Debug changes the debug level. Level 0 means no debug, higher levels will print out more debugging information.

func (*Context) DebugUSB

func (ctx *Context) DebugUSB(level LogLevel)

DebugUSB changes the debug level of the USB subsystem. Level 0 means no debug, higher levels will print out more debugging information.

func (*Context) Do

func (ctx *Context) Do() Action

func (*Context) HasFeature

func (ctx *Context) HasFeature(feature uint32) bool

HasFeature returns true if the X52 device supports the requested feature

func (*Context) Open

func (ctx *Context) Open() bool

Open will try to connect to a supported X52/X52Pro joystick. If the joystick is plugged in and the function succeeds, it returns true, otherwise it returns false. If multiple supported devices are plugged in, then it will pick one of the supported devices in an unspecified manner.

func (*Context) Raw

func (ctx *Context) Raw(index, value uint16) error

Raw sends a raw vendor control packet to the device

func (*Context) ReadInput

func (ctx *Context) ReadInput(cctx context.Context, cb func(r *Report)) error

func (*Context) Reset

func (ctx *Context) Reset() error

Reset resets the connected device. If there is no device connected, it returns errNotConnected, otherwise it will return a corresponding USB error

func (ctx *Context) SetBlink(enable bool) error

SetBlink will enable or disable the blink functionality

func (*Context) SetClockFormat

func (ctx *Context) SetClockFormat(clock ClockID, format ClockFormat) error

SetClockFormat sets the clock format of the given clock

func (*Context) SetDateFormat

func (ctx *Context) SetDateFormat(format DateFormat) error

SetDateFormat sets the date format

func (*Context) SetLed

func (ctx *Context) SetLed(led Led, state LedState) error

SetLed sets the state of the given LED. Not all LEDs support all states, LedFire and LedThrottle only support LedOn and LedOff states, the remaining LEDs support every state except LedOn. **Limitation**: This function will not work on a non-pro X52 at this time.

func (*Context) SetLedBrightness

func (ctx *Context) SetLedBrightness(brightness uint16) error

SetLedBrightness will set the brightness of the LED.

func (*Context) SetLocation

func (ctx *Context) SetLocation(clock ClockID, loc *time.Location) error

SetLocation updates the location of the given clock. You may not update the location of the primary clock, as it is computed when you call SetTime

func (*Context) SetMfdBrightness

func (ctx *Context) SetMfdBrightness(brightness uint16) error

SetMfdBrightness will set the brightness of the MFD.

func (*Context) SetMfdText

func (ctx *Context) SetMfdText(line uint8, data []byte) error

SetMfdText sets the display on the given MFD line. The data must be in the code page recognized by the MFD. This function only accepts line lengths of up to 16 bytes, with any additional data being silently discarded.

func (*Context) SetShift

func (ctx *Context) SetShift(enable bool) error

SetShift will enable or disable the shift functionality

func (*Context) SetTime

func (ctx *Context) SetTime(t time.Time) error

SetTime sets the time of the primary clock. The secondary and tertiary clocks are derived by setting a programmable offset from the primary clock.

func (*Context) Update

func (ctx *Context) Update() error

Update updates the X52 with the saved data

type DateFormat

type DateFormat uint

DateFormat describes the date format on the MFD

const (
	DateFormatDDMMYY DateFormat = iota
	DateFormatMMDDYY
	DateFormatYYMMDD
)

Date Format Identifiers

type Led

type Led uint

Led contains the supported LEDs

func AtoLed

func AtoLed(s string) (Led, error)

AtoLed converts a string to a LED

func (Led) String

func (led Led) String() string

String returns a string representation of the LED

type LedState

type LedState uint

LedState contains the possible LED states. Each LED only supports a subset of the states

const (
	LedOff LedState = iota
	LedOn
	LedRed
	LedAmber
	LedGreen
)

LED State Identifiers

func AtoState

func AtoState(s string) (LedState, error)

AtoState converts a string to a LedState

func (LedState) String

func (state LedState) String() string

String returns a string representation of the LED state

type LogLevel

type LogLevel int
const (
	LogNone LogLevel = iota
	LogError
	LogWarning
	LogInfo
	LogDebug
)

The logging levels correspond to the same levels in libusb

type Report

type Report struct {
	// Axes
	X        uint16
	Y        uint16
	Rz       uint16 // Twist
	Throttle uint8
	Rx       uint8 // Rotary X, Around I button
	Ry       uint8 // Rotary Y, Around E button
	Slider   uint8

	// Buttons (39 bits for Pro, 34 for regular)
	Buttons uint64

	// Hat and thumbstick
	Hat    uint8
	MouseX uint8
	MouseY uint8

	IsPro bool
}

func (*Report) ParseButtons

func (r *Report) ParseButtons() (buttons []Button, mode int)

type TextAlignment

type TextAlignment int
const (
	AlignLeft TextAlignment = iota
	AlignCenter
	AlignRight
)

Directories

Path Synopsis
Package util provides utility functions to simplify controlling the X52
Package util provides utility functions to simplify controlling the X52

Jump to

Keyboard shortcuts

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