-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathboard.py
77 lines (64 loc) · 2.45 KB
/
board.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
65
66
67
68
69
70
71
72
73
74
75
76
77
import config
import display
import formats
class DepartureBoard:
STATE_DELAY, STATE_MINOR_DELAY, STATE_OK, STATE_INACTIVE, STATE_ERROR = range(0,5)
def __init__(self):
self.display = config.DISPLAY()
self.display.backlight(0, 1, 0)
self.formatter = config.FORMATTER()
self.display.clear()
self.current_trains = []
def set_trains(self, trains):
trains = list(trains)
for i, train in enumerate(trains):
self.display_train(train, i)
for i in range(len(trains),len(self.current_trains)):
self.display_row(' '*16, i)
self.current_trains = trains
if trains:
if any([t.delayed for t in trains]):
delays = [t.minutes_late for t in trains]
if any([t.severely_delayed for t in trains]):
self.set_state(self.STATE_DELAY)
else:
self.set_state(self.STATE_MINOR_DELAY)
else:
self.set_state(self.STATE_OK)
else:
self.set_state(self.STATE_INACTIVE)
def set_error(self):
self.set_state(self.STATE_ERROR)
def set_state(self, state):
"""Set the state of the board
OK means everything is running as usual
DELAYED means one or more trains is delayed
INACTIVE means no trains in the system (probably night time)
"""
if state == self.STATE_DELAY:
# Red
self.display.backlight(1, 0, 0)
elif state == self.STATE_MINOR_DELAY:
# Yellow
self.display.backlight(1, 1, 0)
elif state == self.STATE_OK:
# Green
self.display.backlight(0, 1, 0)
elif state == self.STATE_INACTIVE:
# Lights out
self.display_row('*** GOOD ***', 0)
self.display_row('*** NIGHT ***', 1)
self.display.backlight(0, 0, 0)
else:
# Something bad happened... check logs
self.display_row('!!! ERROR !!!', 0)
self.display_row('****************', 1)
self.display.backlight(0, 0, 0)
def display_row(self, text, row):
"""Write a single line of text to the board"""
print(text, row)
self.display.move_to(0, row)
self.display.write(text)
def display_train(self, train, row):
"""Write a single train to the board"""
self.display_row(self.formatter.output(train), row)