Beispiel #1
0
def _upgrade(filename):
    try:
        d = scan_require_one(name='bootloader')
    except RuntimeError:
        d = scan_require_one(name='joulescope')
    d.open()
    upgrade(d, filename, progress_cbk=_progress)
    return 0
Beispiel #2
0
def controller_program(data):
    try:
        d = scan_require_one(name='bootloader')
        fn = _controller_program_from_bootloader
    except RuntimeError:
        d = scan_require_one(name='joulescope')
        fn = _controller_program_from_app
    return fn(d, data)
def on_cmd(args):
    device = scan_require_one(name='Joulescope', config='auto')
    quit_ = False
    data_queue = Queue()

    def do_quit(*args, **kwargs):
        nonlocal quit_
        quit_ = 'quit from SIGINT'

    def on_stop(event, message):
        nonlocal quit_
        quit_ = 'quit from device stop'

    def on_update_fn(data):
        data_queue.put(data)

    signal.signal(signal.SIGINT, do_quit)
    view = None
    frame_count = 0
    time_start = time.time()
    try:
        device.open()
        view = device.view_factory()
        view.on_update_fn = on_update_fn
        view.open()
        view.on_x_change('resize', {'pixels': 5801})  # prime number
        device.start(stop_fn=on_stop)
        while not quit_:
            try:
                data = data_queue.get(timeout=0.01)
                view.refresh()
                frame_count += 1
                i = data['signals']['current']['µ']['value']
                idx_mask = np.isfinite(i)
                if np.count_nonzero(idx_mask):
                    # access the data to ensure memory validity
                    stats = {
                        'mean': np.mean(i[idx_mask]),
                        'var': np.mean(data['signals']['current']['σ2']['value']),  # not really
                        'p2p': data['signals']['current']['max']['value'] - data['signals']['current']['min']['value']
                    }
            except Empty:
                time.sleep(0.001)
        device.stop()
    except Exception as ex:
        logging.getLogger().exception('while streaming data')
        print('Data streaming failed')
        return 1
    finally:
        if view is not None:
            view.close()
        device.close()
    time_stop = time.time()
    duration = time_stop - time_start
    fps = frame_count / duration
    print(f'done streaming data: {frame_count} frames in {duration:.3f} seconds = {fps:.1f} fps')
    return 0
Beispiel #4
0
def sensor_program(data):
    d = scan_require_one(name='joulescope')
    try:
        d.open()
        start_time = time.time()
        d.sensor_firmware_program(data, progress_cbk=_progress)
        stop_time = time.time()
    finally:
        d.close()
    return 0
Beispiel #5
0
def calibration_program(data, is_factory):
    d = scan_require_one(name='joulescope')
    try:
        d.open()
        start_time = time.time()
        d.calibration_program(data, is_factory=is_factory)
        stop_time = time.time()
    finally:
        d.close()
    return 0
Beispiel #6
0
def on_cmd(args):
    quit_ = False
    device = scan_require_one(name='Joulescope', config='off')

    def do_quit(*args, **kwargs):
        nonlocal quit_
        quit_ = 'quit from SIGINT'

    def statistics_cbk(s, indicator=None):
        indicator = '' if indicator is None else indicator
        t = s['time']['range']['value'][0]
        i = s['signals']['current']['µ']
        v = s['signals']['voltage']['µ']
        p = s['signals']['power']['µ']
        c = s['accumulators']['charge']
        e = s['accumulators']['energy']

        fmts = ['{x:.9f}', '{x:.3f}', '{x:.9f}', '{x:.9f}', '{x:.9f}']
        values = []
        for k, fmt in zip([i, v, p, c, e], fmts):
            value = fmt.format(x=k['value'])
            value = f'{value} {k["units"]}'
            values.append(value)
        ', '.join(values)
        print(f"{indicator}{t:.1f}: " + ', '.join(values))

    def on_stop(event=0, message=''):
        print(f'on_stop {event}: {message}')

    signal.signal(signal.SIGINT, do_quit)
    try:
        if args.compare:
            device.statistics_callback_register(
                lambda s: statistics_cbk(s, '< '), 'sensor')
            device.statistics_callback_register(
                lambda s: statistics_cbk(s, '> '), 'stream_buffer')
        else:
            device.statistics_callback_register(statistics_cbk, 'sensor')
        device.parameter_set('buffer_duration', 1)
        device.open()
        device.parameter_set('i_range', 'auto')
        device.parameter_set('v_range', '15V')
        if args.compare:
            device.parameter_set('source', 'raw')
            device.start(stop_fn=on_stop)
        while not quit_:
            device.status()
            time.sleep(0.100)
    except Exception as ex:
        logging.getLogger().exception('While getting statistics')
        print('Data streaming failed')
        return 1
    finally:
        device.close()
    return 0
Beispiel #7
0
def calibration_program(data, is_factory):
    d = scan_require_one(name='joulescope')
    try:
        d.open()
        start_time = time.time()
        d.calibration_program(data, is_factory=is_factory)
        stop_time = time.time()
        print('Firmware program took %.2f seconds' %
              (stop_time - start_time, ))
    finally:
        d.close()
    return 0
Beispiel #8
0
def sensor_program(data):
    d = scan_require_one(name='joulescope')
    try:
        d.open()
        start_time = time.time()
        d.sensor_firmware_program(data)
        stop_time = time.time()
        print('Firmware program took %.2f seconds' %
              (stop_time - start_time, ))
    finally:
        d.close()
    return 0
Beispiel #9
0
def on_cmd(args):
    d = scan_require_one(name='Joulescope')
    d.open()
    try:
        d.parameter_set('sensor_power', 'on')
        d.parameter_set('io_voltage', args.voltage)
        d.parameter_set('gpo0', '0')
        d.parameter_set('gpo1', '0')
        for count in range(17):
            d.parameter_set('gpo0', str(count & 1))
            d.parameter_set('gpo1', str((count & 2) >> 1))
            time.sleep(0.25)

    finally:
        d.close()
Beispiel #10
0
def on_cmd(args):
    device = scan_require_one(name='Joulescope')
    f = lambda: run(device,
                    filename=args.filename,
                    duration=args.duration,
                    contiguous_duration=args.contiguous)
    if args.profile is None:
        return f()
    elif args.profile == 'cProfile':
        import cProfile
        import pstats
        cProfile.runctx('f()', globals(), locals(), "Profile.prof")
        s = pstats.Stats("Profile.prof")
        s.strip_dirs().sort_stats("time").print_stats()
    elif args.profile == 'yappi':
        import yappi
        yappi.start()
        rv = f()
        yappi.get_func_stats().print_all()
        yappi.get_thread_stats().print_all()
        return rv
    else:
        raise ValueError('bad profile argument')
def run():
    import sys
    from joulescope.driver import scan_require_one
    with scan_require_one() as d:
        upgrade(d, sys.argv[1], progress_cbk=print, stage_cbk=print)