joystick

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2018 License: MIT Imports: 9 Imported by: 8

README

joystick

Go Joystick API

GoDoc Build Status

Package joystick implements a Polled API to read the state of an attached joystick. Windows, Linux & OSX are supported. Package requires no external dependencies to be installed.

Mac OSX code developed by: https://github.com/ledyba

Installation:

$ go get github.com/simulatedsimian/joystick/...

Sample Program

$ go install github.com/simulatedsimian/joystick/joysticktest
$ joysticktest 0

Displays the state of the specified joystick

Example:

import "github.com/simulatedsimian/joystick"
js, err := joystick.Open(jsid)
if err != nil {
  panic(err)
}

fmt.Printf("Joystick Name: %s", js.Name())
fmt.Printf("   Axis Count: %d", js.AxisCount())
fmt.Printf(" Button Count: %d", js.ButtonCount())

state, err := joystick.Read()
if err != nil {
  panic(err)
}

fmt.Printf("Axis Data: %v", state.AxisData)
js.Close()

Documentation

Overview

Package joystick implements a Polled API to read the state of an attached joystick. currently Windows & Linux are supported. Package is pure go and requires no external dependencies

Installation:

go get github.com/simulatedsimian/joystick

Example:

js, err := joystick.Open(jsid)
if err != nil {
  panic(err)
}

fmt.Printf("Joystick Name: %s", js.Name())
fmt.Printf("   Axis Count: %d", js.AxisCount())
fmt.Printf(" Button Count: %d", js.ButtonCount())

state, err := joystick.Read()
if err != nil {
   panic(err)
}

fmt.Printf("Axis Data: %v", state.AxisData)
js.Close()

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Joystick

type Joystick interface {
	// AxisCount returns the number of Axis supported by this Joystick
	AxisCount() int
	// ButtonCount returns the number of buttons supported by this Joystick
	ButtonCount() int
	// Name returns the string name of this Joystick
	Name() string
	// Read returns the current State of the joystick.
	// On an error condition (for example, joystick has been unplugged) error is not nil
	Read() (State, error)
	// Close releases this joystick resource
	Close()
}

Interface Joystick provides access to the Joystick opened with the Open() function

func Open

func Open(id int) (Joystick, error)

Open opens the Joystick for reading, with the supplied id

Under linux the id is used to construct the joystick device name:

for example: id 0 will open device: "/dev/input/js0"

Under Windows the id is the actual numeric id of the joystick

If successful, a Joystick interface is returned which can be used to read the state of the joystick, else an error is returned

type State

type State struct {
	// Value of each axis as an integer in the range -32767 to 32768
	AxisData []int
	// The state of each button as a bit in a 32 bit integer. 1 = pressed, 0 = not pressed
	Buttons uint32
}

State holds the current state of the joystick

Directories

Path Synopsis
Simple program that displays the state of the specified joystick go run joysticktest.go 2 displays state of joystick id 2
Simple program that displays the state of the specified joystick go run joysticktest.go 2 displays state of joystick id 2

Jump to

Keyboard shortcuts

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