Skip to content

uCompute — Emulating a TTY Terminal

JPMichon edited this page Oct 31, 2026 · 1 revision

📺 Emulating a TTY Terminal / Rolling Logs on SSD1306 OLED

When your robot is deployed in the field (disconnected from a computer or Thonny IDE), you lose access to the standard output terminal. To debug effectively in real-time, we developed a lightweight TTY (Teletypewriter) emulator function that handles automatic vertical scrolling on tiny 128x64 or 128x32 OLED displays.

This page explains how the rolling log buffer logic works and provides an Object-Oriented upgrade to eliminate global variables.


🧩 How the Scrolling Logic Works

A graphical OLED display doesn't natively know what a "new line" is; it only understands pixel coordinates ((X, Y)). To create a scrolling console, the code manages an array (list) of strings acting as a text buffer:

  1. The Wrapper Guard: If the screen has room left, lines are filled sequentially using an index pointer (TTY_Pointer).
  2. The Buffer Shift: Once the screen is full, the algorithm drops the oldest line (index 0) and shifts every line upward using a loop: TTY[i] = TTY[i+1].
  3. Truncation Guard: Incoming text strings are clipped via slicing (text[:15]) to prevent long characters from breaking the layout boundary on standard 8x8 font dimensions.

💻 Implementation Options

Option A: The Procedural Approach (Current Code)

Simple to read and fast to implement for beginners. It relies on a global variable to track the line index between function executions.

# Initialization configuration
screen_height = 64
WrapScreen = int(screen_height / 10) # ~6 lines available before rolling
TTY_Pointer = 0
TTY = [''] * WrapScreen

def PrintTTY(texte):
    global TTY_Pointer # Persistent state across function calls
    
    if TTY_Pointer < WrapScreen:
        TTY[TTY_Pointer] = texte[:15] # Truncate to fit screen width limits
        TTY_Pointer += 1
    else:
        # Screen is full: shift all lines up by one slot
        for i in range(WrapScreen - 1):
            TTY[i] = TTY[i+1]
        TTY[TTY_Pointer - 1] = texte[:15] # Overwrite the bottom line
        
    # Clear screen and redraw full buffer from scratch
    oled.fill(0)
    for i in range(TTY_Pointer):
        line_ptr = i * 10
        oled.text(TTY[i], 0, line_ptr)
    oled.show() # Render display

Option B: The Object-Oriented Approach (Recommended Upgrade)

To remove namespace pollution (global) and allow multiple separate terminal scopes (e.g., one terminal object for radio logs, another for motor telemetry), we wrap the state tracking directly inside a clean MicroPython class.

class OledTerminal:
    def __init__(self, oled_device, line_height=10, max_chars=15):
        self.oled = oled_device
        self.line_height = line_height
        self.max_chars = max_chars
        self.max_lines = int(self.oled.height / self.line_height)
        self.buffer = [''] * self.max_lines
        self.pointer = 0

    def println(self, text):
        clean_text = text[:self.max_chars]
        
        if self.pointer < self.max_lines:
            self.buffer[self.pointer] = clean_text
            self.pointer += 1
        else:
            # Shift buffer elements up
            for i in range(self.max_lines - 1):
                self.buffer[i] = self.buffer[i+1]
            self.buffer[self.max_lines - 1] = clean_text

        # Render array map on the OLED display
        self.oled.fill(0)
        for i in range(self.pointer):
            self.oled.text(self.buffer[i], 0, i * self.line_height)
        self.oled.show()

How to use the Class:

# Instantiate your terminal object once
terminal = OledTerminal(oled)

# Print cleanly anywhere without dealing with manual index tracking variables
terminal.println("Booting OS...")
terminal.println("Radio init ok")
terminal.println("Error pin 26!") # Automatically pushes "Booting OS..." off the screen!

🛠 Pedaogogical Value for Fablabs & Schools

This function provides an excellent hands-on lesson for STEM students regarding hardware vs. software memory management:

  • It teaches how FIFO buffers (First In, First Out) are simulated in code.
  • It demonstrates how to map high-level text concepts onto fixed low-level matrix arrays of pixel layouts.

Clone this wiki locally