Changed package

This commit is contained in:
Daniel Ledda
2020-10-31 13:09:08 +01:00
parent def8902968
commit fcd13ee800
2 changed files with 24 additions and 30 deletions

54
main.go
View File

@@ -3,48 +3,42 @@ package main
import ( import (
"fmt" "fmt"
"os" "os"
"os/signal"
"syscall" "syscall"
"time" "time"
"github.com/warthog618/gpiod" "github.com/warthog618/gpio"
"github.com/warthog618/gpiod/device/rpi"
) )
// Watches GPIO 4 (Raspberry Pi J8-7) and reports when it changes state. // Watches GPIO 4 (J8 7) and reports when it changes state.
func main() { func main() {
c, err := gpiod.NewChip("gpiochip0") err := gpio.Open()
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer c.Close() defer gpio.Close()
pin := gpio.NewPin(gpio.J8p7)
pin.Input()
pin.PullUp()
offset := rpi.J8p7 // capture exit signals to ensure resources are released on exit.
l, err := c.RequestLine(offset, quit := make(chan os.Signal, 1)
gpiod.WithPullUp, signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
gpiod.WithBothEdges(func(evt gpiod.LineEvent) { defer signal.Stop(quit)
t := time.Now()
edge := "rising" err = pin.Watch(gpio.EdgeBoth, func(pin *gpio.Pin) {
if evt.Type == gpiod.LineEventFallingEdge { fmt.Printf("Pin 4 is %v", pin.Read())
edge = "falling" })
}
fmt.Printf("event:%3d %-7s %s (%s)\n",
evt.Offset,
edge,
t.Format(time.RFC3339Nano),
evt.Timestamp)
}))
if err != nil { if err != nil {
fmt.Printf("RequestLine returned error: %s\n", err) panic(err)
if err == syscall.Errno(22) {
fmt.Println("Note that the WithPullUp option requires kernel V5.5 or later - check your kernel version.")
}
os.Exit(1)
} }
defer l.Close() defer pin.Unwatch()
// In a real application the main thread would do something useful. // In a real application the main thread would do something useful here.
// But we'll just run for a minute then exit. // But we'll just run for a minute then exit.
fmt.Printf("Watching Pin %d...\n", offset) fmt.Println("Watching Pin 4...")
time.Sleep(time.Minute) select {
fmt.Println("exiting...") case <-time.After(time.Minute):
case <-quit:
}
} }

Binary file not shown.