-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcounter.go
86 lines (73 loc) · 1.13 KB
/
counter.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
package main
import (
"time"
"gpio"
)
var defaultBits = map[int]bool{
64: false,
32: false,
16: false,
8: false,
4: false,
2: false,
1: false,
}
var pins = map[int]string{
64: "66",
32: "65",
16: "46",
8: "26",
4: "44",
2: "68",
1: "67",
}
var counter int = 0
func main() {
for {
time.Sleep(1 * time.Second)
binary := toBinary(counter)
bits := flipBits(binary)
disperseValues(bits)
counter++
}
}
func increment() {
counter++
if counter > 255 {
counter = 0
}
}
func toBinary(num int) []bool {
var binary []bool
var calc = num
for calc != 0 {
binary = append(binary, calc%2 != 0)
calc = calc / 2
}
return binary
}
func flipBits(binary []bool) map[int]bool {
bitSet := copyBits()
count := 1
for _, bit := range binary {
bitSet[count] = bit
count *= 2
}
return bitSet
}
func copyBits() (copy map[int]bool) {
copy = map[int]bool{}
for k, v := range defaultBits {
copy[k] = v
}
return copy
}
func disperseValues(bits map[int]bool) {
for k, v := range bits {
setGpio(pins[k], v)
}
}
func setGpio(pin string, value bool) {
gpio := gpio.GPIO{}
gpio.Pin(pin).SetDirection(value)
}