forked from brainsmoke/spacebuttonmk2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
160 lines (142 loc) · 2.99 KB
/
main.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"time"
"os"
"strings"
"net/http"
"flag"
"post6.net/spacestate/gpio"
)
var secretKey string
func init() {
flag.StringVar(&secretKey, "key", "s3cr1t", "state change request key")
}
const statusLedGPIO = 21
const buttonGPIO = 20
const (
Green = iota
Red
)
type SpaceState int
const (
Unknown SpaceState = iota
Closed
Open
)
var strState = []string { "unknown", "closed", "open" }
var blinkPattern = [3][]int {
{Red,Green},
{Red},
{Green},
}
func showStatus(gpioPin int, stateChange chan SpaceState, quit chan int) {
ledGPIO, err := gpio.OpenGPIO(gpioPin, gpio.Output)
if err != nil {
panic(err)
}
defer ledGPIO.Close();
state := Unknown
color := -1
tick := time.NewTicker(time.Second / time.Duration(4))
i := 0
for {
if color != blinkPattern[state][i] {
color = blinkPattern[state][i]
gpio.WriteGPIO(ledGPIO, color)
}
select {
case <- tick.C:
i += 1
i %= len(blinkPattern[state])
case state = <-stateChange:
i %= len(blinkPattern[state])
case <-quit:
return
}
}
}
func buttonPoll(gpioPin int, buttonPress, quit chan int) {
buttonGPIO, err := gpio.OpenGPIO(gpioPin, gpio.Input)
if err != nil {
panic(err)
}
defer buttonGPIO.Close();
tick := time.NewTicker(time.Second / time.Duration(20))
i := 0
oldP := 1
for {
select {
case <- tick.C:
case <- quit:
return
}
if i == 0 {
p := gpio.ReadGPIO(buttonGPIO)
if p == 0 && oldP == 1 {
buttonPress <- 1
}
oldP = p
i = 4
} else {
i -= 1
}
}
}
func getState(url string) SpaceState {
os.Stdout.Write([]byte("doing request: "+url+"\n"))
buf := make([]byte, 1024)
resp, err := http.Get(url)
if err != nil {
return Unknown
}
_, err = resp.Body.Read(buf)
s := strings.ToLower(string(buf))
if strings.Contains(s, "open") {
return Open
} else if strings.Contains(s, "closed") {
return Closed
}
return Unknown
}
func changeState(state SpaceState, stateChange chan SpaceState) {
stateChange <- getState("https://techinc.nl/space/index.php?state="+strState[state]+"&key="+secretKey)
}
func fetchState(stateChange chan SpaceState, quit chan int) {
tick := time.NewTicker(time.Second * time.Duration(30))
for {
state := getState("https://techinc.nl/space/spacestate")
if state != Unknown {
stateChange <- state
}
select {
case <-tick.C:
case <-quit:
return
}
}
}
func main() {
flag.Parse()
stateChangeDisplay := make(chan SpaceState)
stateChangeIn := make(chan SpaceState)
buttonPress := make(chan int)
quit := make(chan int)
go showStatus(statusLedGPIO, stateChangeDisplay, quit)
go buttonPoll(buttonGPIO, buttonPress, quit)
go fetchState(stateChangeIn, quit)
state := Unknown
for {
select {
case state = <-stateChangeIn:
stateChangeDisplay<- state
os.Stdout.Write([]byte("state change: "+strState[state]+"\n"))
case <-buttonPress:
os.Stdout.Write([]byte("button pressed\n"))
newState := Open
if state == Open {
newState = Closed
}
go changeState(newState, stateChangeIn)
}
}
}