-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatacollection.py
95 lines (77 loc) · 2.69 KB
/
datacollection.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
import joystickInput
import threading
import time
import string
import random
class controller_thread(threading.Thread):
def __init__(self,controller):
threading.Thread.__init__(self)
self.controller = controller
self.running = False
def stop(self):
self.running = False
def run(self):
self.running = True
while self.running:
self.controller.process_events()
class log_thread(threading.Thread):
def __init__(self,log_file_name,controller):
threading.Thread.__init__(self)
self.new_charactor = None
self.input_detected = False
self.record_counter = 0
self.record_finished = True
self.new_charactor_in = False
self.log_file = open(log_file_name,'a')
self.controller = controller
self.running = False
def log_info(self):
if not self.new_charactor is None:
self.log_file.write(self.new_charactor+'\n')
self.log_file.flush()
self.new_charactor = None
LSX = self.controller.states['LSX']
LSY = self.controller.states['LSY']
RSX = self.controller.states['RSX']
RSY = self.controller.states['RSY']
self.input_detected = ((LSX**2+LSY**2)**0.5 >= 32767) or ((RSX**2+RSY**2)**0.5 >= 32767)
if self.input_detected and self.new_charactor_in and self.record_finished:
self.record_finished = False
self.record_counter = 20
if self.record_counter > 0:
self.record_counter -= 1
log_str = ','.join([str(i) for i in [LSX,LSY,RSX,RSY]])+'\n'
self.log_file.write(log_str)
self.log_file.flush()
if self.record_counter == 0:
self.record_finished = True
self.new_charactor_in = False
def set_new_charactor(self,charactor):
self.new_charactor_in = True
self.new_charactor = charactor
def stop(self):
self.running = False
self.log_file.close()
def run(self):
self.running = True
while self.running:
time.sleep(0.01)
self.log_info()
if __name__ == '__main__':
controller = joystickInput.XBoxController()
c_thread = controller_thread(controller)
c_thread.start()
l_thread = log_thread('log/data',controller)
l_thread.start()
alphabet = list(string.ascii_uppercase)
testset = alphabet*20
random.seed(time.time)
for i in range(10):
random.shuffle(testset)
for c in testset:
print(c)
l_thread.set_new_charactor(c)
while l_thread.new_charactor_in:
time.sleep(0.01)
l_thread.stop()
c_thread.stop()