-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbidirectionalInterface.py
263 lines (237 loc) · 9.26 KB
/
bidirectionalInterface.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import pygame
from BCI import BciInterface
import socket
import time
import multiprocessing as mp
import atexit
def handle_tobiid_input(bci):
data = None
try:
data = bci.iDsock_bus.recv(512)
data = parse_data(data)
# print "A = ",data
# bci.idStreamer_dev.Append(data)
except:
# bci.nS = False
# bci.dec = 0
pass
return data
def parse_data(data):
try:
#print data
idx1 = data.find('event=\"')
# print 'idx1:', idx1
if idx1 is -1: # does not find
return None
n = len('event=\"')
# print 'idx1', idx1
# print 'substr', data[idx1+n:]
idx2 = data[idx1+n:].find('\"')
# print 'idx2', idx2
# print 'substr2', data[idx1+n:idx1+n+idx2]
# data must be sent in a string format
# value = int(data[idx1+n:idx1+n+idx2])
value = data[idx1 + n:idx1 + n + idx2] + '.' # period (.) is the ending of an event
# print 'value', value
except:
print 'failed to parse data!!!'
value = -9999
return value
def wait_for_connection(sock_host, terminate_key):
print 'Waiting for clients...'
while True:
try:
s, addr = sock_host.accept()
except socket.timeout:
s = None
addr = None
if s is not None:
print addr, ' is connected.'
return s, addr, False, time.time()
for event in pygame.event.get():
if event.type is pygame.KEYDOWN:
key = pygame.key.name(event.key)
print key + " pressed"
if key == terminate_key:
return s, addr, True, time.time()
def create_server(server_address):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(server_address)
s.listen(1)
return s
class BidirectionalInterface:
def __init__(self, ip_cnbiloop='192.168.1.1', port_tid_from_cnbiloop=9999,
ip_python='192.168.1.1', port_tid_to_cnbiloop=9990,
from_cnbiloop=True, to_cnbiloop=True, manual_trigger=False):
pygame.init()
screen = pygame.display.set_mode((60, 60))
pygame.display.set_caption('Pygame Keyboard Test')
self.finish = False
self.bci = BciInterface(connect_ip=ip_cnbiloop)
self.ip_cnbiloop = ip_cnbiloop
self.ip_python = ip_python
self.port_tid_to_protocol = port_tid_from_cnbiloop
self.port_tid_from_protocol = port_tid_to_cnbiloop
self.to_cnbiloop = to_cnbiloop
self.from_cnbiloop = from_cnbiloop
self.manual_trigger = manual_trigger
self.terminateKey = 'c'
self.videoMode = False
self.reconnect = True
self.proc = []
self.run()
'''
# Host servers
if self.from_cnbiloop:
server_address = (ip_python, port_tid_from_cnbiloop)
self.sockHostToProtocol = create_server(server_address)
if self.to_cnbiloop:
server_address = (ip_python, port_tid_to_cnbiloop)
self.sockHostFromProtocol = create_server(server_address)
'''
'''
# Wait for connection
if self.from_cnbiloop:
self.sockClientToProtocol, self.clientAddress, self.finish, self.established_time = \
wait_for_connection(self.sockHostToProtocol, self.terminateKey)
print 'connected to: ', self.clientAddress
if self.to_cnbiloop:
self.sockClientFromProtocol, self.clientAddressFromProtocol, self.finish, self.established_time = \
wait_for_connection(self.sockHostFromProtocol, self.terminateKey)
print 'connected to: ', self.clientAddressFromProtocol
if to_cnbiloop:
if manual_trigger:
self.init_mp_process(self.send_manual_trigger_daemon)
else:
self.init_mp_process(self.send_tid_daemon)
if from_cnbiloop:
self.init_mp_process(self.receive_tid_daemon)
'''
def run(self):
host_server = True
while True:
# check if reconnection is required
if self.reconnect:
print 'here'
# Host servers
if self.from_cnbiloop and host_server:
server_address = (self.ip_python, self.port_tid_to_protocol)
self.sockHostToProtocol = create_server(server_address)
if self.to_cnbiloop and host_server:
server_address = (self.ip_python, self.port_tid_from_protocol)
self.sockHostFromProtocol = create_server(server_address)
self.bci.close()
self.finish = [True, True]
if self.from_cnbiloop:
self.sockClientToProtocol, self.clientAddress, self.finish[0], self.established_time = \
wait_for_connection(self.sockHostToProtocol, self.terminateKey)
print 'connected to: ', self.clientAddress
if self.to_cnbiloop:
self.sockClientFromProtocol, self.clientAddressFromProtocol, self.finish[1], self.established_time = \
wait_for_connection(self.sockHostFromProtocol, self.terminateKey)
print 'connected to: ', self.clientAddressFromProtocol
if self.finish[0] is True and self.finish[1] is True:
return
self.reconnect = False
self.bci = BciInterface(self.ip_cnbiloop)
self.proc = []
if self.to_cnbiloop:
if self.manual_trigger:
self.init_mp_process(self.send_manual_trigger_daemon)
else:
self.init_mp_process(self.send_tid_daemon)
if self.from_cnbiloop:
self.init_mp_process(self.receive_tid_daemon)
atexit.register(self._close) # close socket on program termination, no matter what!
self.sockHostToProtocol.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.sockHostFromProtocol.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
for proc in self.proc:
print proc
proc.join()
self.reconnect = True
host_server = False # no need to host again...
def _close(self):
for i in range(0, len(self.proc)):
self.proc[i].join()
self.bci.close()
if self.from_cnbiloop:
self.sockHostToProtocol.close()
if self.to_cnbiloop:
self.sockHostFromProtocol.close()
# self.sockClientFromProtocol.close()
# self.sockClientToProtocol.close()
def init_mp_process(self, target_func):
self.proc.append(mp.Process(group=None, target=target_func))
self.proc[-1].start()
def send_manual_trigger_daemon(self):
while True:
try:
data = int(raw_input('Trigger value (1-255)? ').strip())
assert 1 <= data <= 255
self.bci.id_msg_bus.SetEvent(data)
self.bci.iDsock_bus.sendall(self.bci.id_serializer_bus.Serialize())
print('Sent', data)
except KeyboardInterrupt:
self.finish = True
break
def send_tid_daemon(self):
while True:
# data = receive(self.sockHostFromProtocol)
data = []
try:
msg = self.sockClientFromProtocol.recv(1024)
# print 'msg', msg
data = msg.split(',')
# print 'data split', data
if len(data) >= 2:
data = data[-2] # take the last one with number, if just last one, it will be an empty string
else:
break
'''
self.sockClientFromProtocol, self.clientAddressFromProtocol, self.finish, self.established_time = \
wait_for_connection(self.sockHostFromProtocol, self.terminateKey)
data = []
'''
except socket.error as e:
# print e
pass
# send to tid
# print len(data)
if len(data) is not 0:
data = (int)(data)
print 'sending: ', data
self.bci.id_msg_bus.SetEvent(data)
self.bci.iDsock_bus.sendall(self.bci.id_serializer_bus.Serialize())
def is_ndf_event(self, data):
# should convert to num or check in string format...
return True
def receive_tid_daemon(self):
n_data = 0
while True:
# receive TiD Event
data = handle_tobiid_input(self.bci)
# if data:
# print data
# transmit TiD Event if input buffer is not empty
if data is not None:
try:
n_data = n_data + 1
# print data
cur_time = time.time()
elapsed_time = cur_time - self.established_time
if elapsed_time > 0.0 and not self.videoMode:
# print elapsedTime
print data, n_data / elapsed_time
if self.is_ndf_event(data):
self.sockClientToProtocol.sendall(data)
except socket.error:
print 'Failed to send data...reconnecting...'
break
'''
self.bci.close()
self.sockClientToProtocol, self.clientAddress, self.finish, self.established_time = \
wait_for_connection(self.sockHostToProtocol, self.terminateKey)
self.bci = BciInterface(self.ip_cnbiloop)
n_data = 0
'''