fix chan swap

This commit is contained in:
awe
2026-04-10 21:13:54 +03:00
parent 44a89b8da3
commit 93823b9798
2 changed files with 31 additions and 4 deletions

View File

@ -500,18 +500,27 @@ class SweepAssembler:
self._reset_current()
if event.ch is not None:
self._cur_channel = int(event.ch)
self._cur_channels.add(int(event.ch))
return packet
point_ch = int(event.ch)
packet: Optional[SweepPacket] = None
if self._cur_channel is None:
self._cur_channel = int(event.ch)
self._cur_channels.add(int(event.ch))
self._cur_channel = point_ch
elif point_ch != self._cur_channel:
if self._xs:
# Never mix channels in a single sweep packet: otherwise
# identical step indexes can overwrite each other.
packet = self.finalize_current()
self._reset_current()
self._cur_channel = point_ch
self._cur_channels.add(point_ch)
self._xs.append(int(event.x))
self._ys.append(float(event.y))
if event.aux is not None:
self._aux_1.append(float(event.aux[0]))
self._aux_2.append(float(event.aux[1]))
return None
return packet
def finalize_current(self) -> Optional[SweepPacket]:
if not self._xs:

View File

@ -315,6 +315,24 @@ class SweepParserCoreTests(unittest.TestCase):
self.assertEqual(aux[0][1], 100.0)
self.assertEqual(aux[1][2], 95.0)
def test_sweep_assembler_splits_packet_on_channel_switch(self):
assembler = SweepAssembler(fancy=False, apply_inversion=False)
self.assertIsNone(assembler.consume(PointEvent(ch=1, x=1, y=10.0)))
packet = assembler.consume(PointEvent(ch=2, x=1, y=20.0))
self.assertIsNotNone(packet)
sweep_1, info_1, aux_1 = packet
self.assertIsNone(aux_1)
self.assertEqual(info_1["ch"], 1)
self.assertEqual(info_1["chs"], [1])
self.assertAlmostEqual(float(sweep_1[1]), 10.0, places=6)
sweep_2, info_2, aux_2 = assembler.finalize_current()
self.assertIsNone(aux_2)
self.assertEqual(info_2["ch"], 2)
self.assertEqual(info_2["chs"], [2])
self.assertAlmostEqual(float(sweep_2[1]), 20.0, places=6)
if __name__ == "__main__":
unittest.main()