This commit is contained in:
2022-12-30 18:42:48 +01:00
parent 5b25457224
commit 005b167f5e
6 changed files with 155 additions and 42 deletions

View File

@@ -3,28 +3,26 @@ package PinControlService
import (
"errors"
log "github.com/sirupsen/logrus"
"github.com/stianeikeland/go-rpio"
)
type Pin struct {
Id int
Name string
Direction PinDirection
PullConfig PinPull
InitialState PinCommand
PinHandle rpio.Pin
Id int
Name string
Direction PinDirection
PullConfig PinPull
InitialState PinCommand
PinHandle HardwarePinInterface
SendPollingEvents bool
SendChangeEvents bool
SendChangeEvents bool
}
func NewPin(config PinConfig) Pin {
p := Pin{
Direction: config.Direction,
Direction: config.Direction,
PullConfig: config.PullConfig,
Name: config.Name,
Id: config.PinNumber,
PinHandle: rpio.Pin(config.PinNumber),
Name: config.Name,
Id: config.PinNumber,
PinHandle: NewHardwarePin(config.PinNumber),
}
if config.SendPollingEvents != nil {
@@ -49,14 +47,14 @@ func NewPin(config PinConfig) Pin {
}
func (p *Pin) State() PinState {
if state := p.PinHandle.Read(); state == rpio.High {
if state := p.PinHandle.Read(); State(state) == HighState {
return StateOn
} else {
return StateOff
}
}
func (p *Pin) Command(cmd PinCommand) error{
func (p *Pin) Command(cmd PinCommand) error {
log.Debugf("try to send command %s for pin %s (pin no: %d)", cmd, p.Name, p.Id)
if p.Direction != Output {
return errors.New("pin is not an output")
@@ -83,7 +81,7 @@ func (p *Pin) Configure() {
_ = p.Command(p.InitialState)
}
p.PinHandle.Detect(rpio.AnyEdge)
p.PinHandle.Detect(AnyEdge)
if p.PullConfig == PullUp {
p.PinHandle.PullUp()
@@ -98,4 +96,3 @@ func (p *Pin) Configure() {
func (p *Pin) Changed() bool {
return p.PinHandle.EdgeDetected()
}