def run(): _quit = False statistics_queue = queue.Queue() # resynchronize to main thread def stop_fn(*args, **kwargs): nonlocal _quit _quit = True signal.signal(signal.SIGINT, stop_fn) # also quit on CTRL-C devices = scan(config='off') try: for device in devices: cbk = statistics_callback_factory(device, statistics_queue) device.statistics_callback_register(cbk, 'sensor') device.open() device.parameter_set('i_range', 'auto') device.parameter_set('v_range', '15V') while not _quit: for device in devices: device.status() time.sleep(0.1) handle_queue(statistics_queue) finally: for device in devices: device.close()
def open(self): self._quit = None os.makedirs(BASE_PATH, exist_ok=True) if self._resume: self._on_resume() else: self._start_time_s = time.time() self._time_str = now_str() base_filename = 'jslog_%s_%s' % (self._time_str, os.getpid()) self._base_filename = os.path.join(BASE_PATH, base_filename) event_filename = self._base_filename + '.txt' self._f_event = open(event_filename, 'at') self.on_event('LOGGER', 'OPEN') self.on_event('PARAM', f'start_time={self._start_time_s}') self.on_event('PARAM', f'start_str={self._time_str}') self.on_event('PARAM', f'downsample={self._downsample}') self.on_event('PARAM', f'frequency={self._frequency}') self.on_event( 'PARAM', f'jls_sampling_frequency={self._jls_sampling_frequency}') self.on_event('PARAM', f'source={self._source}') devices = joulescope.scan() device_strs = [str(device) for device in devices] if not len(device_strs): raise RuntimeError('No Joulescopes found') self.log.info('Found %d Joulescopes', len(device_strs)) self.on_event('DEVICES', ','.join(device_strs)) print('Found ' + joulescope_count_to_str(len(device_strs))) self._devices_create(device_strs)
def on_cmd(args): frozen = getattr(sys, 'frozen', False) if frozen: frozen = getattr(sys, '_MEIPASS', frozen) print('System information') print(f' Python: {sys.version}') print(f' Platform: {platform.platform()} ({sys.platform})') print(f' Processor: {platform.processor()}') print(f' executable: {sys.executable}') print(f' frozen: {frozen}') print('') print(f'joulescope version: {joulescope.__version__}') devices = joulescope.scan() device_count = len(devices) if device_count == 0: print('Found 0 connected Joulescopes.') elif device_count == 1: print('Found 1 connected Joulescope:') else: print(f'Found {device_count} connected Joulescopes:') logging.getLogger().setLevel(logging.WARNING) for device in devices: try: with device: info = device.info() ctl_fw = info.get('ctl', {}).get('fw', {}).get('ver', '') sensor_fw = info.get('sensor', {}).get('fw', {}).get('ver', '') info = f' ctl={ctl_fw:<15} sensor={sensor_fw}' except: info = '' print(f' {device} {info}')
def select_prompt(): devices = scan() while True: for idx, device in enumerate(devices): print(f'{idx + 1}: {device.device_serial_number}') value = input('Type number and enter: ') idx = int(value) if 1 <= idx <= len(devices): device = devices[idx - 1] return device.device_serial_number
def on_cmd(args): d = scan(name='Joulescope') if not(len(d)): print('No devices found') return 1 elif len(d) != 1: print('More than on device found)') return 2 device = d[0] return run(device, filename=args.filename, duration=args.duration)
def run(): quit_ = False def do_quit(*args, **kwargs): nonlocal quit_ quit_ = 'quit from SIGINT' def on_stop(event, message): nonlocal quit_ quit_ = 'quit from stop' args = get_parser().parse_args() filename_base, filename_ext = os.path.splitext(args.out) signal.signal(signal.SIGINT, do_quit) devices = scan(config='auto') items = [] try: for device in devices: if args.frequency: try: device.parameter_set('sampling_frequency', int(args.frequency)) except Exception: # bad frequency selected, display warning & exit gracefully freqs = [ f[2][0] for f in device.parameters( 'sampling_frequency').options ] print(f'Unsupported frequency selected: {args.frequency}') print(f'Supported frequencies = {freqs}') quit_ = True break fname = f'{filename_base}_{device.device_serial_number}{filename_ext}' device.open() recorder = DataRecorder(fname, calibration=device.calibration) items.append([device, recorder]) device.stream_process_register(recorder) if not quit_: for device, _ in items: device.start(stop_fn=on_stop, duration=args.duration) print('Capturing data: type CTRL-C to stop') while not quit_: time.sleep(0.01) finally: for device, recorder in items: try: device.stop() recorder.close() device.close() except Exception: print('exception during close') return 0
def _open_devices(self, do_notify=True): closed_devices = [d for d in self._devices if not d.is_open] closed_count = len(closed_devices) if closed_count: if do_notify: time_now = time.time() if (self._user_notify_time_last + USER_NOTIFY_INTERVAL_S) <= time_now: self.log.warning('Missing ' + joulescope_count_to_str(closed_count)) self._user_notify_time_last = time_now all_devices = joulescope.scan() for closed_device in closed_devices: for all_device in all_devices: if str(closed_device) == str(all_device): closed_device.open(all_device) break else: self._user_notify_time_last = 0.0 return closed_count
def scan_by_serial_number(serial_number, name: str = None, config=None): devices = scan(name, config) for device in devices: if serial_number == device.device_serial_number: return device raise KeyError(f'Device not found with serial number {serial_number}')