Commit 23e64852 authored by Boris Mühmer's avatar Boris Mühmer
Browse files

make linter happy

parent 688f3ffe
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
/*
Package joysticks, provides simplified event routing, through channels, from the Linux joystick driver File-like interface.
Package joysticks provides simplified event routing, through channels, from the Linux joystick driver File-like interface.

events can be listened for from any thread, dynamically re-mapped and simulated.

+36 −38
Original line number Diff line number Diff line
@@ -6,8 +6,11 @@ import (
	//"fmt"
)

var LongPressDelay = time.Second / 2
var DoublePressDelay = time.Second / 10
// some constants
var (
	LongPressDelay   = time.Second / 2
	DoublePressDelay = time.Second / 10
)

type hatAxis struct {
	number   uint8
@@ -58,7 +61,7 @@ type HID struct {
	Events   map[eventSignature]chan Event
}

// Events always have the time they occurred.
// Event always have the time they occurred.
type Event interface {
	Moment() time.Duration
}
@@ -71,15 +74,14 @@ func (b when) Moment() time.Duration {
	return b.Time
}


// button changed
// ButtonEvent is raised when a button changed
type ButtonEvent struct {
	when
	number uint8
	value  bool
}

// hat changed
// HatEvent is raised when a hat changed
type HatEvent struct {
	when
	number uint8
@@ -87,25 +89,25 @@ type HatEvent struct {
	value  float32
}

// Hat position event type. X,Y{-1...1}
// CoordsEvent holds hat position event type. X,Y{-1...1}
type CoordsEvent struct {
	when
	X, Y float32
}

// Hat Axis event type. V{-1...1}
// AxisEvent holds Hat Axis event type. V{-1...1}
type AxisEvent struct {
	when
	V float32
}

// Hat angle event type. Angle{-Pi...Pi}
// AngleEvent holds Hat angle event type. Angle{-Pi...Pi}
type AngleEvent struct {
	when
	Angle float32
}

// Hat radius event type. Radius{0...√2}
// RadiusEvent holds Hat radius event type. Radius{0...√2}
type RadiusEvent struct {
	when
	Radius float32
@@ -222,7 +224,7 @@ func (d HID) ParcelOutEvents() {
	}
}

// Type of register-able methods and the index they are called with. (Note: the event type is indicated by the method.)
// Channel type of register-able methods and the index they are called with. (Note: the event type is indicated by the method.)
type Channel struct {
	Number uint8
	Method func(HID, uint8) chan Event
@@ -248,99 +250,98 @@ func Capture(registrees ...Channel) []chan Event {
	return chans
}


// button changes event channel.
// OnButton handles button changes event channel.
func (d HID) OnButton(index uint8) chan Event {
	c := make(chan Event)
	d.Events[eventSignature{buttonChange, index}] = c
	return c
}

// button goes open event channel.
// OnOpen handles button goes open event channel.
func (d HID) OnOpen(index uint8) chan Event {
	c := make(chan Event)
	d.Events[eventSignature{buttonOpen, index}] = c
	return c
}

// button goes closed event channel.
// OnClose handles button goes closed event channel.
func (d HID) OnClose(index uint8) chan Event {
	c := make(chan Event)
	d.Events[eventSignature{buttonClose, index}] = c
	return c
}

// button goes open and the previous event, closed, was more than LongPressDelay ago, event channel.
// OnLong handles button goes open and the previous event, closed, was more than LongPressDelay ago, event channel.
func (d HID) OnLong(index uint8) chan Event {
	c := make(chan Event)
	d.Events[eventSignature{buttonLongPress, index}] = c
	return c
}

// button goes closed and the previous event, open, was less than DoublePressDelay ago, event channel.
// OnDouble handles button goes closed and the previous event, open, was less than DoublePressDelay ago, event channel.
func (d HID) OnDouble(index uint8) chan Event {
	c := make(chan Event)
	d.Events[eventSignature{buttonDoublePress, index}] = c
	return c
}

// hat moved event channel.
// OnHat handles hat moved event channel.
func (d HID) OnHat(index uint8) chan Event {
	c := make(chan Event)
	d.Events[eventSignature{hatChange, index}] = c
	return c
}

// hat position changed event channel.
// OnMove handles hat position changed event channel.
func (d HID) OnMove(index uint8) chan Event {
	c := make(chan Event)
	d.Events[eventSignature{hatPosition, index}] = c
	return c
}

// hat axis-X moved event channel.
// OnPanX handles hat axis-X moved event channel.
func (d HID) OnPanX(index uint8) chan Event {
	c := make(chan Event)
	d.Events[eventSignature{hatPanX, index}] = c
	return c
}

// hat axis-Y moved event channel.
// OnPanY handles hat axis-Y moved event channel.
func (d HID) OnPanY(index uint8) chan Event {
	c := make(chan Event)
	d.Events[eventSignature{hatPanY, index}] = c
	return c
}

// hat axis-X speed changed event channel.
// OnSpeedX handles hat axis-X speed changed event channel.
func (d HID) OnSpeedX(index uint8) chan Event {
	c := make(chan Event)
	d.Events[eventSignature{hatVelocityX, index}] = c
	return c
}

// hat axis-Y speed changed event channel.
// OnSpeedY handles hat axis-Y speed changed event channel.
func (d HID) OnSpeedY(index uint8) chan Event {
	c := make(chan Event)
	d.Events[eventSignature{hatVelocityY, index}] = c
	return c
}

// hat angle changed event channel.
// OnRotate handles hat angle changed event channel.
func (d HID) OnRotate(index uint8) chan Event {
	c := make(chan Event)
	d.Events[eventSignature{hatAngle, index}] = c
	return c
}

// hat moved event channel.
// OnCenter handles hat moved event channel.
func (d HID) OnCenter(index uint8) chan Event {
	c := make(chan Event)
	d.Events[eventSignature{hatCentered, index}] = c
	return c
}

// hat moved to edge
// OnEdge handles hat moved to edge.
func (d HID) OnEdge(index uint8) chan Event {
	c := make(chan Event)
	d.Events[eventSignature{hatEdge, index}] = c
@@ -356,8 +357,7 @@ func (d HID) OnEdge(index uint8) chan Event {
//	return c
//}


// see if Button exists.
// ButtonExists checks if Button exists.
func (d HID) ButtonExists(index uint8) (ok bool) {
	for _, v := range d.Buttons {
		if v.number == index {
@@ -367,7 +367,7 @@ func (d HID) ButtonExists(index uint8) (ok bool) {
	return
}

// see if Hat exists.
// HatExists checks if Hat exists.
func (d HID) HatExists(index uint8) (ok bool) {
	for _, v := range d.HatAxes {
		if v.number == index {
@@ -377,12 +377,12 @@ func (d HID) HatExists(index uint8) (ok bool) {
	return
}

// Button current state.
// ButtonClosed checks Button current state.
func (d HID) ButtonClosed(index uint8) bool {
	return d.Buttons[index].value
}

// Hat latest position.
// HatCoords return Hat latest position.
// provided coords slice needs to be long enough to hold all the hat's axis.
func (d HID) HatCoords(index uint8, coords []float32) {
	for _, h := range d.HatAxes {
@@ -393,9 +393,7 @@ func (d HID) HatCoords(index uint8, coords []float32) {
	return
}

// insert events as if from hardware.
// InsertSyntheticEvent insert events as if from hardware.
func (d HID) InsertSyntheticEvent(v int16, t uint8, i uint8) {
	d.OSEvents <- osEventRecord{Value: v, Type: t, Index: i}
}

+4 −5
Original line number Diff line number Diff line
@@ -23,7 +23,7 @@ const maxValue = 1<<15 - 1
// common path root, so Connect and DeviceExists are not thread safe.
var inputPathSlice = []byte("/dev/input/js ")[0:13]

// see if Device exists.
// DeviceExists checks if Device exists.
func DeviceExists(index uint8) bool {
	_,err:=os.Stat(string(strconv.AppendUint(inputPathSlice, uint64(index-1), 10)))
	return err==nil
@@ -52,20 +52,19 @@ func (d HID) populate() {
		switch evt.Type {
		case 0x81:
			d.Buttons[evt.Index] = button{uint8(buttonNumber), toDuration(evt.Time), evt.Value != 0}
			buttonNumber += 1
			buttonNumber++
		case 0x82:
			d.HatAxes[evt.Index] = hatAxis{uint8(hatNumber), uint8(axisNumber), false, toDuration(evt.Time), float32(evt.Value) / maxValue}
			axisNumber += 1
			axisNumber++
			if axisNumber > 2 {
				axisNumber = 1
				hatNumber += 1
				hatNumber++
			}
		default:
			go func() { d.OSEvents <- evt }() // have to consume a real event to know we reached the end of the synthetic burst, so refire it.
			return
		}
	}
	return
}

// pipe any readable events onto channel.
+48 −47
Original line number Diff line number Diff line
@@ -12,11 +12,13 @@ import (
// TODO coords to two pans
// TODO 1-d integrator

var DefaultRepeat = time.Second /4
var VelocityRepeat =  time.Second / 10

// some variables
var (
	DefaultRepeat  = time.Second / 4
	VelocityRepeat = time.Second / 10
)

// duplicate event onto two chan's
// Duplicator duplicates event onto two chan's
func Duplicator(c chan Event) (chan Event, chan Event) {
	c1 := make(chan Event)
	c2 := make(chan Event)
@@ -31,8 +33,7 @@ func Duplicator(c chan Event)(chan Event,chan Event){
	return c1, c2
}


// creates a chan on which you get CoordsEvent's that are the time integration of the CoordsEvent's on the parameter chan. 
// PositionFromVelocity creates a chan on which you get CoordsEvent's that are the time integration of the CoordsEvent's on the parameter chan.
func PositionFromVelocity(c chan Event) chan Event {
	extra := make(chan Event)
	var x, y, vx, vy float32
@@ -76,10 +77,11 @@ func PositionFromVelocity(c chan Event) chan Event{
	return extra
}


// creates a channel that, after receiving any event on the first parameter chan, and until any event on second chan parameter, regularly receives 'when' events.
// the repeat interval is DefaultRepeat, and is stored, so retriggering is not effected by changing DefaultRepeat.
func Repeater(c1,c2 chan Event)(chan Event){
// Repeater creates a channel that, after receiving any event on the first
// parameter chan, and until any event on second chan parameter, regularly
// receives 'when' events. the repeat interval is DefaultRepeat, and is stored,
// so retriggering is not effected by changing DefaultRepeat.
func Repeater(c1, c2 chan Event) chan Event {
	c := make(chan Event)
	go func() {
		interval := DefaultRepeat
@@ -98,4 +100,3 @@ func Repeater(c1,c2 chan Event)(chan Event){
	}()
	return c
}