def download(): """Function to deal with 'download' action argument""" oxi = cms50ew.CMS50EW() if not oxi.setup_device(target=args.device, is_bluetooth=args.bluetooth): raise Exception('Connection attempt unsuccessful.') oxi.initiate_device() oxi.get_session_count() if oxi.sess_available == 'No': raise Exception('No stored session data available.') oxi.get_session_duration() oxi.send_cmd(oxi.cmd_get_session_data) counter = 1 while oxi.download_data(): print('Downloading data point ' + str(counter) + ' of ' + str(oxi.sess_data_points)) counter += 1 print('Downloaded data points:', len(oxi.stored_data)) if args.csv: print('Saving downloaded data to: ' + str(args.csv) + ' ...') oxi.write_csv(args.csv) if args.pygal: print('Plotting downloaded data with Pygal and saving plot to: ' + str(args.pygal) + ' ...') oxi.plot_pygal() oxi.write_svg(args.pygal) if args.mpl: print('Plotting downloaded data with Matplotlib and displaying it ...') oxi.plot_mpl()
def getDeviceInformation(self): self.scanButton.setText('Retrieving info from ' + self.target + ' ...') QtGui.QApplication.processEvents() oxi = cms50ew.CMS50EW() oxi.setup_device(self.target, is_bluetooth=self.is_bluetooth) if not oxi.initiate_device(): self.scanButton.setText('No response from device') QtGui.QApplication.processEvents() else: oxi.get_model() oxi.get_vendor() oxi.get_user() oxi.get_session_count() oxi.get_session_duration() self.infoTable.setItem(0, 0, QTableWidgetItem(oxi.vendor)) self.infoTable.setItem(1, 0, QTableWidgetItem(oxi.model)) self.infoTable.setItem(2, 0, QTableWidgetItem(oxi.user)) self.infoTable.setItem( 3, 0, QTableWidgetItem( str(oxi.sess_available + ' (Duration: ' + str(oxi.sess_duration) + ')'))) self.scanButton.setText('Device information received') QtGui.QApplication.processEvents()
def on_openSessAction(self): filename = QFileDialog.getOpenFileName(self)[0] if filename: self.oxi = cms50ew.CMS50EW() self.oxi.open_csv(filename) sessDialog = SessionDialog(is_csv=True) sessDialog.exec_()
def setupDevice(self): self.scanButton.setText('Connecting to ' + self.target + ' ...') QtGui.QApplication.processEvents() w.oxi = cms50ew.CMS50EW() if w.oxi.setup_device(self.target, is_bluetooth=self.is_bluetooth): if w.oxi.is_bluetooth: w.statusBar.showMessage('Status: Connected to Bluetooth device') else: w.statusBar.showMessage('Status: Opened serial port') w.liveRunAction.setEnabled(True) w.sessDialogAction.setEnabled(True) self.close() else: w.statusBar.showMessage('Status: Connection attempt unsuccessful') self.close()
def main(stdscr): """Sets up a curses screen.""" curses.init_pair(1, curses.COLOR_GREEN, curses.COLOR_BLACK) curses.init_pair(2, curses.COLOR_CYAN, curses.COLOR_BLACK) curses.curs_set(0) stdscr.nodelay(1) global stdscr_height stdscr_height = stdscr.getmaxyx()[0] def no_data(status): """Updates screen when no data is available.""" stdscr.clear() stdscr.addstr(0, 0, 'Pulse rate: ') stdscr.addstr('n/a', curses.A_BOLD) stdscr.addstr(1, 0, 'SpO2: ') stdscr.addstr('n/a', curses.A_BOLD) stdscr.addstr(2, 0, 'Status: ') stdscr.addstr(status, curses.A_BLINK) stdscr.addstr(stdscr_height - 1, 0, "Press 'q' to quit") stdscr.refresh() def data_update(status, pulse_rate, spo2): """Updates screen.""" stdscr.clear() stdscr.addstr(0, 0, 'Pulse rate: ') stdscr.addstr(str(pulse_rate), curses.color_pair(1) | curses.A_BOLD) stdscr.addstr(' bpm') stdscr.addstr(1, 0, 'SpO2: ') stdscr.addstr(str(spo2), curses.color_pair(2) | curses.A_BOLD) stdscr.addstr(' %') stdscr.addstr(2, 0, 'Status: ') stdscr.addstr(status) stdscr.addstr(stdscr_height - 1, 0, "Press 'q' to quit") stdscr.refresh() def init_live_data(): """Initiates live data feed and keeps it alive.""" while True: oxi.initiate_device() oxi.send_cmd(oxi.cmd_get_live_data) try: update_live_data() except (TypeError, bluetooth.btcommon.BluetoothError): # Every once in a while (every ~30 seconds) the data stream # interrupts for reasons unknown. So we just restart. pass def update_live_data(): """Gets live data from oximeter instance and displays it.""" finger_out = False low_signal_quality = False global stdscr_height counter = 0 while True: c = stdscr.getch() data = oxi.process_data() finger = data[0] pulse_rate = data[1] spo2 = data[2] if c == ord('q'): oxi.close_device() sys.exit() elif c == curses.KEY_RESIZE: stdscr_height = stdscr.getmaxyx()[0] if finger == 'Y': # The counter > n condition serves to suppress hiccups where # the oximeter reports "Finger out" when it isn't. if not finger_out and counter > 10: no_data('Finger out') finger_out = True low_signal_quality = False counter = 0 elif not finger_out and counter < 11: counter += 1 elif (pulse_rate == 0) or (spo2 == 0): no_data('Low signal quality') finger_out = False low_signal_quality = True else: data_update('Processing data', pulse_rate, spo2) finger_out = False low_signal_quality = False # Set up an oximeter instance and initiate live data stream oxi = cms50ew.CMS50EW() if not oxi.setup_device(target=args.device, is_bluetooth=args.bluetooth): print('Connection attempt unsuccessful.') sys.exit(1) init_live_data()
parser_download.add_argument( '-b', '--bluetooth', help= 'specify if connection is to be established via Bluetooth (default is serial)', action='store_true') parser_download.add_argument( 'device', help='specify serial port or MAC address of Bluetooth device') parser_download.add_argument('--csv', metavar='file', help='store saved data in CSV file') parser_download.add_argument('--pygal', metavar='file', help='plot data with Pygal and store it as SVG') parser_download.add_argument('--mpl', help='plot data with Matplotlib and display it', action='store_true') parser_download.add_argument( '--datetime', help='specify start time of recording, e.g. 16 Mar 2017 22:30') # Parse arguments args = parser.parse_args() # Set up an oximeter instance and introduce signal handling oxi = cms50ew.CMS50EW() signal.signal(signal.SIGINT, exit_nicely) # Run action function args.func()