def connect(self, device_name=None, mac_address=None): r""" Connects to the nearby device. If there are more than one device, the user is asked to choose one of them. Args: device_name (str): Device name("Explore_XXXX"). Either mac address or name should be in the input mac_address (str): The MAC address in format "XX:XX:XX:XX:XX:XX" """ if device_name: self.device_name = device_name else: self.device_name = 'Explore_' + mac_address[-5:-3] + mac_address[ -2:] logger.info(f"Connecting to {self.device_name} ...") self.stream_processor = StreamProcessor() self.stream_processor.start(device_name=device_name, mac_address=mac_address) cnt = 0 while "adc_mask" not in self.stream_processor.device_info: logger.info("Waiting for device info packet...") time.sleep(1) if cnt >= 10: raise ConnectionAbortedError( "Could not get info packet from the device") cnt += 1 logger.info( 'Device info packet has been received. Connection has been established. Streaming...' ) logger.info("Device info: " + str(self.stream_processor.device_info)) self.is_connected = True self.stream_processor.send_timestamp()
def connect(self, device_name=None, mac_address=None): r""" Connects to the nearby device. If there are more than one device, the user is asked to choose one of them. Args: device_name (str): Device name("Explore_XXXX"). Either mac address or name should be in the input mac_address (str): The MAC address in format "XX:XX:XX:XX:XX:XX" """ if device_name: self.device_name = device_name else: self.device_name = 'Explore_' + mac_address[-5:-3] + mac_address[ -2:] self.stream_processor = StreamProcessor() self.stream_processor.start(device_name=device_name, mac_address=mac_address) while "adc_mask" not in self.stream_processor.device_info: print('Waiting for device info packet...') time.sleep(.2) print( 'Device info packet has been received. Connection has been established. Streaming...' ) self.is_connected = True self.stream_processor.send_timestamp()
plot.x_range.follow = "end" plot.x_range.follow_interval = t_length plot.x_range.range_padding = 0. plot.x_range.min_interval = t_length def get_fft(exg, s_rate): """Compute FFT""" n_point = 1024 exg -= exg.mean(axis=1)[:, np.newaxis] freq = s_rate * np.arange(int(n_point / 2)) / n_point fft_content = np.fft.fft(exg, n=n_point) / n_point fft_content = np.abs(fft_content[:, range(int(n_point / 2))]) fft_content = gaussian_filter1d(fft_content, 1) return fft_content[:, 1:], freq[1:] if __name__ == '__main__': from explorepy import Explore from explorepy.stream_processor import StreamProcessor explore = Explore() explore.stream_processor = StreamProcessor() explore.stream_processor.device_info = {'firmware_version': '0.0.0', 'adc_mask': [1 for i in range(8)], 'sampling_rate': 250} dashboard = Dashboard(explore=explore) dashboard.start_server() dashboard.start_loop()
def convert_bin(self, bin_file, out_dir='', file_type='edf', do_overwrite=False): """Convert a binary file to EDF or CSV file Args: bin_file (str): Path to the binary file recorded by Explore device out_dir (str): Output directory path (must be relative path to the current working directory) file_type (str): Output file type: 'edf' for EDF format and 'csv' for CSV format do_overwrite (bool): Whether to overwrite an existing file """ if file_type not in ['edf', 'csv']: raise ValueError('Invalid file type is given!') self.recorders['file_type'] = file_type head_path, full_filename = os.path.split(bin_file) filename, extension = os.path.splitext(full_filename) assert os.path.isfile(bin_file), "Error: File does not exist!" assert extension == '.BIN', "File type error! File extension must be BIN." out_full_path = os.path.join(os.getcwd(), out_dir) exg_out_file = out_full_path + filename + '_exg' orn_out_file = out_full_path + filename + '_orn' marker_out_file = out_full_path + filename + '_marker' self.stream_processor = StreamProcessor() self.stream_processor.read_device_info(bin_file=bin_file) self.recorders['exg'] = create_exg_recorder( filename=exg_out_file, file_type=self.recorders['file_type'], fs=self.stream_processor.device_info['sampling_rate'], adc_mask=self.stream_processor.device_info['adc_mask'], do_overwrite=do_overwrite) self.recorders['orn'] = create_orn_recorder( filename=orn_out_file, file_type=self.recorders['file_type'], do_overwrite=do_overwrite) if self.recorders['file_type'] == 'csv': self.recorders['marker'] = create_marker_recorder( filename=marker_out_file, do_overwrite=do_overwrite) else: self.recorders['marker'] = self.recorders['exg'] self.stream_processor.subscribe( callback=self.recorders['exg'].write_data, topic=TOPICS.raw_ExG) self.stream_processor.subscribe( callback=self.recorders['orn'].write_data, topic=TOPICS.raw_orn) self.stream_processor.subscribe( callback=self.recorders['marker'].set_marker, topic=TOPICS.marker) def device_info_callback(packet): new_device_info = packet.get_info() if not self.stream_processor.compare_device_info(new_device_info): new_file_name = exg_out_file + "_" + str( np.round(packet.timestamp, 0)) logger.warning("Creating a new file: " + new_file_name + '.' + self.recorders['file_type']) self.stream_processor.unsubscribe( callback=self.recorders['exg'].write_data, topic=TOPICS.raw_ExG) self.stream_processor.unsubscribe( callback=self.recorders['marker'].set_marker, topic=TOPICS.marker) self.recorders['exg'].stop() self.recorders['exg'] = create_exg_recorder( filename=new_file_name, file_type=self.recorders['file_type'], fs=self.stream_processor.device_info['sampling_rate'], adc_mask=self.stream_processor.device_info['adc_mask'], do_overwrite=do_overwrite) self.recorders['marker'] = self.recorders['exg'] self.stream_processor.subscribe( callback=self.recorders['exg'].write_data, topic=TOPICS.raw_ExG) self.stream_processor.subscribe( callback=self.recorders['marker'].set_marker, topic=TOPICS.marker) self.stream_processor.subscribe(callback=device_info_callback, topic=TOPICS.device_info) self.stream_processor.open_file(bin_file=bin_file) logger.info("Converting...") while self.stream_processor.is_connected: time.sleep(.1) logger.info('Conversion finished.')