-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpio_out.go
44 lines (39 loc) · 887 Bytes
/
gpio_out.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package main
import (
"context"
rpio "github.com/stianeikeland/go-rpio"
"log"
)
// GpioWriter
type GpioWriter struct {
// C is the channel for changing state of the GPIO
C chan bool
config GpioWriterCfg
}
// NewGpioWriter takes a configuration value and returns a GpioWriter instance
func NewGpioWriter(c GpioWriterCfg) *GpioWriter {
g := &GpioWriter{
C: make(chan bool, 32),
config: c,
}
return g
}
// Start contains the main loop for the GpioWriter, should be run as a goroutine
func (g *GpioWriter) Start(ctx context.Context) {
pin := rpio.Pin(g.config.Pin)
pin.Output()
for {
select {
case <-ctx.Done():
return
case st := <-g.C:
if st == g.config.Invert {
log.Printf("[gpioOut:%d] Setting pin to high", g.config.Pin)
pin.High()
} else {
log.Printf("[gpioOut:%d] Setting pin to low", g.config.Pin)
pin.Low()
}
}
}
}