diff --git a/go.mod b/go.mod index 4c6b812..6475696 100644 --- a/go.mod +++ b/go.mod @@ -26,6 +26,7 @@ require ( github.com/miekg/dns v1.1.4 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/stianeikeland/go-rpio/v4 v4.6.0 // indirect github.com/tadglines/go-pkgs v0.0.0-20140924210655-1f86682992f1 // indirect golang.org/x/crypto v0.0.0-20190621222207-cc06ce4a13d4 // indirect golang.org/x/net v0.0.0-20191004110552-13f9640d40b9 // indirect diff --git a/go.sum b/go.sum index 6f19add..79902aa 100644 --- a/go.sum +++ b/go.sum @@ -56,6 +56,8 @@ github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnIn github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s= +github.com/stianeikeland/go-rpio/v4 v4.6.0 h1:eAJgtw3jTtvn/CqwbC82ntcS+dtzUTgo5qlZKe677EY= +github.com/stianeikeland/go-rpio/v4 v4.6.0/go.mod h1:A3GvHxC1Om5zaId+HqB3HKqx4K/AqeckxB7qRjxMK7o= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= diff --git a/pkg/dashboard/button.go b/pkg/dashboard/button.go new file mode 100644 index 0000000..ace9ff8 --- /dev/null +++ b/pkg/dashboard/button.go @@ -0,0 +1,29 @@ +package dashboard + +import ( + "fmt" + "time" + + "github.com/stianeikeland/go-rpio/v4" +) + +func (a *App) WatchButton() { + if err := rpio.Open(); err != nil { + fmt.Println(err) + return + } + defer rpio.Close() + buttonPin := rpio.Pin(a.ButtonPin) + + buttonPin.Input() + buttonPin.PullUp() + buttonPin.Detect(rpio.FallEdge) // enable falling edge event detection + defer buttonPin.Detect(rpio.NoEdge) // disable edge event detection + + for { + if buttonPin.EdgeDetected() { // check if event occured + fmt.Println("button pressed") + } + time.Sleep(time.Second / 2) + } +} diff --git a/pkg/dashboard/dashboard.go b/pkg/dashboard/dashboard.go index c65ca72..a85575b 100644 --- a/pkg/dashboard/dashboard.go +++ b/pkg/dashboard/dashboard.go @@ -27,10 +27,11 @@ var ( // App encapsulates all the config for the server type App struct { - Router *mux.Router - Port int - Array *neopixel.LEDArray - Screen *screen.Display + Router *mux.Router + Port int + Array *neopixel.LEDArray + Screen *screen.Display + ButtonPin int64 } func getBaseTemplate() (*template.Template, error) { @@ -106,6 +107,8 @@ func (a *App) Initialize() { } } + a.ButtonPin = 4 + a.Router = router } @@ -113,6 +116,7 @@ func (a *App) Initialize() { func (a *App) Run() { http.Handle("/", a.Router) klog.Infof("Starting dashboard server on port %d", a.Port) + go a.WatchButton() defer a.Array.WS.Fini() klog.Fatalf("%v", http.ListenAndServe(fmt.Sprintf(":%d", a.Port), nil)) }