From dca0c4229d94fcfbe20d0ad12edbb9b0753a7af0 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Sat, 24 Sep 2022 21:33:06 -0700 Subject: [PATCH] Super explicit states --- ultrasonic2relay/main.go | 49 ++++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/ultrasonic2relay/main.go b/ultrasonic2relay/main.go index 431136b..72b7fb0 100644 --- a/ultrasonic2relay/main.go +++ b/ultrasonic2relay/main.go @@ -35,6 +35,14 @@ type Ultrasonic struct { Echo int } +type state int + +const ( + onTimer state = iota + offTimer + watching +) + func main() { flag.Parse() @@ -64,8 +72,8 @@ func main() { last[i] = math.MaxFloat64 } - onUntil := time.Time{} - offUntil := time.Time{} + var st state = watching + var onUntil, offUntil time.Time for { // Fetch new values @@ -88,25 +96,28 @@ func main() { } } - if !r.IsOn() && on > 0 && onUntil.IsZero() && offUntil.IsZero() { - // Not on, no timers, something detected - r.On() - onUntil = time.Now().Add(time.Duration(cf.OnSeconds * float64(time.Second))) - log.Printf("on %s", fmtDists(last)) - } + switch st { + case watching: + if on > 0 { + log.Printf("on %s", fmtDists(last)) + r.On() + onUntil = time.Now().Add(time.Duration(cf.OnSeconds * float64(time.Second))) + st = onTimer + } - if r.IsOn() && onUntil.Before(time.Now()) { - // On timer expired, turn off - r.Off() - offUntil = time.Now().Add(time.Duration(cf.OffSeconds * float64(time.Second))) - log.Printf("off %s", fmtDists(last)) - } + case onTimer: + if time.Now().After(onUntil) { + log.Printf("off %s", fmtDists(last)) + r.Off() + offUntil = time.Now().Add(time.Duration(cf.OffSeconds * float64(time.Second))) + st = offTimer + } - if !r.IsOn() && on == 0 && off == len(uss) && !offUntil.IsZero() && offUntil.Before(time.Now()) { - // All quiet and timers expired, reset state - onUntil = time.Time{} - offUntil = time.Time{} - log.Printf("reset %s", fmtDists(last)) + case offTimer: + if time.Now().After(offUntil) && on == 0 && off == len(uss) { + log.Printf("reset %s", fmtDists(last)) + st = watching + } } } }