-
Notifications
You must be signed in to change notification settings - Fork 0
/
led_stopwatch.py
133 lines (94 loc) · 3.66 KB
/
led_stopwatch.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
from ledMatrix import LedMatrix
from threading import Thread
import time, sys, glob, serial
class StopWatchThread(Thread):
def __init__(self, stopwatch_instance):
Thread.__init__(self)
self.stopwatch_instance = stopwatch_instance
self.ledMatrix = self.stopwatch_instance.ledMatrix
def run(self):
start_time = self.stopwatch_instance.start_time
while self.stopwatch_instance.stopwatch_running:
elapsed_time = str(round(time.time() - start_time, 2))
self.ledMatrix.writeToMatrix(elapsed_time)
self.ledMatrix.sendMatrixDataToPanel()
class StopWatch():
def __init__(self):
self.stopwatch_running = False
self.start_time = None
self.ledMatrix = LedMatrix()
def find_serial_device(self):
""" Lists serial port names
:raises EnvironmentError:
On unsupported or unknown platforms
:returns:
A list of the serial ports available on the system
"""
if sys.platform.startswith('win'):
ports = ['COM%s' % (i + 1) for i in range(256)]
elif sys.platform.startswith('linux') or sys.platform.startswith('cygwin'):
# this excludes your current terminal "/dev/tty"
ports = glob.glob('/dev/tty[A-Za-z]*')
elif sys.platform.startswith('darwin'):
ports = glob.glob('/dev/tty.*')
else:
raise EnvironmentError('Unsupported platform')
result = []
for port in ports:
try:
s = serial.Serial(port)
s.close()
result.append(port)
except (OSError, serial.SerialException):
pass
return result[0]
def startStopWatch(self, usb_port=""):
if usb_port == "":
usb_port = self.find_serial_device()
self.ledMatrix.openPort(usb_port)
#Countdown
countdown_start_time = time.time()
countdown_from = 5
while ((time.time() - countdown_start_time) < countdown_from):
self.ledMatrix.writeToMatrix(" " + str(countdown_from - int(time.time() - countdown_start_time)))
self.ledMatrix.sendMatrixDataToPanel()
time.sleep(0.05)
self.stopwatch_thread = StopWatchThread(self)
self.start_time = time.time()
self.stopwatch_running = True
self.stopwatch_thread.start()
def fill_display(self):
for i, row in enumerate(self.ledMatrix.matrix):
for j in range(len(row)):
row[j] = 1
self.ledMatrix.sendMatrixDataToPanel()
def clear_display(self):
self.ledMatrix.initializeMatrix()
self.ledMatrix.sendMatrixDataToPanel()
def stopStopWatch(self):
self.stopwatch_running = False
elapsed_time = time.time() - self.start_time
for i in range(20):
self.fill_display()
time.sleep(0.03)
self.clear_display()
time.sleep(0.03)
for i in range(10):
self.ledMatrix.writeToMatrix(str(elapsed_time))
self.ledMatrix.sendMatrixDataToPanel()
time.sleep(0.2)
self.ledMatrix.writeToMatrix(str(elapsed_time))
self.invert(self.ledMatrix.matrix)
self.ledMatrix.sendMatrixDataToPanel()
time.sleep(0.2)
self.ledMatrix.closePort()
return elapsed_time
def invert(self, matrix):
for row in matrix:
for i, value in enumerate(row):
row[i] = (value + 1) % 2
if __name__ == "__main__":
stopwatch = StopWatch()
stopwatch.startStopWatch()
time.sleep(5)
stopwatch.stopStopWatch()