vtterm.console

Put the board’s REPL on a Terminal with os.dupterm.

Console is the stream os.dupterm reads from and writes to. What the REPL prints is drawn on the display; what you type reaches the REPL from a key source you give it (KeyEvents, or anything with a read() that returns bytes) or from bytes you Console.push. The USB or serial REPL keeps working beside it.

The console does what a tty does for a terminal: a bare \n starts its new line at column 0, so print on the unix port lines up.

Drawing is paced: while output streams, the display is redrawn at most every interval_ms, and everything left over is drawn the next time the REPL asks for input.

Example

>>> from board_config import display_drv
>>> from vtterm import Terminal
>>> from vtterm.console import Console
>>> console = Console(Terminal(display_drv))
>>> console.attach()
class vtterm.console.Console(terminal, keys=None, interval_ms=40, idle_ms=5)[source]

Bases: IOBase

A terminal as an os.dupterm stream.

Parameters:
  • terminal (Terminal) – Where the REPL’s output is drawn.

  • keys – A key source with read() returning bytes or None, polled whenever the REPL looks for input. Optional.

  • interval_ms (int) – Shortest time between redraws while output streams.

  • idle_ms (int) – How long read waits when there’s nothing to read, so a REPL that polls in a loop (the unix port) doesn’t spin.

attach(index=0)[source]

Hand this console to os.dupterm slot index.

Raises:

OSError – If another stream (WebREPL, the BLE REPL) holds the slot.

detach()[source]

Give the dupterm slot back and draw what’s pending.

push(data)[source]

Queue data as typed input and tell the REPL it’s there.

Ctrl-C in data interrupts a running program, as it does over USB, on ports with os.dupterm_notify.

refresh()[source]

Draw what’s waiting, unless the console is drawing already.

Call it from a timer, so output a program printed shows up while nothing else redraws. A call that lands inside write does nothing.

Returns:

int – How many rows were drawn.

write(buf)[source]

Draw what the REPL printed. Returns len(buf).

readinto(buf)[source]

Fill buf with typed input. Returns the count, or None when there’s none.

read(n=-1)[source]

Return up to n bytes of typed input, or None when there’s none.

ioctl(op, arg)[source]

Answer os.dupterm’s poll: readable when input is waiting.