split wrap protocol parsing into input.button

This commit is contained in:
bel
2023-03-24 20:25:15 -06:00
parent 9990273b19
commit b319ed7e6d
8 changed files with 72 additions and 27 deletions

View File

@@ -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