Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GPIO update #31

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions cmd/raspberry-pi/button.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ package main
import (
"time"

"github.com/warthog618/gpiod"
"github.com/warthog618/go-gpiocdev"
)

type ButtonEvent struct {
Expand All @@ -32,13 +32,13 @@ type ButtonEvent struct {
type Button struct {
Name string
Debounce time.Duration
Line *gpiod.Line
Line *gpiocdev.Line
PressCount int

btnChan chan ButtonEvent
evtChan chan gpiod.LineEvent
evtChan chan gpiocdev.LineEvent
state int
lastEvent gpiod.LineEvent
lastEvent gpiocdev.LineEvent
}

func NewButton(chip string, pin int, debounce time.Duration, name string, btnChan chan ButtonEvent) (*Button, error) {
Expand All @@ -48,14 +48,14 @@ func NewButton(chip string, pin int, debounce time.Duration, name string, btnCha
PressCount: 0,
state: 0,
btnChan: btnChan,
evtChan: make(chan gpiod.LineEvent),
lastEvent: gpiod.LineEvent{
Type: gpiod.LineEventRisingEdge,
evtChan: make(chan gpiocdev.LineEvent),
lastEvent: gpiocdev.LineEvent{
Type: gpiocdev.LineEventRisingEdge,
},
}

line, err := gpiod.RequestLine(chip, pin, gpiod.WithBothEdges, gpiod.WithPullDown,
gpiod.WithEventHandler(func(evt gpiod.LineEvent) {
line, err := gpiocdev.RequestLine(chip, pin, gpiocdev.WithBothEdges, gpiocdev.WithPullDown,
gpiocdev.WithEventHandler(func(evt gpiocdev.LineEvent) {
btn.evtChan <- evt
}))

Expand All @@ -74,7 +74,7 @@ func (btn *Button) Close() {
}

func (btn *Button) watch() {
events := make([]gpiod.LineEvent, 0, 2)
events := make([]gpiocdev.LineEvent, 0, 2)

// Wait for button presses
for {
Expand All @@ -83,14 +83,14 @@ func (btn *Button) watch() {
if btn.lastEvent.Type == evt.Type {
// Insert an event of the other type, 'cause we can't
// get two of the same edge in a row!
if evt.Type == gpiod.LineEventRisingEdge {
events = append(events, gpiod.LineEvent{
Type: gpiod.LineEventFallingEdge,
if evt.Type == gpiocdev.LineEventRisingEdge {
events = append(events, gpiocdev.LineEvent{
Type: gpiocdev.LineEventFallingEdge,
Timestamp: evt.Timestamp,
})
} else {
events = append(events, gpiod.LineEvent{
Type: gpiod.LineEventRisingEdge,
events = append(events, gpiocdev.LineEvent{
Type: gpiocdev.LineEventRisingEdge,
Timestamp: evt.Timestamp,
})
}
Expand All @@ -102,7 +102,7 @@ func (btn *Button) watch() {
delta := evt.Timestamp - btn.lastEvent.Timestamp
btn.lastEvent = evt

fallingEdge := (evt.Type == gpiod.LineEventFallingEdge)
fallingEdge := (evt.Type == gpiocdev.LineEventFallingEdge)

// edge := "UP"

Expand Down
10 changes: 5 additions & 5 deletions cmd/raspberry-pi/hw.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"fmt"
"time"

"github.com/warthog618/gpiod"
"github.com/warthog618/go-gpiocdev"
)

type HardwareStuff struct {
Expand All @@ -34,7 +34,7 @@ type HardwareStuff struct {
button *Button
rotary *RotaryEncoder

leds map[string]*gpiod.Line
leds map[string]*gpiocdev.Line

btnChan chan ButtonEvent
rotaryChan chan RotaryEvent
Expand All @@ -59,7 +59,7 @@ func NewHardwareStuff(rotaryAPin, rotaryBPin, buttonPin, ledGreenPin, ledRedPin
return nil, fmt.Errorf("could not create rotary encoder on lines %d and %d: %s", rotaryAPin, rotaryBPin, err)
}

redLED, err := gpiod.RequestLine("gpiochip0", ledRedPin, gpiod.AsOutput(1), gpiod.LineDrivePushPull)
redLED, err := gpiocdev.RequestLine("gpiochip0", ledRedPin, gpiocdev.AsOutput(1), gpiocdev.LineDrivePushPull)

if err != nil {
btn.Close()
Expand All @@ -68,7 +68,7 @@ func NewHardwareStuff(rotaryAPin, rotaryBPin, buttonPin, ledGreenPin, ledRedPin
return nil, fmt.Errorf("could not create red LED on line %d: %s", ledRedPin, err)
}

greenLED, err := gpiod.RequestLine("gpiochip0", ledGreenPin, gpiod.AsOutput(1), gpiod.LineDrivePushPull)
greenLED, err := gpiocdev.RequestLine("gpiochip0", ledGreenPin, gpiocdev.AsOutput(1), gpiocdev.LineDrivePushPull)

if err != nil {
btn.Close()
Expand All @@ -87,7 +87,7 @@ func NewHardwareStuff(rotaryAPin, rotaryBPin, buttonPin, ledGreenPin, ledRedPin

button: btn,
rotary: rotary,
leds: map[string]*gpiod.Line{
leds: map[string]*gpiocdev.Line{
"red": redLED,
"green": greenLED,
},
Expand Down
18 changes: 9 additions & 9 deletions cmd/raspberry-pi/rotary.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"sync"
"time"

"github.com/warthog618/gpiod"
"github.com/warthog618/go-gpiocdev"
)

type RotaryEvent struct {
Expand All @@ -43,16 +43,16 @@ type rotaryDebugEvent struct {
type RotaryEncoder struct {
Name string
Debounce time.Duration
LineA *gpiod.Line
LineB *gpiod.Line
LineA *gpiocdev.Line
LineB *gpiocdev.Line
Position int

Debug bool

lock sync.Mutex

evtChan chan RotaryEvent
gpioChan chan gpiod.LineEvent
gpioChan chan gpiocdev.LineEvent
debounceChan chan interface{}
debounceTimer *time.Timer
state int
Expand All @@ -70,22 +70,22 @@ func NewRotaryEncoder(chip string, pinA int, pinB int, debounce time.Duration, n
state: 3, // Assume we're starting with the knob stationary with both pins high.
evtChan: evtChan,
debounceChan: make(chan interface{}),
gpioChan: make(chan gpiod.LineEvent),
gpioChan: make(chan gpiocdev.LineEvent),

debugEvents: make([]rotaryDebugEvent, 0, 10),
}

lineA, err := gpiod.RequestLine(chip, pinA, gpiod.WithBothEdges, gpiod.WithPullUp,
gpiod.WithEventHandler(func(evt gpiod.LineEvent) {
lineA, err := gpiocdev.RequestLine(chip, pinA, gpiocdev.WithBothEdges, gpiocdev.WithPullUp,
gpiocdev.WithEventHandler(func(evt gpiocdev.LineEvent) {
enc.gpioChan <- evt
}))

if err != nil {
return nil, err
}

lineB, err := gpiod.RequestLine(chip, pinB, gpiod.WithBothEdges, gpiod.WithPullUp,
gpiod.WithEventHandler(func(evt gpiod.LineEvent) {
lineB, err := gpiocdev.RequestLine(chip, pinB, gpiocdev.WithBothEdges, gpiocdev.WithPullUp,
gpiocdev.WithEventHandler(func(evt gpiocdev.LineEvent) {
enc.gpioChan <- evt
}))

Expand Down
12 changes: 6 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ go 1.23
toolchain go1.23.2

require (
github.com/warthog618/gpiod v0.8.2
github.com/warthog618/go-gpiocdev v0.9.0
google.golang.org/grpc v1.67.1
google.golang.org/protobuf v1.34.2
google.golang.org/protobuf v1.35.1
)

require (
golang.org/x/net v0.28.0 // indirect
golang.org/x/sys v0.24.0 // indirect
golang.org/x/text v0.17.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142 // indirect
golang.org/x/net v0.30.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/text v0.19.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20241007155032-5fefd90f89a9 // indirect
)
28 changes: 14 additions & 14 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/warthog618/go-gpiocdev v0.9.0 h1:AZWUq1WObgKCO9cJCACFpwWQw6yu8vJbIE6fRZ+6cbY=
github.com/warthog618/go-gpiocdev v0.9.0/go.mod h1:GV4NZC82fWJERqk7Gu0+KfLSDIBEDNm6aPGiHlmT5fY=
github.com/warthog618/go-gpiosim v0.1.0 h1:2rTMTcKUVZxpUuvRKsagnKAbKpd3Bwffp87xywEDVGI=
github.com/warthog618/go-gpiosim v0.1.0/go.mod h1:Ngx/LYI5toxHr4E+Vm6vTgCnt0of0tktsSuMUEJ2wCI=
github.com/warthog618/gpiod v0.8.2 h1:2HgQ9pNowPp7W77sXhX5ut5Tqq1WoS3t7bXYDxtYvxc=
github.com/warthog618/gpiod v0.8.2/go.mod h1:O7BNpHjCn/4YS5yFVmoFZAlY1LuYuQ8vhPf0iy/qdi4=
golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE=
golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg=
golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142 h1:e7S5W7MGGLaSu8j3YjdezkZ+m1/Nm0uRVRMEMGk26Xs=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142/go.mod h1:UqMtugtsSgubUsoxbuAoiCXvqvErP7Gf0so0mK9tHxU=
golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4=
golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU=
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
google.golang.org/genproto/googleapis/rpc v0.0.0-20241007155032-5fefd90f89a9 h1:QCqS/PdaHTSWGvupk2F/ehwHtGc0/GYkT+3GAcR1CCc=
google.golang.org/genproto/googleapis/rpc v0.0.0-20241007155032-5fefd90f89a9/go.mod h1:GX3210XPVPUjJbTUbvwI8f2IpZDMZuPJWDzDuebbviI=
google.golang.org/grpc v1.67.1 h1:zWnc1Vrcno+lHZCOofnIMvycFcc0QRGIzm9dhnDX68E=
google.golang.org/grpc v1.67.1/go.mod h1:1gLDyUQU7CTLJI90u3nXZ9ekeghjeM7pTDZlqFNg2AA=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
google.golang.org/protobuf v1.35.1 h1:m3LfL6/Ca+fqnjnlqQXNpFPABW1UD7mjh8KO2mKFytA=
google.golang.org/protobuf v1.35.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=