@@ -26,7 +26,7 @@ def __init__(self, parameter):
2626 file .close () # Important for data to get written
2727
2828
29- def bin_to_voltage (self , s_bin , pga ):
29+ def bin_to_voltage (self , s_bin , pga , board_code ):
3030 # =================================================================
3131 # Convert binary into volts
3232 # Input
@@ -36,29 +36,36 @@ def bin_to_voltage(self, s_bin, pga):
3636 # Output
3737 # - voltage Float (microvolts)
3838 # =================================================================
39- s_bin = int (s_bin ) # requieres int
40- sign_bit = 0
41- if s_bin == 0 :
42- return 0
43- if s_bin > 0 and s_bin <= 8388607 :
44- sign_bit = s_bin
45- elif s_bin > 8388607 and s_bin <= 2 * 8388607 :
46- sign_bit = - 2 * 8388607 + s_bin - 1
39+
40+ if board_code == 1 :
41+ return s_bin
4742 else :
48- print ('sign_bit not assigned, returning 0' )
49-
50- voltage = (4.5 * sign_bit )/ (pga * 8388607.0 )
51- voltage = voltage * 1000000 # Convert to microvolts
43+ s_bin = int (s_bin ) # requieres int
44+ sign_bit = 0
45+ if s_bin == 0 :
46+ return 0
47+ if s_bin > 0 and s_bin <= 8388607 :
48+ sign_bit = s_bin
49+ elif s_bin > 8388607 and s_bin <= 2 * 8388607 :
50+ sign_bit = - 2 * 8388607 + s_bin - 1
51+ else :
52+ print ('sign_bit not assigned, returning 0' )
53+
54+ voltage = (4.5 * sign_bit )/ (pga * 8388607.0 )
55+ voltage = voltage * 1000000 # Convert to microvolts
5256
53- #voltage = random.randrange(-200, 200)
54- return voltage
57+ #voltage = random.randrange(-200, 200)
58+ return voltage
5559
5660
57- def channel_assignment (self , str_message , s_chans ):
61+ def messge_to_samples (self , str_message , s_chans , board_code ):
5862 # -----------------------------------------------------------------
5963 # Extract samples from board
6064 # Input
61- # str_message Raw message string coming from board
65+ # str_message (str) Raw message string coming from board
66+ # s_chans (int) How many channels shall the samples be
67+ # assigned to
68+ # board code (int) Defines code pipeline based on board type
6269 # Default in case of faulty message
6370 eeg_array = expand_dims (
6471 array ([0 ] * s_chans , dtype = float ), # Crucial to set float
@@ -68,66 +75,83 @@ def channel_assignment(self, str_message, s_chans):
6875 # eeg_valid Boolean whether workable data or not
6976 # -----------------------------------------------------------------
7077
71- str_message = str_message [
72- str_message .find ('{' ):str_message .find ('}' )+ 1 ]
73-
74- # Build Python dictionnary
75- try :
76- chanDict = loads (str_message )
77- except decoder .JSONDecodeError :
78- # Corrupt message: not all channels present
79- print ("Skipped message" )
80- return eeg_array , eeg_valid
81-
82- if len (chanDict ) != s_chans :
78+ if board_code == 0 : # Neuri board
79+ str_message = str_message [
80+ str_message .find ('{' ):str_message .find ('}' )+ 1 ]
81+
82+ # Build Python dictionnary
83+ try :
84+ chanDict = loads (str_message )
85+ except decoder .JSONDecodeError :
86+ # Corrupt message: not all channels present
87+ print ("Skipped message" )
88+ return eeg_array , eeg_valid
89+
90+ if len (chanDict ) != s_chans :
91+ return eeg_array , eeg_valid
92+
93+ eeg_array = expand_dims (
94+ fromiter (chanDict .values (), dtype = float ), # Crucial to set float
95+ axis = 1 )
96+ eeg_valid = True
97+
8398 return eeg_array , eeg_valid
8499
85- eeg_array = expand_dims (
86- fromiter (chanDict .values (), dtype = float ), # Crucial to set float
87- axis = 1 )
88- eeg_valid = True
89-
90- return eeg_array , eeg_valid
91-
92-
93- def fetch_sample (self , receiver , transmitter , parameter , shared_buffer , shared_timestamp , gui_running ):
100+ elif board_code == 1 : # Upside Down Labs
101+ str_message = str_message .replace ("b'" , "" )
102+ str_message = str_message .replace ("\\ r\\ n\' " , "" )
103+
104+ try :
105+ eeg_array [0 , 0 ] = float (str_message )
106+ except ValueError :
107+ return eeg_array , eeg_valid
108+
109+ eeg_valid = True
110+ return eeg_array , eeg_valid
111+
94112
95- if parameter .firmfeedback == 2 : # USB
113+ def setup_neuri_board (self , receiver , start_code ):
114+ # TO-DO: These functions below do not empty the buffer at the
115+ # port as they are supposed to do
116+ receiver .flush ()
117+ receiver .reset_input_buffer ()
118+ receiver .reset_output_buffer ()
119+ receiver .write (bytes (str (start_code ), 'utf-8' ))
120+
121+ board_booting = True
122+ print ('Board is booting up ...' )
123+ while board_booting :
124+ raw_message = str (receiver .readline ())
125+ print (raw_message )
126+ if 'Listening ...' in raw_message :
127+ receiver .write (bytes (str (start_code ), 'utf-8' )) # Try again
128+ sleep (1 )
129+ elif '{' in raw_message and '}' in raw_message :
130+ print ('Fully started' )
131+ board_booting = False
132+
133+
134+ def fetch_sample (self , receiver , transmitter , parameter , shared_buffer ,
135+ shared_timestamp , gui_running ):
136+
137+ if "Neuri" in parameter .board :
138+ board_code = 0
96139 s_per_buffer = 1
140+ if parameter .start_code == 3 :
141+ s_per_buffer = 10
142+ print ('Ordered board to send data via Bluetooth. Switching mode ...' )
143+ elif "EXG Pill" in parameter .board :
144+ s_per_buffer = 1 # all boards
145+ board_code = 1
97146 print ('Ordered board to send data via USB. Switching mode ...' )
98- elif parameter .firmfeedback == 3 : # Bluetooth
99- s_per_buffer = 10
100- print ('Ordered board to send data via Bluetooth. Switching mode ...' )
101-
102- if receiver .port == '' :
103- raise Exception ('Verify that desired connection type (USB or Bluetooth) are indeed available' )
104147
105148 with receiver as r :
106149
107150 # Open communication ----------------------------------------------
108151 sleep (1 )
109152
110- # TO-DO: These functions below do not empty the buffer at the
111- # port as they are supposed to do
112- r .flush ()
113- r .reset_input_buffer ()
114- r .reset_output_buffer ()
115-
116- r .write (bytes (str (parameter .firmfeedback ), 'utf-8' ))
117- # time.sleep(1)
118-
119- board_booting = True
120- print ('Board is booting up ...' )
121- while board_booting :
122- raw_message = str (r .readline ())
123- print (raw_message )
124- if 'Listening ...' in raw_message :
125- r .write (bytes (str (parameter .firmfeedback ), 'utf-8' )) # Try again
126- sleep (1 )
127- elif '{' in raw_message and '}' in raw_message :
128- print ('Fully started' )
129- board_booting = False
130-
153+ if board_code == 0 :
154+ self .setup_neuri_board (r , parameter .start_code )
131155
132156 # Prealloate values of loop ---------------------------------------
133157 start_time = int (perf_counter () * 1000 )
@@ -162,7 +186,8 @@ def fetch_sample(self, receiver, transmitter, parameter, shared_buffer, shared_t
162186
163187 raw_message = str (r .readline ())
164188
165- buffer_in , valid_eeg = self .channel_assignment (raw_message , s_chans )
189+ buffer_in , valid_eeg = self .messge_to_samples (
190+ raw_message , s_chans , board_code )
166191
167192 if not valid_eeg :
168193 # TO-DO: Implement an interpolation system
@@ -186,7 +211,7 @@ def fetch_sample(self, receiver, transmitter, parameter, shared_buffer, shared_t
186211
187212 # Convert binary to voltage values
188213 for iBin in range (s_chans ):
189- sample [iBin ] = self .bin_to_voltage (sample [iBin ], pga )
214+ sample [iBin ] = self .bin_to_voltage (sample [iBin ], pga , board_code )
190215 relay_array ["" .join (["c" , str (iBin + 1 )])] = str (sample [iBin ])
191216
192217 update_buffer = concatenate ((buffer , expand_dims (sample , 1 )), axis = 1 )
@@ -202,7 +227,6 @@ def fetch_sample(self, receiver, transmitter, parameter, shared_buffer, shared_t
202227 # Update shared memory allocations for frontend
203228 shared_buffer [:] = reshape (buffer , buffer .size ) # Has left edge for filtering
204229 shared_timestamp .value = time_stamp_now
205- # pipe_conn.put((buffer, time_stamp_now))
206230
207231 # Write out samples to file -----------------------------------
208232 if sample_count == saving_interval :
0 commit comments