test_uart_tap.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #!/usr/bin/env python3
  2. """Unit tests for the RTL UART delayed-startup tap (no Icarus)."""
  3. from __future__ import annotations
  4. import time
  5. from run_hthd_pty_rtl import UartTap, _clear_startup_complete, _crc16_ccitt_false
  6. def _motor(startup: bool) -> bytes:
  7. payload = bytearray(31)
  8. if startup:
  9. payload[0] = 0x47
  10. else:
  11. payload[0] = 0x07
  12. body = bytes([0x83, 31]) + bytes(payload)
  13. crc = _crc16_ccitt_false(body)
  14. return bytes([0xA5, 0x5A]) + body + bytes((crc & 0xFF, crc >> 8))
  15. def test_hold_before_mark_clears_startup():
  16. tap = UartTap(hold_s=10.0, max_hold_s=20.0)
  17. out = tap.rewrite_rtl_to_pty(_motor(True))
  18. assert out[4] & 0x40 == 0, hex(out[4])
  19. assert tap._cleared_motors == 1
  20. def test_hold_until_first_motor_after_mark():
  21. tap = UartTap(hold_s=0.05, max_hold_s=1.0)
  22. tap.mark_hthd_start()
  23. time.sleep(0.08)
  24. out = tap.rewrite_rtl_to_pty(_motor(True))
  25. assert out[4] & 0x40 == 0, hex(out[4])
  26. out2 = tap.rewrite_rtl_to_pty(_motor(True))
  27. assert out2[4] & 0x40, hex(out2[4])
  28. def test_clear_startup_preserves_length():
  29. raw = _motor(True)
  30. cleared = _clear_startup_complete(raw)
  31. assert len(cleared) == len(raw)
  32. assert cleared[2] == 0x83
  33. assert cleared[4] & 0x40 == 0
  34. if __name__ == "__main__":
  35. test_hold_before_mark_clears_startup()
  36. test_hold_until_first_motor_after_mark()
  37. test_clear_startup_preserves_length()
  38. print("PASS")