-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
91 lines (67 loc) · 1.55 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
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
"time"
rpio "github.com/stianeikeland/go-rpio/v4"
)
func main() {
fmt.Printf("Starting traffic lights at %s\n", time.Now())
// Opens memory range for GPIO access in /dev/mem
if err := rpio.Open(); err != nil {
fmt.Printf("Cannot access GPIO: %s\n", time.Now())
fmt.Println(err)
os.Exit(1)
}
// Get the pin for each of the lights (refers to the bcm2835 layout)
redPin := rpio.Pin(2)
yellowPin := rpio.Pin(3)
greenPin := rpio.Pin(4)
fmt.Printf("GPIO pins set up: %s\n", time.Now())
// Set the pins to output mode
redPin.Output()
yellowPin.Output()
greenPin.Output()
fmt.Printf("GPIO output set up: %s\n", time.Now())
// Clean up on ctrl-c and turn lights out
//c := make(chan os.Signal, 1)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
fmt.Printf("Switching off traffic lights at %s\n", time.Now())
redPin.Low()
yellowPin.Low()
greenPin.Low()
os.Exit(0)
}()
defer rpio.Close()
// Turn lights off to start.
redPin.Low()
yellowPin.Low()
greenPin.Low()
fmt.Printf("All traffic lights switched off at %s\n\n", time.Now())
// A while true loop.
for {
fmt.Println("\tSwitching lights on and off")
// Red
redPin.High()
time.Sleep(time.Second * 2)
// Yellow
redPin.Low()
yellowPin.High()
time.Sleep(time.Second)
// Green
yellowPin.Low()
greenPin.High()
time.Sleep(time.Second * 2)
// Yellow
greenPin.Low()
yellowPin.High()
time.Sleep(time.Second * 2)
// Yellow off
yellowPin.Low()
}
}