21 lines
507 B
Python
21 lines
507 B
Python
from typing import Literal
|
|
|
|
|
|
def format_comm_debug(
|
|
prefix: Literal["RX", "TX"], data: bytes, include_ascii: bool = False
|
|
) -> str:
|
|
hex_representation = " ".join(f"{byte:02X}" for byte in data)
|
|
|
|
output = [
|
|
f"{prefix}: ",
|
|
hex_representation,
|
|
]
|
|
|
|
if include_ascii:
|
|
ascii_representation = "".join(
|
|
(chr(byte) if 32 <= byte <= 126 else ".") for byte in data
|
|
)
|
|
output.append(" // ASCII: " + ascii_representation)
|
|
|
|
return "".join(output)
|