Fix button timing and event decoding and avoid redundant menu refreshes
This commit is contained in:
8 files changed
+276
-44
No files matched your search
@@ -10,7 +10,7 @@ import (
|
||||
"github.com/BeatGlow/beatglow/hardware"
|
||||
)
|
||||
|
||||
var LongPressDuration = 350 * time.Millisecond
|
||||
var LongPressDuration = 750 * time.Millisecond
|
||||
|
||||
type Encoder interface {
|
||||
hardware.Driver
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
package input
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"io"
|
||||
"time"
|
||||
)
|
||||
|
||||
// linuxInputEvent holds a decoded evdev record, independent of native word size.
|
||||
type linuxInputEvent struct {
|
||||
Secs, USecs int64
|
||||
Type, Code uint16
|
||||
Value int32
|
||||
}
|
||||
|
||||
func (e linuxInputEvent) String() string {
|
||||
return fmt.Sprintf("time=%s type=%#04x code=%#04x value=%d", e.Time(), e.Type, e.Code, e.Value)
|
||||
}
|
||||
|
||||
func (e linuxInputEvent) Time() time.Time {
|
||||
return time.Unix(e.Secs, e.USecs*int64(time.Microsecond))
|
||||
}
|
||||
|
||||
// readLinuxInputEvent reads one little-endian Linux event for a 4- or 8-byte
|
||||
// native word. Short records return the error from io.ReadFull.
|
||||
func readLinuxInputEvent(r io.Reader, wordSize int) (linuxInputEvent, error) {
|
||||
var e linuxInputEvent
|
||||
if wordSize != 4 && wordSize != 8 {
|
||||
return e, fmt.Errorf("input: unsupported word size %d", wordSize)
|
||||
}
|
||||
var data [24]byte
|
||||
if _, err := io.ReadFull(r, data[:2*wordSize+8]); err != nil {
|
||||
return e, err
|
||||
}
|
||||
if wordSize == 4 {
|
||||
e.Secs = int64(int32(binary.LittleEndian.Uint32(data[:4])))
|
||||
e.USecs = int64(binary.LittleEndian.Uint32(data[4:8]))
|
||||
} else {
|
||||
e.Secs = int64(binary.LittleEndian.Uint64(data[:8]))
|
||||
e.USecs = int64(binary.LittleEndian.Uint64(data[8:16]))
|
||||
}
|
||||
offset := 2 * wordSize
|
||||
e.Type = binary.LittleEndian.Uint16(data[offset:])
|
||||
e.Code = binary.LittleEndian.Uint16(data[offset+2:])
|
||||
e.Value = int32(binary.LittleEndian.Uint32(data[offset+4:]))
|
||||
return e, nil
|
||||
}
|
||||
|
||||
// buttonState pairs one press with one release and ignores contact bounce.
|
||||
// Access is confined to the button's event-reading goroutine.
|
||||
type buttonState struct {
|
||||
pressedAt, releasedAt time.Time
|
||||
pressed bool
|
||||
}
|
||||
|
||||
func (b *buttonState) press(at time.Time) {
|
||||
if b.pressed || (!b.releasedAt.IsZero() && at.Sub(b.releasedAt) < 30*time.Millisecond) {
|
||||
return
|
||||
}
|
||||
b.pressedAt, b.pressed = at, true
|
||||
}
|
||||
|
||||
func (b *buttonState) release(at time.Time) (time.Duration, bool) {
|
||||
if !b.pressed {
|
||||
return 0, false
|
||||
}
|
||||
b.pressed = false
|
||||
b.releasedAt = at
|
||||
duration := at.Sub(b.pressedAt)
|
||||
return duration, duration >= 0
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
package input
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"io"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestInputEventRecordSizes(t *testing.T) {
|
||||
for _, wordSize := range []int{4, 8} {
|
||||
var stream bytes.Buffer
|
||||
for _, value := range []int32{1, 0} {
|
||||
if wordSize == 4 {
|
||||
binary.Write(&stream, binary.LittleEndian, uint32(100))
|
||||
binary.Write(&stream, binary.LittleEndian, uint32(950000))
|
||||
} else {
|
||||
binary.Write(&stream, binary.LittleEndian, uint64(100))
|
||||
binary.Write(&stream, binary.LittleEndian, uint64(950000))
|
||||
}
|
||||
binary.Write(&stream, binary.LittleEndian, uint16(1))
|
||||
binary.Write(&stream, binary.LittleEndian, uint16(28))
|
||||
binary.Write(&stream, binary.LittleEndian, value)
|
||||
}
|
||||
for _, value := range []int32{1, 0} {
|
||||
event, err := readLinuxInputEvent(&stream, wordSize)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if event.Type != 1 || event.Code != 28 || event.Value != value {
|
||||
t.Fatalf("wordSize=%d: misaligned event %+v", wordSize, event)
|
||||
}
|
||||
if !event.Time().Equal(time.Unix(100, 950000000)) {
|
||||
t.Fatal("microseconds decoded incorrectly")
|
||||
}
|
||||
}
|
||||
if _, err := readLinuxInputEvent(&stream, wordSize); !errors.Is(err, io.EOF) {
|
||||
t.Fatalf("got %v, want EOF", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestButtonShortPressAcrossSecondBoundary(t *testing.T) {
|
||||
var b buttonState
|
||||
b.press((linuxInputEvent{Secs: 100, USecs: 950000}).Time())
|
||||
duration, ok := b.release((linuxInputEvent{Secs: 101, USecs: 50000}).Time())
|
||||
if !ok || duration != 100*time.Millisecond {
|
||||
t.Fatalf("got %s, %v, want 100ms short press", duration, ok)
|
||||
}
|
||||
}
|
||||
|
||||
func TestButtonIgnoresDuplicateAndBouncedEvents(t *testing.T) {
|
||||
var b buttonState
|
||||
start := time.Unix(100, 0)
|
||||
if _, ok := b.release(start); ok {
|
||||
t.Fatal("unpaired release produced a click")
|
||||
}
|
||||
b.press(start)
|
||||
b.press(start.Add(200 * time.Millisecond))
|
||||
if duration, ok := b.release(start.Add(500 * time.Millisecond)); !ok || duration != 500*time.Millisecond {
|
||||
t.Fatal("repeated press reset the long-press timer")
|
||||
}
|
||||
if _, ok := b.release(start.Add(501 * time.Millisecond)); ok {
|
||||
t.Fatal("duplicate release produced a second click")
|
||||
}
|
||||
b.press(start.Add(505 * time.Millisecond))
|
||||
if _, ok := b.release(start.Add(510 * time.Millisecond)); ok {
|
||||
t.Fatal("contact bounce produced another click")
|
||||
}
|
||||
b.press(start.Add(time.Second))
|
||||
if duration, ok := b.release(start.Add(1100 * time.Millisecond)); !ok || duration != 100*time.Millisecond {
|
||||
t.Fatal("subsequent intentional click lost")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTruncatedInputEvent(t *testing.T) {
|
||||
for _, wordSize := range []int{4, 8} {
|
||||
_, err := readLinuxInputEvent(bytes.NewReader(make([]byte, 2*wordSize+7)), wordSize)
|
||||
if !errors.Is(err, io.ErrUnexpectedEOF) {
|
||||
t.Fatalf("got %v, want unexpected EOF", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,11 @@
|
||||
package input
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
|
||||
@@ -168,32 +166,16 @@ func linuxPheripherals() (ps []*linuxPheripheral, err error) {
|
||||
return ps, nil
|
||||
}
|
||||
|
||||
type linuxInputEvent struct {
|
||||
Secs uint32
|
||||
USecs uint32
|
||||
Type uint16
|
||||
Code uint16
|
||||
Value int32
|
||||
}
|
||||
|
||||
func (e linuxInputEvent) String() string {
|
||||
return fmt.Sprintf("time=%s type=%#04x code=%#04x value=%d", e.Time(), e.Type, e.Code, e.Value)
|
||||
}
|
||||
|
||||
func (e linuxInputEvent) Time() time.Time {
|
||||
return time.Unix(int64(e.Secs), int64(e.USecs))
|
||||
}
|
||||
|
||||
func readLinuxInputEvents(name string, cb func(linuxInputEvent) error) error {
|
||||
f, err := os.Open(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var raw linuxInputEvent
|
||||
defer f.Close()
|
||||
for {
|
||||
if err := binary.Read(f, binary.LittleEndian, &raw); err != nil {
|
||||
_ = f.Close()
|
||||
raw, err := readLinuxInputEvent(f, strconv.IntSize/8)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := cb(raw); err != nil {
|
||||
@@ -232,24 +214,28 @@ func (s *linuxButton) readEvents(events chan<- hardware.InputEvent) {
|
||||
WithField("input", "button").
|
||||
WithField("path", s.p.path)
|
||||
|
||||
var last time.Time
|
||||
var button buttonState
|
||||
err := readLinuxInputEvents(s.p.path, func(ev linuxInputEvent) error {
|
||||
if ev.Code == 0 {
|
||||
return nil // Ignore
|
||||
if ev.Type != 1 || ev.Code == 0 { // EV_KEY only; ignore synchronization events.
|
||||
return nil
|
||||
}
|
||||
|
||||
log.WithField("data", ev).Trace("button event")
|
||||
|
||||
var event hardware.InputEvent
|
||||
switch ev.Value {
|
||||
case 0: // Released
|
||||
if ev.Time().Sub(last) > LongPressDuration {
|
||||
case 1: // Pressed; repeated presses must not restart the timer.
|
||||
button.press(ev.Time())
|
||||
return nil
|
||||
case 0: // Released.
|
||||
duration, ok := button.release(ev.Time())
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
if duration >= LongPressDuration {
|
||||
event = hardware.RotaryLongPressEvent{Time: ev.Time()}
|
||||
} else {
|
||||
event = hardware.RotaryPressEvent{Time: ev.Time()}
|
||||
}
|
||||
case 1: // Pressed
|
||||
last = ev.Time()
|
||||
default: // Ignore key autorepeat and unknown values.
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -297,7 +283,7 @@ func (s *linuxRotary) readEvents(events chan<- hardware.InputEvent) {
|
||||
WithField("path", s.p.path)
|
||||
|
||||
err := readLinuxInputEvents(s.p.path, func(ev linuxInputEvent) error {
|
||||
if ev.Value == 0 {
|
||||
if ev.Type != 2 || ev.Value == 0 { // EV_REL only.
|
||||
return nil // Ignore
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user