-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvwyfd.py
69 lines (50 loc) · 1.8 KB
/
vwyfd.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
import serial
import time
from qdsply import Qdsply
from dbi import Dbi
def milli(): # integer time in milliseconds
return int(time.time() * 1000)
def checkbrk(srl): # check breakbeam
brk = srl.read()
if brk == ord("a"):
return True, True # gateway a break
if brk == ord("b"):
return True, False # gateway b break
return False, True # no break event
STPSPAN = 100 # time between animation steps
QSPAN = 18 * 10**5 # (30 mins) time between questions
HBSPAN = 6 * 10**4 # (1 min) time between heartbeats
qd = Qdsply() # question display to manage flipdot displays
dbi = Dbi() # database interface
lststp = None # last question animation step
lstnwq = None # last new question
lsthb = None # last heartbeat
qid = None
with serial.Serial("/dev/ttyUSB0", 57600) as dsply_srl:
with serial.Serial("/dev/ttyUSB1", 57600, timeout=0.01) as brk_srl:
lststp = lstnwq = lsthb = milli() # init timestamps
# init question
qid, q, a, b = dbi.getNextQuestion()
qd.ask(q, a, b)
qd.wipe(srl, True) # wipe displays all white on startup
time.sleep(0.5)
qd.wipe(srl, False) # wipe displays all black
time.sleep(0.5)
while True:
# check breakbeam serial for votes
has, a = checkbrk(brk_srl)
if has:
ratio = dbi.addVote(qid, a)
qd.vote(a, ratio)
# do stuff when its time
now = milli()
if now - lstnwq > QSPAN:
lstnwq = now
qid, q, a, b = dbi.getNextQuestion()
qd.ask(q, a, b)
if now - lsthb > HBSPAN:
lsthb = now
dbi.logQuestion(qid)
if now - lststp > STPSPAN:
lststp = now
qd.step(srl)