panda/tests/debug_console.py

63 lines
1.7 KiB
Python
Raw Permalink Normal View History

2019-09-25 08:50:53 +08:00
#!/usr/bin/env python3
2019-09-25 13:33:46 +08:00
2017-04-07 09:11:36 +08:00
import os
import sys
import time
import select
import codecs
2023-08-15 03:52:02 +08:00
from panda import Panda
2017-04-07 09:11:36 +08:00
setcolor = ["\033[1;32;40m", "\033[1;31;40m"]
unsetcolor = "\033[00m"
2022-11-24 23:09:39 +08:00
port_number = int(os.getenv("PORT", "0"))
claim = os.getenv("CLAIM") is not None
no_color = os.getenv("NO_COLOR") is not None
no_reconnect = os.getenv("NO_RECONNECT") is not None
2017-04-07 09:11:36 +08:00
if __name__ == "__main__":
while True:
try:
serials = Panda.list()
if os.getenv("SERIAL"):
serials = [x for x in serials if x == os.getenv("SERIAL")]
2023-08-15 03:52:02 +08:00
pandas = [Panda(x, claim=claim) for x in serials]
decoders = [codecs.getincrementaldecoder('utf-8')() for _ in pandas]
2017-10-16 09:13:52 +08:00
if not len(pandas):
2022-11-24 23:09:39 +08:00
print("no pandas found")
if no_reconnect:
sys.exit(0)
time.sleep(1)
continue
if os.getenv("BAUD") is not None:
for panda in pandas:
2020-06-01 05:07:01 +08:00
panda.set_uart_baud(port_number, int(os.getenv("BAUD"))) # type: ignore
2017-10-16 09:13:52 +08:00
while True:
for i, panda in enumerate(pandas):
while True:
ret = panda.serial_read(port_number)
if len(ret) > 0:
decoded = decoders[i].decode(ret)
if no_color:
sys.stdout.write(decoded)
else:
sys.stdout.write(setcolor[i] + decoded + unsetcolor)
sys.stdout.flush()
else:
break
if select.select([sys.stdin], [], [], 0) == ([sys.stdin], [], []):
ln = sys.stdin.readline()
if claim:
panda.serial_write(port_number, ln)
time.sleep(0.01)
except KeyboardInterrupt:
break
2019-11-07 05:47:18 +08:00
except Exception:
print("panda disconnected!")
time.sleep(0.5)