-
Notifications
You must be signed in to change notification settings - Fork 0
/
stoplight.py
64 lines (59 loc) · 2.14 KB
/
stoplight.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
61
62
63
64
# -*- coding: utf-8 -*-
from time import sleep
from gpiozero import LED
class Stoplight:
def __init__(self, states, gpio, fakeGpio=False):
self.states = states
self.gpio = gpio
self.controller = {}
self.fakeGpio = fakeGpio
self.ON = True
self.OFF = False
# self.state = "null"
for pinout in gpio:
print("assigning " + pinout + " to GPIO pin: " + str(gpio[pinout]))
# self.controller[pinout] = gpio[pinout]
if not self.fakeGpio:
self.controller[pinout] = LED(gpio[pinout])
self.controller[pinout].off()
sleep(1)
else:
self.controller[pinout] = gpio[pinout]
print("Imagine I just turned off LED " + str(gpio[pinout]))
for state in self.states:
print("testing: " + state)
self.assert_state(state)
if not self.fakeGpio:
sleep(2)
self.assert_state("null")
def control_pin(self, pin, instruction):
if instruction:
if not self.fakeGpio:
self.controller[pin].on()
# else:
print(
"GPIO " + str(self.controller[pin]).zfill(2) + " O " + pin)
else:
if not self.fakeGpio:
self.controller[pin].off()
# else:
print(
"GPIO " + str(self.controller[pin]).zfill(2) + " X " + pin)
def blink(self, pin, interval, duration):
oscilations = duration/(interval * 2)
for i in range(int(oscilations)):
print("pin ON")
self.control_pin(pin, self.ON)
sleep(interval)
print("pin OFF")
self.control_pin(pin, self.OFF)
sleep(interval)
self.control_pin(pin, self.ON)
def assert_state(self, state):
print("state assertion has been received")
for key in self.states[state]:
if self.states[state][key]:
self.control_pin(key, self.ON)
else:
self.control_pin(key, self.OFF)
print("state assertion has been completed")