#include <WiFi.h>
#include "fonts/font_sanserif_8x16.h"
void setup()
{
}
void loop()
{
InputBox ib;
ib.setBackgroundColor(RGB888(0, 0, 0));
ib.onPaint = [&](Canvas * canvas) {
canvas->selectFont(&fabgl::FONT_SANSERIF_8x16);
canvas->setPenColor(RGB888(255, 255, 0));
canvas->drawText(85, 5, "InputBox Demo - www.fabgl.com - by Fabrizio Di Vittorio");
};
ib.setAutoOK(10);
ib.message("Welcome!", "Welcome to FabGL InputBox demo!");
InputResult r = ib.progressBox(
"Example of Progress Bar",
"Abort",
true, 200, [&](fabgl::ProgressForm * form) {
for (int i = 0; i <= 100; ++i) {
if (!form->update(i, "Index is %d/100", i))
break;
delay(40);
}
delay(800);
});
if (r == InputResult::Cancel)
ib.message("", "Operation Aborted");
int s = ib.menu("Simple Menu", "Click on one item", "Item number zero;Item number one;Item number two;Item number three");
ib.messageFmt("", nullptr, "OK", "You have selected item %d", s);
fabgl::StringList list;
list.append("Option Zero");
list.append("Option One");
list.append("Option Two");
list.append("Option Three");
list.select(1, true);
s = ib.menu("Menu", "Click on an item", &list);
ib.messageFmt("", nullptr, "OK", "You have selected item %d", s);
list.clear();
list.append("Item 0");
list.append("Item 1");
list.append("Item 2");
for (bool loop = true; loop; ) {
ib.setupButton(0, "Add");
ib.setupButton(1, "Remove");
ib.setupButton(2, "Options", "Edit;Restore;Advanced", 50);
ib.setMinButtonsWidth(60);
auto r = ib.select("Items selection", "Select an item", &list, "Cancel", "OK");
switch (r) {
case InputResult::Enter:
ib.messageFmt(nullptr, nullptr, "OK", "You have selected item %d", list.getFirstSelected());
loop = false;
break;
case InputResult::ButtonExt0:
{
char value[32] = "";
if (ib.textInput("New Item", "Please enter item value", value, 31) == InputResult::Enter) {
list.takeStrings();
list.append(value);
}
break;
}
case InputResult::ButtonExt1:
if (list.getFirstSelected() > -1 && ib.message("Please confirm", "Remove Item?", "No", "Yes") == InputResult::Enter)
list.remove(list.getFirstSelected());
break;
case InputResult::ButtonExt2:
switch (ib.selectedSubItem()) {
case 0:
ib.message(nullptr, "Edit - not implented!");
break;
case 1:
ib.message(nullptr, "Restore - not implented!");
break;
case 2:
ib.message(nullptr, "Advanced - not implented!");
break;
}
break;
default:
loop = false;
break;
}
}
fabgl::StringList quiz;
quiz.append("A) Connecting to the network");
quiz.append("B) Creating new user accounts");
quiz.append("C) Providing power to the computer");
quiz.append("D) Connecting smartphones and other peripherals");
if (ib.select("QUIZ", "What is an Ethernet port used for?", &quiz, nullptr, "Submit") == InputResult::Enter) {
ib.message("Result", quiz.selected(0) ? "That's right, great job!" : "Sorry, that's not correct");
}
char name[32] = "";
if (ib.textInput("Asking name", "What's your name?", name, 31) == InputResult::Enter)
ib.messageFmt("", nullptr, "OK", "Nice to meet you %s!", name);
char const * path = nullptr;
if (FileBrowser::mountSDCard(false, "/SD"))
path = "/SD";
else if (FileBrowser::mountSPIFFS(true, "/Flash"))
path = "/Flash";
if (path) {
ib.folderBrowser("Folder Browser", path);
}
if (path) {
char filename[16] = "";
char directory[32];
strcpy(directory, path);
if (ib.fileSelector("File Select", "Filename: ", directory, sizeof(directory) - 1, filename, sizeof(filename) - 1) == InputResult::Enter) {
ib.messageFmt("", nullptr, "OK", "Folder = %s, File = %s", directory, filename);
}
FileBrowser::unmountSDCard();
FileBrowser::unmountSPIFFS();
}
if (ib.message("WiFi Configuration", "Configure WiFi?", "No", "Yes") == InputResult::Enter) {
int networksCount = 0;
ib.progressBox("", nullptr, false, 200, [&](fabgl::ProgressForm * form) {
form->update(0, "Scanning WiFi networks...");
networksCount = WiFi.scanNetworks();
});
if (networksCount > 0) {
fabgl::StringList list;
for (int i = 0; i < networksCount; ++i)
list.appendFmt("%s (%d dBm)", WiFi.SSID(i).c_str(), WiFi.RSSI(i));
int s = ib.menu("WiFi Configuration", "Please select a WiFi network", &list);
if (s > -1) {
char psw[32] = "";
if (ib.textInput("WiFi Configuration", "Insert WiFi password", psw, 31, "Cancel", "OK", true) == InputResult::Enter) {
char SSID[50];
strcpy(SSID, WiFi.SSID(s).c_str());
bool connected = false;
ib.progressBox("", nullptr, true, 200, [&](fabgl::ProgressForm * form) {
WiFi.begin(SSID, psw);
for (int i = 0; i < 32 && WiFi.status() != WL_CONNECTED; ++i) {
if (!form->update(i * 100 / 32, "Connecting to %s...", SSID))
break;
delay(500);
if (i == 16)
WiFi.reconnect();
}
connected = (WiFi.status() == WL_CONNECTED);
});
ib.message("", connected ? "Connection succeeded" : "Connection failed");
}
}
} else {
ib.message("", "No WiFi network found!");
}
WiFi.scanDelete();
}
ib.message("Restart", "Ok, press OK to restart!");
ib.end();
}
This file is the all in one include file. Application can just include this file to use FabGL library...
InputResult
Result of InputBox dialogs helper class.