mirror of
https://github.com/jonathanhogg/scopething
synced 2025-07-14 03:02:09 +01:00
Fix breaks from changes in SerialStream initialisation; default to first matching USB device; return sample times from capture()
This commit is contained in:
15
scope.py
15
scope.py
@ -21,16 +21,16 @@ class Scope(vm.VirtualMachine):
|
|||||||
@classmethod
|
@classmethod
|
||||||
async def connect(cls, device=None):
|
async def connect(cls, device=None):
|
||||||
if device is None:
|
if device is None:
|
||||||
scope = cls(streams.SerialStream())
|
reader = writer = streams.SerialStream.stream_matching(0x0403, 0x6001)
|
||||||
elif os.path.exists(device):
|
elif os.path.exists(device):
|
||||||
scope = cls(streams.SerialStream(device=device))
|
reader = writer = streams.SerialStream(device=device)
|
||||||
elif ':' in device:
|
elif ':' in device:
|
||||||
host, port = device.split(':', 1)
|
host, port = device.split(':', 1)
|
||||||
Log.info("Connecting to remote scope at {}:{}".format(host, port))
|
Log.info("Connecting to remote scope at {}:{}".format(host, port))
|
||||||
reader, writer = await asyncio.open_connection(host, int(port))
|
reader, writer = await asyncio.open_connection(host, int(port))
|
||||||
scope = cls(reader, writer)
|
|
||||||
else:
|
else:
|
||||||
raise ValueError("Don't know what to do with '{}'".format(device))
|
raise ValueError("Don't know what to do with '{}'".format(device))
|
||||||
|
scope = cls(reader, writer)
|
||||||
await scope.setup()
|
await scope.setup()
|
||||||
return scope
|
return scope
|
||||||
|
|
||||||
@ -166,7 +166,7 @@ class Scope(vm.VirtualMachine):
|
|||||||
if code != 2:
|
if code != 2:
|
||||||
break
|
break
|
||||||
address = int((await self.read_replies(1))[0], 16) // nsamples_multiplier
|
address = int((await self.read_replies(1))[0], 16) // nsamples_multiplier
|
||||||
traces = {}
|
traces = {'t': [t*nsamples_multiplier*self.capture_clock_period for t in range(timestamp-nsamples*ticks, timestamp, ticks)]}
|
||||||
for dump_channel, channel in enumerate(sorted(channels)):
|
for dump_channel, channel in enumerate(sorted(channels)):
|
||||||
async with self.transaction():
|
async with self.transaction():
|
||||||
await self.set_registers(SampleAddress=(address - nsamples) * nsamples_multiplier % buffer_width,
|
await self.set_registers(SampleAddress=(address - nsamples) * nsamples_multiplier % buffer_width,
|
||||||
@ -300,7 +300,7 @@ import numpy as np
|
|||||||
async def main():
|
async def main():
|
||||||
global s, x, y, data
|
global s, x, y, data
|
||||||
parser = argparse.ArgumentParser(description="scopething")
|
parser = argparse.ArgumentParser(description="scopething")
|
||||||
parser.add_argument('device', type=str, help="Device to connect to")
|
parser.add_argument('device', nargs='?', default=None, type=str, help="Device to connect to")
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
s = await Scope.connect(args.device)
|
s = await Scope.connect(args.device)
|
||||||
x = np.linspace(0, 2*np.pi, s.awg_wavetable_size, endpoint=False)
|
x = np.linspace(0, 2*np.pi, s.awg_wavetable_size, endpoint=False)
|
||||||
@ -315,8 +315,11 @@ def capture(*args, **kwargs):
|
|||||||
def calibrate(*args, **kwargs):
|
def calibrate(*args, **kwargs):
|
||||||
return asyncio.get_event_loop().run_until_complete(s.calibrate(*args, **kwargs))
|
return asyncio.get_event_loop().run_until_complete(s.calibrate(*args, **kwargs))
|
||||||
|
|
||||||
|
def generate(*args, **kwargs):
|
||||||
|
return asyncio.get_event_loop().run_until_complete(s.start_generator(*args, **kwargs))
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
import sys
|
import sys
|
||||||
logging.basicConfig(level=logging.DEBUG, stream=sys.stderr)
|
logging.basicConfig(level=logging.INFO, stream=sys.stderr)
|
||||||
asyncio.get_event_loop().run_until_complete(main())
|
asyncio.get_event_loop().run_until_complete(main())
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@ import asyncio
|
|||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import serial
|
import serial
|
||||||
|
from serial.tools.list_ports import comports
|
||||||
|
|
||||||
|
|
||||||
Log = logging.getLogger('streams')
|
Log = logging.getLogger('streams')
|
||||||
@ -10,9 +11,16 @@ Log = logging.getLogger('streams')
|
|||||||
|
|
||||||
class SerialStream:
|
class SerialStream:
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def stream_matching(cls, vid, pid, **kwargs):
|
||||||
|
for port in comports():
|
||||||
|
if port.vid == vid and port.pid == pid:
|
||||||
|
return SerialStream(port.device, **kwargs)
|
||||||
|
|
||||||
def __init__(self, device, loop=None, **kwargs):
|
def __init__(self, device, loop=None, **kwargs):
|
||||||
self._device = device
|
self._device = device
|
||||||
self._connection = serial.Serial(self._device, timeout=0, write_timeout=0, **kwargs)
|
self._connection = serial.Serial(self._device, timeout=0, write_timeout=0, **kwargs)
|
||||||
|
Log.debug("Opened SerialStream on {}".format(device))
|
||||||
self._loop = loop if loop is not None else asyncio.get_event_loop()
|
self._loop = loop if loop is not None else asyncio.get_event_loop()
|
||||||
self._input_buffer = bytes()
|
self._input_buffer = bytes()
|
||||||
self._output_buffer = bytes()
|
self._output_buffer = bytes()
|
||||||
|
Reference in New Issue
Block a user