forked from dchote/talkiepi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpio.go
81 lines (64 loc) · 1.49 KB
/
gpio.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package talkiepi
import (
"fmt"
"time"
"github.com/dchote/gpio"
"github.com/stianeikeland/go-rpio"
)
func (b *Talkiepi) initGPIO() {
// we need to pull in rpio to pullup our button pin
if err := rpio.Open(); err != nil {
fmt.Println(err)
b.GPIOEnabled = false
return
} else {
b.GPIOEnabled = true
}
ButtonPinPullUp := rpio.Pin(ButtonPin)
ButtonPinPullUp.PullUp()
rpio.Close()
// unfortunately the gpio watcher stuff doesnt work for me in this context, so we have to poll the button instead
b.Button = gpio.NewInput(ButtonPin)
go func() {
for {
currentState, err := b.Button.Read()
if currentState != b.ButtonState && err == nil {
b.ButtonState = currentState
if b.Stream != nil {
if b.ButtonState == 1 {
fmt.Printf("Button is released\n")
b.TransmitStop()
} else {
fmt.Printf("Button is pressed\n")
b.TransmitStart()
}
}
}
time.Sleep(500 * time.Millisecond)
}
}()
// then we can do our gpio stuff
b.OnlineLED = gpio.NewOutput(OnlineLEDPin, false)
b.ParticipantsLED = gpio.NewOutput(ParticipantsLEDPin, false)
b.TransmitLED = gpio.NewOutput(TransmitLEDPin, false)
}
func (b *Talkiepi) LEDOn(LED gpio.Pin) {
if b.GPIOEnabled == false {
return
}
LED.High()
}
func (b *Talkiepi) LEDOff(LED gpio.Pin) {
if b.GPIOEnabled == false {
return
}
LED.Low()
}
func (b *Talkiepi) LEDOffAll() {
if b.GPIOEnabled == false {
return
}
b.LEDOff(b.OnlineLED)
b.LEDOff(b.ParticipantsLED)
b.LEDOff(b.TransmitLED)
}