From a450f3162b57a321659c694eda9612a256428601 Mon Sep 17 00:00:00 2001 From: Thomas Vogl Date: Sun, 8 Jan 2023 11:09:25 +0100 Subject: [PATCH] - fix RPI crash when changing state of output pin (reason: DetectAnyEdge() was also configured for output pins) - version string has now also build date - updated golang dependencies --- Makefile | 2 +- go.mod | 15 +++++----- go.sum | 23 +++++++++++++++ internal/PinControlService/HardwarePinEmu.go | 21 ++++++++++---- internal/PinControlService/HardwarePinRpi.go | 2 +- internal/PinControlService/Pin.go | 28 +++++++++---------- .../PinControlService/PinControlService.go | 2 +- 7 files changed, 62 insertions(+), 31 deletions(-) diff --git a/Makefile b/Makefile index c8c450b..e1f51df 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ BINARY_NAME=rpicontrol -GIT_COMMIT=$(shell git rev-list -1 HEAD --abbrev-commit) +GIT_COMMIT=$(shell git rev-list -1 HEAD --abbrev-commit)-$(shell date +%y%m%d_%H%M%S) VERSION=1.0.0 get: diff --git a/go.mod b/go.mod index aea4869..ed0fa1c 100644 --- a/go.mod +++ b/go.mod @@ -3,14 +3,15 @@ module rpiMqttControl go 1.17 require ( - github.com/eclipse/paho.mqtt.golang v1.3.5 - github.com/sirupsen/logrus v1.8.1 - github.com/stianeikeland/go-rpio v4.2.0+incompatible - gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b + github.com/eclipse/paho.mqtt.golang v1.4.2 + github.com/sirupsen/logrus v1.9.0 + github.com/stianeikeland/go-rpio/v4 v4.6.0 + gopkg.in/yaml.v3 v3.0.1 ) require ( - github.com/gorilla/websocket v1.4.2 // indirect - golang.org/x/net v0.0.0-20200425230154-ff2c4b7c35a0 // indirect - golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd // indirect + github.com/gorilla/websocket v1.5.0 // indirect + golang.org/x/net v0.5.0 // indirect + golang.org/x/sync v0.1.0 // indirect + golang.org/x/sys v0.4.0 // indirect ) diff --git a/go.sum b/go.sum index 8ef3b20..88354b3 100644 --- a/go.sum +++ b/go.sum @@ -1,26 +1,49 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/eclipse/paho.mqtt.golang v1.3.5 h1:sWtmgNxYM9P2sP+xEItMozsR3w0cqZFlqnNN1bdl41Y= github.com/eclipse/paho.mqtt.golang v1.3.5/go.mod h1:eTzb4gxwwyWpqBUHGQZ4ABAV7+Jgm1PklsYT/eo8Hcc= +github.com/eclipse/paho.mqtt.golang v1.4.2 h1:66wOzfUHSSI1zamx7jR6yMEI5EuHnT1G6rNA5PM12m4= +github.com/eclipse/paho.mqtt.golang v1.4.2/go.mod h1:JGt0RsEwEX+Xa/agj90YJ9d9DH2b7upDZMK9HRbFvCA= github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= +github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE= github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= +github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0= +github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/stianeikeland/go-rpio v4.2.0+incompatible h1:CUOlIxdJdT+H1obJPsmg8byu7jMSECLfAN9zynm5QGo= github.com/stianeikeland/go-rpio v4.2.0+incompatible/go.mod h1:Sh81rdJwD96E2wja2Gd7rrKM+XZ9LrwvN2w4IXrqLR8= +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 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/net v0.0.0-20200425230154-ff2c4b7c35a0 h1:Jcxah/M+oLZ/R4/z5RzfPzGbPXnVDPkEDtf2JnuxN+U= golang.org/x/net v0.0.0-20200425230154-ff2c4b7c35a0/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= +golang.org/x/net v0.5.0 h1:GyT4nK/YDHSqa1c4753ouYCDajOYKTja9Xb/OHtgvSw= +golang.org/x/net v0.5.0/go.mod h1:DivGGAXEgPSlEBzxGzZI+ZLohi+xUj054jfeKui00ws= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= +golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd h1:xhmwyvizuTgC2qz7ZlMluP20uW+C3Rm0FD/WLDX8884= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18= +golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 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.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +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/internal/PinControlService/HardwarePinEmu.go b/internal/PinControlService/HardwarePinEmu.go index d571dbe..8096d66 100644 --- a/internal/PinControlService/HardwarePinEmu.go +++ b/internal/PinControlService/HardwarePinEmu.go @@ -3,26 +3,35 @@ package PinControlService type PinEmu struct { + state State + number int } -func (*PinEmu) Toggle() {} -func (*PinEmu) High() {} -func (*PinEmu) Low() {} +func (p *PinEmu) Toggle() { + if p.state == LowState { + p.state = HighState + } else { + p.state = LowState + } + +} +func (p *PinEmu) High() { p.state = HighState } +func (p *PinEmu) Low() { p.state = LowState } func (*PinEmu) Input() {} func (*PinEmu) Output() {} func (*PinEmu) Detect(Edge) {} func (*PinEmu) PullUp() {} func (*PinEmu) PullDown() {} func (*PinEmu) PullOff() {} -func (*PinEmu) Read() State { return LowState } +func (p *PinEmu) Read() State { return p.state } func (*PinEmu) EdgeDetected() bool { return false } type HardwarePin struct { Pin PinEmu } -func NewHardwarePin(n int) HardwarePinInterface { - return &PinEmu{} +func NewHardwarePin(num int) HardwarePinInterface { + return &PinEmu{state: LowState, number: num} } func HardwarePinOpen() error { diff --git a/internal/PinControlService/HardwarePinRpi.go b/internal/PinControlService/HardwarePinRpi.go index 2a9ebe1..ba503f2 100644 --- a/internal/PinControlService/HardwarePinRpi.go +++ b/internal/PinControlService/HardwarePinRpi.go @@ -2,7 +2,7 @@ package PinControlService -import "github.com/stianeikeland/go-rpio" +import "github.com/stianeikeland/go-rpio/v4" type PinRpi struct { pin rpio.Pin diff --git a/internal/PinControlService/Pin.go b/internal/PinControlService/Pin.go index 80dabe4..500c941 100644 --- a/internal/PinControlService/Pin.go +++ b/internal/PinControlService/Pin.go @@ -70,22 +70,8 @@ func (p *Pin) Command(cmd PinCommand) error { } func (p *Pin) Configure() { - if p.Direction == Input { - log.Infof("configuring pin %s (pin no: %d) as Input", p.Name, p.Id) - p.PinHandle.Input() - } else if p.Direction == Output { - log.Infof("configuring pin %s (pin no: %d) as Output", p.Name, p.Id) - p.PinHandle.Output() - if p.InitialState != nil { - log.Infof("set initial state \"%s\" for pin %s (pin no: %d)", *p.InitialState, p.Name, p.Id) - _ = p.Command(*p.InitialState) - } - - } - - p.PinHandle.Detect(AnyEdge) - if p.PullConfig != nil { + log.Infof("configuring Pull Resistor for pin %s (pin no: %d) as %s", p.Name, p.Id, *p.PullConfig) if *p.PullConfig == PullUp { p.PinHandle.PullUp() } else if *p.PullConfig == PullDown { @@ -97,6 +83,18 @@ func (p *Pin) Configure() { } } + if p.Direction == Input { + log.Infof("configuring pin %s (pin no: %d) as Input", p.Name, p.Id) + p.PinHandle.Input() + p.PinHandle.Detect(AnyEdge) + } else if p.Direction == Output { + log.Infof("configuring pin %s (pin no: %d) as Output", p.Name, p.Id) + p.PinHandle.Output() + if p.InitialState != nil { + log.Infof("set initial state \"%s\" for pin %s (pin no: %d)", *p.InitialState, p.Name, p.Id) + _ = p.Command(*p.InitialState) + } + } } func (p *Pin) Changed() bool { diff --git a/internal/PinControlService/PinControlService.go b/internal/PinControlService/PinControlService.go index b1fd6f0..bb09082 100644 --- a/internal/PinControlService/PinControlService.go +++ b/internal/PinControlService/PinControlService.go @@ -32,7 +32,7 @@ func (p *PinControlService) Command(pinName string, command PinCommand) error { func (p *PinControlService) Start() { if err := HardwarePinOpen(); err != nil { log.Fatal(err) - panic(err) + log.Exit(1) } for _, v := range p.Pins {