From 035df9e7fdf56dfe74c453f2a54aba7e9439ad83 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Sat, 24 Sep 2022 17:20:10 -0700 Subject: [PATCH] Conf file move, multi config support, on time support --- go.mod | 5 +- go.sum | 4 ++ hhio/relay.go | 7 +++ ultrasonic2relay/main.go | 119 +++++++++++++++++++++++++++++++++---- ultrasonic2relay/test.yaml | 11 ++++ 5 files changed, 133 insertions(+), 13 deletions(-) create mode 100644 ultrasonic2relay/test.yaml diff --git a/go.mod b/go.mod index d5e6878..0c5ed3a 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module github.com/firestuff/hh go 1.18 -require github.com/stianeikeland/go-rpio/v4 v4.6.0 +require ( + github.com/stianeikeland/go-rpio/v4 v4.6.0 + gopkg.in/yaml.v3 v3.0.1 +) diff --git a/go.sum b/go.sum index 7276cad..9067b9e 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,6 @@ 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= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/hhio/relay.go b/hhio/relay.go index 8562595..122d74c 100644 --- a/hhio/relay.go +++ b/hhio/relay.go @@ -4,6 +4,7 @@ import "github.com/stianeikeland/go-rpio/v4" type Relay struct { pin rpio.Pin + on bool } func NewRelay(pin int) *Relay { @@ -18,8 +19,14 @@ func NewRelay(pin int) *Relay { func (r *Relay) On() { r.pin.High() + r.on = true } func (r *Relay) Off() { r.pin.Low() + r.on = false +} + +func (r *Relay) IsOn() bool { + return r.on } diff --git a/ultrasonic2relay/main.go b/ultrasonic2relay/main.go index 04a08e8..f84da04 100644 --- a/ultrasonic2relay/main.go +++ b/ultrasonic2relay/main.go @@ -1,29 +1,124 @@ package main import "context" -import "fmt" +import "flag" +import "math" +import "os" +import "time" import "github.com/firestuff/hh/hhio" +import "gopkg.in/yaml.v3" + +var conf = flag.String("conf", "", "path to config file") + +type Config struct { + Relay + Ultrasonics []Ultrasonic + + MedianBuffer int + + OnCM float64 + OffCM float64 + + OnSeconds float64 +} + +type Relay struct { + Control int +} + +type Ultrasonic struct { + Trigger int + Echo int +} func main() { - err := hhio.Open() + flag.Parse() + + cf, err := readConf() + if err != nil { + panic(err) + } + + err = hhio.Open() if err != nil { panic(err) } defer hhio.Close() - us := hhio.NewUltrasonic(context.Background(), 6, 5) - mf := hhio.NewMedianFilter(us.C, 47) + uss := []chan float64{} - r := hhio.NewRelay(21) + for _, uscf := range cf.Ultrasonics { + us := hhio.NewUltrasonic(context.Background(), uscf.Trigger, uscf.Echo) + mf := hhio.NewMedianFilter(us.C, cf.MedianBuffer) + uss = append(uss, mf) + } - for dist := range mf { - fmt.Printf("%f\n", dist) + r := hhio.NewRelay(cf.Relay.Control) - if dist < 50 { - r.On() - } else if dist > 70 { - r.Off() - } + last := make([]float64, len(uss)) + for i := range uss { + last[i] = math.MaxFloat64 + } + + onUntil := time.Time{} + + for { + // Fetch new values + for i, us := range uss { + select { + case dist := <-us: + last[i] = dist + + default: + } + } + + // Count votes + var on, off int + for _, v := range last { + if v < cf.OnCM { + on++ + } else if v > cf.OffCM { + off++ + } + } + + if on > 0 { + // At least one on, turn on + if onUntil.IsZero() { + r.On() + onUntil = time.Now().Add(time.Duration(cf.OnSeconds * float64(time.Second))) + } + } else if off == len(uss) { + // All off, turn off + if !r.IsOn() { + onUntil = time.Time{} + } + } + + if !onUntil.IsZero() && onUntil.Before(time.Now()) { + r.Off() + } } } + +func readConf() (*Config, error) { + fh, err := os.Open(*conf) + if err != nil { + return nil, err + } + defer fh.Close() + + dec := yaml.NewDecoder(fh) + dec.KnownFields(true) + + c := &Config{} + + err = dec.Decode(c) + if err != nil { + return nil, err + } + + return c, nil +} diff --git a/ultrasonic2relay/test.yaml b/ultrasonic2relay/test.yaml new file mode 100644 index 0000000..091ddba --- /dev/null +++ b/ultrasonic2relay/test.yaml @@ -0,0 +1,11 @@ +relay: + control: 21 +ultrasonics: +- trigger: 6 + echo: 5 +- trigger: 23 + echo: 22 +medianbuffer: 47 +oncm: 50 +offcm: 70 +onseconds: 5