forked from sbcshop/PiTraffic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPiTraffic.py
More file actions
72 lines (55 loc) · 1.74 KB
/
PiTraffic.py
File metadata and controls
72 lines (55 loc) · 1.74 KB
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
#!/usr/bin/python
# Library for PiTraffic
# Developed by: SB Components
# Author: Ankur
# Project: PiTraffic
# Python: 3.4.2
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
def closeGPIO():
GPIO.cleanup()
class Buzzer:
def __init__(self):
self.pin = 12
GPIO.setup(self.pin,GPIO.OUT)
GPIO.output(self.pin, GPIO.LOW)
def on(self):
print("Buzzer - ON")
GPIO.output(self.pin,GPIO.HIGH)
def off(self):
print("Buzzer - OFF")
GPIO.output(self.pin,GPIO.LOW)
class Traffic:
''' Class to handle LED's
Arguments:
direction = (i.e. "EAST","WEST","NORTH","SOUTH")
color = Color of LED
'''
traffic_pins = {"SOUTH":{'RED':11,'YELLOW':13, "GREEN":15},
"WEST" :{'RED':16,'YELLOW':18, "GREEN":22},
"NORTH":{'RED':29,'YELLOW':31, "GREEN":33},
"EAST" :{'RED':36,'YELLOW':38, "GREEN":40}}
def __init__(self, direction, color):
self.pin = self.traffic_pins[direction][color]
self.direction = direction
self.color = color
GPIO.setup(self.pin,GPIO.OUT)
GPIO.output(self.pin, GPIO.LOW)
def on(self):
print(self.direction + " " + self.color + " - ON")
GPIO.output(self.pin,GPIO.HIGH)
def off(self):
print(self.direction + " " + self.color + " - OFF")
GPIO.output(self.pin,GPIO.LOW)
class Button:
Pressed = False
def __init__(self):
self.pin = 7
GPIO.setup(self.pin,GPIO.IN, pull_up_down=GPIO.PUD_UP)
def press(self):
input_state = GPIO.input(self.pin)
if input_state == False:
print("Button - Pressed")
self.Pressed = True