51 lines
955 B
Go
51 lines
955 B
Go
package input
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"periph.io/x/conn/v3/gpio"
|
|
|
|
"github.com/BeatGlow/beatglow/config"
|
|
"github.com/BeatGlow/beatglow/hardware"
|
|
)
|
|
|
|
var LongPressDuration = 750 * time.Millisecond
|
|
|
|
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
|
|
}
|