panda/tests/echo.py

34 lines
961 B
Python
Raw Normal View History

2019-11-07 18:45:16 -07:00
#!/usr/bin/env python3
import os
import sys
2019-11-25 14:09:58 -07:00
import time
import _thread
2019-11-07 18:45:16 -07:00
sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), ".."))
from panda import Panda # noqa: E402
2019-11-07 18:45:16 -07:00
# This script is intended to be used in conjunction with the echo_loopback_test.py test script from panda jungle.
# It sends a reversed response back for every message received containing b"test".
2019-11-25 14:09:58 -07:00
def heartbeat_thread(p):
while True:
try:
p.send_heartbeat()
time.sleep(1)
except Exception:
2019-11-25 14:09:58 -07:00
break
2019-11-07 18:45:16 -07:00
# Resend every CAN message that has been received on the same bus, but with the data reversed
if __name__ == "__main__":
p = Panda()
2019-11-25 14:09:58 -07:00
_thread.start_new_thread(heartbeat_thread, (p,))
2019-11-07 18:45:16 -07:00
p.set_safety_mode(Panda.SAFETY_ALLOUTPUT)
2019-11-25 14:09:58 -07:00
p.set_power_save(False)
2019-11-07 18:45:16 -07:00
while True:
incoming = p.can_recv()
for message in incoming:
address, notused, data, bus = message
if b'test' in data:
p.can_send(address, data[::-1], bus)