forked from dchote/talkiepi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpio.go
98 lines (77 loc) · 1.77 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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
}
TransmitButtonPinPullUp := rpio.Pin(TransmitButtonPin)
TransmitButtonPinPullUp.PullUp()
rpio.Close()
b.Buttons = []TalkieButton{
TalkieButton{
Pin: gpio.NewInput(TransmitButtonPin),
State: 1,
OnPress: func() {
fmt.Printf("Transmit is pressed\n")
b.TransmitStart()
},
OnRelease: func() {
fmt.Printf("Transmit is released\n")
b.TransmitStop()
},
},
}
// unfortunately the gpio watcher stuff doesnt work for me in this context, so we have to poll the buttons instead
go func() {
for {
for i := 0; i < len(b.Buttons); i++ {
currentState, err := b.Buttons[i].Pin.Read()
if currentState != b.Buttons[i].State && err == nil {
b.Buttons[i].State = currentState
if b.Stream != nil {
if b.Buttons[i].State == 1 {
b.Buttons[i].OnRelease()
} else {
b.Buttons[i].OnPress()
}
}
}
}
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)
}