Simple test

Ensure the library works with this simple test.

examples/prompt_toolkit_simpletest.py
 1# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
 2# SPDX-FileCopyrightText: Copyright (c) 2023 Scott Shawcroft for Adafruit Industries
 3#
 4# SPDX-License-Identifier: Unlicense
 5
 6# Rename import to make the rest of the code compatible with CPython's prompt_toolkit library.
 7import adafruit_prompt_toolkit as prompt_toolkit
 8
 9# This basic example doesn't do much more than input but it's where to start.
10while True:
11    response = prompt_toolkit.prompt("$ ")
12    print("->", response)

Second USB

Use the library over a second USB CDC serial connection. boot.py must include usb_cdc.enable(console=True, data=True). console can be False.

Example boot.py:

import usb_cdc

# Enable console and data
usb_cdc.enable(console=True, data=True)

Example code.py:

examples/prompt_toolkit_second_cdc.py
 1# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
 2# SPDX-FileCopyrightText: Copyright (c) 2023 Scott Shawcroft for Adafruit Industries
 3#
 4# SPDX-License-Identifier: Unlicense
 5
 6# This example works over the second CDC and supports history.
 7
 8import usb_cdc
 9
10# Rename import to make the rest of the code compatible with CPython's prompt_toolkit library.
11import adafruit_prompt_toolkit as prompt_toolkit
12
13# If the second CDC is available, then use it instead.
14serial = usb_cdc.console
15if usb_cdc.data:
16    serial = usb_cdc.data
17
18session = prompt_toolkit.PromptSession(input=serial, output=serial)
19
20while True:
21    response = prompt_toolkit.prompt("$ ")
22    print("->", response, file=serial)