split wrap protocol parsing into input.button
This commit is contained in:
@@ -2,6 +2,7 @@ package wrap
|
||||
|
||||
import (
|
||||
"context"
|
||||
"mayhem-party/src/device/input/button"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -61,7 +62,7 @@ func (b *Buffered) Close() {
|
||||
b.can()
|
||||
}
|
||||
|
||||
func (b *Buffered) Read() []Button {
|
||||
func (b *Buffered) Read() []button.Button {
|
||||
for b.ctx.Err() == nil {
|
||||
result := b.read()
|
||||
if len(result) > 0 {
|
||||
@@ -72,19 +73,19 @@ func (b *Buffered) Read() []Button {
|
||||
case <-time.After(b.listenInterval):
|
||||
}
|
||||
}
|
||||
return []Button{}
|
||||
return []button.Button{}
|
||||
}
|
||||
|
||||
func (b *Buffered) read() []Button {
|
||||
func (b *Buffered) read() []button.Button {
|
||||
b.lock.Lock()
|
||||
defer b.lock.Unlock()
|
||||
|
||||
result := make([]Button, 0, len(b.keys))
|
||||
result := make([]button.Button, 0, len(b.keys))
|
||||
for k, v := range b.keys {
|
||||
isFresh := v > 0
|
||||
isStale := v < 0 && time.Since(time.Unix(0, -1*v)) > b.expirationInterval
|
||||
if isFresh || isStale {
|
||||
result = append(result, Button{Char: k, Down: isFresh})
|
||||
result = append(result, button.Button{Char: k, Down: isFresh})
|
||||
}
|
||||
if isFresh {
|
||||
b.keys[k] = -1 * v
|
||||
|
||||
Reference in New Issue
Block a user