Simple test

Ensure your device works with this simple test.

examples/dang_simpletest.py
  1# SPDX-FileCopyrightText: Copyright (c) 2025 Tim Cocks for Adafruit Industries
  2#
  3# SPDX-License-Identifier: MIT
  4
  5import time
  6
  7import supervisor
  8import terminalio
  9from displayio import Group, Palette, TileGrid
 10from terminalio import Terminal
 11
 12import adafruit_dang as curses
 13
 14
 15class Window:
 16    def __init__(self, n_rows, n_cols, row=0, col=0):
 17        self.n_rows = n_rows
 18        self.n_cols = n_cols
 19        self.row = row
 20        self.col = col
 21
 22    @property
 23    def bottom(self):
 24        return self.row + self.n_rows - 1
 25
 26    def up(self, cursor):  # pylint: disable=invalid-name
 27        if cursor.row == self.row - 1 and self.row > 0:
 28            self.row -= 1
 29
 30    def down(self, buffer, cursor):
 31        if cursor.row == self.bottom + 1 and self.bottom < len(buffer) - 1:
 32            self.row += 1
 33
 34    def horizontal_scroll(self, cursor, left_margin=5, right_margin=2):
 35        n_pages = cursor.col // (self.n_cols - right_margin)
 36        self.col = max(n_pages * self.n_cols - right_margin - left_margin, 0)
 37
 38    def translate(self, cursor):
 39        return cursor.row - self.row, cursor.col - self.col
 40
 41
 42def helloworld_main(stdscr, terminal_tilegrid):
 43    window = Window(terminal_tilegrid.height, terminal_tilegrid.width)
 44    stdscr.erase()
 45    img = [None] * window.n_rows
 46
 47    user_input = ""
 48    user_entered_message = ""
 49    last_key_press = ""
 50
 51    def setline(row, line):
 52        if img[row] == line:
 53            return
 54        img[row] = line
 55        line += " " * (window.n_cols - len(line) - 1)
 56        stdscr.addstr(row, 0, line)
 57
 58    while True:
 59        header = "Hello World Adafruit Dang"
 60        margin = (window.n_cols - 1 - len(header)) // 2
 61        setline(1, f"{' ' * margin}{header}")
 62
 63        key_press_message = f"Last key pressed: {last_key_press}"
 64        margin = (window.n_cols - 1 - len(key_press_message)) // 2
 65        setline(4, f"{' ' * margin}{key_press_message}")
 66
 67        last_entered = f"Entered Message: {user_entered_message}"
 68        margin = (window.n_cols - 1 - len(last_entered)) // 2
 69        setline(6, f"{' ' * margin}{last_entered}")
 70
 71        user_input_row = window.n_rows - 2
 72        if user_input:
 73            setline(user_input_row, user_input)
 74        else:
 75            setline(user_input_row, " " * (window.n_cols - 1))
 76
 77        status_message_row = terminal_tilegrid.height - 1
 78        status_message = f" Adafruit Dang | Demo | Fruit Jam | {int(time.monotonic())}"
 79        status_message += " " * (window.n_cols - len(status_message) - 1)
 80        line = f"{status_message}"
 81        setline(status_message_row, line)
 82
 83        k = stdscr.getkey()
 84        if k is not None:
 85            if len(k) == 1 and " " <= k <= "~":
 86                user_input += k
 87                last_key_press = k
 88            elif k == "\n":
 89                user_entered_message = user_input
 90                user_input = ""
 91            elif k in {"KEY_BACKSPACE", "\x7f", "\x08"}:
 92                user_input = user_input[:-1]
 93
 94
 95def run_helloworld_main(terminal, terminal_tilegrid):
 96    return curses.custom_terminal_wrapper(terminal, helloworld_main, terminal_tilegrid)
 97
 98
 99main_group = Group()
100display = supervisor.runtime.display
101font = terminalio.FONT
102char_size = font.get_bounding_box()
103print(f"char_size: {char_size}")
104screen_size = (display.width // char_size[0], display.height // char_size[1])
105
106terminal_palette = Palette(2)
107terminal_palette[0] = 0x000000
108terminal_palette[1] = 0xFFFFFF
109
110terminal_area = TileGrid(
111    bitmap=font.bitmap,
112    width=screen_size[0],
113    height=screen_size[1],
114    tile_width=char_size[0],
115    tile_height=char_size[1],
116    pixel_shader=terminal_palette,
117)
118
119main_group.append(terminal_area)
120terminal = Terminal(terminal_area, font)
121
122display.root_group = main_group
123
124run_helloworld_main(terminal, terminal_area)