-
Notifications
You must be signed in to change notification settings - Fork 2
/
trafficlight.py
executable file
·60 lines (46 loc) · 1.33 KB
/
trafficlight.py
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
from enum import Enum
import RPi.GPIO as GPIO
class TrafficLight(object):
"""
Traffic light control for a traffic Light connected to
a RaspBerry Pi with GPIO board.
This code is written for a Solid State Relais board
from BitWizard from the Netherlands.
Website: http://www.bitwizard.nl/
Product Code: RPi-solid-state-relay
This board has 4 SSRs and three are used for controlling
a traffic light.
"""
class Color(Enum):
green = 1
orange = 2
red = 3
relaymap = {
Color.green: 11,
Color.orange: 12,
Color.red: 13
}
def __init__(self):
self.prev = None
GPIO.setmode(GPIO.BOARD)
for light, id in self.relaymap.items():
GPIO.setup(id, GPIO.OUT)
def switch(self, color):
try:
relay = self.relaymap.get(color)
if self.prev is not None and self.prev != relay:
self.off(self.prev)
if self.prev != relay:
self.on(relay)
self.prev = relay
except:
raise
def on(self, relay):
GPIO.output(relay, True)
def off(self, relay):
GPIO.output(relay, False)
def dark(self):
for light, id in self.relaymap.items():
self.off(id)
def __del__(self):
GPIO.cleanup()