-
Notifications
You must be signed in to change notification settings - Fork 0
/
mon.go
103 lines (91 loc) · 1.7 KB
/
mon.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
package main
import (
"fmt"
"os"
"time"
)
var (
ledGreenGPIO = 23
ledRedGPIO = 45
greenOn = false
greenBlink = false
redOn = false
redBlink = false
)
func setGPIO(gpio int, on bool) {
// Open file
fd, err := os.OpenFile(fmt.Sprintf("/sys/class/gpio/gpio%d/value", gpio), os.O_RDWR|os.O_SYNC, 0666)
if err != nil {
fmt.Printf("Unable to open GPIO %d. Skipping.", gpio)
return
}
defer fd.Close()
// Write value
if on {
_, err = fmt.Fprintln(fd, "1")
if err != nil {
fmt.Printf("Unable to write to GPIO %d. Skipping.", gpio)
return
}
} else {
_, err = fmt.Fprintln(fd, "0")
if err != nil {
fmt.Printf("Unable to write to GPIO %d. Skipping.", gpio)
return
}
}
}
func SetGreenLed(on bool, blink bool) {
greenOn = on
greenBlink = blink
}
func SetRedLed(on bool, blink bool) {
redOn = on
redBlink = blink
}
func StartLed() {
isNowGreenOn := false
isNowRedOn := false
go (func() {
for {
// Update state every second
time.Sleep(time.Second)
// Green
if greenOn {
if greenBlink {
if isNowGreenOn {
isNowGreenOn = false
setGPIO(ledGreenGPIO, false)
} else {
isNowGreenOn = true
setGPIO(ledGreenGPIO, true)
}
} else {
isNowGreenOn = true
setGPIO(ledGreenGPIO, true)
}
} else {
isNowGreenOn = false
setGPIO(ledGreenGPIO, false)
}
// Red
if redOn {
if redBlink {
if isNowRedOn {
isNowRedOn = false
setGPIO(ledRedGPIO, false)
} else {
isNowRedOn = true
setGPIO(ledRedGPIO, true)
}
} else {
isNowRedOn = true
setGPIO(ledRedGPIO, true)
}
} else {
isNowRedOn = false
setGPIO(ledRedGPIO, false)
}
}
})()
}