This example is primarily focused on demonstrating how to process the C-style string provided by the input while incorporating error handling. A useful reference for this is the description of the scanf function.
Another trick here is that once one input is completed, you can seamlessly start another. Using this approach, you can even create a list-like flow to collect various types of data from the user.
char inputBuffer[ 30 ];
int width = 0;
int height = 0;
void heightCallback(
char* buffer,
int bufferSize,
Shellminator* parent ){
int scan_result;
scan_result = sscanf( buffer, "%d", &height );
if( buffer[ 0 ] == '\0' ){
parent -> channel -> println();
parent -> channel -> print( "The input is empty. The input must be a number." );
parent -> input( inputBuffer, sizeof( inputBuffer ), "Plane height[ mm ]: ", heightCallback );
return;
}
if( scan_result != 1 ){
parent -> channel -> println();
parent -> channel -> print( "The input must be a number. " );
parent -> channel -> print( buffer );
parent -> channel -> print( " is not a number." );
parent -> input( inputBuffer, sizeof( inputBuffer ), "Plane height[ mm ]: ", heightCallback );
return;
}
if( height <= 0 ){
parent -> channel -> println();
parent -> channel -> print( "The input must be a positive, non-zero number." );
parent -> input( inputBuffer, sizeof( inputBuffer ), "Plane height[ mm ]: ", heightCallback );
return;
}
int area = width * height;
parent -> channel -> println();
parent -> channel -> print( "The area of the plane is: " );
parent -> channel -> print( area );
parent -> channel -> print( "mm2" );
}
void widthCallback(
char* buffer,
int bufferSize,
Shellminator* parent ){
int scan_result;
scan_result = sscanf( buffer, "%d", &width );
if( buffer[ 0 ] == '\0' ){
parent -> channel -> println();
parent -> channel -> print( "The input is empty. The input must be a number." );
parent -> input( inputBuffer, sizeof( inputBuffer ), "Plane width[ mm ]: ", widthCallback );
return;
}
if( scan_result != 1 ){
parent -> channel -> println();
parent -> channel -> print( "The input must be a number. " );
parent -> channel -> print( buffer );
parent -> channel -> print( " is not a number." );
parent -> input( inputBuffer, sizeof( inputBuffer ), "Plane width[ mm ]: ", widthCallback );
return;
}
if( width <= 0 ){
parent -> channel -> println();
parent -> channel -> print( "The input must be a positive, non-zero number." );
parent -> input( inputBuffer, sizeof( inputBuffer ), "Plane width[ mm ]: ", widthCallback );
return;
}
parent -> input( inputBuffer, sizeof( inputBuffer ), "Plane height[ mm ]: ", heightCallback );
}
void setup(){
Serial.begin(115200);
shell.begin( "arnold" );
shell.clear();
shell.input( inputBuffer, sizeof( inputBuffer ), "Plane width[ mm ]: ", widthCallback );
}
void loop(){
shell.update();
}