Conf file move, multi config support, on time support
This commit is contained in:
5
go.mod
5
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
|
||||
)
|
||||
|
||||
4
go.sum
4
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=
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
11
ultrasonic2relay/test.yaml
Normal file
11
ultrasonic2relay/test.yaml
Normal file
@@ -0,0 +1,11 @@
|
||||
relay:
|
||||
control: 21
|
||||
ultrasonics:
|
||||
- trigger: 6
|
||||
echo: 5
|
||||
- trigger: 23
|
||||
echo: 22
|
||||
medianbuffer: 47
|
||||
oncm: 50
|
||||
offcm: 70
|
||||
onseconds: 5
|
||||
Reference in New Issue
Block a user