Add: power on with context, SIM808 support
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
package gpio
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"log"
|
||||
"time"
|
||||
@ -8,6 +9,10 @@ import (
|
||||
gpio "github.com/stianeikeland/go-rpio/v4"
|
||||
)
|
||||
|
||||
const (
|
||||
waitCtxTimeout = 100 * time.Microsecond
|
||||
)
|
||||
|
||||
type gpioPin struct {
|
||||
logger *log.Logger
|
||||
pin gpio.Pin
|
||||
@ -16,7 +21,7 @@ type gpioPin struct {
|
||||
type Pin interface {
|
||||
Init() error
|
||||
PowerOn()
|
||||
PowerOff()
|
||||
PowerOnCtx(ctx context.Context)
|
||||
io.Closer
|
||||
}
|
||||
|
||||
@ -31,32 +36,47 @@ func (p gpioPin) Init() error {
|
||||
return gpio.Open()
|
||||
}
|
||||
|
||||
func (p gpioPin) sendOnOffSignal() {
|
||||
func waitCtx(ctx context.Context, timeout time.Duration) {
|
||||
deadline := time.Now().Add(timeout)
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
default:
|
||||
if time.Now().After(deadline) {
|
||||
return
|
||||
}
|
||||
}
|
||||
time.Sleep(waitCtxTimeout)
|
||||
}
|
||||
}
|
||||
|
||||
func (p gpioPin) sendOnOffSignal(ctx context.Context) {
|
||||
p.pin.Output()
|
||||
p.logger.Println("Power on 0/3 + 100ms")
|
||||
p.pin.Low()
|
||||
p.pin.Toggle()
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
waitCtx(ctx, 100*time.Millisecond)
|
||||
|
||||
p.logger.Println("Power on 1/3 + 3s")
|
||||
p.pin.High()
|
||||
p.pin.Toggle()
|
||||
time.Sleep(3 * time.Second)
|
||||
waitCtx(ctx, 3*time.Second)
|
||||
|
||||
p.logger.Println("Power on 2/3 + 30s")
|
||||
p.pin.Low()
|
||||
p.pin.Toggle()
|
||||
time.Sleep(30 * time.Second)
|
||||
waitCtx(ctx, 30*time.Second)
|
||||
|
||||
p.logger.Println("Power on 3/3")
|
||||
}
|
||||
|
||||
func (p gpioPin) PowerOn() {
|
||||
p.sendOnOffSignal()
|
||||
p.sendOnOffSignal(context.Background())
|
||||
}
|
||||
|
||||
func (p gpioPin) PowerOff() {
|
||||
p.sendOnOffSignal()
|
||||
func (p gpioPin) PowerOnCtx(ctx context.Context) {
|
||||
p.sendOnOffSignal(ctx)
|
||||
}
|
||||
|
||||
func (p gpioPin) Close() error {
|
||||
|
Reference in New Issue
Block a user