Skip to main content

Introduction

Documentation Status Discord Build Status Code Style: Ruff

A subset of the curses framework. Used for making terminal based applications.

Dependencies

This driver depends on:

Please ensure all dependencies are available on the CircuitPython filesystem. This is easily achieved by downloading the Adafruit library and driver bundle or individual libraries can be installed using circup.

Works with any displayio based display.

Installing from PyPI

On supported GNU/Linux systems like the Raspberry Pi, you can install the driver locally from PyPI. To install for current user:

pip3 install adafruit-circuitpython-dang

To install system-wide (this may be required in some cases):

sudo pip3 install adafruit-circuitpython-dang

To install in a virtual environment in your current project:

mkdir project-name && cd project-name
python3 -m venv .venv
source .env/bin/activate
pip3 install adafruit-circuitpython-dang

Installing to a Connected CircuitPython Device with Circup

Make sure that you have circup installed in your Python environment. Install it with the following command if necessary:

pip3 install circup

With circup installed and your CircuitPython device connected use the following command to install:

circup install adafruit_dang

Or the following command to update an existing version:

circup update

Usage Example

import time

import supervisor
import terminalio
from displayio import Group, Palette, TileGrid
from terminalio import Terminal

import adafruit_dang as curses


class Window:
    def __init__(self, n_rows, n_cols, row=0, col=0):
        self.n_rows = n_rows
        self.n_cols = n_cols
        self.row = row
        self.col = col

    @property
    def bottom(self):
        return self.row + self.n_rows - 1

    def up(self, cursor):  # pylint: disable=invalid-name
        if cursor.row == self.row - 1 and self.row > 0:
            self.row -= 1

    def down(self, buffer, cursor):
        if cursor.row == self.bottom + 1 and self.bottom < len(buffer) - 1:
            self.row += 1

    def horizontal_scroll(self, cursor, left_margin=5, right_margin=2):
        n_pages = cursor.col // (self.n_cols - right_margin)
        self.col = max(n_pages * self.n_cols - right_margin - left_margin, 0)

    def translate(self, cursor):
        return cursor.row - self.row, cursor.col - self.col


def helloworld_main(stdscr, terminal_tilegrid):
    window = Window(terminal_tilegrid.height, terminal_tilegrid.width)
    stdscr.erase()
    img = [None] * window.n_rows

    user_input = ""
    user_entered_message = ""
    last_key_press = ""

    def setline(row, line):
        if img[row] == line:
            return
        img[row] = line
        line += " " * (window.n_cols - len(line) - 1)
        stdscr.addstr(row, 0, line)

    while True:
        header = "Hello World Adafruit Dang"
        margin = (window.n_cols - 1 - len(header)) // 2
        setline(1, f"{' ' * margin}{header}")

        key_press_message = f"Last key pressed: {last_key_press}"
        margin = (window.n_cols - 1 - len(key_press_message)) // 2
        setline(4, f"{' ' * margin}{key_press_message}")

        last_entered = f"Entered Message: {user_entered_message}"
        margin = (window.n_cols - 1 - len(last_entered)) // 2
        setline(6, f"{' ' * margin}{last_entered}")

        user_input_row = window.n_rows - 2
        if user_input:
            setline(user_input_row, user_input)
        else:
            setline(user_input_row, " " * (window.n_cols - 1))

        status_message_row = terminal_tilegrid.height - 1
        status_message = f" Adafruit Dang | Demo | Fruit Jam | {int(time.monotonic())}"
        status_message += " " * (window.n_cols - len(status_message) - 1)
        line = f"{status_message}"
        setline(status_message_row, line)

        k = stdscr.getkey()
        if k is not None:
            if len(k) == 1 and " " <= k <= "~":
                user_input += k
                last_key_press = k
            elif k == "\n":
                user_entered_message = user_input
                user_input = ""
            elif k in {"KEY_BACKSPACE", "\x7f", "\x08"}:
                user_input = user_input[:-1]


def run_helloworld_main(terminal, terminal_tilegrid):
    return curses.custom_terminal_wrapper(terminal, helloworld_main, terminal_tilegrid)


main_group = Group()
display = supervisor.runtime.display
font = terminalio.FONT
char_size = font.get_bounding_box()
print(f"char_size: {char_size}")
screen_size = (display.width // char_size[0], display.height // char_size[1])

terminal_palette = Palette(2)
terminal_palette[0] = 0x000000
terminal_palette[1] = 0xFFFFFF

terminal_area = TileGrid(
    bitmap=font.bitmap,
    width=screen_size[0],
    height=screen_size[1],
    tile_width=char_size[0],
    tile_height=char_size[1],
    pixel_shader=terminal_palette,
)

main_group.append(terminal_area)
terminal = Terminal(terminal_area, font)

display.root_group = main_group

run_helloworld_main(terminal, terminal_area)

Documentation

API documentation for this library can be found on Read the Docs.

For information on building library documentation, please check out this guide.

Contributing

Contributions are welcome! Please read our Code of Conduct before contributing to help this project stay welcoming.

Metadata

Release files for adafruit-circuitpython-dang 1.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for adafruit-circuitpython-dang 1.0.3
File Size Uploaded
adafruit_circuitpython_dang-1.0.3.tar.gz 26.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for adafruit-circuitpython-dang 1.0.3
File Interpreter ABI Platform
adafruit_circuitpython_dang-1.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 33.0 kB

Release files / adafruit_circuitpython_dang-1.0.3.tar.gz

Download URL adafruit_circuitpython_dang-1.0.3.tar.gz
Size 26.2 kB
Tags Source
SHA-256 checksum
How to use checksums
d37ae25863ac459ef040ffe0e98917da861fc78c7685da2afe4ae0acc3f86e1e
BLAKE2b-256 checksum
How to use checksums
2ff3b11730693bef6eceb4a981943979c0d885c4f6c42493048267c276e13140
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.15

Release files / adafruit_circuitpython_dang-1.0.3-py3-none-any.whl

Download URL adafruit_circuitpython_dang-1.0.3-py3-none-any.whl
Size 6.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
90958e445d6f3a872f4090d1d20c06bdcc5b83e3e3713f29266a09b86b7abe48
BLAKE2b-256 checksum
How to use checksums
b706a68a12202397ae14288aad0d41535455c1ceb281786dcb7a5c43542a5eb3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.15

Release history Release notifications | RSS feed

This release

1.0.3 This release

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page