-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
95 lines (83 loc) · 2.5 KB
/
main.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
import digits
import clocks
import time
from machine import Pin
laneSensors = []
laneSensors.append(Pin(18, Pin.IN, Pin.PULL_DOWN))
laneSensors.append(Pin(19, Pin.IN, Pin.PULL_DOWN))
laneSensors.append(Pin(20, Pin.IN, Pin.PULL_DOWN))
laneSensors.append(Pin(21, Pin.IN, Pin.PULL_DOWN))
startSwitch = Pin(4, Pin.IN, Pin.PULL_UP)
finishTimes = [None,None,None,None]
leaderboard = []
# Use IRQ to detect finishers, for maximum granularity.
def laneCallback(pin):
now = time.ticks_ms()
global finishTimes
global leaderboard
# Get the index of the pin that fired interrupt. This will be the same as the lane index.
index = laneSensors.index(pin)
# Only record the first time this beam gets broken
if(finishTimes[index] is not None):
return
#end if
finishTimes[index]=now
leaderboard.append(index)
#End laneCallback()
# Apply laneCallback() to each lane's laser-sensor IRQ:
for lane in laneSensors:
lane.irq(trigger=Pin.IRQ_RISING, handler=laneCallback)
#end for
# Show "LINE UP" on the displays
def displayWaiting():
clocks.showUP()
digits.showLINE()
#end displayWaiting()
# When starting switch is pressed, blank all times & screens
def gateIsClosed():
return not startSwitch.value()
#end gateIsOpen()
def waitForStart():
while gateIsClosed():
time.sleep(0.001)
return
#end waitForStart()
def watchForFinishers(startTime):
global leaderboard
# Wipe record of previous race:
leaderboard.clear()
for x in range(len(finishTimes)):
finishTimes[x] = None
numDisplayed = 0
# Wipe the "LINE UP" message
digits.blankAll()
clocks.blankAll()
while True:
# If they close the gate, reset for next race:
if gateIsClosed():
return
#end if
# Gate's still open, so potentially cars are still racing
if len(leaderboard) > numDisplayed:
# Update displays, gold-first
for rank in range(len(leaderboard)):
lane = leaderboard[rank]
lapTime = time.ticks_diff(finishTimes[lane], startTime)
digits.showX(rank+1,lane)
clocks.showTimeOnX(lapTime, lane)
#end for
numDisplayed += 1;
#end if
#end while
#end watchForFinishers()
def REPL():
while True:
# Display "LINE UP"
displayWaiting()
# Wait for gate to open
waitForStart()
# Gate opened, cars must be racing.
watchForFinishers(time.ticks_ms())
#end while
#end REPL()
REPL()