Main: Loop function is more readable/understandable. Server: Added a specific function to show IP. Fixed some constexpr typo and initialisation warnings.

This commit is contained in:
Yohan Boujon 2023-12-13 18:47:35 +01:00
parent c2b18fc052
commit 0fe4c2ecee
5 changed files with 37 additions and 25 deletions

View file

@ -7,7 +7,7 @@
using namespace Display; using namespace Display;
Screen::Screen() : _booted(0), _bootFrame(0) Screen::Screen() : _bootFrame(0), _booted(0)
{ {
_screen = new U8G2_SSD1306_128X64_NONAME_F_HW_I2C(U8G2_R0, U8X8_PIN_NONE, SCL, SDA); _screen = new U8G2_SSD1306_128X64_NONAME_F_HW_I2C(U8G2_R0, U8X8_PIN_NONE, SCL, SDA);
_screen->begin(); _screen->begin();
@ -36,7 +36,7 @@ void Screen::Setup(uint8_t *font)
std::make_shared<TextBox>(TextBox("Connected to Wi-Fi !", StyleWidth::LEFT, StyleHeight::CENTERED, U8G2_BTN_BW0, 0, 2)), std::make_shared<TextBox>(TextBox("Connected to Wi-Fi !", StyleWidth::LEFT, StyleHeight::CENTERED, U8G2_BTN_BW0, 0, 2)),
std::make_shared<TextBox>(TextBox("IP address: ", StyleWidth::LEFT, StyleHeight::CENTERED, U8G2_BTN_BW0, 0, 2)), std::make_shared<TextBox>(TextBox("IP address: ", StyleWidth::LEFT, StyleHeight::CENTERED, U8G2_BTN_BW0, 0, 2)),
std::make_shared<TextBox>(TextBox("addr", StyleWidth::CENTERED, StyleHeight::CENTERED, U8G2_BTN_BW0, 0, 2))}); std::make_shared<TextBox>(TextBox("addr", StyleWidth::CENTERED, StyleHeight::CENTERED, U8G2_BTN_BW0, 0, 2))});
bootWindow.Add(std::make_shared<SpriteBox>(SpriteBox(clover_frames[0].data, clover_frames[0].height, clover_frames[0].width, StyleWidth::CENTERED, StyleHeight::CENTERED))); bootWindow.Add(std::make_shared<SpriteBox>(SpriteBox(CLOVER_FRAMES[0].data, CLOVER_FRAMES[0].height, CLOVER_FRAMES[0].width, StyleWidth::CENTERED, StyleHeight::CENTERED)));
loopWindow.Add(std::make_shared<TextBox>(TextBox("Welcome to Clover!", StyleWidth::CENTERED, StyleHeight::CENTERED, U8G2_BTN_BW0, 0, 2))); loopWindow.Add(std::make_shared<TextBox>(TextBox("Welcome to Clover!", StyleWidth::CENTERED, StyleHeight::CENTERED, U8G2_BTN_BW0, 0, 2)));
} }
@ -100,7 +100,7 @@ void Screen::boot()
// Component // Component
bootWindow.Display(); bootWindow.Display();
_bootFrame++; _bootFrame++;
bootWindow.Update(0,clover_frames[(_bootFrame >= 10 ? 10 : _bootFrame)]); bootWindow.Update(0,CLOVER_FRAMES[(_bootFrame >= 10 ? 10 : _bootFrame)]);
_screen->sendBuffer(); _screen->sendBuffer();
} }
@ -117,4 +117,4 @@ uint16_t Screen::getHeight() { return _height; }
uint16_t Screen::getWidth() { return _width; } uint16_t Screen::getWidth() { return _width; }
U8G2_SSD1306_128X64_NONAME_F_HW_I2C &Screen::getScreen() { return *_screen; } U8G2_SSD1306_128X64_NONAME_F_HW_I2C &Screen::getScreen() { return *_screen; }
uint16_t Screen::getTextWidth(const char *str) { return _screen->getStrWidth(str); } uint16_t Screen::getTextWidth(const char *str) { return _screen->getStrWidth(str); }
bool Screen::isBooting() { return (_bootFrame<=20); } bool Screen::isBooting() { return (_bootFrame<=MAX_BOOT_FRAMES); }

View file

