Skip to content

Commit

Permalink
initial commit of the implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
tamarakaufler committed Sep 30, 2019
1 parent c8f7a35 commit 3055387
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,9 @@

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Development
*.swp
*.swo
*.bak*
vendor
13 changes: 13 additions & 0 deletions Dockerfile
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"]
5 changes: 5 additions & 0 deletions go.mod
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
2 changes: 2 additions & 0 deletions go.sum
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=
80 changes: 80 additions & 0 deletions main.go
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()
}

}

0 comments on commit 3055387

Please sign in to comment.