diff --git a/main.go b/main.go index 884b6bd..673df1b 100644 --- a/main.go +++ b/main.go @@ -3,48 +3,42 @@ package main import ( "fmt" "os" + "os/signal" "syscall" "time" - "github.com/warthog618/gpiod" - "github.com/warthog618/gpiod/device/rpi" + "github.com/warthog618/gpio" ) -// 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() { - c, err := gpiod.NewChip("gpiochip0") + err := gpio.Open() if err != nil { panic(err) } - defer c.Close() + defer gpio.Close() + pin := gpio.NewPin(gpio.J8p7) + pin.Input() + pin.PullUp() - 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) - })) + // capture exit signals to ensure resources are released on exit. + quit := make(chan os.Signal, 1) + signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM) + defer signal.Stop(quit) + + err = pin.Watch(gpio.EdgeBoth, func(pin *gpio.Pin) { + fmt.Printf("Pin 4 is %v", pin.Read()) + }) 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) + panic(err) } - 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. - fmt.Printf("Watching Pin %d...\n", offset) - time.Sleep(time.Minute) - fmt.Println("exiting...") + fmt.Println("Watching Pin 4...") + select { + case <-time.After(time.Minute): + case <-quit: + } } \ No newline at end of file diff --git a/temperature-test b/temperature-test deleted file mode 100755 index 7feb2e0..0000000 Binary files a/temperature-test and /dev/null differ