Checkpoint

This commit is contained in:
maze authored and maze committed 2026-10-29 17:28:51 +01:00
1 parent 7b6db17870
commit 0295eb1ad2
125 files changed
+59376

No files matched your search

+115
View File
@@ -0,0 +1,115 @@
package audio
import (
"encoding/binary"
"fmt"
"image"
"image/color"
"image/draw"
"io"
"net"
"os"
"git.maze.io/go/bragi/audio"
"git.maze.io/go/bragi/hardware"
"periph.io/x/conn/v3/physic"
)
// Visualizer processes raw audio that can be rendered onto a display.
type Visualizer interface {
Process(audio.Slice)
Render(dst draw.Image, origin image.Point, c color.Color)
}
// Reader can read samples.
type Reader interface {
hardware.Resource
Channels() int
SampleRate() physic.Frequency
ReadSamples(audio.Slice) (int, error)
}
type Filter interface {
Filter(audio.Slice) audio.Slice
}
type pcm16Reader struct {
name string
channels int
sampleRate physic.Frequency
rc io.ReadCloser
byteOrder binary.ByteOrder
b audio.Int16
}
// OpenPCM16 reads from a raw linear 16-bit PCM file (or fifo).
func OpenPCM16(name string, byteOrder binary.ByteOrder, channels int, sampleRate physic.Frequency) (Reader, error) {
f, err := os.OpenFile(name, os.O_RDONLY, 0)
if err != nil {
return nil, err
}
return &pcm16Reader{
name: name,
channels: channels,
sampleRate: sampleRate,
rc: f,
byteOrder: byteOrder,
}, nil
}
// OpenPCM16TCP reads from a raw linear 16-bit PCM stream over TCP.
func OpenPCM16TCP(addr string, byteOrder binary.ByteOrder, channels int, sampleRate physic.Frequency) (Reader, error) {
c, err := net.Dial("tcp", addr)
if err != nil {
return nil, err
}
return &pcm16Reader{
name: "tcp://" + addr,
channels: channels,
sampleRate: sampleRate,
rc: c,
byteOrder: byteOrder,
}, nil
}
func (r *pcm16Reader) String() string {
return fmt.Sprintf("pcm reader %s", r.name)
}
func (r *pcm16Reader) Halt() error {
return r.rc.Close()
}
func (r *pcm16Reader) Channels() int {
return r.channels
}
func (r *pcm16Reader) SampleRate() physic.Frequency {
return r.sampleRate
}
func (r *pcm16Reader) ReadSamples(p audio.Slice) (n int, err error) {
var (
data audio.Int16
ok bool
)
if data, ok = p.(audio.Int16); !ok {
length := p.Len()
if l := len(r.b); l < length {
r.b = make(audio.Int16, p.Len())
} else {
r.b = r.b[:length]
}
data = r.b
}
if err = binary.Read(r.rc, r.byteOrder, &data); err != nil {
return
}
if !ok {
// Convert back
r.b.CopyTo(p)
}
return p.Len(), nil
}
+150
View File
@@ -0,0 +1,150 @@
package audio
import (
"image/color"
"image/draw"
)
func drawSprite(p draw.Image, x, y int, sprite []uint8, c color.Color) {
/*
for dy, row := range sprite {
for dx := 0; dx < 8; dx++ {
if row&(1<<(7-dx)) != 0 {
p.Set(x+dx, y+dy, c)
}
}
}
*/
w, h, data := sprite[0], sprite[1], sprite[2:]
for dy := 0; dy < int(h); dy++ {
//pix := data[dy*int(w>>3):]
for dx := 0; dx < int(w); dx++ {
//b := pix[dx>>3]
//if b&(1<<(7-(dx%8))) != 0 {
// p.Set(x+dx, y+dy, c)
//}
byteOffset := int(dy/8)*int(w) + dx
bitOffset := dy % 8
if data[byteOffset]&(1<<bitOffset) != 0 {
p.Set(x+dx, y+dy, c)
}
}
}
}
func drawLine(p draw.Image, x1, y1, x2, y2 int, c color.Color) {
var dx, dy, e, slope int
// Because drawing p1 -> p2 is equivalent to draw p2 -> p1,
// I sort points in x-axis order to handle only half of possible cases.
if x1 > x2 {
x1, y1, x2, y2 = x2, y2, x1, y1
}
dx, dy = x2-x1, y2-y1
// Because point is x-axis ordered, dx cannot be negative
if dy < 0 {
dy = -dy
}
switch {
// Is line a point ?
case x1 == x2 && y1 == y2:
p.Set(x1, y1, c)
// Is line an horizontal ?
case y1 == y2:
for ; dx != 0; dx-- {
p.Set(x1, y1, c)
x1++
}
p.Set(x1, y1, c)
// Is line a vertical ?
case x1 == x2:
if y1 > y2 {
y1, y2 = y2, y1
}
for ; dy != 0; dy-- {
p.Set(x1, y1, c)
y1++
}
p.Set(x1, y1, c)
// Is line a diagonal ?
case dx == dy:
if y1 < y2 {
for ; dx != 0; dx-- {
p.Set(x1, y1, c)
x1++
y1++
}
} else {
for ; dx != 0; dx-- {
p.Set(x1, y1, c)
x1++
y1--
}
}
p.Set(x1, y1, c)
// wider than high ?
case dx > dy:
if y1 < y2 {
// BresenhamDxXRYD(img, x1, y1, x2, y2, col)
dy, e, slope = 2*dy, dx, 2*dx
for ; dx != 0; dx-- {
p.Set(x1, y1, c)
x1++
e -= dy
if e < 0 {
y1++
e += slope
}
}
} else {
// BresenhamDxXRYU(img, x1, y1, x2, y2, col)
dy, e, slope = 2*dy, dx, 2*dx
for ; dx != 0; dx-- {
p.Set(x1, y1, c)
x1++
e -= dy
if e < 0 {
y1--
e += slope
}
}
}
p.Set(x2, y2, c)
// higher than wide.
default:
if y1 < y2 {
// BresenhamDyXRYD(img, x1, y1, x2, y2, col)
dx, e, slope = 2*dx, dy, 2*dy
for ; dy != 0; dy-- {
p.Set(x1, y1, c)
y1++
e -= dx
if e < 0 {
x1++
e += slope
}
}
} else {
// BresenhamDyXRYU(img, x1, y1, x2, y2, col)
dx, e, slope = 2*dx, dy, 2*dy
for ; dy != 0; dy-- {
p.Set(x1, y1, c)
y1--
e -= dx
if e < 0 {
x1++
e += slope
}
}
}
p.Set(x2, y2, c)
}
}
+35
View File
@@ -0,0 +1,35 @@
package audio
// Deinterleave multi channel samples to separate slices.
func Deinterleave(samples []int16, slices [][]int16) {
if len(samples) == 0 {
return
}
if len(slices) == 1 {
copy(slices[0], samples)
return
}
channels := len(slices)
for i, sample := range samples {
j, k := i%channels, i/channels
slices[j][k] = sample
}
}
// Interleave multi channel samples to a single slice.
func Interleave(slices [][]int16, samples []int16) {
if len(slices) == 0 {
return
}
if len(slices) == 1 {
copy(samples, slices[0])
return
}
channels := len(slices)
for i := range samples {
j, k := i%channels, i/channels
samples[i] = slices[j][k]
}
}
+142
View File
@@ -0,0 +1,142 @@
package audio
import (
"image"
"image/color"
"image/draw"
"math"
"sort"
"git.maze.io/go/bragi/audio"
"periph.io/x/conn/v3/physic"
)
var (
spriteLeft = []uint8{
0x08, /* width */
0x05, /* height */
0b10000,
0b10000,
0b10000,
0b10000,
0b11110,
}
spriteRight = []uint8{
0x08, /* width */
0x05, /* height */
0b11110,
0b10001,
0b11110,
0b10100,
0b10010,
}
)
type Peak struct {
rate physic.Frequency
channels int
level audio.Float64
peaks []audio.Float64
}
func NewPeak(sampleRate physic.Frequency, channels int, keep int) *Peak {
if channels < 1 {
channels = 1
} else if channels > 2 {
channels = 2
}
v := &Peak{
rate: sampleRate,
channels: channels,
level: make(audio.Float64, channels),
}
if keep > 0 {
for i := 0; i < channels; i++ {
v.peaks = append(v.peaks, make(audio.Float64, keep))
}
}
return v
}
func (v *Peak) Process(samples audio.Slice) {
sum := make([]float64, v.channels)
for i, l := 0, samples.Len(); i < l; i++ {
k := samples.At(i)
if k < 0 {
k = -k
}
if k > 1 {
k = 1
}
n := i % v.channels
sum[n] += math.Pow(k, 2)
}
//log.Printf("max: %+v", max)
for n, k := range sum {
l := math.Sqrt(k / float64(samples.Len()))
v.level[n] = l
if v.peaks != nil {
copy(v.peaks[n][1:], v.peaks[n])
v.peaks[n][0] = l
//sortSlice(v.peaks[n], true)
}
}
}
func (v *Peak) Render(dst draw.Image, origin image.Point, c color.Color) {
var (
r = image.Rect(0, 0, 64, 11)
scale = 1.0 / float64(r.Dx()-6)
)
// Clear buffer
draw.Draw(dst, r.Add(origin), image.Black, image.Point{}, draw.Src)
ox, oy := origin.X, origin.Y
// Draw sprites
drawSprite(dst, ox-3, oy+0, spriteLeft[:], c)
drawSprite(dst, ox-3, oy+6, spriteRight[:], c)
// Draw line
ox += 6
drawLine(dst, ox, oy+2, ox+r.Dx()-6, oy+2, c)
drawLine(dst, ox, oy+8, ox+r.Dx()-6, oy+8, c)
// Draw bar
lw := int(v.level[0] / scale)
rw := lw
if v.channels > 1 {
rw = int(v.level[1] / scale)
}
drawLine(dst, ox, oy+1, ox+lw, oy+1, c)
drawLine(dst, ox, oy+3, ox+lw, oy+3, c)
drawLine(dst, ox, oy+7, ox+rw, oy+7, c)
drawLine(dst, ox, oy+9, ox+rw, oy+9, c)
if v.peaks != nil {
lw = int(audio.Max(v.peaks[0]) / scale) //int(v.peaks[0][0] / scale)
rw = lw
if v.channels > 1 {
rw = int(audio.Max(v.peaks[1]) / scale) //int(v.peaks[1][0] / scale)
}
drawLine(dst, ox+lw, oy+1, ox+lw, oy+3, c)
drawLine(dst, ox+rw, oy+7, ox+rw, oy+9, c)
}
}
func sortSlice(s audio.Float64, reverse bool) {
sort.Slice(s, func(i, j int) bool {
if reverse {
return s[i] > s[j]
}
return s[i] < s[j]
})
}
var _ Visualizer = (*Peak)(nil)
+1
View File
@@ -0,0 +1 @@
package audio
+205
View File
@@ -0,0 +1,205 @@
package audio
import (
"image"
"image/color"
"image/draw"
"math"
"git.maze.io/go/bragi/audio"
"periph.io/x/conn/v3/physic"
)
type VUMeter struct {
rate physic.Frequency
channel int
channels int
level float64
//peaks []audio.Float64
}
func NewVUMeter(sampleRate physic.Frequency, channel, channels int) *VUMeter {
if channel < 1 {
channel = 1
} else if channel > 2 {
channel = 2
}
if channels < 1 {
channels = 1
} else if channels > 2 {
channels = 2
}
v := &VUMeter{
rate: sampleRate,
channel: channel,
channels: channels,
}
return v
}
func (v *VUMeter) Process(samples audio.Slice) {
var sum float64
for i, l := 0, samples.Len(); i < l; i++ {
k := samples.At(i)
if k < 0 {
k = -k
}
if k > 1 {
k = 1
}
if n := i % v.channels; n == v.channel {
sum += math.Pow(k, 2)
}
}
// RMS level
v.level = math.Sqrt(sum / float64(samples.Len()))
}
func (v *VUMeter) Render(dst draw.Image, origin image.Point, c color.Color) {
// Clear buffer
draw.Draw(dst, dst.Bounds(), image.Black, image.Point{}, draw.Src)
// Draw background
drawSprite(dst, origin.X, origin.Y, vumeterBackground[v.channel-1], c)
var (
scale = 1.0 / float64(len(needleCoordinates))
angle = int(v.level / scale)
point = needleCoordinates[angle].Add(origin)
)
drawLine(dst, point.X, point.Y, origin.X+64, origin.Y+96, c)
}
var needleCoordinates = []image.Point{
{0, 23},
{1, 23},
{2, 22},
{3, 21},
{3, 21},
{4, 20},
{5, 19},
{6, 19},
{7, 18},
{8, 17},
{9, 17},
{10, 16},
{10, 16},
{11, 15},
{12, 14},
{13, 14},
{14, 13},
{15, 13},
{16, 12},
{17, 12},
{18, 11},
{19, 11},
{20, 10},
{21, 10},
{22, 9},
{23, 9},
{24, 8},
{25, 8},
{26, 7},
{27, 7},
{28, 6},
{29, 6},
{30, 6},
{31, 5},
{32, 5},
{33, 5},
{34, 4},
{35, 4},
{36, 4},
{37, 3},
{38, 3},
{39, 3},
{40, 2},
{41, 2},
{42, 2},
{43, 2},
{45, 1},
{46, 1},
{47, 1},
{48, 1},
{49, 1},
{50, 0},
{51, 0},
{52, 0},
{53, 0},
{54, 0},
{55, 0},
{56, 0},
{57, 0},
{59, 0},
{60, 0},
{61, 0},
{62, 0},
{63, 0},
{64, 0},
{65, 0},
{66, 0},
{67, 0},
{68, 0},
{70, 0},
{71, 0},
{72, 0},
{73, 0},
{74, 0},
{75, 0},
{76, 0},
{77, 0},
{78, 1},
{79, 1},
{80, 1},
{81, 1},
{82, 1},
{84, 2},
{85, 2},
{86, 2},
{87, 2},
{88, 3},
{89, 3},
{90, 3},
{91, 4},
{92, 4},
{93, 4},
{94, 5},
{95, 5},
{96, 5},
{97, 6},
{98, 6},
{99, 6},
{100, 7},
{101, 7},
{102, 8},
{103, 8},
{104, 9},
{105, 9},
{106, 10},
{107, 10},
{108, 11},
{109, 11},
{110, 12},
{111, 12},
{112, 13},
{113, 13},
{114, 14},
{115, 14},
{116, 15},
{117, 16},
{117, 16},
{118, 17},
{119, 17},
{120, 18},
{121, 19},
{122, 19},
{123, 20},
{124, 21},
{124, 21},
{125, 22},
{126, 23},
{127, 23},
}
+137
View File
@@ -0,0 +1,137 @@
package audio
var vumeterBackground = [2][]uint8{{
0x80, /* = width */
0x40, /* = height */
0x04, 0x04, 0x04, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x04, 0x1f, 0x04, 0x04,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xd0, 0x50, 0x70, 0x00, 0xf0, 0x10, 0xf0, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xf0, 0x00, 0xf0, 0x10, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, 0x50,
0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x10, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xd0, 0x50, 0x70, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, 0x50, 0xf0,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x78, 0x41, 0x41, 0x41, 0x40, 0x40, 0x40,
0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
0x41, 0x40, 0x79, 0x41, 0x41, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x60, 0x40, 0x40, 0x40,
0x40, 0x40, 0x40, 0x40, 0x40, 0x60, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x41, 0x79,
0x41, 0x40, 0x40, 0x40, 0x40, 0x40, 0x60, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x60, 0x40, 0x40,
0x40, 0x40, 0x40, 0x40, 0x40, 0x41, 0x79, 0x01, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x79,
0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x61, 0x79, 0x61, 0x60, 0x60,
0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x61, 0x79, 0x01,
0xc0, 0x4e, 0xc2, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
0x02, 0x02, 0x02, 0x02, 0x42, 0x42, 0xc2, 0x06, 0xc2, 0x42, 0xc2, 0x02, 0x02, 0x02, 0x02, 0x02,
0x02, 0x02, 0x02, 0x02, 0x02, 0xc2, 0x02, 0x82, 0x06, 0xc2, 0x42, 0xc2, 0x02, 0x02, 0x02, 0x02,
0x02, 0x02, 0x02, 0x02, 0x02, 0xc2, 0x42, 0x42, 0x06, 0xc2, 0x42, 0xc2, 0x02, 0x02, 0x02, 0x02,
0x02, 0x02, 0x02, 0xc2, 0x42, 0xc2, 0x06, 0xc2, 0x42, 0xc2, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
0x02, 0x02, 0xc2, 0x02, 0xc2, 0x42, 0xce, 0x00, 0xc0, 0x40, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x07, 0x04, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x07, 0x05, 0x05, 0x00, 0x07, 0x04, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x02, 0x07, 0x00, 0x07, 0x04, 0x07, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x05, 0x07, 0x00, 0x07, 0x04, 0x07, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x07, 0x05, 0x07, 0x00, 0x07, 0x04, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x07, 0x00, 0x07, 0x04, 0x07, 0x00, 0x07, 0x04, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x04, 0xf8, 0x00, 0x00, 0x00, 0xf8, 0x04, 0xf8, 0x00,
0x00, 0xf8, 0x04, 0xf8, 0x00, 0x00, 0x00, 0xf8, 0x04, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x10, 0x2f, 0x50, 0xa0, 0x50, 0x2f, 0x10, 0x0f, 0x00,
0x00, 0x3f, 0x40, 0xbf, 0xa0, 0xa0, 0xa0, 0xbf, 0x40, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7e, 0x22, 0x22, 0x3e,
0x00, 0x7e, 0x52, 0x52, 0x42, 0x00, 0x7e, 0x12, 0x12, 0x7e, 0x00, 0x7e, 0x08, 0x08, 0x76, 0x00,
}, {
0x80, /* = width */
0x40, /* = height */
0x04, 0x04, 0x1f, 0x04, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x04, 0x04, 0x04,
0x50, 0x50, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0xd0, 0x50, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x10, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50,
0x50, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x00, 0xf0, 0x10, 0xf0,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0xd0, 0x50, 0x70, 0x00, 0xf0, 0x10, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0x79, 0x61, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60,
0x60, 0x60, 0x61, 0x79, 0x61, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60,
0x79, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x01, 0x79, 0x41, 0x40, 0x40, 0x40, 0x40, 0x40,
0x40, 0x40, 0x60, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x60, 0x40, 0x40, 0x40, 0x40, 0x40, 0x41,
0x79, 0x41, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x60, 0x40, 0x40, 0x40, 0x40, 0x40,
0x40, 0x40, 0x40, 0x60, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x41, 0x40, 0x79, 0x41, 0x41,
0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
0x40, 0x40, 0x40, 0x41, 0x41, 0x41, 0x78, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x00, 0xc0, 0x40, 0xce, 0x02, 0xc2, 0x42, 0xc2, 0x02, 0x02,
0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0xc2, 0x42, 0xc2, 0x06, 0xc2, 0x42, 0xc2, 0x02, 0x02, 0x02,
0x02, 0x02, 0x02, 0x02, 0xc2, 0x42, 0x42, 0x06, 0xc2, 0x42, 0xc2, 0x02, 0x02, 0x02, 0x02, 0x02,
0x02, 0x02, 0x02, 0x02, 0xc2, 0x02, 0x82, 0x06, 0xc2, 0x42, 0xc2, 0x02, 0x02, 0x02, 0x02, 0x02,
0x02, 0x02, 0x02, 0x02, 0x02, 0x42, 0x42, 0xc2, 0x06, 0xc2, 0x42, 0xc2, 0x02, 0x02, 0x02, 0x02,
0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0xc2, 0x4e, 0xc0,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x07, 0x04, 0x07, 0x00, 0x07, 0x04, 0x07, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x05, 0x07, 0x00, 0x07, 0x04, 0x07, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x07, 0x05, 0x07, 0x00, 0x07, 0x04, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x03, 0x02, 0x07, 0x00, 0x07, 0x04, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x05, 0x05, 0x00, 0x07, 0x04, 0x07, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x04, 0x07,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x04, 0xf8, 0x00, 0x00, 0x00, 0xf8, 0x04, 0xf8, 0x00,
0x00, 0xf8, 0x04, 0xf8, 0x00, 0x00, 0x00, 0xf8, 0x04, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x10, 0x2f, 0x50, 0xa0, 0x50, 0x2f, 0x10, 0x0f, 0x00,
0x00, 0x3f, 0x40, 0xbf, 0xa0, 0xa0, 0xa0, 0xbf, 0x40, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x7e, 0x22, 0x22, 0x3e, 0x00, 0x7e, 0x52, 0x52, 0x42, 0x00, 0x7e, 0x12, 0x12, 0x7e, 0x00,
0x7e, 0x08, 0x08, 0x76, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
}}
+43
View File
@@ -0,0 +1,43 @@
package audio
import (
"image"
"image/color"
"image/draw"
"math"
)
type Waveform struct {
bins []int8
height int
scale float64
decimate int
r image.Rectangle
}
func NewWaveform(x, y int) *Waveform {
return &Waveform{
bins: make([]int8, x),
height: y,
scale: float64(y>>1) / math.MaxInt16,
decimate: 1,
r: image.Rect(0, 0, x, y),
}
}
func (v *Waveform) Process(sample int16) {
v.shift()
v.bins[0] = int8(float64(sample) * v.scale)
}
func (v *Waveform) shift() {
copy(v.bins[1:], v.bins)
v.bins[0] = 0
}
func (v *Waveform) Draw(i draw.Image, o image.Point) {
draw.Draw(i, v.r.Add(o), image.Black, image.Point{}, draw.Src)
for x, h := range v.bins {
drawLine(i, o.X+x, o.Y+int(h), o.X+x, o.Y+v.height, color.White)
}
}
+111
View File
@@ -0,0 +1,111 @@
package input
import (
"fmt"
"time"
)
type Event interface {
String() string
// Source this event originated from.
Source() Source
// Time of the event.
Time() time.Time
}
type (
baseEvent struct {
source Source
time time.Time
}
// Button event.
Button struct {
baseEvent
// Pressed indicates if the button is pressed or released.
Pressed bool
}
// Rotary encoder event.
Rotary struct {
baseEvent
// Delta is the rate of change.
Delta int
// Switch indicates if the rotary switch is pressed or released.
Switch bool
}
// Key event.
Key struct {
baseEvent
// Pressed indicates if the key is pressed or released.
Pressed bool
// Code is the key scan code.
Code uint
}
)
func (e baseEvent) Source() Source { return e.source }
func (e baseEvent) Time() time.Time { return e.time }
func (e *baseEvent) SetTime(t time.Time) { e.time = t }
func makeBaseEvent(source Source) baseEvent {
return baseEvent{
source: source,
time: time.Now(),
}
}
func (e Button) String() string {
if e.Pressed {
return "button pressed"
}
return "button released"
}
func NewButtonEvent(source Source, pressed bool) Event {
return &Button{
baseEvent: makeBaseEvent(source),
Pressed: pressed,
}
}
func (e Rotary) String() string {
if e.Delta == 0 {
if e.Switch {
return "rotary pressed"
}
return "rotary released"
}
return fmt.Sprintf("rotary delta=%d", e.Delta)
}
func NewRotaryEvent(source Source, delta int, pressed bool) Event {
return &Rotary{
baseEvent: makeBaseEvent(source),
Delta: delta,
Switch: pressed,
}
}
func (e Key) String() string {
if e.Pressed {
return "key pressed"
}
return "key released"
}
func NewKeyEvent(source Source, pressed bool, code uint) Event {
return &Key{
baseEvent: makeBaseEvent(source),
Pressed: pressed,
Code: code,
}
}
+23
View File
@@ -0,0 +1,23 @@
package input
var detectors []func() ([]Source, error)
// Detect returns a list of input sources.
func Detect() ([]Source, error) {
var s []Source
for _, d := range detectors {
ds, err := d()
if err != nil {
return nil, err
}
s = append(s, ds...)
}
return s, nil
}
// Register a new input source detector.
func Register(detector func() ([]Source, error)) {
detectors = append(detectors, detector)
}
+279
View File
@@ -0,0 +1,279 @@
package input
import (
"encoding/binary"
"os"
"path/filepath"
"strings"
"time"
"github.com/sirupsen/logrus"
)
const (
devfsPath = "/dev"
sysfsPath = "/sys"
)
func init() {
detectors = append(detectors, detectLinux)
}
func detectLinux() ([]Source, error) {
log := logrus.WithField("input", "detect")
ps, err := linuxPheripherals()
if err != nil {
return nil, err
}
log.Debugf("detected %d peripherals", len(ps))
ss := make([]Source, 0, len(ps))
for _, p := range ps {
if i := strings.IndexByte(p.name, '@'); i != -1 {
switch p.name[:i] {
case "button":
log.WithField("path", p.path).Debug("detected button")
ss = append(ss, newLinuxButton(p))
case "rotary":
log.WithField("path", p.path).Debug("detected rotary encoder")
ss = append(ss, newLinuxRotary(p))
default:
log.WithField("path", p.path).Debugf("unsupported %q peripheral", p.name[:i])
}
}
}
return ss, nil
}
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("/proc/bus/input/devices")
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")
}
}
// Interface checks
var (
_ Source = (*linuxButton)(nil)
)
+121
View File
@@ -0,0 +1,121 @@
package rotary
import (
"context"
"github.com/sirupsen/logrus"
"periph.io/x/conn/v3/gpio"
)
type Rotary struct {
s1, s2, key gpio.PinIO
state [4]state
}
func NewRotary(s1, s2, key gpio.PinIO) (*Rotary, error) {
return &Rotary{
s1: s1,
s2: s2,
key: key,
}, nil
}
func (r *Rotary) log() *logrus.Entry {
return logrus.WithField("input", "rotary")
}
func (r *Rotary) Run(ctx context.Context) <-chan Event {
r.s1.In(gpio.PullNoChange, gpio.BothEdges)
r.s2.In(gpio.PullNoChange, gpio.BothEdges)
update := make(chan state)
go r.waitForEdgeOnPin(ctx, r.s1, update)
go r.waitForEdgeOnPin(ctx, r.s2, update)
go r.waitForEdgeOnPin(ctx, r.key, update)
keys := make(chan Event)
go r.process(ctx, update, keys)
return keys
}
func (r *Rotary) waitForEdgeOnPin(ctx context.Context, pin gpio.PinIO, update chan<- state) error {
log := r.log().WithField("pin", pin.Name())
for {
select {
case <-ctx.Done():
log.Debug("context done")
return ctx.Err()
default:
log.Trace("waiting for edge")
if !pin.WaitForEdge(-1) {
log.Trace("wait for edge failed")
continue
}
level := pin.Read()
log.WithField("level", level).Trace("edge detected")
update <- state{
pin: pin,
level: level,
}
}
}
}
func (r *Rotary) process(ctx context.Context, update <-chan state, keys chan<- Event) {
log := r.log()
for {
select {
case <-ctx.Done():
return
case newState := <-update:
switch newState.pin {
case r.key:
if newState.level == gpio.High {
log.Debug("key release detected")
keys <- KeyRelease
} else {
log.Debug("key press detected")
keys <- KeyPress
}
default:
if newState == r.state[3] {
continue
}
r.state = [4]state{
r.state[1],
r.state[2],
r.state[3],
newState,
}
if isCW(r.state) {
log.Debug("clockwise rotation detected")
keys <- Clockwise
} else if isCCW(r.state) {
log.Debug("counter clockwise rotation detected")
keys <- CounterClockwise
}
}
}
}
}
type state struct {
pin gpio.PinIO
level gpio.Level
}
func stateEq(a, b [4]state) bool {
return a[0].level == b[0].level && a[1].level == b[1].level && a[2].level == b[2].level && a[3].level == b[3].level
}
func isCW(s [4]state) bool {
return s[0].level == gpio.High && s[1].level == gpio.High && s[2].level == gpio.Low && s[3].level == gpio.Low
}
func isCCW(s [4]state) bool {
return s[0].level == gpio.High && s[1].level == gpio.Low && s[2].level == gpio.Low && s[3].level == gpio.High
}
+10
View File
@@ -0,0 +1,10 @@
package rotary
type Event uint8
const (
Clockwise Event = iota
CounterClockwise
KeyPress
KeyRelease
)
+9
View File
@@ -0,0 +1,9 @@
package input
type Source interface {
// Name returns the name of the source.
Name() string
// Events returns a channel that will be used to send events to the source.
Events() (<-chan Event, error)
}