Files
Freddy-Fazbear-Digital-Eyes/firmware/pi_vision/test_gaze_link.py
T
2026-10-02 12:23:58 -04:00

55 lines
1.9 KiB
Python

import io
import os
import pty
import select
import termios
import unittest
from gaze_link import GazeLink, center_to_gaze, crc16_ccitt, encode_frame, open_pi_uart
class GazeLinkTests(unittest.TestCase):
def test_crc_standard_vector_and_esp32_fixture(self):
self.assertEqual(crc16_ccitt(b"123456789"), 0x29B1)
# Generated by the current C++ gaze::encode used on the ESP32 boards.
self.assertEqual(
encode_frame(0x1234, 655, 774, True).hex(),
"a55a010134128f020603fa76",
)
def test_stream_and_sequence_wrap(self):
stream = io.BytesIO()
link = GazeLink(stream)
link.sequence = 0xFFFF
self.assertEqual(link.send(500, 500, False), 0xFFFF)
self.assertEqual(link.send(0, 1000, True), 0)
self.assertEqual(len(stream.getvalue()), 24)
self.assertEqual(stream.getvalue()[12:14], b"\xA5\x5A")
def test_box_center_and_invalid_inputs(self):
self.assertEqual(center_to_gaze((0, 0, 319, 239), 320, 240), (498, 497))
with self.assertRaises(ValueError):
center_to_gaze((0, 0, 320, 239), 320, 240)
with self.assertRaises(ValueError):
encode_frame(0, 1001, 0, True)
def test_serial_port_sends_115200_baud_frame(self):
master, slave = pty.openpty()
try:
link = open_pi_uart(os.ttyname(slave))
try:
settings = termios.tcgetattr(slave)
self.assertEqual(settings[4:6], [termios.B115200, termios.B115200])
link.send(200, 500, True)
self.assertTrue(select.select([master], [], [], 1)[0])
self.assertEqual(os.read(master, 12), encode_frame(0, 200, 500, True))
finally:
link.stream.close()
finally:
os.close(slave)
os.close(master)
if __name__ == "__main__":
unittest.main()