This repository has been archived by the owner on Jun 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buttons.py
94 lines (63 loc) · 2.79 KB
/
buttons.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
import collections
Chord = collections.namedtuple("Chord",
"timestamp black green red white")
class Record_entry():
def __init__(self, timestamp, chord, target_chord):
# check arguments:
#assert type(btn_states) is list and \
# len(btn_states) == 4 and \
# sum([i in (0, 1) for i in btn_states]) == 4, \
# "btn_states must be a list (0,1) of length 4."
assert type(timestamp) is float, "timestamp must be float."
self.timestamp = timestamp
self.black = chord['black']
self.green = chord['green']
self.red = chord['red']
self.white = chord['white']
self.target_chord = target_chord
def __eq__(self, other):
if isinstance(other, Record_entry):
return(
self.black == other.black and \
self.green == other.green and \
self.red == other.red and\
self.white == other.white
)
return(False)
def number_pressed(self):
return(self.black + self.green + self.red + self.white)
def __str__(self):
return("%f [%i, %i, %i, %i] tgt: %s" % \
(self.timestamp, \
self.black, self.green, self.red, self.white, \
self.target_chord))
def csv(self, condition):
return("%f, %i, %i, %i, %i, %s, %s" % (self.timestamp, self.black, self.green, self.red, self.white, self.target_chord, condition))
def is_empty(self): return self.black + self.green + self.red + self.white == 0
def code(self):
return(self.black + 2*self.green + 4*self.red + 8*self.white)
class Record():
def __init__(self):
self.entries = []
def add_entry(self, entry):
assert type(entry) is Record_entry, "attempt to add something other than a Record_entry to Record."
self.entries.append(entry)
def csv(self, condition = 9):
entrylist = [ entry.csv(condition = condition) for entry in self.entries ]
return('\n'.join(entrylist))
def last(self):
if len(self.entries) > 0:
return(self.entries[-1])
def second_last(self):
if len(self.entries) > 1:
return(self.entries[-2])
def len(self):
return(len(self.entries))
def chop(self, n=1):
self.entries = self.entries[:-n]
# test if the Record ends on the codes given in codeseq
# used for manipulating the box (new session etc.)
def testcode(self, codeseq):
if len(codeseq) > len(self.entries): return
reference = [ i.code() for i in self.entries[-len(codeseq):] ]
return(codeseq == reference)