Skip to content

Commit

Permalink
add graceful shutdown
Browse files Browse the repository at this point in the history
Signed-off-by: Nahshon Unna-Tsameret <[email protected]>
  • Loading branch information
nunnatsa committed Mar 20, 2021
1 parent 6f71531 commit 66b6d76
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 7 deletions.
13 changes: 12 additions & 1 deletion controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ func (c *Controller) do() {
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt, syscall.SIGTERM)

defer c.stop(signals)

c.hat.Start()

msg := c.state.CreateDisplayMessage()
Expand Down Expand Up @@ -74,7 +76,16 @@ func (c *Controller) do() {

if changed {
msg := c.state.CreateDisplayMessage()
c.screenEvents <- msg
go func() {
c.screenEvents <- msg
}()
}
}
}

func (c *Controller) stop(signals chan os.Signal) {
c.hat.Stop()
<-c.joystickEvents // wait for the hat graceful shutdown
signal.Stop(signals)
close(c.done)
}
12 changes: 8 additions & 4 deletions controller/hatMock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ type hatMock struct {
se chan hat.DisplayMessage
}

func (h *hatMock) Start() {
/* Implement hat.Interface */
}

func (h *hatMock) Stop() {
close(h.je)
}

func (h *hatMock) MoveUp() {
h.je <- hat.MoveUp
}
Expand All @@ -25,10 +33,6 @@ func (h *hatMock) MoveLeft() {
h.je <- hat.MoveLeft
}

func (h *hatMock) Start() {

}

func (h *hatMock) Press() {
h.je <- hat.Pressed
}
23 changes: 22 additions & 1 deletion hat/hat.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

type Interface interface {
Start()
Stop()
}

// The format of the HAT color is 16-bit: 5 MS bits are the red color, the middle 6 bits are
Expand Down Expand Up @@ -52,18 +53,27 @@ func NewDisplayMessage(mat [][]common.Color, x, y uint8) DisplayMessage {
type Hat struct {
events chan<- Event
screen <-chan DisplayMessage
done chan bool
input *stick.Device
}

func NewHat(joystickEvents chan<- Event, screenEvents <-chan DisplayMessage) *Hat {
return &Hat{events: joystickEvents, screen: screenEvents}
return &Hat{
events: joystickEvents,
screen: screenEvents,
done: make(chan bool),
}
}

func (h *Hat) Start() {
h.init()
go h.do()
}

func (h *Hat) Stop() {
h.done <- true
}

func (h *Hat) init() {
var err error
h.input, err = stick.Open(defaultJoystickFile)
Expand All @@ -77,6 +87,8 @@ func (h *Hat) init() {
}

func (h *Hat) do() {
defer h.gracefulShutDown()

for {
select {
case event := <-h.input.Events:
Expand Down Expand Up @@ -104,6 +116,9 @@ func (h *Hat) do() {

case screenChange := <-h.screen:
h.drawScreen(screenChange)

case <-h.done:
return
}
}
}
Expand All @@ -123,3 +138,9 @@ func (h *Hat) drawScreen(screenChange DisplayMessage) {
log.Println("error while printing to HAT display:", err)
}
}

func (h Hat) gracefulShutDown() {
screen.Clear()
// signal the controller we've done
close(h.events)
}
8 changes: 7 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package main

import "github.com/nunnatsa/piHatDraw/controller"
import (
"fmt"

"github.com/nunnatsa/piHatDraw/controller"
)

func main() {
control := controller.NewController()
<-control.Start()

fmt.Println("\nGood Bye!")
}

0 comments on commit 66b6d76

Please sign in to comment.