Skip to content

Commit

Permalink
Switch GPIO library to its new name of go-gpiocdev, at version 0.9.0.
Browse files Browse the repository at this point in the history
Signed-off-by: Flynn <[email protected]>
  • Loading branch information
kflynn committed Oct 14, 2024
1 parent cb31c58 commit c696361
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 35 deletions.
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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
)
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ 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=
Expand Down

0 comments on commit c696361

Please sign in to comment.