Checkpoint
This commit is contained in:
54 files changed
+6855
-526
No files matched your search
@@ -0,0 +1,46 @@
|
||||
package input
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"go.maze.io/harmon/config"
|
||||
"go.maze.io/harmon/hardware"
|
||||
"periph.io/x/conn/v3/gpio"
|
||||
)
|
||||
|
||||
type Encoder interface {
|
||||
hardware.Driver
|
||||
|
||||
Events() (<-chan hardware.InputEvent, error)
|
||||
}
|
||||
|
||||
func NewEncoder(config config.Encoder) (Encoder, error) {
|
||||
switch config.Type {
|
||||
case "input":
|
||||
return newDevInputEncoder(config.Switch)
|
||||
|
||||
case "gpio":
|
||||
return newGPIOEncoder(config.CW, config.CCW, config.Switch)
|
||||
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported encoder type %q", config.Type)
|
||||
}
|
||||
}
|
||||
|
||||
type gpioEncoder struct {
|
||||
cw, ccw, click gpio.PinIn
|
||||
}
|
||||
|
||||
func newGPIOEncoder(cw, ccw, click string) (enc Encoder, err error) {
|
||||
e := new(gpioEncoder)
|
||||
if e.cw, err = hardware.LookupPin(cw); err != nil {
|
||||
return
|
||||
}
|
||||
if e.ccw, err = hardware.LookupPin(ccw); err != nil {
|
||||
return
|
||||
}
|
||||
if e.click, err = hardware.LookupPin(click); err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,270 @@
|
||||
package input
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"go.maze.io/harmon/hardware"
|
||||
"periph.io/x/conn/v3/gpio"
|
||||
)
|
||||
|
||||
const (
|
||||
devfsPath = "/dev"
|
||||
procfsPath = "/proc"
|
||||
procInputDevices = procfsPath + "/bus/input/devices"
|
||||
)
|
||||
|
||||
type devInputEncoder struct {
|
||||
path string
|
||||
click gpio.PinIn
|
||||
}
|
||||
|
||||
func newDevInputEncoder(click string) (enc Encoder, err error) {
|
||||
e := new(devInputEncoder)
|
||||
if e.click, err = hardware.LookupPin(click); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
var ps []*linuxPheripheral
|
||||
if ps, err = linuxPheripherals(); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
for _, p := range ps {
|
||||
if strings.HasPrefix(p.name, "rotary@") {
|
||||
e.path = p.path
|
||||
return e, nil
|
||||
}
|
||||
}
|
||||
return nil, errors.New("no rotary encoders found in " + procInputDevices)
|
||||
}
|
||||
|
||||
type linuxPheripheral struct {
|
||||
name string
|
||||
bus string
|
||||
vendor string
|
||||
product string
|
||||
version string
|
||||
phys string
|
||||
path string
|
||||
}
|
||||
|
||||
/*
|
||||
I: Bus=0019 Vendor=0001 Product=0001 Version=0100
|
||||
N: Name="button@10"
|
||||
P: Phys=gpio-keys/input0
|
||||
S: Sysfs=/devices/platform/button@10/input/input0
|
||||
U: Uniq=
|
||||
H: Handlers=kbd event0
|
||||
B: PROP=0
|
||||
B: EV=3
|
||||
B: KEY=20000 0 0 0
|
||||
|
||||
I: Bus=0019 Vendor=0000 Product=0000 Version=0000
|
||||
N: Name="rotary@14"
|
||||
P: Phys=
|
||||
S: Sysfs=/devices/platform/rotary@14/input/input1
|
||||
U: Uniq=
|
||||
H: Handlers=event1
|
||||
B: PROP=0
|
||||
B: EV=5
|
||||
B: REL=1
|
||||
*/
|
||||
func linuxPheripherals() (ps []*linuxPheripheral, err error) {
|
||||
log := logrus.WithField("input", "linux")
|
||||
log.Trace("detecting peripherals")
|
||||
|
||||
b, err := os.ReadFile(procInputDevices)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, segment := range strings.Split(string(b), "\n\n") {
|
||||
lines := strings.Split(segment, "\n")
|
||||
perip := new(linuxPheripheral)
|
||||
for _, line := range lines {
|
||||
if line == "" {
|
||||
continue
|
||||
}
|
||||
log.WithField("line", line).Trace("parsing line")
|
||||
switch {
|
||||
case strings.HasPrefix(line, "I: "):
|
||||
for _, kv := range strings.Split(strings.TrimPrefix(line, "I: "), " ") {
|
||||
kv := strings.SplitN(kv, "=", 2)
|
||||
switch kv[0] {
|
||||
case "Bus":
|
||||
case "Vendor":
|
||||
perip.vendor = kv[1]
|
||||
case "Product":
|
||||
perip.product = kv[1]
|
||||
case "Version":
|
||||
perip.version = kv[1]
|
||||
}
|
||||
}
|
||||
case strings.HasPrefix(line, "N: Name="):
|
||||
perip.name = strings.TrimFunc(strings.TrimPrefix(line, "N: Name="), func(r rune) bool { return r == '"' || r == '@' })
|
||||
case strings.HasPrefix(line, "P: Phys="):
|
||||
perip.phys = strings.TrimPrefix(line, "P: Phys=")
|
||||
//case strings.HasPrefix(line, "S: Sysfs="):
|
||||
// perip.sysfs = filepath.Join(sysfsPath, strings.TrimPrefix(line, "S: Sysfs="), "device")
|
||||
case strings.HasPrefix(line, "H: Handlers="):
|
||||
for _, handler := range strings.Split(strings.TrimPrefix(line, "H: Handlers="), " ") {
|
||||
if strings.HasPrefix(handler, "event") {
|
||||
perip.path = filepath.Join(devfsPath, "input", handler)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if perip.path != "" {
|
||||
log.WithFields(logrus.Fields{
|
||||
"name": perip.name,
|
||||
"bus": perip.bus,
|
||||
"vendor": perip.vendor,
|
||||
"product": perip.product,
|
||||
"version": perip.version,
|
||||
"phys": perip.phys,
|
||||
"path": perip.path,
|
||||
}).Trace("found peripheral")
|
||||
ps = append(ps, perip)
|
||||
}
|
||||
}
|
||||
|
||||
return ps, nil
|
||||
}
|
||||
|
||||
type linuxInputEvent struct {
|
||||
Secs uint32
|
||||
USecs uint32
|
||||
Type uint16
|
||||
Code uint16
|
||||
Value int32
|
||||
}
|
||||
|
||||
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
|
||||
for {
|
||||
if err := binary.Read(f, binary.LittleEndian, &raw); err != nil {
|
||||
_ = f.Close()
|
||||
return err
|
||||
}
|
||||
if err := cb(raw); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type linuxButton struct {
|
||||
p *linuxPheripheral
|
||||
}
|
||||
|
||||
func newLinuxButton(p *linuxPheripheral) *linuxButton {
|
||||
return &linuxButton{p: p}
|
||||
}
|
||||
|
||||
func (s *linuxButton) Name() string {
|
||||
return s.p.name
|
||||
}
|
||||
|
||||
func (s *linuxButton) Events() (<-chan Event, error) {
|
||||
log := logrus.
|
||||
WithField("input", "button").
|
||||
WithField("path", s.p.path)
|
||||
log.Debug("opening linux button source")
|
||||
|
||||
events := make(chan Event)
|
||||
go s.readEvents(events)
|
||||
return events, nil
|
||||
}
|
||||
|
||||
func (s *linuxButton) readEvents(events chan<- Event) {
|
||||
defer close(events)
|
||||
|
||||
log := logrus.
|
||||
WithField("input", "button").
|
||||
WithField("path", s.p.path)
|
||||
|
||||
err := readLinuxInputEvents(s.p.path, func(ev linuxInputEvent) error {
|
||||
log.WithField("data", ev).Debug("button event")
|
||||
|
||||
if ev.Code == 0 {
|
||||
return nil // not sure what this is
|
||||
}
|
||||
|
||||
var pressed bool
|
||||
if ev.Value == 1 {
|
||||
pressed = true
|
||||
}
|
||||
|
||||
select {
|
||||
case events <- NewButtonEvent(s, pressed):
|
||||
default:
|
||||
log.Warn("dropping event")
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
log.WithError(err).Error("failed to read events")
|
||||
}
|
||||
}
|
||||
|
||||
type linuxRotary struct {
|
||||
p *linuxPheripheral
|
||||
}
|
||||
|
||||
func newLinuxRotary(p *linuxPheripheral) *linuxRotary {
|
||||
return &linuxRotary{p: p}
|
||||
}
|
||||
|
||||
func (s *linuxRotary) Name() string {
|
||||
return s.p.name
|
||||
}
|
||||
|
||||
func (s *linuxRotary) Events() (<-chan Event, error) {
|
||||
log := logrus.
|
||||
WithField("input", "rotary").
|
||||
WithField("path", s.p.path)
|
||||
log.Debug("opening linux rotary source")
|
||||
|
||||
events := make(chan Event)
|
||||
go s.readEvents(events)
|
||||
return events, nil
|
||||
}
|
||||
|
||||
func (s *linuxRotary) readEvents(events chan<- Event) {
|
||||
defer close(events)
|
||||
|
||||
log := logrus.
|
||||
WithField("input", "rotary").
|
||||
WithField("path", s.p.path)
|
||||
|
||||
err := readLinuxInputEvents(s.p.path, func(ev linuxInputEvent) error {
|
||||
log.WithField("data", ev).Debug("rotary event")
|
||||
|
||||
select {
|
||||
case events <- NewRotaryEvent(s, int(ev.Value), ev.Value == 0):
|
||||
default:
|
||||
log.Warn("dropping event")
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
log.WithError(err).Error("failed to read events")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
//go:build !linux
|
||||
|
||||
package input
|
||||
|
||||
import "errors"
|
||||
|
||||
func newDevInputEncoder(string) (Encoder, error) {
|
||||
return nil, errors.New("input encoders are only supported in Linux")
|
||||
}
|
||||
Reference in new issue
Block a user