Checkpoint
This commit is contained in:
160 files changed
+76293
-387
No files matched your search
@@ -0,0 +1,124 @@
|
||||
package hardware
|
||||
|
||||
import (
|
||||
"image"
|
||||
"image/color"
|
||||
|
||||
"github.com/BeatGlow/display"
|
||||
)
|
||||
|
||||
type Combined struct {
|
||||
displays []display.Display
|
||||
size image.Point
|
||||
stride int
|
||||
}
|
||||
|
||||
func NewCombined(displays ...display.Display) display.Display {
|
||||
s := displays[0].Bounds().Size()
|
||||
d := &Combined{
|
||||
displays: displays,
|
||||
size: s,
|
||||
stride: s.X,
|
||||
}
|
||||
return d
|
||||
}
|
||||
|
||||
func (d *Combined) At(x, y int) color.Color {
|
||||
if x < 0 || y < 0 || y >= d.size.Y {
|
||||
return color.Black
|
||||
}
|
||||
for _, display := range d.displays {
|
||||
if x < d.size.X {
|
||||
return display.At(x, y)
|
||||
}
|
||||
x -= d.stride
|
||||
}
|
||||
return color.Black
|
||||
}
|
||||
|
||||
func (d *Combined) Close() error {
|
||||
var err error
|
||||
for _, p := range d.displays {
|
||||
if err1 := p.Close(); err1 != nil && err != nil {
|
||||
err = err1
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (d *Combined) Clear() {
|
||||
for _, p := range d.displays {
|
||||
p.Clear()
|
||||
}
|
||||
}
|
||||
|
||||
func (d *Combined) Fill(c color.Color) {
|
||||
for _, p := range d.displays {
|
||||
p.Fill(c)
|
||||
}
|
||||
}
|
||||
|
||||
func (d *Combined) SetContrast(level uint8) error {
|
||||
for _, p := range d.displays {
|
||||
if err := p.SetContrast(level); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Combined) SetRotation(rotation display.Rotation) error {
|
||||
for _, p := range d.displays {
|
||||
if err := p.SetRotation(rotation); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Combined) Show(show bool) error {
|
||||
for _, p := range d.displays {
|
||||
if err := p.Show(show); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Combined) Bounds() image.Rectangle {
|
||||
return image.Rectangle{Max: d.Size()}
|
||||
}
|
||||
|
||||
func (d *Combined) ColorModel() color.Model {
|
||||
return d.displays[0].ColorModel()
|
||||
}
|
||||
|
||||
func (d *Combined) Refresh() error {
|
||||
for _, p := range d.displays {
|
||||
if err := p.Refresh(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Combined) Set(x, y int, c color.Color) {
|
||||
if x < 0 || y < 0 || y >= d.size.Y {
|
||||
return
|
||||
}
|
||||
for _, p := range d.displays {
|
||||
if x < d.size.X {
|
||||
p.Set(x, y, c)
|
||||
return
|
||||
}
|
||||
x -= d.stride
|
||||
}
|
||||
}
|
||||
|
||||
func (d *Combined) Size() image.Point {
|
||||
return image.Point{X: d.stride * len(d.displays), Y: d.size.Y}
|
||||
}
|
||||
|
||||
func (Combined) Type() string {
|
||||
return "combined"
|
||||
}
|
||||
+90
-19
@@ -1,32 +1,103 @@
|
||||
package hardware
|
||||
|
||||
import (
|
||||
"image"
|
||||
"image/color"
|
||||
"image/draw"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"periph.io/x/conn/v3/gpio/gpioreg"
|
||||
|
||||
"github.com/BeatGlow/beatglow/config"
|
||||
"github.com/BeatGlow/display"
|
||||
)
|
||||
|
||||
// Display driver.
|
||||
type Display interface {
|
||||
Driver
|
||||
func Display(config *config.DisplayConfig) (display.Display, error) {
|
||||
log.Printf("display: %#+v", config)
|
||||
var (
|
||||
conn display.Conn
|
||||
err error
|
||||
)
|
||||
switch strings.ToUpper(config.Mode) {
|
||||
case "", "I2C", "I²C":
|
||||
var (
|
||||
addr uint64
|
||||
bus = -1
|
||||
)
|
||||
if addr, err = strconv.ParseUint(config.Address, 16, 16); err != nil {
|
||||
return nil, fmt.Errorf("invalid display addr %q: %w", config.Address, err)
|
||||
}
|
||||
if config.Bus != "" {
|
||||
if bus, err = strconv.Atoi(config.Bus); err != nil {
|
||||
return nil, fmt.Errorf("invalid bus %q: %w", config.Bus, err)
|
||||
}
|
||||
}
|
||||
conn, err = display.OpenI2C(&display.I2CConfig{
|
||||
Addr: uint8(addr),
|
||||
Device: bus,
|
||||
Reset: gpioreg.ByName(config.Reset),
|
||||
})
|
||||
|
||||
// PowerSave puts the display to sleep.
|
||||
PowerSave(on bool) error
|
||||
case "SPI":
|
||||
conn, err = display.OpenSPI(&display.SPIConfig{
|
||||
Reset: gpioreg.ByName(config.Reset),
|
||||
DC: gpioreg.ByName(config.DC),
|
||||
})
|
||||
|
||||
// Draw an image on the display.
|
||||
Draw(r image.Rectangle, src image.Image, sp image.Point) error
|
||||
default:
|
||||
return nil, fmt.Errorf("display: unknown mode %q", config.Mode)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Size of the display.
|
||||
Size() image.Point
|
||||
var options = &display.Config{
|
||||
Reset: gpioreg.ByName(config.Reset),
|
||||
Backlight: gpioreg.ByName(config.BL),
|
||||
}
|
||||
|
||||
// Clear the display buffer.
|
||||
Clear()
|
||||
if options.Width, options.Height, err = parseSize(config.Size); err != nil {
|
||||
_ = conn.Close()
|
||||
return nil, fmt.Errorf("display: invalid size %q: %w", config.Size, err)
|
||||
}
|
||||
|
||||
// Fill with one color.
|
||||
Fill(color.Color) error
|
||||
switch strings.ToUpper(config.Type) {
|
||||
case "SH1106":
|
||||
return display.SH1106(conn, options)
|
||||
case "SSD1322":
|
||||
return display.SSD1322(conn, options)
|
||||
case "ST7735":
|
||||
return display.ST7735(conn, options)
|
||||
case "ST7789":
|
||||
return display.ST7789(conn, options)
|
||||
default:
|
||||
_ = conn.Close()
|
||||
return nil, fmt.Errorf("display: unknown type %q", config.Type)
|
||||
}
|
||||
}
|
||||
|
||||
// Refresh the display.
|
||||
Refresh() error
|
||||
func parseSize(size string) (width, height int, err error) {
|
||||
if size == "" {
|
||||
return
|
||||
}
|
||||
|
||||
draw.Image
|
||||
i := strings.IndexByte(size, 'x')
|
||||
if i == -1 {
|
||||
return 0, 0, errors.New("invalid size format; expected WxH, Wx or xH")
|
||||
} else if i == 0 {
|
||||
height, err = strconv.Atoi(size[1:])
|
||||
return
|
||||
} else if i == len(size)-1 {
|
||||
width, err = strconv.Atoi(size[:len(size)-1])
|
||||
return
|
||||
}
|
||||
|
||||
if width, err = strconv.Atoi(size[:i]); err != nil {
|
||||
return
|
||||
}
|
||||
if height, err = strconv.Atoi(size[i+1:]); err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
+27
-1
@@ -1,12 +1,16 @@
|
||||
package hardware
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Input interface {
|
||||
Events() <-chan InputEvent
|
||||
}
|
||||
|
||||
type InputEvent interface {
|
||||
String() string
|
||||
}
|
||||
|
||||
type (
|
||||
@@ -19,10 +23,32 @@ type (
|
||||
RotaryPressEvent struct {
|
||||
Time time.Time
|
||||
Input Input
|
||||
Code int
|
||||
}
|
||||
|
||||
RotaryLongPressEvent struct {
|
||||
Time time.Time
|
||||
Input Input
|
||||
Code int
|
||||
}
|
||||
)
|
||||
|
||||
func (e RotaryEvent) String() string {
|
||||
return fmt.Sprintf("rotary event delta=%d", e.Delta)
|
||||
}
|
||||
|
||||
type RotaryHandler interface {
|
||||
Rotary(event RotaryEvent)
|
||||
}
|
||||
|
||||
func (e RotaryPressEvent) String() string {
|
||||
return "rotary press"
|
||||
}
|
||||
|
||||
type RotaryPressHandler interface {
|
||||
RotaryPress(event RotaryPressEvent)
|
||||
}
|
||||
|
||||
func (e RotaryLongPressEvent) String() string {
|
||||
return "rotary long press"
|
||||
}
|
||||
@@ -2,12 +2,16 @@ package input
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"go.maze.io/harmon/config"
|
||||
"go.maze.io/harmon/hardware"
|
||||
"periph.io/x/conn/v3/gpio"
|
||||
|
||||
"github.com/BeatGlow/beatglow/config"
|
||||
"github.com/BeatGlow/beatglow/hardware"
|
||||
)
|
||||
|
||||
var LongPressDuration = 450 * time.Millisecond
|
||||
|
||||
type Encoder interface {
|
||||
hardware.Driver
|
||||
|
||||
|
||||
@@ -3,14 +3,15 @@ package input
|
||||
import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"go.maze.io/harmon/hardware"
|
||||
"periph.io/x/conn/v3/gpio"
|
||||
|
||||
"github.com/BeatGlow/beatglow/hardware"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -20,28 +21,57 @@ const (
|
||||
)
|
||||
|
||||
type devInputEncoder struct {
|
||||
path string
|
||||
click gpio.PinIn
|
||||
path string
|
||||
button *linuxButton
|
||||
rotary *linuxRotary
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
var (
|
||||
rotary *linuxRotary
|
||||
button *linuxButton
|
||||
)
|
||||
|
||||
for _, p := range ps {
|
||||
if strings.HasPrefix(p.name, "rotary@") {
|
||||
e.path = p.path
|
||||
return e, nil
|
||||
switch {
|
||||
case strings.HasPrefix(p.name, "rotary@"):
|
||||
rotary = &linuxRotary{p: p}
|
||||
case strings.HasPrefix(p.name, "button@") && (click == "" || strings.HasSuffix(p.name, "@"+click)):
|
||||
button = &linuxButton{p: p}
|
||||
}
|
||||
}
|
||||
return nil, errors.New("no rotary encoders found in " + procInputDevices)
|
||||
|
||||
if rotary == nil {
|
||||
return nil, errors.New("no rotary encoder found, is the rotary_encoder dtoveraly loaded?")
|
||||
}
|
||||
if button == nil {
|
||||
return nil, errors.New("no button found, is the gpio-key dtoverlay loaded?")
|
||||
}
|
||||
|
||||
return &devInputEncoder{
|
||||
button: button,
|
||||
rotary: rotary,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (e devInputEncoder) Halt() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e devInputEncoder) Type() string {
|
||||
return e.path
|
||||
}
|
||||
|
||||
func (e devInputEncoder) Events() (<-chan hardware.InputEvent, error) {
|
||||
events := make(chan hardware.InputEvent, 8)
|
||||
go e.rotary.readEvents(events)
|
||||
go e.button.readEvents(events)
|
||||
return events, nil
|
||||
}
|
||||
|
||||
type linuxPheripheral struct {
|
||||
@@ -146,6 +176,10 @@ type linuxInputEvent struct {
|
||||
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))
|
||||
}
|
||||
@@ -180,38 +214,47 @@ func (s *linuxButton) Name() string {
|
||||
return s.p.name
|
||||
}
|
||||
|
||||
func (s *linuxButton) Events() (<-chan Event, error) {
|
||||
func (s *linuxButton) Events() (<-chan hardware.InputEvent, error) {
|
||||
log := logrus.
|
||||
WithField("input", "button").
|
||||
WithField("path", s.p.path)
|
||||
log.Debug("opening linux button source")
|
||||
|
||||
events := make(chan Event)
|
||||
events := make(chan hardware.InputEvent)
|
||||
go s.readEvents(events)
|
||||
return events, nil
|
||||
}
|
||||
|
||||
func (s *linuxButton) readEvents(events chan<- Event) {
|
||||
func (s *linuxButton) readEvents(events chan<- hardware.InputEvent) {
|
||||
defer close(events)
|
||||
|
||||
log := logrus.
|
||||
WithField("input", "button").
|
||||
WithField("path", s.p.path)
|
||||
|
||||
var last time.Time
|
||||
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
|
||||
return nil // Ignore
|
||||
}
|
||||
|
||||
var pressed bool
|
||||
if ev.Value == 1 {
|
||||
pressed = true
|
||||
log.WithField("data", ev).Trace("button event")
|
||||
|
||||
var event hardware.InputEvent
|
||||
switch ev.Value {
|
||||
case 0: // Released
|
||||
if ev.Time().Sub(last) > LongPressDuration {
|
||||
event = hardware.RotaryLongPressEvent{Time: ev.Time()}
|
||||
} else {
|
||||
event = hardware.RotaryPressEvent{Time: ev.Time()}
|
||||
}
|
||||
case 1: // Pressed
|
||||
last = ev.Time()
|
||||
return nil
|
||||
}
|
||||
|
||||
select {
|
||||
case events <- NewButtonEvent(s, pressed):
|
||||
case events <- event:
|
||||
default:
|
||||
log.Warn("dropping event")
|
||||
}
|
||||
@@ -235,18 +278,18 @@ func (s *linuxRotary) Name() string {
|
||||
return s.p.name
|
||||
}
|
||||
|
||||
func (s *linuxRotary) Events() (<-chan Event, error) {
|
||||
func (s *linuxRotary) Events() (<-chan hardware.InputEvent, error) {
|
||||
log := logrus.
|
||||
WithField("input", "rotary").
|
||||
WithField("path", s.p.path)
|
||||
log.Debug("opening linux rotary source")
|
||||
|
||||
events := make(chan Event)
|
||||
events := make(chan hardware.InputEvent)
|
||||
go s.readEvents(events)
|
||||
return events, nil
|
||||
}
|
||||
|
||||
func (s *linuxRotary) readEvents(events chan<- Event) {
|
||||
func (s *linuxRotary) readEvents(events chan<- hardware.InputEvent) {
|
||||
defer close(events)
|
||||
|
||||
log := logrus.
|
||||
@@ -254,10 +297,18 @@ func (s *linuxRotary) readEvents(events chan<- Event) {
|
||||
WithField("path", s.p.path)
|
||||
|
||||
err := readLinuxInputEvents(s.p.path, func(ev linuxInputEvent) error {
|
||||
log.WithField("data", ev).Debug("rotary event")
|
||||
if ev.Value == 0 {
|
||||
return nil // Ignore
|
||||
}
|
||||
|
||||
log.WithField("data", ev).Trace("rotary event")
|
||||
|
||||
event := hardware.RotaryEvent{
|
||||
Time: ev.Time(),
|
||||
Delta: int(ev.Value),
|
||||
}
|
||||
select {
|
||||
case events <- NewRotaryEvent(s, int(ev.Value), ev.Value == 0):
|
||||
case events <- event:
|
||||
default:
|
||||
log.Warn("dropping event")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
package hardware
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"image"
|
||||
"image/color"
|
||||
|
||||
"github.com/BeatGlow/display"
|
||||
)
|
||||
|
||||
// View is a (partial) view on the display.
|
||||
type View struct {
|
||||
display.Display
|
||||
Window image.Rectangle
|
||||
}
|
||||
|
||||
func MakeView(display display.Display, window image.Rectangle) View {
|
||||
return View{
|
||||
Display: display,
|
||||
Window: window,
|
||||
}
|
||||
}
|
||||
|
||||
func (d View) Bounds() image.Rectangle {
|
||||
return d.Window.Sub(d.Window.Min)
|
||||
}
|
||||
|
||||
func (d View) Clear() {
|
||||
if d.Window.Eq(d.Display.Bounds()) {
|
||||
d.Display.Clear()
|
||||
return
|
||||
}
|
||||
|
||||
for y := d.Window.Min.Y; y < d.Window.Max.Y; y++ {
|
||||
for x := d.Window.Min.X; x < d.Window.Max.X; x++ {
|
||||
d.Display.Set(x, y, color.Transparent)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Close doesn't do anything.
|
||||
func (View) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d View) Type() string {
|
||||
return fmt.Sprintf("view:%s:%s", d.Display, d.Window)
|
||||
}
|
||||
|
||||
func (d View) Size() image.Point {
|
||||
return d.Window.Size()
|
||||
}
|
||||
|
||||
func (d View) At(x, y int) color.Color {
|
||||
if x < 0 || y < 0 || x >= d.Window.Dx() || y >= d.Window.Dy() {
|
||||
return d.Display.ColorModel().Convert(color.Black)
|
||||
}
|
||||
x += d.Window.Min.X
|
||||
y += d.Window.Min.Y
|
||||
return d.Display.At(x, y)
|
||||
}
|
||||
|
||||
func (d View) Set(x, y int, c color.Color) {
|
||||
if x < 0 || y < 0 || x >= d.Window.Dx() || y >= d.Window.Dy() {
|
||||
return
|
||||
}
|
||||
x += d.Window.Min.X
|
||||
y += d.Window.Min.Y
|
||||
d.Display.Set(x, y, c)
|
||||
}
|
||||
Reference in new issue
Block a user