Commit 41305524 authored by simon's avatar simon
Browse files

PositionFromVelocity

parent 7a13f97f
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -5,7 +5,9 @@ uses Linux kernel 'input' interface, available on a wide range of linux devices,

uses go channels to pipe around events, for flexibility and multi-threading.

implements many high-level UI abstractions directly, to enable commonality between projects.(see On???() HID methods in docs)
make event channels from methods on HID type.  'HID.On***()'  

also some higher-level UI abstractions to help standard usage.

Overview/docs: [![GoDoc](https://godoc.org/github.com/splace/joysticks?status.svg)](https://godoc.org/github.com/splace/joysticks)

+2 −2
Original line number Diff line number Diff line
@@ -15,7 +15,7 @@ Use methods to add (or alter) 'Event' channels.

Start running by calling 'ParcelOutEvents()'.

(event index to channel mappings can be changed dynamically.)
(unlike highlevel, event index to channel mappings can be changed dynamically.)

Lowlevel

@@ -27,7 +27,7 @@ Interface

'Event' interface, provides a time.Duration through a call to the Moment() method, returning whatever the underlying Linux driver provides as the events timestamp, as a time.Duration.

returned 'Event's need asserting to their underlying type to access data other than moment.
returned 'Event's need asserting to their underlying type ( '***Event' ) to access data other than moment.

*/
package joysticks
+30 −3
Original line number Diff line number Diff line
@@ -8,9 +8,9 @@ import (

// TODO drag event
// TODO move plus edge continue events (self generating)
// TODO smoothed/hysteresis
// TODO smoother from PID

// TODO integrate event
// TODO integration event
// TODO repeated event dropper

var LongPressDelay = time.Second / 2
@@ -271,6 +271,34 @@ func Duplicator(c chan Event)(chan Event,chan Event){
	return c1,c2
}

// 
func PositionFromVelocity(c chan Event) chan Event{
	extra := make(chan Event)
	go func(){
		var x,y float32
		var lt time.Duration
		for e:=range c{
			if ce,ok:= e.(CoordsEvent);ok{
				lt=ce.Moment()
				break
			}
		}		
		for e:=range c{
			if ce,ok:=e.(CoordsEvent);ok{
				dt:=(ce.Moment()-lt).Seconds()
				if dt>0 {
					x+=float32(float64(ce.X)/dt)
					y+=float32(float64(ce.Y)/dt)
					lt=	ce.Moment()
					extra <-CoordsEvent{when{e.Moment()},x,y}			
					lt=	ce.Moment()
				}
			}
		}
	}()
	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.
@@ -445,4 +473,3 @@ func (d HID) InsertSyntheticEvent(v int16, t uint8, i uint8) {
}

+3 −2
Original line number Diff line number Diff line
@@ -31,7 +31,7 @@ func TestHIDsAdvanced(t *testing.T) {
	b5r := Repeater(js1.OnClose(5),js1.OnOpen(5))
	
	quit := js1.OnOpen(10)
	h3 := js1.OnMove(1)
	h3 :=  PositionFromVelocity(js1.OnMove(1))
	h4 := js1.OnPanX(2)
	h5 := js1.OnPanY(2)
	h6 := js1.OnEdge(1)
@@ -100,10 +100,11 @@ func TestHIDsMutipleCapture(t *testing.T) {
		Channel{1, HID.OnClose}, 
	)
	hatEvents := Capture(
		Channel{1, HID.OnMove},  
		Channel{1, HID.OnHat},  
		Channel{2, HID.OnSpeedX},
		Channel{2, HID.OnPanX},
	)

	var x float32 = .5
	var f time.Duration = time.Second / 440
	for {