#include <WiFi.h>
#include "esp_wifi.h"
#include "esp_event.h"
struct TestApp : public uiApp {
uiFrame * frame;
uiButton * soundButton;
SoundGenerator * soundGenerator;
uiTimerHandle soundTimer;
uiButton * sdButton;
uiLabel * sdResultLabel;
uiComboBox * gpioInComboBox;
uiButton * gpioInState;
uiComboBox * gpioOutComboBox;
uiButton * gpioOutState;
uiTimerHandle gpioTimer;
uiButton * wifiButton;
uiLabel * wifiResultLabel;
uiLabel * extgpioLabel[16];
uiButton * extgpioPlay;
int gpioIn, gpioOut;
bool gpioInPrevState;
~TestApp() {
delete soundGenerator;
}
void init() {
rootWindow()->frameStyle().backgroundColor =
Color::Cyan;
frame = new uiFrame(rootWindow(), "Hardware test", UIWINDOW_PARENTCENTER, Size(600, 420));
int y = 20;
new uiLabel(frame, "COLORS TEST:", Point(10, y));
y += 20;
static const char * COLORSL[8] = { "Black", "Red", "Green", "Yellow", "Blue", "Magenta", "Cyan", "White" };
static const char * COLORSH[8] = { "B. Black", "B. Red", "B. Green", "B. Yellow", "B. Blue", "B. Magenta", "B. Cyan", "B. White" };
for (int i = 0; i < 8; ++i) {
int left = 60;
int hspc = 60;
int vspc = 30;
new uiLabel(frame, COLORSL[i], Point(left + i * hspc, y));
new uiColorBox(frame, Point(left + i * hspc, y + 15), Size(50, 14), (
Color)i);
new uiLabel(frame, COLORSH[i], Point(left + i * hspc, y + vspc));
new uiColorBox(frame, Point(left + i * hspc, y + 15 + vspc), Size(50, 14), (
Color)(i+ 8));
}
y += 80;
new uiLabel(frame, "KEYBOARD TEST:", Point(10, y));
new uiTextEdit(frame, "Please type something", Point(120, y - 3), Size(450, 20));
y += 40;
soundGenerator = new SoundGenerator;
new uiLabel(frame, "SOUND TEST:", Point(10, y));
soundButton = new uiButton(frame, "Start", Point(120, y - 3), Size(80, 20), uiButtonKind::Switch);
soundButton->onChange = [&]() {
if (soundButton->down()) {
soundButton->setText("Stop");
soundTimer = setTimer(frame, 250);
} else {
soundButton->setText("Start");
killTimer(soundTimer);
}
};
y += 40;
new uiLabel(frame, "SDCARD TEST:", Point(10, y));
sdButton = new uiButton(frame, "Start", Point(120, y - 3), Size(80, 20));
sdButton->onClick = [&]() {
testSD();
};
sdResultLabel = new uiLabel(frame, "", Point(210, y));
y += 44;
static const int GPIOS_IN[] = { 0, 1, 2, 3, 12, 13, 14, 16, 17, 18, 34, 35, 36, 39 };
static const int GPIOS_OUT[] = { 0, 1, 2, 3, 12, 13, 14, 16, 17, 18 };
new uiLabel(frame, "GPIO TEST:", Point(10, y));
new uiLabel(frame, "In", Point(120, y - 18));
gpioInComboBox = new uiComboBox(frame, Point(120, y - 3), Size(34, 22), 60);
for (int i = 0; i < sizeof(GPIOS_IN) / sizeof(int); ++i)
gpioInComboBox->items().appendFmt("%d", GPIOS_IN[i]);
gpioInComboBox->onChange = [&]() {
gpioIn = gpioInComboBox->selectedItem() > -1 ? GPIOS_IN[gpioInComboBox->selectedItem()] : GPIOS_IN[10];
pinMode(gpioIn, INPUT);
};
gpioIn = GPIOS_IN[10];
pinMode(gpioIn, INPUT);
gpioInComboBox->selectItem(10);
new uiLabel(frame, "State", Point(162, y - 18));
gpioInState = new uiButton(frame, "", Point(162, y - 3), Size(25, 22), uiButtonKind::Switch);
gpioInPrevState = 0;
new uiLabel(frame, "Out", Point(220, y - 18));
gpioOutComboBox = new uiComboBox(frame, Point(220, y - 3), Size(34, 22), 60);
for (int i = 0; i < sizeof(GPIOS_OUT) / sizeof(int); ++i)
gpioOutComboBox->items().appendFmt("%d", GPIOS_OUT[i]);
gpioOutComboBox->onChange = [&]() {
gpioOut = gpioOutComboBox->selectedItem() > -1 ? GPIOS_OUT[gpioOutComboBox->selectedItem()] : GPIOS_OUT[2];
pinMode(gpioOut, OUTPUT);
updateGPIOOutState();
};
gpioOut = GPIOS_OUT[2];
pinMode(gpioOut, OUTPUT);
gpioOutComboBox->selectItem(2);
new uiLabel(frame, "State", Point(262, y - 18));
gpioOutState = new uiButton(frame, "", Point(262, y - 3), Size(25, 22), uiButtonKind::Switch);
gpioOutState->setDown(false);
gpioOutState->onChange = [&]() {
updateGPIOOutState();
};
updateGPIOOutState();
gpioTimer = setTimer(frame, 100);
y += 44;
if (initMCP()) {
new uiLabel(frame, "EXT GPIO TEST:", Point(10, y));
new uiLabel(frame, "Outputs", Point(120, y - 13));
new uiLabel(frame, "Inputs", Point(365, y - 13));
for (int i = 0; i < 16; ++i) {
constexpr int w = 20;
constexpr int h = 22;
extgpioLabel[i] = new uiLabel(frame, "", Point(120 + i * (w + 2), y + 3), Size(w, h));
extgpioLabel[i]->labelStyle().textAlign = uiHAlign::Center;
extgpioLabel[i]->setTextFmt("%c%d", i < 8 ? 'A' : 'B', i & 7);
if (i >= MCP_B3) {
extgpioLabel[i]->labelStyle().backgroundColor = RGB888(64, 0, 0);
} else {
extgpioLabel[i]->labelStyle().backgroundColor = RGB888(0, 64, 0);
auto pmcp = &mcp;
extgpioLabel[i]->onClick = [=]() {
extGPIOSet(i, !pmcp->readGPIO(i));
};
}
}
extgpioPlay = new uiButton(frame, "Play", Point(490, y + 3), Size(42, 22));
extgpioPlay->onClick = [&]() { playExtGPIOS(); };
y += 50;
}
new uiLabel(frame, "WIFI TEST:", Point(10, y));
wifiButton = new uiButton(frame, "Start", Point(120, y - 3), Size(80, 20));
wifiButton->onClick = [&]() {
testWifi();
};
wifiResultLabel = new uiLabel(frame, "", Point(210, y));
frame->onTimer = [&](uiTimerHandle h) {
if (h == soundTimer) {
soundGenerator->playSound(SineWaveformGenerator(), random(100, 3000), 230, 100);
}
if (h == gpioTimer) {
updateGPIOInState();
}
};
y = frame->size().height - 40;
new uiLabel(frame, "FabGL - Copyright 2019-2021 by Fabrizio Di Vittorio", Point(175, y));
new uiLabel(frame, "WWW.FABGL.COM", Point(260, y + 17));
}
void updateGPIOOutState() {
if (gpioOutState->down()) {
gpioOutState->setText("1");
digitalWrite(gpioOut, HIGH);
} else {
gpioOutState->setText("0");
digitalWrite(gpioOut, LOW);
}
}
void updateGPIOInState() {
if (gpioInPrevState != digitalRead(gpioIn)) {
gpioInPrevState = !gpioInPrevState;
gpioInState->setText(gpioInPrevState ? "1" : "0");
gpioInState->repaint();
}
for (int i = MCP_B3; i <= MCP_B7; ++i) {
extgpioLabel[i]->labelStyle().backgroundColor = mcp.
readGPIO(i) ? RGB888(255, 0, 0) : RGB888(64, 0, 0);
extgpioLabel[i]->repaint();
}
}
}
bool initMCP() {
}
void extGPIOSet(int gpio, bool value) {
extgpioLabel[gpio]->labelStyle().backgroundColor = value ? RGB888(0, 255, 0) : RGB888(0, 64, 0);
extgpioLabel[gpio]->repaint();
}
void playExtGPIOS() {
showWindow(extgpioPlay, false);
for (int j = 0; j < 4; ++j) {
for (int i = MCP_A0; i <= MCP_B2; ++i) {
extGPIOSet(i, true);
if (i > MCP_A0)
extGPIOSet(i - 1, false);
processEvents();
delay(80);
}
for (int i = MCP_B2; i >= MCP_A0; --i) {
extGPIOSet(i, true);
if (i < MCP_B2)
extGPIOSet(i + 1, false);
processEvents();
delay(80);
}
}
for (int j = 0; j < 4; ++j) {
for (int i = MCP_A0; i <= MCP_B2; ++i) {
extGPIOSet(i, true);
processEvents();
delay(80);
}
for (int i = MCP_A0; i <= MCP_B2; ++i) {
extGPIOSet(i, false);
processEvents();
delay(80);
}
}
for (int j = 0; j < 10; ++j) {
for (int i = MCP_A0; i <= MCP_B2; ++i)
extGPIOSet(i, true);
processEvents();
delay(120);
for (int i = MCP_A0; i <= MCP_B2; ++i)
extGPIOSet(i, false);
processEvents();
delay(120);
}
showWindow(extgpioPlay, true);
}
void testSD() {
FileBrowser fb;
fb.unmountSDCard();
bool r = fb.mountSDCard(false, "/SD", 1);
if (!r) {
sdResultLabel->setText("Mount Failed!");
return;
}
fb.setDirectory("/SD");
constexpr int size = 16384;
bool ok = true;
auto fname = fb.createTempFilename();
auto f = fopen(fname, "wb");
if (f) {
for (int i = 0; i < size; ++i)
if (fputc(i & 0xff, f) != (i & 0xff)) {
ok = false;
break;
}
fclose(f);
}
if (!f || !ok) {
free(fname);
sdResultLabel->setText("Write Failed!");
return;
}
f = fopen(fname, "rb");
if (f) {
for (int i = 0; i < size; ++i)
if (fgetc(f) != (i & 0xff)) {
ok = false;
break;
}
fclose(f);
}
free(fname);
fb.unmountSDCard();
initMCP();
if (!f || !ok) {
sdResultLabel->setText("Read Failed!");
return;
}
sdResultLabel->setText("Success!");
}
void testWifi() {
esp_event_loop_create_default();
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
esp_wifi_init(&cfg);
uint16_t number = 8;
wifi_ap_record_t ap_info[number];
uint16_t ap_count = 0;
memset(ap_info, 0, sizeof(ap_info));
esp_wifi_set_mode(WIFI_MODE_STA);
esp_wifi_start();
auto r = esp_wifi_scan_start(NULL, true);
esp_wifi_scan_get_ap_records(&number, ap_info);
esp_wifi_scan_get_ap_num(&ap_count);
esp_wifi_stop();
esp_wifi_deinit();
esp_event_loop_delete_default();
if (r != ESP_OK) {
wifiResultLabel->setText("Wifi Scan Failed!");
} else if (ap_count == 0) {
wifiResultLabel->setText("Ok, No Network Found!");
} else {
wifiResultLabel->setTextFmt("Ok, Found %d Networks", ap_count);
}
}
} app;
void setup()
{
PS2Controller.
begin(PS2Preset::KeyboardPort0_MousePort1, KbdMode::GenerateVirtualKeys);
DisplayController.begin();
}
void loop()
{
app.runAsync(&DisplayController);
vTaskDelete(NULL);
}