Linux and macOS have powerful Terminal emulators, such as screen, that can be used to directly connect to the device's Read-Eval-Print Loop (REPL) over a serial (USB) connection. This recipe will show how to connect to the REPL and start running a Python code interactively.
Using screen to access the REPL
Getting ready
Either macOS or a Linux computer may be used for this recipe and may require the screen command to be available. On macOS, the Screen application is built-in and so requires no installation. On Ubuntu, the Linux Screen can be installed with the apt install screen command.
How to do it...
Let's have a look at how to connect the REPL and run the code:
- Open the computer's Terminal application.
- List device names before plugging in device by running ls /dev/ttyACM* on Linux or ls /dev/tty.* on macOS.
- Connect the board to your computer with a USB cable.
- List the device names again with the same command to discover the device name of the board.
- If the device name is /dev/ttyACM0, then the screen command would be screen /dev/ttyACM0 115200.
- Enter the command in the Terminal and start the Screen application.
- If Screen is able to connect successfully, the Python REPL should appear on the Terminal with output similar to the following text:
Adafruit CircuitPython 3.1.2 on 2019-01-07; Adafruit CircuitPlayground Express with samd21g18
>>>
- If the prompt doesn't appear, you can try pressing Ctrl + C and then press Enter, which will stop the currently running Python script and run the REPL with the following message:
Press any key to enter the REPL. Use CTRL-D to reload.
- Once the REPL prompt appears, we will have to test if the prompt is working by evaluating the 1+1 expression. It should produce the following output:
>>> 1+1
2
How it works...
The Circuit Playground Express exposes a serial device over the USB connection, which can be accessed by a number of different Terminal emulator programs. Besides screen, there are other programs, such as picocom and minicom, that may also be used.
The last parameter that was set as 115,200 in the command sets the baud rate of the connection, which should be set at that speed. Once the connection is successfully established, an interactive session is commenced that allows expressions to be directly evaluated on the device and the output is directly displayed on the Terminal.
There's more...
Many of the recipes in the book will introduce the different parts of a script using the REPL. This will give you a chance to get immediate feedback as you run each snippet of code. Once you've entered the different snippets in the REPL you can also use REPL features to assist in your experimentation with the code. You can use the up and down arrow keys to move through the history of commands that have been entered in the REPL. For example, if you had just executed a line of code in the REPL that turned on a specific pixel on the board, you could press the up key and change which pixel is lit up by editing the line and pressing Enter again.
See also
Here are a few references:
- The use of the REPL on CircuitPython boards is discussed in detail at https://learn.adafruit.com/welcome-to-circuitpython/the-repl.
- Details on using the REPL to access MicroPython can be found at https://learn.adafruit.com/micropython-basics-how-to-load-micropython-on-a-board/serial-terminal.