...
This commit is contained in:
parent
5b25457224
commit
005b167f5e
2
go.mod
2
go.mod
|
@ -5,12 +5,12 @@ 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
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/gorilla/websocket v1.4.2 // indirect
|
||||
github.com/stianeikeland/go-rpio v4.2.0+incompatible // indirect
|
||||
golang.org/x/net v0.0.0-20200425230154-ff2c4b7c35a0 // indirect
|
||||
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd // indirect
|
||||
)
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
//go:build !arm && !arm64
|
||||
|
||||
package PinControlService
|
||||
|
||||
type PinEmu struct {
|
||||
}
|
||||
|
||||
func (*PinEmu) Toggle() {}
|
||||
func (*PinEmu) High() {}
|
||||
func (*PinEmu) Low() {}
|
||||
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 (*PinEmu) EdgeDetected() bool { return false }
|
||||
|
||||
type HardwarePin struct {
|
||||
Pin PinEmu
|
||||
}
|
||||
|
||||
func NewHardwarePin(n int) HardwarePinInterface {
|
||||
return &PinEmu{}
|
||||
}
|
||||
|
||||
func HardwarePinOpen() error {
|
||||
return nil
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
package PinControlService
|
||||
|
||||
type Mode uint8
|
||||
type State uint8
|
||||
type Pull uint8
|
||||
type Edge uint8
|
||||
|
||||
type HardwarePinInterface interface {
|
||||
Toggle()
|
||||
High()
|
||||
Low()
|
||||
Input()
|
||||
Output()
|
||||
Detect(Edge)
|
||||
PullUp()
|
||||
PullDown()
|
||||
PullOff()
|
||||
Read() State
|
||||
EdgeDetected() bool
|
||||
}
|
||||
|
||||
const (
|
||||
ModeInput Mode = iota
|
||||
ModeOutput
|
||||
ModeClock
|
||||
ModePwm
|
||||
)
|
||||
|
||||
const (
|
||||
LowState State = iota
|
||||
HighState
|
||||
)
|
||||
|
||||
const (
|
||||
NoEdge Edge = iota
|
||||
RiseEdge
|
||||
FallEdge
|
||||
AnyEdge = RiseEdge | FallEdge
|
||||
)
|
|
@ -0,0 +1,55 @@
|
|||
//go:build (arm || arm64) && linux
|
||||
|
||||
package PinControlService
|
||||
|
||||
import "github.com/stianeikeland/go-rpio"
|
||||
|
||||
type PinRpi struct {
|
||||
pin rpio.Pin
|
||||
}
|
||||
|
||||
func (p *PinRpi) Toggle() {
|
||||
p.pin.Toggle()
|
||||
}
|
||||
|
||||
func (p *PinRpi) High() {
|
||||
p.pin.High()
|
||||
}
|
||||
func (p *PinRpi) Low() {
|
||||
p.pin.Low()
|
||||
}
|
||||
func (p *PinRpi) Input() {
|
||||
p.pin.Input()
|
||||
}
|
||||
func (p *PinRpi) Output() {
|
||||
p.pin.Output()
|
||||
}
|
||||
func (p *PinRpi) Detect(e Edge) {
|
||||
p.pin.Detect(rpio.Edge(e))
|
||||
}
|
||||
func (p *PinRpi) PullUp() {
|
||||
p.pin.PullUp()
|
||||
}
|
||||
func (p *PinRpi) PullDown() {
|
||||
p.pin.PullDown()
|
||||
}
|
||||
func (p *PinRpi) PullOff() {
|
||||
p.pin.PullOff()
|
||||
}
|
||||
func (p *PinRpi) Read() State {
|
||||
return State(p.pin.Read())
|
||||
|
||||
}
|
||||
func (p *PinRpi) EdgeDetected() bool {
|
||||
return p.pin.EdgeDetected()
|
||||
}
|
||||
|
||||
func NewHardwarePin(n int) HardwarePinInterface {
|
||||
return &PinRpi{
|
||||
pin: rpio.Pin(n),
|
||||
}
|
||||
}
|
||||
|
||||
func HardwarePinOpen() error {
|
||||
return rpio.Open()
|
||||
}
|
|
@ -3,7 +3,6 @@ package PinControlService
|
|||
import (
|
||||
"errors"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/stianeikeland/go-rpio"
|
||||
)
|
||||
|
||||
type Pin struct {
|
||||
|
@ -12,10 +11,9 @@ type Pin struct {
|
|||
Direction PinDirection
|
||||
PullConfig PinPull
|
||||
InitialState PinCommand
|
||||
PinHandle rpio.Pin
|
||||
PinHandle HardwarePinInterface
|
||||
SendPollingEvents bool
|
||||
SendChangeEvents bool
|
||||
|
||||
}
|
||||
|
||||
func NewPin(config PinConfig) Pin {
|
||||
|
@ -24,7 +22,7 @@ func NewPin(config PinConfig) Pin {
|
|||
PullConfig: config.PullConfig,
|
||||
Name: config.Name,
|
||||
Id: config.PinNumber,
|
||||
PinHandle: rpio.Pin(config.PinNumber),
|
||||
PinHandle: NewHardwarePin(config.PinNumber),
|
||||
}
|
||||
|
||||
if config.SendPollingEvents != nil {
|
||||
|
@ -49,7 +47,7 @@ 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
|
||||
|
@ -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()
|
||||
}
|
||||
|
||||
|
|
|
@ -3,24 +3,17 @@ package PinControlService
|
|||
import (
|
||||
"errors"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/stianeikeland/go-rpio"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
type PinControlService struct {
|
||||
Pins map[string]Pin
|
||||
timer *time.Ticker
|
||||
exit chan bool
|
||||
OnChangeCallback PinCallback
|
||||
OnCycleCallback PinCallback
|
||||
|
||||
}
|
||||
|
||||
|
||||
func (p *PinControlService) AddPin(
|
||||
config PinConfig) {
|
||||
pin := NewPin(config)
|
||||
|
@ -37,7 +30,7 @@ func (p*PinControlService) Command(pinName string, command PinCommand) error {
|
|||
}
|
||||
|
||||
func (p *PinControlService) Start() {
|
||||
if err := rpio.Open(); err != nil {
|
||||
if err := HardwarePinOpen(); err != nil {
|
||||
log.Fatal(err)
|
||||
panic(err)
|
||||
}
|
||||
|
@ -79,7 +72,6 @@ func (p*PinControlService) _task() {
|
|||
|
||||
}
|
||||
|
||||
|
||||
func NewPinControl(config *PinControlConfig) (*PinControlService, error) {
|
||||
p := PinControlService{
|
||||
Pins: make(map[string]Pin),
|
||||
|
|
Loading…
Reference in New Issue