initial commit
This commit is contained in:
101
internal/PinControlService/Pin.go
Normal file
101
internal/PinControlService/Pin.go
Normal file
@@ -0,0 +1,101 @@
|
||||
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
|
||||
SendPollingEvents bool
|
||||
SendChangeEvents bool
|
||||
|
||||
}
|
||||
|
||||
func NewPin(config PinConfig) Pin {
|
||||
p := Pin{
|
||||
Direction: config.Direction,
|
||||
PullConfig: config.PullConfig,
|
||||
Name: config.Name,
|
||||
Id: config.PinNumber,
|
||||
PinHandle: rpio.Pin(config.PinNumber),
|
||||
}
|
||||
|
||||
if config.SendPollingEvents != nil {
|
||||
p.SendPollingEvents = *config.SendPollingEvents
|
||||
} else {
|
||||
p.SendPollingEvents = true
|
||||
}
|
||||
|
||||
if config.SendChangeEvents != nil {
|
||||
p.SendChangeEvents = *config.SendChangeEvents
|
||||
} else {
|
||||
p.SendChangeEvents = true
|
||||
}
|
||||
|
||||
if config.InitialState != "" {
|
||||
p.InitialState = PinCommand(config.InitialState)
|
||||
} else {
|
||||
p.InitialState = Off
|
||||
}
|
||||
|
||||
return p
|
||||
}
|
||||
|
||||
func (p *Pin) State() PinState {
|
||||
if state := p.PinHandle.Read(); state == rpio.High {
|
||||
return StateOn
|
||||
} else {
|
||||
return StateOff
|
||||
}
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
|
||||
if cmd == On {
|
||||
p.PinHandle.High()
|
||||
} else if cmd == Off {
|
||||
p.PinHandle.Low()
|
||||
} else if cmd == Toggle {
|
||||
p.PinHandle.Toggle()
|
||||
} else {
|
||||
return errors.New("unknown command")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Pin) Configure() {
|
||||
if p.Direction == Input {
|
||||
p.PinHandle.Input()
|
||||
} else if p.Direction == Output {
|
||||
p.PinHandle.Output()
|
||||
_ = p.Command(p.InitialState)
|
||||
}
|
||||
|
||||
p.PinHandle.Detect(rpio.AnyEdge)
|
||||
|
||||
if p.PullConfig == PullUp {
|
||||
p.PinHandle.PullUp()
|
||||
} else if p.PullConfig == PullDown {
|
||||
p.PinHandle.PullDown()
|
||||
} else if p.PullConfig == PullOff {
|
||||
p.PinHandle.PullOff()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (p *Pin) Changed() bool {
|
||||
return p.PinHandle.EdgeDetected()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user