impoved tty parser binary mode: now it supports 32-bit values of intensity

This commit is contained in:
2026-02-18 23:01:34 +03:00
parent 8b1d424cbe
commit ece30f1cd5
3 changed files with 41 additions and 35 deletions

Binary file not shown.

View File

@ -40,9 +40,9 @@ class SweepReader(threading.Thread):
self._n_valid_hist = deque() self._n_valid_hist = deque()
@staticmethod @staticmethod
def _u16_to_i16(v: int) -> int: def _u32_to_i32(v: int) -> int:
"""Преобразование 16-bit слова в знаковое значение.""" """Преобразование 32-bit слова в знаковое значение."""
return v - 0x10000 if (v & 0x8000) else v return v - 0x1_0000_0000 if (v & 0x8000_0000) else v
def _finalize_current(self, xs, ys, channels: Optional[set]): def _finalize_current(self, xs, ys, channels: Optional[set]):
if not xs: if not xs:
@ -211,10 +211,7 @@ class SweepReader(threading.Thread):
ys: list[int] = [] ys: list[int] = []
cur_channel: Optional[int] = None cur_channel: Optional[int] = None
cur_channels: set[int] = set() cur_channels: set[int] = set()
waiting_channel = False words = deque()
waiting_first_point = False
point_word: Optional[int] = None
value_word: Optional[int] = None
buf = bytearray() buf = bytearray()
while not self._stop.is_set(): while not self._stop.is_set():
@ -232,47 +229,53 @@ class SweepReader(threading.Thread):
i = 0 i = 0
while i < usable: while i < usable:
w = int(buf[i]) | (int(buf[i + 1]) << 8) w = int(buf[i]) | (int(buf[i + 1]) << 8)
words.append(w)
i += 2 i += 2
if waiting_channel: # Бинарный протокол:
cur_channel = int(w) # старт свипа (актуальный): 0xFFFF, 0xFFFF, 0xFFFF, (ch<<8)|0x0A
cur_channels.add(cur_channel) # старт свипа (legacy): 0xFFFF, 0xFFFF, channel, 0x0A0A
waiting_channel = False # точка: step, value_hi, value_lo, 0x000A
waiting_first_point = True while len(words) >= 4:
continue w0 = int(words[0])
w1 = int(words[1])
w2 = int(words[2])
w3 = int(words[3])
if w == 0xFFFF: if w0 == 0xFFFF and w1 == 0xFFFF and w2 == 0xFFFF and (w3 & 0x00FF) == 0x000A:
self._finalize_current(xs, ys, cur_channels) self._finalize_current(xs, ys, cur_channels)
xs.clear() xs.clear()
ys.clear() ys.clear()
cur_channel = None
cur_channels.clear() cur_channels.clear()
waiting_channel = True cur_channel = (w3 >> 8) & 0x00FF
waiting_first_point = False cur_channels.add(cur_channel)
point_word = None for _ in range(4):
value_word = None words.popleft()
continue continue
if point_word is None: if w0 == 0xFFFF and w1 == 0xFFFF and w3 == 0x0A0A:
if waiting_first_point and (w == 0x0A0A or w == 0x000A): self._finalize_current(xs, ys, cur_channels)
continue xs.clear()
point_word = int(w) ys.clear()
waiting_first_point = False cur_channels.clear()
cur_channel = w2
cur_channels.add(cur_channel)
for _ in range(4):
words.popleft()
continue continue
if value_word is None: if w3 == 0x000A:
value_word = int(w)
continue
is_point_end = (w == 0x000A) or ((w & 0x00FF) == 0x0A) or ((w >> 8) == 0x0A)
if is_point_end:
if cur_channel is not None: if cur_channel is not None:
cur_channels.add(cur_channel) cur_channels.add(cur_channel)
xs.append(point_word) xs.append(w0)
ys.append(self._u16_to_i16(value_word)) value_u32 = (w1 << 16) | w2
ys.append(self._u32_to_i32(value_u32))
for _ in range(4):
words.popleft()
continue
point_word = None # Поток может начаться с середины пакета; сдвигаемся по слову до ресинхронизации.
value_word = None words.popleft()
del buf[:usable] del buf[:usable]
if len(buf) > 1_000_000: if len(buf) > 1_000_000:

View File

@ -81,7 +81,10 @@ def build_parser() -> argparse.ArgumentParser:
"--bin", "--bin",
dest="bin_mode", dest="bin_mode",
action="store_true", action="store_true",
help="Бинарный протокол: 16-bit поток, 0xFFFF+канал для старта свипа, точки point,value,'\\n'", help=(
"Бинарный протокол: старт свипа 0xFFFF,0xFFFF,0xFFFF,(CH<<8)|0x0A; "
"точки step,uint32(hi16,lo16),0x000A"
),
) )
return parser return parser