Sometimes, a user needs to choose from a fixed set of options. The ShellminatorList class provides an easy way to handle this. It allows users to select from a predefined list of choices with minimal effort.
First, include the necessary library:
Then, define the list of options the user can choose from:
const char* listOptions[] = {
"Red Pill",
"Blue Pill"
};
You’ll also need a short instruction message to guide the user:
const char* listText = "Choose your destiny, Neo";
Now, you can create a ShellminatorList object:
It’s a good idea to define a callback function that will be triggered when the user makes a selection. The function must have the following signature:
void listCallback(
const char* optionsList[],
int listSize,
int selected,
ShellminatorScreen*);
This is an abstract object for graphics element creation.
In the init section, attach this callback function to the list:
neoList.attachCallback(listCallback);
Now, let’s define what happens when the user selects an option:
void listCallback(
const char* optionsList[],
int listSize,
int selected,
ShellminatorScreen* screen) {
if (parent == NULL) {
return;
}
if (selected == 0) {
notification.setText("You stay in Wonderland and I show\nyou how deep the rabbit hole goes.");
} else {
notification.setText("The story ends, you wake up in your bed and\nbelieve whatever you want to believe.");
}
}
void swapScreen(ShellminatorScreen *screen_p, int updatePeriod=250)
Shellminator * getParent()
How It Works?
- The user sees a list of options.
- When they make a selection, the callback function is triggered.
- The function checks which option was chosen and displays the appropriate notification message.
Live Demo
Whole Code
const char* listOptions[] = {
"Red Pill",
"Blue Pill"
};
const char* listText = "Choose your destiny Neo";
void listCallback(
const char* optionsList[],
int listSize,
int selected,
ShellminatorScreen* );
void setup(){
Serial.begin(115200);
shell.clear();
Serial.println( "Program Start!" );
neoList.attachCallback( listCallback );
shell.begin( "arnold" );
shell.beginScreen( &neoList );
}
void loop(){
shell.update();
}
void listCallback(
const char* optionsList[],
int listSize,
int selected,
ShellminatorScreen* screen ){
parent = screen -> getParent();
if( parent == NULL ){
return;
}
if( selected == 0 ){
notification.
setText(
"You stay in Wonderland and I show\nyou how deep the rabbit hole goes." );
}
else{
notification.
setText(
"The story ends, you wake up in your bed and\nbelieve whatever you want to believe." );
}
parent -> swapScreen( ¬ification );
}
void setText(const char *text_p)