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
|
2017-06-14 09:50:47 +08:00
|
|
|
|
|
|
|
sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), ".."))
|
2020-06-01 16:49:26 +08:00
|
|
|
from panda import Panda # noqa: E402
|
2017-04-07 09:11:36 +08:00
|
|
|
|
|
|
|
setcolor = ["\033[1;32;40m", "\033[1;31;40m"]
|
|
|
|
unsetcolor = "\033[00m"
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2019-08-29 03:57:42 +08:00
|
|
|
while True:
|
|
|
|
try:
|
2020-06-03 07:27:07 +08:00
|
|
|
port_number = int(os.getenv("PORT", "0"))
|
2019-08-29 03:57:42 +08:00
|
|
|
claim = os.getenv("CLAIM") is not None
|
2017-04-07 09:11:36 +08:00
|
|
|
|
2019-08-29 03:57:42 +08:00
|
|
|
serials = Panda.list()
|
|
|
|
if os.getenv("SERIAL"):
|
2020-06-01 16:49:26 +08:00
|
|
|
serials = [x for x in serials if x == os.getenv("SERIAL")]
|
2017-05-17 08:01:29 +08:00
|
|
|
|
2019-09-25 13:33:46 +08:00
|
|
|
pandas = list([Panda(x, claim=claim) for x in serials])
|
2017-10-16 09:13:52 +08:00
|
|
|
|
2019-08-29 03:57:42 +08:00
|
|
|
if not len(pandas):
|
|
|
|
sys.exit("no pandas found")
|
2019-04-02 06:01:20 +08:00
|
|
|
|
2019-08-29 03:57:42 +08:00
|
|
|
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
|
|
|
|
2017-06-14 09:50:47 +08:00
|
|
|
while True:
|
2019-08-29 03:57:42 +08:00
|
|
|
for i, panda in enumerate(pandas):
|
|
|
|
while True:
|
2019-09-28 06:22:13 +08:00
|
|
|
ret = panda.serial_read(port_number)
|
|
|
|
if len(ret) > 0:
|
|
|
|
sys.stdout.write(setcolor[i] + ret.decode('ascii') + unsetcolor)
|
|
|
|
sys.stdout.flush()
|
|
|
|
else:
|
|
|
|
break
|
2019-08-29 03:57:42 +08:00
|
|
|
if select.select([sys.stdin], [], [], 0) == ([sys.stdin], [], []):
|
2019-09-28 06:22:13 +08:00
|
|
|
ln = sys.stdin.readline()
|
|
|
|
if claim:
|
|
|
|
panda.serial_write(port_number, ln)
|
2019-08-29 03:57:42 +08:00
|
|
|
time.sleep(0.01)
|
2019-11-07 05:47:18 +08:00
|
|
|
except Exception:
|
2019-08-29 03:57:42 +08:00
|
|
|
print("panda disconnected!")
|
2020-06-01 16:49:26 +08:00
|
|
|
time.sleep(0.5)
|