fix
This commit is contained in:
@ -32,6 +32,11 @@ def log_pair_to_sweep(avg_1: int, avg_2: int) -> float:
|
||||
return abs(value_1 - value_2) * LOG_POSTSCALER
|
||||
|
||||
|
||||
def tty_ch_pair_to_sweep(ch_1: int, ch_2: int) -> float:
|
||||
"""Reduce a raw CH1/CH2 TTY point to a single sweep value."""
|
||||
return float(abs(int(ch_1) - int(ch_2)))
|
||||
|
||||
|
||||
class AsciiSweepParser:
|
||||
"""Incremental parser for ASCII sweep streams."""
|
||||
|
||||
@ -139,7 +144,7 @@ class ComplexAsciiSweepParser:
|
||||
|
||||
|
||||
class LegacyBinaryParser:
|
||||
"""Byte-resynchronizing parser for legacy 8-byte binary records."""
|
||||
"""Byte-resynchronizing parser for supported 8-byte binary record formats."""
|
||||
|
||||
def __init__(self):
|
||||
self._buf = bytearray()
|
||||
@ -158,6 +163,7 @@ class LegacyBinaryParser:
|
||||
w0 = self._u16_at(self._buf, 0)
|
||||
w1 = self._u16_at(self._buf, 2)
|
||||
w2 = self._u16_at(self._buf, 4)
|
||||
w3 = self._u16_at(self._buf, 6)
|
||||
if w0 == 0xFFFF and w1 == 0xFFFF and w2 == 0xFFFF and self._buf[6] == 0x0A:
|
||||
self._last_step = None
|
||||
self._seen_points = False
|
||||
@ -174,6 +180,29 @@ class LegacyBinaryParser:
|
||||
events.append(PointEvent(ch=ch, x=int(w0), y=float(value)))
|
||||
del self._buf[:8]
|
||||
continue
|
||||
if w0 == 0x000A and w1 == 0xFFFF and w2 == 0xFFFF and w3 == 0xFFFF:
|
||||
self._last_step = None
|
||||
self._seen_points = False
|
||||
events.append(StartEvent(ch=0))
|
||||
del self._buf[:8]
|
||||
continue
|
||||
if w0 == 0x000A and w1 != 0xFFFF:
|
||||
if self._seen_points and self._last_step is not None and w1 <= self._last_step:
|
||||
events.append(StartEvent(ch=0))
|
||||
self._seen_points = True
|
||||
self._last_step = int(w1)
|
||||
ch_1 = u16_to_i16(w2)
|
||||
ch_2 = u16_to_i16(w3)
|
||||
events.append(
|
||||
PointEvent(
|
||||
ch=0,
|
||||
x=int(w1),
|
||||
y=tty_ch_pair_to_sweep(ch_1, ch_2),
|
||||
aux=(float(ch_1), float(ch_2)),
|
||||
)
|
||||
)
|
||||
del self._buf[:8]
|
||||
continue
|
||||
del self._buf[:1]
|
||||
return events
|
||||
|
||||
|
||||
@ -22,6 +22,7 @@ from rfg_adc_plotter.types import ParserEvent, PointEvent, SweepPacket
|
||||
_PARSER_16_BIT_X2_PROBE_BYTES = 64 * 1024
|
||||
_LEGACY_STREAM_MIN_RECORDS = 32
|
||||
_LEGACY_STREAM_MIN_MATCH_RATIO = 0.95
|
||||
_TTY_STREAM_MIN_MATCH_RATIO = 0.60
|
||||
_DEBUG_FRAME_LOG_EVERY = 10
|
||||
|
||||
|
||||
@ -30,27 +31,42 @@ def _u16le_at(data: bytes, offset: int) -> int:
|
||||
|
||||
|
||||
def _looks_like_legacy_8byte_stream(data: bytes) -> bool:
|
||||
"""Heuristically detect the legacy 8-byte stream on an arbitrary byte offset."""
|
||||
"""Heuristically detect supported 8-byte binary streams on an arbitrary byte offset."""
|
||||
buf = bytes(data)
|
||||
for offset in range(8):
|
||||
blocks = (len(buf) - offset) // 8
|
||||
if blocks < _LEGACY_STREAM_MIN_RECORDS:
|
||||
continue
|
||||
min_matches = max(_LEGACY_STREAM_MIN_RECORDS, int(blocks * _LEGACY_STREAM_MIN_MATCH_RATIO))
|
||||
matched_steps: list[int] = []
|
||||
matched_steps_legacy: list[int] = []
|
||||
matched_steps_tty: list[int] = []
|
||||
for block_idx in range(blocks):
|
||||
base = offset + (block_idx * 8)
|
||||
if (_u16le_at(buf, base + 6) & 0x00FF) != 0x000A:
|
||||
w0 = _u16le_at(buf, base)
|
||||
w1 = _u16le_at(buf, base + 2)
|
||||
if w0 == 0x000A and w1 != 0xFFFF:
|
||||
matched_steps_tty.append(w1)
|
||||
continue
|
||||
matched_steps.append(_u16le_at(buf, base))
|
||||
if len(matched_steps) < min_matches:
|
||||
continue
|
||||
monotonic_or_reset = 0
|
||||
for prev_step, next_step in zip(matched_steps, matched_steps[1:]):
|
||||
if next_step == (prev_step + 1) or next_step <= prev_step:
|
||||
monotonic_or_reset += 1
|
||||
if monotonic_or_reset >= max(4, len(matched_steps) - 4):
|
||||
return True
|
||||
matched_steps_legacy.append(_u16le_at(buf, base))
|
||||
|
||||
if len(matched_steps_legacy) >= min_matches:
|
||||
monotonic_or_reset = 0
|
||||
for prev_step, next_step in zip(matched_steps_legacy, matched_steps_legacy[1:]):
|
||||
if next_step == (prev_step + 1) or next_step <= prev_step:
|
||||
monotonic_or_reset += 1
|
||||
if monotonic_or_reset >= max(4, len(matched_steps_legacy) - 4):
|
||||
return True
|
||||
|
||||
tty_min_matches = max(_LEGACY_STREAM_MIN_RECORDS, int(blocks * _TTY_STREAM_MIN_MATCH_RATIO))
|
||||
if len(matched_steps_tty) >= tty_min_matches:
|
||||
monotonic_or_reset = 0
|
||||
for prev_step, next_step in zip(matched_steps_tty, matched_steps_tty[1:]):
|
||||
if next_step == (prev_step + 1) or next_step <= 2:
|
||||
monotonic_or_reset += 1
|
||||
if monotonic_or_reset >= max(4, len(matched_steps_tty) - 4):
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user