diff --git a/embedded/lib/Display/Components/Box.hpp b/embedded/lib/Display/Components/Box.hpp index a68cc2c..5b7b426 100644 --- a/embedded/lib/Display/Components/Box.hpp +++ b/embedded/lib/Display/Components/Box.hpp @@ -26,11 +26,12 @@ namespace Display UNDEFINED, TOP, CENTERED, - BOTTOM + BOTTOM, + FORCE_CENTERED }; /** - * @brief Box is a virtual class which can be derived into many box elements + * @brief Box is an abstract class which can be derived into many box elements * and will be used in the 'Components' object. * It is composed of 4 functions which may or not be used. * Each box should use the Screen Interface in the Display function, diff --git a/embedded/lib/Display/Components/Components.cpp b/embedded/lib/Display/Components/Components.cpp index 07fc7d7..d23913b 100644 --- a/embedded/lib/Display/Components/Components.cpp +++ b/embedded/lib/Display/Components/Components.cpp @@ -32,8 +32,10 @@ void Components::Display() { const auto size_boxes = GetSize((*it)->getStyleHeight()); (*it)->Display(size_boxes, totalSize); - // Index and verticalPadding only incrementing for the same style. (eg : it and it+1 as the same style.) - if (it + 1 != _boxes.end() && ((*(it+1))->getStyleHeight() == (*it)->getStyleHeight())) + + /* Index and verticalPadding only incrementing for the same style. (eg : it and it+1 as the same style.) + Plus, it has to not be FORCED to be incremented.*/ + if (it + 1 != _boxes.end() && ((*(it+1))->getStyleHeight() == (*it)->getStyleHeight()) && ((*it)->getStyleHeight() != StyleHeight::FORCE_CENTERED)) totalSize += (*it)->getHeight() + (2*(*it)->getPadding()); else totalSize = 0; diff --git a/embedded/lib/Display/Components/SpriteBox.cpp b/embedded/lib/Display/Components/SpriteBox.cpp index 44d32bb..683da52 100644 --- a/embedded/lib/Display/Components/SpriteBox.cpp +++ b/embedded/lib/Display/Components/SpriteBox.cpp @@ -45,6 +45,7 @@ void SpriteBox::Display(u8g2_uint_t size, u8g2_uint_t size_pos) switch(this->_styleHeight) { case StyleHeight::CENTERED: + case StyleHeight::FORCE_CENTERED: // idk must be the size of all the above Screen::GetInstance().getScreen().drawXBM(_x, static_cast((centeredOffset / 2)) + size_pos,_width,_height,_sprite); break; diff --git a/embedded/lib/Display/Components/TextBox.cpp b/embedded/lib/Display/Components/TextBox.cpp index c0fc3e1..9494fd1 100644 --- a/embedded/lib/Display/Components/TextBox.cpp +++ b/embedded/lib/Display/Components/TextBox.cpp @@ -46,6 +46,7 @@ void TextBox::Display(u8g2_uint_t size, u8g2_uint_t size_pos) switch (this->_styleHeight) { case StyleHeight::CENTERED: + case StyleHeight::FORCE_CENTERED: Screen::GetInstance().getScreen().drawButtonUTF8(_paddingWidth + _x, static_cast((centeredOffset / 2)) + size_pos + _height, _style, _textWidth, this->_paddingHeight, _paddingWidth, _text.c_str()); break; case StyleHeight::BOTTOM: diff --git a/embedded/lib/Display/Screen.cpp b/embedded/lib/Display/Screen.cpp index 23ab7c4..244c979 100644 --- a/embedded/lib/Display/Screen.cpp +++ b/embedded/lib/Display/Screen.cpp @@ -5,6 +5,7 @@ // XBM Files #include "Components/SpriteBox.hpp" #include "../Pictures/clover.xbm" +#include "../Pictures/failed.xbm" using namespace Display; @@ -30,11 +31,14 @@ void Screen::Setup(uint8_t *font) // Static Components connectingWindow.Add({std::make_shared(headerSetup), std::make_shared(TextBox("connect", StyleWidth::CENTERED, StyleHeight::CENTERED, U8G2_BTN_BW0))}); + connectionfailedWindow.Add({std::make_shared(headerSetup), + std::make_shared(TextBox("Failed to connect.", StyleWidth::RIGHT, StyleHeight::CENTERED, U8G2_BTN_BW0,3)), + std::make_shared(SpriteBox(failed_bits,failed_height,failed_width,StyleWidth::LEFT,StyleHeight::FORCE_CENTERED))}); connectedWindow.Add({std::make_shared(headerSetup), std::make_shared(TextBox("Connected to Wi-Fi !", StyleWidth::LEFT, StyleHeight::CENTERED, U8G2_BTN_BW0, 0, 2)), std::make_shared(TextBox("IP address: ", StyleWidth::LEFT, StyleHeight::CENTERED, U8G2_BTN_BW0, 0, 2)), std::make_shared(TextBox("addr", StyleWidth::CENTERED, StyleHeight::CENTERED, U8G2_BTN_BW0, 0, 2))}); - loopWindow.Add(std::make_shared(SpriteBox(clover_bits,clover_height,clover_width,StyleWidth::CENTERED,StyleHeight::CENTERED))); + loopWindow.Add(std::make_shared(SpriteBox(clover_bits, clover_height, clover_width, StyleWidth::CENTERED, StyleHeight::CENTERED))); } void Screen::connecting(uint8_t state) @@ -63,6 +67,15 @@ void Screen::connecting(uint8_t state) _screen->sendBuffer(); } +void Screen::notConnected() +{ + _screen->clearBuffer(); + // Component + connectionfailedWindow.Display(); + // Displaying + _screen->sendBuffer(); +} + void Screen::connected(const char *ipaddress, uint8_t timing) { _screen->clearBuffer(); diff --git a/embedded/lib/Display/Screen.hpp b/embedded/lib/Display/Screen.hpp index 9f26ff0..c7731cd 100644 --- a/embedded/lib/Display/Screen.hpp +++ b/embedded/lib/Display/Screen.hpp @@ -19,7 +19,8 @@ namespace Display } // Public functions void Setup(uint8_t *font); - void connecting(uint8_t state = 0); + void connecting(uint8_t state=0); + void notConnected(); void connected(const char *ipaddress, uint8_t timing); void loop(); // Getters @@ -44,6 +45,7 @@ namespace Display // Static Components TextBox headerSetup; Components connectingWindow; + Components connectionfailedWindow; Components connectedWindow; Components loopWindow; }; diff --git a/embedded/lib/Pictures/failed.xbm b/embedded/lib/Pictures/failed.xbm new file mode 100644 index 0000000..2a1ffec --- /dev/null +++ b/embedded/lib/Pictures/failed.xbm @@ -0,0 +1,6 @@ +#define failed_width 16 +#define failed_height 16 +static unsigned char failed_bits[] = { + 0x00, 0x00, 0x06, 0x60, 0x0e, 0x70, 0x1c, 0x38, 0x38, 0x1c, 0x70, 0x0e, + 0xe0, 0x07, 0xc0, 0x03, 0xc0, 0x03, 0xe0, 0x07, 0x70, 0x0e, 0x38, 0x1c, + 0x1c, 0x38, 0x0e, 0x70, 0x06, 0x60, 0x00, 0x00 }; diff --git a/embedded/lib/ServerHandler/ServerHandler.cpp b/embedded/lib/ServerHandler/ServerHandler.cpp index fee89d0..8da17e4 100644 --- a/embedded/lib/ServerHandler/ServerHandler.cpp +++ b/embedded/lib/ServerHandler/ServerHandler.cpp @@ -1,42 +1,53 @@ #include "ServerHandler.hpp" #include "../Display/Screen.hpp" -ServerHandler::ServerHandler() : server(80), display_time(0) { +ServerHandler::ServerHandler() : server(80), display_time(0), _connected(false) +{ } ServerHandler::~ServerHandler() {} -void ServerHandler::setup(const char* ssid, const char* password) { // On utilise les scope resolution operator pour définir les méthodes la classe ServerHandle qui elle est dans hpp +void ServerHandler::setup(const char *ssid, const char *password) +{ // On utilise les scope resolution operator pour définir les méthodes la classe ServerHandle qui elle est dans hpp uint8_t state(0); + uint16_t tryConnection(0); Serial.begin(9600); WiFi.begin(ssid, password); - while (WiFi.status() != WL_CONNECTED) { + // Testing connection + while ((WiFi.status() != WL_CONNECTED) && (tryConnection < MAX_CONNECT_TRIES)) + { delay(500); Display::Screen::GetInstance().connecting(state); - state >= 3 ? state = 0: state++; + state >= 3 ? state = 0 : state++; + tryConnection++; } - server.begin(); - server.on("/", [this]() { this->handleRoot(); }); // fonction lamda pour gérer les requettes get + if (tryConnection < MAX_CONNECT_TRIES) + { + _connected = true; + server.begin(); + server.on("/", [this]() + { this->handleRoot(); }); // fonction lamda pour gérer les requettes get + } } -void ServerHandler::loop() { - if(display_time < MAX_TIME) +void ServerHandler::loop() +{ + if (display_time < MAX_TIME) { - Display::Screen::GetInstance().connected(WiFi.localIP().toString().c_str(),display_time); + Display::Screen::GetInstance().connected(WiFi.localIP().toString().c_str(), display_time); display_time++; } server.handleClient(); } -bool ServerHandler::showNext() -{ - return (display_time >= MAX_TIME); -} +bool ServerHandler::showNext() { return (display_time >= MAX_TIME); } +bool ServerHandler::isConnected() { return _connected; } -void ServerHandler::handleRoot() { - auto& dataHandler = DataHandler::GetInstance(); +void ServerHandler::handleRoot() +{ + auto &dataHandler = DataHandler::GetInstance(); String jsonFormattedData = dataHandler.getJsonData(); server.send(200, "application/json", jsonFormattedData); } diff --git a/embedded/lib/ServerHandler/ServerHandler.hpp b/embedded/lib/ServerHandler/ServerHandler.hpp index bf233ae..20a390b 100644 --- a/embedded/lib/ServerHandler/ServerHandler.hpp +++ b/embedded/lib/ServerHandler/ServerHandler.hpp @@ -7,6 +7,7 @@ #include "DataHandler.hpp" constexpr uint8_t MAX_TIME = 28; +constexpr uint16_t MAX_CONNECT_TRIES = 20; class ServerHandler { public: @@ -21,6 +22,7 @@ public: void loop(); // Return if the screen needs to be changed. bool showNext(); + bool isConnected(); private: // Singleton @@ -32,6 +34,7 @@ private: void handleRoot(); ESP8266WebServer server; uint8_t display_time; + bool _connected; }; #endif diff --git a/embedded/platformio.ini b/embedded/platformio.ini index 6f09bfb..1f38097 100644 --- a/embedded/platformio.ini +++ b/embedded/platformio.ini @@ -11,6 +11,7 @@ [env:nodemcuv2] platform = espressif8266 board = nodemcuv2 +build_flags = -fexceptions framework = arduino lib_deps = tzapu/WiFiManager@^0.16.0 diff --git a/embedded/src/main.cpp b/embedded/src/main.cpp index 31d961b..83b1a66 100644 --- a/embedded/src/main.cpp +++ b/embedded/src/main.cpp @@ -28,6 +28,13 @@ void loop() auto& dataHandler = DataHandler::GetInstance(); auto& screen = Display::Screen::GetInstance(); + // If could not connect, show screen failure + if(!serverHandler.isConnected()) + { + screen.notConnected(); + return; + } + // If serverHandler finished showing ip. if (serverHandler.showNext()) screen.loop();