-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initial commit of the implementation
- Loading branch information
1 parent
c8f7a35
commit 3055387
Showing
5 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
FROM golang:1.13.1-buster as builder | ||
WORKDIR /app | ||
COPY . . | ||
|
||
ENV GOARCH arm | ||
ENV GOARM 7 | ||
ENV GOOS linux | ||
RUN ["go", "build", "-o", "trafficlights", "."] | ||
|
||
FROM raspbian/jessie:latest | ||
WORKDIR /app | ||
COPY --from=builder /app/trafficlights /app | ||
CMD ["/app/trafficlights"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module tamarakaufler/traffic-lights | ||
|
||
go 1.12 | ||
|
||
require github.com/stianeikeland/go-rpio v4.2.0+incompatible |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
github.com/stianeikeland/go-rpio v4.2.0+incompatible h1:CUOlIxdJdT+H1obJPsmg8byu7jMSECLfAN9zynm5QGo= | ||
github.com/stianeikeland/go-rpio v4.2.0+incompatible/go.mod h1:Sh81rdJwD96E2wja2Gd7rrKM+XZ9LrwvN2w4IXrqLR8= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
"time" | ||
|
||
"github.com/stianeikeland/go-rpio" | ||
) | ||
|
||
func main() { | ||
fmt.Printf("Starting traffic lights at %s\n", time.Now()) | ||
|
||
if err := rpio.Open(); err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
|
||
// Get the pin for each of the lights | ||
redPin := rpio.Pin(2) | ||
yellowPin := rpio.Pin(3) | ||
greenPin := rpio.Pin(4) | ||
|
||
// Set the pins to output mode | ||
redPin.Output() | ||
yellowPin.Output() | ||
greenPin.Output() | ||
|
||
// Clean up on ctrl-c and turn lights out | ||
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 { | ||
// 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() | ||
} | ||
|
||
} |