Loading joysticks.go +30 −10 Original line number Diff line number Diff line Loading @@ -3,6 +3,7 @@ package joysticks import ( "math" "time" //"fmt" ) // TODO drag event Loading @@ -10,12 +11,11 @@ import ( // TODO smoothed/hysteresis // TODO integrate event // TODO event repeater // TODO event duplicator // TODO repeat event dropper // TODO repeated event dropper var LongPressDelay = time.Second / 2 var DoublePressDelay = time.Second / 10 var DefaultRepeat = time.Second /4 type hatAxis struct { number uint8 Loading Loading @@ -72,13 +72,14 @@ type Event interface { } type when struct { time time.Duration Time time.Duration } func (b when) Moment() time.Duration { return b.time return b.Time } // button changed type ButtonEvent struct { when Loading Loading @@ -256,7 +257,7 @@ func Capture(registrees ...Channel) []chan Event { } // duplicate event onto two chan's func TeeEvents(c chan Event)(chan Event,chan Event){ func Duplicator(c chan Event)(chan Event,chan Event){ c1 := make(chan Event) c2 := make(chan Event) go func(){ Loading @@ -271,6 +272,29 @@ func TeeEvents(c chan Event)(chan Event,chan Event){ } // regularly repeat a when event, on receiving any event on the first parameter chan, until any event on second chan parameter. // remembers interval, so can make different duration Repeaters by changing DefaultRepeat. func Repeater(c1,c2 chan Event)(chan Event){ c := make(chan Event) interval:=DefaultRepeat go func(){ var ticker *time.Ticker for { e:= <-c1 go func(startTime time.Time){ ticker=time.NewTicker(interval) for t:=range ticker.C{ c <- when{e.Moment()+t.Sub(startTime)} } }(time.Now()) <-c2 ticker.Stop() } }() return c } // button changes event channel. func (d HID) OnButton(button uint8) chan Event { Loading Loading @@ -421,8 +445,4 @@ func (d HID) InsertSyntheticEvent(v int16, t uint8, i uint8) { } /* Hal3 Wed 26 Apr 19:04:12 BST 2017 go version go1.6.2 linux/amd64 FAIL _/home/simon/Dropbox/github/working/joysticks [build failed] Wed 26 Apr 19:04:20 BST 2017 */ joysticks_test.go +5 −5 Original line number Diff line number Diff line Loading @@ -27,7 +27,9 @@ func TestHIDsAdvanced(t *testing.T) { b2 := js1.OnClose(2) b3 := js1.OnClose(3) b4 := js1.OnClose(4) b5 := js1.OnClose(5) DefaultRepeat=time.Second/10 b5r := Repeater(js1.OnClose(5),js1.OnOpen(5)) quit := js1.OnOpen(10) h3 := js1.OnMove(1) h4 := js1.OnPanX(2) Loading @@ -49,10 +51,8 @@ func TestHIDsAdvanced(t *testing.T) { play(NewSound(NewTone(time.Second/250, 1), time.Second/3)) case <-b4: play(NewSound(NewTone(time.Second/150, 1), time.Second/3)) case <-b5: coord := make([]float32, 2) js1.HatCoords(1, coord) fmt.Println(coord) case <-b5r: go play(NewSound(NewTone(time.Second/440, 1), time.Second/20)) case h := <-h3: fmt.Printf("hat 1 moved %+v\n", h) case h := <-h4: Loading Loading
joysticks.go +30 −10 Original line number Diff line number Diff line Loading @@ -3,6 +3,7 @@ package joysticks import ( "math" "time" //"fmt" ) // TODO drag event Loading @@ -10,12 +11,11 @@ import ( // TODO smoothed/hysteresis // TODO integrate event // TODO event repeater // TODO event duplicator // TODO repeat event dropper // TODO repeated event dropper var LongPressDelay = time.Second / 2 var DoublePressDelay = time.Second / 10 var DefaultRepeat = time.Second /4 type hatAxis struct { number uint8 Loading Loading @@ -72,13 +72,14 @@ type Event interface { } type when struct { time time.Duration Time time.Duration } func (b when) Moment() time.Duration { return b.time return b.Time } // button changed type ButtonEvent struct { when Loading Loading @@ -256,7 +257,7 @@ func Capture(registrees ...Channel) []chan Event { } // duplicate event onto two chan's func TeeEvents(c chan Event)(chan Event,chan Event){ func Duplicator(c chan Event)(chan Event,chan Event){ c1 := make(chan Event) c2 := make(chan Event) go func(){ Loading @@ -271,6 +272,29 @@ func TeeEvents(c chan Event)(chan Event,chan Event){ } // regularly repeat a when event, on receiving any event on the first parameter chan, until any event on second chan parameter. // remembers interval, so can make different duration Repeaters by changing DefaultRepeat. func Repeater(c1,c2 chan Event)(chan Event){ c := make(chan Event) interval:=DefaultRepeat go func(){ var ticker *time.Ticker for { e:= <-c1 go func(startTime time.Time){ ticker=time.NewTicker(interval) for t:=range ticker.C{ c <- when{e.Moment()+t.Sub(startTime)} } }(time.Now()) <-c2 ticker.Stop() } }() return c } // button changes event channel. func (d HID) OnButton(button uint8) chan Event { Loading Loading @@ -421,8 +445,4 @@ func (d HID) InsertSyntheticEvent(v int16, t uint8, i uint8) { } /* Hal3 Wed 26 Apr 19:04:12 BST 2017 go version go1.6.2 linux/amd64 FAIL _/home/simon/Dropbox/github/working/joysticks [build failed] Wed 26 Apr 19:04:20 BST 2017 */
joysticks_test.go +5 −5 Original line number Diff line number Diff line Loading @@ -27,7 +27,9 @@ func TestHIDsAdvanced(t *testing.T) { b2 := js1.OnClose(2) b3 := js1.OnClose(3) b4 := js1.OnClose(4) b5 := js1.OnClose(5) DefaultRepeat=time.Second/10 b5r := Repeater(js1.OnClose(5),js1.OnOpen(5)) quit := js1.OnOpen(10) h3 := js1.OnMove(1) h4 := js1.OnPanX(2) Loading @@ -49,10 +51,8 @@ func TestHIDsAdvanced(t *testing.T) { play(NewSound(NewTone(time.Second/250, 1), time.Second/3)) case <-b4: play(NewSound(NewTone(time.Second/150, 1), time.Second/3)) case <-b5: coord := make([]float32, 2) js1.HatCoords(1, coord) fmt.Println(coord) case <-b5r: go play(NewSound(NewTone(time.Second/440, 1), time.Second/20)) case h := <-h3: fmt.Printf("hat 1 moved %+v\n", h) case h := <-h4: Loading