Cornell University ECE4760
Serial Interface using PuTTY

Pi Pico RP2040

Interface using serial
Combining a tokenizing parser with terminal emulator (e.g. PuTTY or MobaXterm) can produce a user interface that allows for command or menu control of the rp2040. It is easy to define a simple command language and use color, cursor position and font to make a better looking command interface to the microconroller. For instance controlling an Karplus-Strong string sound synthesizer unit takes about 9 parameters. As shown below, It is handy to have the parameters displayed on the serial terminal, along with the commands to change the parameters, and the command line itself. In this example, the current parameter values are below the title bar in white, followed by the commands in green, then the actual command line. When you enter the command, the entire display is refreshed (no scrolling) in less than 60 mSec.

Command structure
Since any given command may be a series of tokens different data types, the easiest way to determine the values of the tokens is to use the C tokenizer function str_tok_r to chop up the input command string, then build a state machine that parses down thorugh each different command, assigning values as it goes. To illustrate, I built a slightly silly example (see image below) that changes the graphics of the serial display based on a few different commands. Note that in this example the protothreads serialread function is used for input, but a simple printf is used for output. This is becuase printf times are short and predictable, and because two threads can print. Using printf does not do a thread context change, and so is thread-safe.

The command interpreter first tokenizes the input string using space delimiters. The individual tokens are then converted to internal format depending on their type (float, int, string, character) ans stored appropriately. This works well for many command formats but is strained for the calc command, where we ae used to writing operators adjacent to operands. The calc command uses a different scheme on the portion of the input string representing the numbers and operator. It uses strchr to first find the operator, saves it, then inserts a space in place of the operator. The two numbers are then determined from a simple sscanf. The saved operator is placed back into the string for printing.

Screen control
The basic tokenizer by itself will produce a scrolling, quite useable, interface. Sometimes it is nice to have a more formatted terminal display. Most modern terminal emulators, including PuTTY, support a number of escape sequences which directly control the display.  or example, if you use a printf to send the character sequence "\033[10;15H" to the terminal, then the terminal will respond by moving the cursor to line 10, character position 15 horizontally. A more practical way to use this feature would be to write a macro
#define cursor_pos(line,col) printf("\033[%d;%dH", line, col)
to allow easy line/column positioning.

Macros were written for a useful subset of the available escape sequences including font type (bold, normal double-wide), 8-bit font/background colors, and cursor control. These enable you to write a single screen form, with no scrolling, precise positioning of elements, and color coding. There are two threads: the serial command handler, and a timer thread that blinks a frw characters when enabled.

Code, project ZIP


Copyright Cornell University February 16, 2024