This package provides a shell that can be used with mbed and USB Serial emulator.
Add the following to your platformio.ini
:
lib_deps =
https://github.com/rhoban/mbed-shell.git
You can simply include:
#include "shell.h"
And then initialize it in your main()
:
shell_init_usb();
The shell will then run on a background task with lowest priority.
You can declare commands:
SHELL_COMMAND(hello, "Say hello")
{
if (argc == 0) {
shell_println("Hello!");
} else {
shell_print("Hello ");
shell_print(argv[0]);
shell_println("!");
}
}
You can declare parameters with following macros:
SHELL_PARAMETER_INT(i, "An int", 5);
SHELL_PARAMETER_FLOAT(f, "A float", 12.5);
SHELL_PARAMETER_INT(b, "A bool", false);
Those parameters can be used as regular read/write variables in the code, and read or assigned in the terminal.
It is possible to create an endless command by checking for availability of data with shell_available()
:
SHELL_COMMAND(count, "Count")
{
int k = 0;
while (!shell_available()) {
k += 1;
shell_println(k);
ThisThread::sleep_for(1000);
}
}
This will display numbers until you press any key.
You can use shell_printf()
, but it is known to be a very slow function, and floats are not supported.
Alternatively, you can also call shell_print()
and shell_println()
functions. It is possible to print floats
with a provided precision (default=2 decimals):
float x = 0.123456;
shell_print("x=");
shell_println(x, 6);