mirror of
https://github.com/jonathanhogg/scopething
synced 2025-07-14 03:02:09 +01:00
Actually, simplify the SerialStream again and don't use the delay stuff from the Storm version
This commit is contained in:
4
scope.py
4
scope.py
@ -162,7 +162,7 @@ class Scope(vm.VirtualMachine):
|
||||
await self.set_registers(TraceMode=clock_mode.TraceMode, BufferMode=clock_mode.BufferMode,
|
||||
SampleAddress=0, ClockTicks=ticks, ClockScale=1,
|
||||
TraceIntro=total_samples//2, TraceOutro=total_samples//2, TraceDelay=0,
|
||||
Timeout=int(round((period*5 if timeout is None else timeout) / self.trigger_timeout_tick)),
|
||||
Timeout=max(1, int(round((period*5 if timeout is None else timeout) / self.trigger_timeout_tick))),
|
||||
TriggerMask=0x7f, TriggerLogic=0x80, TriggerLevel=trigger_level, SpockOption=spock_option,
|
||||
TriggerIntro=trigger_intro, TriggerOutro=2 if hair_trigger else 4, Prelude=0,
|
||||
ConverterLo=lo, ConverterHi=hi,
|
||||
@ -345,6 +345,6 @@ def generate(*args, **kwargs):
|
||||
|
||||
if __name__ == '__main__':
|
||||
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())
|
||||
|
||||
|
66
streams.py
66
streams.py
@ -22,7 +22,6 @@ class SerialStream:
|
||||
self._connection = serial.Serial(self._device, timeout=0, write_timeout=0, **kwargs)
|
||||
Log.debug(f"Opened SerialStream on {device}")
|
||||
self._loop = loop if loop is not None else asyncio.get_event_loop()
|
||||
self._input_buffer = bytes()
|
||||
self._output_buffer = bytes()
|
||||
self._output_buffer_empty = None
|
||||
|
||||
@ -52,20 +51,9 @@ class SerialStream:
|
||||
self._output_buffer_empty = self._loop.create_future()
|
||||
self._loop.add_writer(self._connection, self._feed_data)
|
||||
|
||||
async def send_break(self):
|
||||
baudrate = self._connection.baudrate
|
||||
await self.drain()
|
||||
self._connection.baudrate = 600
|
||||
self.write(b'\0')
|
||||
await self.drain()
|
||||
self._connection.baudrate = baudrate
|
||||
|
||||
async def drain(self):
|
||||
if self._output_buffer_empty is not None:
|
||||
await self._output_buffer_empty
|
||||
n = self._connection.out_waiting
|
||||
if n:
|
||||
await asyncio.sleep(n * 10 / self._connection.baudrate)
|
||||
|
||||
def _feed_data(self):
|
||||
try:
|
||||
@ -75,7 +63,7 @@ class SerialStream:
|
||||
except Exception as e:
|
||||
Log.exception("Error writing to stream")
|
||||
self._output_buffer_empty.set_exception(e)
|
||||
self.remove_writer(self._connection, self._feed_data)
|
||||
self.remove_writer(self._connection)
|
||||
if n:
|
||||
Log.debug(f"Write {self._output_buffer[:n]!r}")
|
||||
self._output_buffer = self._output_buffer[n:]
|
||||
@ -86,46 +74,22 @@ class SerialStream:
|
||||
|
||||
async def read(self, n=None):
|
||||
while True:
|
||||
if self._input_buffer:
|
||||
if n is None:
|
||||
data, self._input_buffer = self._input_buffer, bytes()
|
||||
else:
|
||||
data, self._input_buffer = self._input_buffer[:n], self._input_buffer[n:]
|
||||
w = self._connection.in_waiting
|
||||
if w:
|
||||
data = self._connection.read(w if n is None else min(n,w))
|
||||
Log.debug(f"Read {data!r}")
|
||||
return data
|
||||
if n is None:
|
||||
self._input_buffer += await self._read()
|
||||
else:
|
||||
self._input_buffer += await self._read(n - len(self._input_buffer))
|
||||
future = self._loop.create_future()
|
||||
self._loop.add_reader(self._connection, future.set_result, None)
|
||||
try:
|
||||
await future
|
||||
finally:
|
||||
self._loop.remove_reader(self._connection)
|
||||
|
||||
async def readexactly(self, n):
|
||||
while True:
|
||||
if len(self._input_buffer) >= n:
|
||||
data, self._input_buffer = self._input_buffer[:n], self._input_buffer[n:]
|
||||
return data
|
||||
self._input_buffer += await self._read(n - len(self._input_buffer))
|
||||
|
||||
async def readuntil(self, separator):
|
||||
while True:
|
||||
index = self._input_buffer.find(separator)
|
||||
if index >= 0:
|
||||
index += len(separator)
|
||||
data, self._input_buffer = self._input_buffer[:index], self._input_buffer[index:]
|
||||
return data
|
||||
self._input_buffer += await self._read()
|
||||
|
||||
async def _read(self, n=None):
|
||||
future = self._loop.create_future()
|
||||
self._loop.add_reader(self._connection, self._handle_data, n, future)
|
||||
try:
|
||||
data = await future
|
||||
Log.debug(f"Read {data}")
|
||||
return data
|
||||
finally:
|
||||
self._loop.remove_reader(self._connection)
|
||||
|
||||
def _handle_data(self, n, future):
|
||||
if not future.cancelled():
|
||||
data = self._connection.read(n if n is not None else self._connection.in_waiting)
|
||||
future.set_result(data)
|
||||
|
||||
data = b''
|
||||
while len(data) < n:
|
||||
data += await self.read(n-len(data))
|
||||
return data
|
||||
|
||||
|
Reference in New Issue
Block a user