Skip to content

Commit

Permalink
🐛 prevent overload rpi3, read rpi3 output
Browse files Browse the repository at this point in the history
  • Loading branch information
ianchen-tw committed Mar 18, 2021
1 parent 98ceec5 commit b11abe7
Showing 1 changed file with 37 additions and 3 deletions.
40 changes: 37 additions & 3 deletions scripts/transfer_kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,13 @@ def main():
f" :package:image size: [white]{img.size()} bytes", style="bold green"
)

tty = SerialSender(portname=arg.tty, dry=arg.dry)
tty = SerialSender(portname=arg.tty, dry=arg.dry, console=console)

def print_all_from_tty():
for i in range(10):
# Prevent raspi3 from overloading
time.sleep(0.03)
tty.read()

# Protocal
# 1. send command to use
Expand All @@ -67,9 +73,11 @@ def main():
check_sum = encode_literal_num(img.check_sum(), 32)
tty.send(check_sum)

print_all_from_tty()

# 4. send the whole kernel in binary format
total_bytes = img.size()
with TransferProgress(console, total_bytes) as p:
with TransferProgress(console, total_bytes + 1) as p:
local_checksum = 0
for i, b in enumerate(img.bytes()):
local_checksum += int(b[0])
Expand All @@ -88,14 +96,22 @@ def main():
f"byte [green]{i}[reset]: {d:02x}(hex), {d:3d}(dec), checksum:{local_checksum}"
)
tty.send(b)
tty.read()
p.advance_bytes(1)

# Read output from raspi3
print_all_from_tty()
p.advance_bytes(1)
tty.send("reboot\n".encode("utf-8"))


class SerialSender:
def __init__(self, portname: str, baud: int = 115200, dry=True):
def __init__(self, portname: str, console=None, baud: int = 115200, dry=True):
self.portname: str = portname
self.baud: int = baud
self.dry: bool = dry
self.screen: str = ""
self.console = console if console else Console()
if not dry:
self.serial: Any = serial.Serial(self.portname, self.baud)

Expand All @@ -104,6 +120,8 @@ def _write(self, data):
time.sleep(0.0003)
else:
self.serial.write(data)
# Prevent raspi3 from overloading
time.sleep(0.003)

def sendln(self, s: str):
s += "\n"
Expand All @@ -114,6 +132,22 @@ def send(self, data: bytes):
assert type(data) == bytes
self._write(data)

def read(self):
if self.serial.in_waiting:
self.screen += self.serial.read(size=self.serial.in_waiting).decode("utf-8")

while True:
possible_lines = self.screen.split("\r\n")
if len(possible_lines) > 1:
data = possible_lines[0].strip()
self.screen = "\r\n".join(possible_lines[1:])
indent = " " * 50
self.console.print(
indent + f"[yellow]\[rpi3][blue]{data}[reset]"
) # noqa
else:
break


class KernelImg:
def __init__(self, file_path: str):
Expand Down

0 comments on commit b11abe7

Please sign in to comment.