Skip to content

Commit

Permalink
initial button watcher in place
Browse files Browse the repository at this point in the history
  • Loading branch information
sudermanjr committed Feb 18, 2023
1 parent 03f4f62 commit f345eb2
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
29 changes: 29 additions & 0 deletions pkg/dashboard/button.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
12 changes: 8 additions & 4 deletions pkg/dashboard/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -106,13 +107,16 @@ func (a *App) Initialize() {
}
}

a.ButtonPin = 4

a.Router = router
}

// Run starts the http server
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))
}
Expand Down

0 comments on commit f345eb2

Please sign in to comment.