This repository was archived by the owner on Jan 23, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathconsole.py
More file actions
62 lines (51 loc) · 1.78 KB
/
console.py
File metadata and controls
62 lines (51 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import sys
import termios
import tty
from contextlib import contextmanager
from anyio import create_task_group
from anyio.streams.file import FileReadStream, FileWriteStream
from jumpstarter.client import DriverClient
class ConsoleExit(Exception):
pass
class Console:
def __init__(self, serial_client: DriverClient):
self.serial_client = serial_client
def run(self):
with self.setraw():
self.serial_client.portal.call(self.__run)
@contextmanager
def setraw(self):
original = termios.tcgetattr(sys.stdin.fileno())
try:
tty.setraw(sys.stdin.fileno())
yield
finally:
termios.tcsetattr(sys.stdin.fileno(), termios.TCSADRAIN, original)
async def __run(self):
async with self.serial_client.stream_async(method="connect") as stream:
try:
async with create_task_group() as tg:
tg.start_soon(self.__serial_to_stdout, stream)
tg.start_soon(self.__stdin_to_serial, stream)
except* ConsoleExit:
pass
async def __serial_to_stdout(self, stream):
stdout = FileWriteStream(sys.stdout.buffer)
while True:
data = await stream.receive()
await stdout.send(data)
sys.stdout.flush()
async def __stdin_to_serial(self, stream):
stdin = FileReadStream(sys.stdin.buffer)
ctrl_b_count = 0
while True:
data = await stdin.receive(max_bytes=1)
if not data:
continue
if data == b"\x02": # Ctrl-B
ctrl_b_count += 1
if ctrl_b_count == 3:
raise ConsoleExit
else:
ctrl_b_count = 0
await stream.send(data)