@ -22,7 +22,7 @@
namespace Display namespace Display
{ {
constexpr Picture clover_frames[] = { constexpr Picture CLOVER_FRAMES[] = {
{clover1_bits, clover1_width, clover1_height}, {clover1_bits, clover1_width, clover1_height},
{clover2_bits, clover2_width, clover2_height}, {clover2_bits, clover2_width, clover2_height},
{clover3_bits, clover3_width, clover3_height}, {clover3_bits, clover3_width, clover3_height},
@ -35,6 +35,7 @@ namespace Display
{clover10_bits, clover10_width, clover10_height}, {clover10_bits, clover10_width, clover10_height},
{clover11_bits, clover11_width, clover11_height}, {clover11_bits, clover11_width, clover11_height},
}; };
constexpr uint8_t MAX_BOOT_FRAMES = 25;
class Screen class Screen
{ {

View file

@ -34,15 +34,15 @@ void ServerHandler::setup(const char *ssid, const char *password)
void ServerHandler::loop() void ServerHandler::loop()
{ {
if (display_time < MAX_TIME)
{
Display::Screen::GetInstance().connected(WiFi.localIP().toString().c_str(), display_time);
display_time++;
}
server.handleClient(); server.handleClient();
} }
bool ServerHandler::showNext() { return (display_time >= MAX_TIME); } void ServerHandler::showIp()
{
Display::Screen::GetInstance().connected(WiFi.localIP().toString().c_str(), display_time);
display_time++;
}
bool ServerHandler::isConnected() { return _connected; } bool ServerHandler::isConnected() { return _connected; }
bool ServerHandler::showBoot() { return (display_time >= MAX_TIME); } bool ServerHandler::showBoot() { return (display_time >= MAX_TIME); }

View file

@ -19,9 +19,8 @@ public:
} }
// Public functions // Public functions
void setup(const char* ssid, const char* password); void setup(const char* ssid, const char* password);
void showIp();
void loop(); void loop();
// Return if the screen needs to be changed.
bool showNext();
bool isConnected(); bool isConnected();
bool showBoot(); bool showBoot();

View file

@ -24,34 +24,46 @@ void setup()
void loop() void loop()
{ {
// Creating variables to access singletons
auto& serverHandler = ServerHandler::GetInstance(); auto& serverHandler = ServerHandler::GetInstance();
auto& dataHandler = DataHandler::GetInstance(); auto& dataHandler = DataHandler::GetInstance();
auto& screen = Display::Screen::GetInstance(); auto& screen = Display::Screen::GetInstance();
// Could not connect, show screen failure // Could not connect after setup: Showing screen failure
if(!serverHandler.isConnected()) if(!serverHandler.isConnected())
{ {
screen.notConnected(); screen.notConnected();
return; return;
} }
// Is booting // Server showing IP
if(!serverHandler.showBoot())
{
serverHandler.showIp();
delay(250);
return;
}
// When Screen can boot (isBooting) and Server finished showing IP (showBoot)
if(screen.isBooting() && serverHandler.showBoot()) if(screen.isBooting() && serverHandler.showBoot())
{ {
screen.boot(); screen.boot();
delay(166); delay(100);
return;
} }
// If serverHandler finished showing ip. // Data gathered from various sensors
if (!screen.isBooting())
screen.loop();
dataHandler.updateTemperatureData(random(1800, 2200) / 100.0);
// 0 -> air(0), 0-300 -> dry(20), 300-700 -> humid (580), 700-950 -> water(940) // 0 -> air(0), 0-300 -> dry(20), 300-700 -> humid (580), 700-950 -> water(940)
dataHandler.updateHumidityData(static_cast<float>(std::any_cast<int>(humidity.getValue()))); auto humidityData = static_cast<float>(std::any_cast<int>(humidity.getValue()));
Serial.println(dataHandler.getJsonData()); auto temperatureData = random(300, 150) / 10.0;
// When showing IP, delay is faster.
delay(serverHandler.showNext() ? 0 : 250);
// Updating the data handler
dataHandler.updateTemperatureData(temperatureData);
dataHandler.updateHumidityData(humidityData);
// (debug) Printing to serial the data
Serial.println(dataHandler.getJsonData());
// Screen showing
screen.loop();
// Server sending data
serverHandler.loop(); serverHandler.loop();
} }