commit 330a26fb73796c9ad02c247d5554d82b3a0525bf Author: Daniel Ledda Date: Sat Oct 31 12:37:02 2020 +0100 first commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..723ef36 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea \ No newline at end of file diff --git a/main.go b/main.go new file mode 100644 index 0000000..884b6bd --- /dev/null +++ b/main.go @@ -0,0 +1,50 @@ +package main + +import ( + "fmt" + "os" + "syscall" + "time" + + "github.com/warthog618/gpiod" + "github.com/warthog618/gpiod/device/rpi" +) + +// Watches GPIO 4 (Raspberry Pi J8-7) and reports when it changes state. +func main() { + c, err := gpiod.NewChip("gpiochip0") + if err != nil { + panic(err) + } + defer c.Close() + + offset := rpi.J8p7 + l, err := c.RequestLine(offset, + gpiod.WithPullUp, + gpiod.WithBothEdges(func(evt gpiod.LineEvent) { + t := time.Now() + edge := "rising" + if evt.Type == gpiod.LineEventFallingEdge { + edge = "falling" + } + fmt.Printf("event:%3d %-7s %s (%s)\n", + evt.Offset, + edge, + t.Format(time.RFC3339Nano), + evt.Timestamp) + })) + if err != nil { + fmt.Printf("RequestLine returned error: %s\n", 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() + + // In a real application the main thread would do something useful. + // But we'll just run for a minute then exit. + fmt.Printf("Watching Pin %d...\n", offset) + time.Sleep(time.Minute) + fmt.Println("exiting...") +} \ No newline at end of file