diff --git a/Platformio/HAL/HardwareModules/wifiHandlerInterface.h b/Platformio/HAL/HardwareModules/wifiHandlerInterface.h index a7b3640..f6c2f80 100644 --- a/Platformio/HAL/HardwareModules/wifiHandlerInterface.h +++ b/Platformio/HAL/HardwareModules/wifiHandlerInterface.h @@ -21,4 +21,5 @@ class wifiHandlerInterface{ virtual void connect(std::shared_ptr ssid, std::shared_ptr password) = 0; virtual void onScanDone(std::function>)> function) = 0; virtual void onStatusUpdate(std::function)> function) = 0; + virtual void begin() = 0; }; \ No newline at end of file diff --git a/Platformio/HAL/MPMCQueueInterface.hpp b/Platformio/HAL/MPMCQueueInterface.hpp new file mode 100644 index 0000000..d3d396e --- /dev/null +++ b/Platformio/HAL/MPMCQueueInterface.hpp @@ -0,0 +1,9 @@ +#pragma once +#include "SPSCQueueInterface.hpp" + +template +class MPMCQueueInterface: public SPSCQueueInterface +{ + public: + bool push(T obj, bool overwrite = false); +}; \ No newline at end of file diff --git a/Platformio/HAL/SPSCQueueInterface.hpp b/Platformio/HAL/SPSCQueueInterface.hpp new file mode 100644 index 0000000..9efe787 --- /dev/null +++ b/Platformio/HAL/SPSCQueueInterface.hpp @@ -0,0 +1,12 @@ +#pragma once +#include + +template +class SPSCQueueInterface { + public: + virtual bool push(T obj) = 0; + virtual std::optional pop() = 0; + virtual std::optional peek() = 0; + virtual bool isFull() = 0; + virtual bool isEmpty() = 0; +}; \ No newline at end of file diff --git a/Platformio/HAL/Targets/ESP32/freeRTOSMPMCQueue.cpp b/Platformio/HAL/Targets/ESP32/freeRTOSMPMCQueue.cpp new file mode 100644 index 0000000..f302ed7 --- /dev/null +++ b/Platformio/HAL/Targets/ESP32/freeRTOSMPMCQueue.cpp @@ -0,0 +1,70 @@ +#include "freeRTOSMPMCQueue.hpp" + +template +freeRTOSMPMCQueue::freeRTOSMPMCQueue(uint32_t size) +{ + this->queue = xQueueCreate(size, sizeof(T)); +} + +template +freeRTOSMPMCQueue::~freeRTOSMPMCQueue() +{ + vQueueDelete(this->queue); +} + +template +bool freeRTOSMPMCQueue::push(T obj) +{ + return xQueueSendToBack(this->queue, &obj, 0) == pdTRUE; +} + +template +bool freeRTOSMPMCQueue::push(T obj, bool overwrite) +{ + if (overwrite == true) + { + xQueueOverwrite(this->queue, obj); + return true; + } + else + { + return this->push(obj); + } +} + +template +std::optional freeRTOSMPMCQueue::pop() +{ + T retval; + + if (xQueueReceive(this->queue, &retval, 0) == pdTRUE) + { + return retval; + } + + return std::nullopt; +} + +template +std::optional freeRTOSMPMCQueue::peek() +{ + T retval; + + if (xQueuePeek(this->queue, &retval, 0) == pdTRUE) + { + return retval; + } + return std::nullopt; +} + +template +bool freeRTOSMPMCQueue::isFull() +{ + return (xQueueIsQueueFullFromISR(this->queue) == pdTRUE); +} + +template +bool freeRTOSMPMCQueue::isEmpty() +{ + return (xQueueIsQueueEmptyFromISR(this->queue) == pdTRUE); +} \ No newline at end of file diff --git a/Platformio/HAL/Targets/ESP32/freeRTOSMPMCQueue.hpp b/Platformio/HAL/Targets/ESP32/freeRTOSMPMCQueue.hpp new file mode 100644 index 0000000..013bfc5 --- /dev/null +++ b/Platformio/HAL/Targets/ESP32/freeRTOSMPMCQueue.hpp @@ -0,0 +1,18 @@ +#pragma once +#include "MPMCQueueInterface.hpp" +#include "Arduino.h" +template +class freeRTOSMPMCQueue: public MPMCQueueInterface +{ + public: + freeRTOSMPMCQueue(uint32_t size); + ~freeRTOSMPMCQueue(); + bool push (T obj); + bool push (T obj, bool overwrite); + std::optional pop(); + std::optional peek(); + bool isFull(); + bool isEmpty(); + private: + QueueHandle_t queue; +}; \ No newline at end of file diff --git a/Platformio/HAL/Targets/ESP32/wifiHandler/wifihandler.cpp b/Platformio/HAL/Targets/ESP32/wifiHandler/wifihandler.cpp index e6c8b34..66884ed 100644 --- a/Platformio/HAL/Targets/ESP32/wifiHandler/wifihandler.cpp +++ b/Platformio/HAL/Targets/ESP32/wifiHandler/wifihandler.cpp @@ -2,6 +2,7 @@ #include #include #include "HardwareAbstract.hpp" +#include "WiFi.h" std::shared_ptr wifiHandler::mInstance = nullptr; @@ -37,6 +38,10 @@ void wifiHandler::WiFiEvent(WiFiEvent_t event){ //this->display.wifi_scan_complete( no_networks); } this->scan_notification.notify(info); + if (WiFi.isConnected() == false) + { + WiFi.reconnect(); + } break; } case ARDUINO_EVENT_WIFI_STA_GOT_IP: @@ -47,6 +52,8 @@ void wifiHandler::WiFiEvent(WiFiEvent_t event){ case ARDUINO_EVENT_WIFI_STA_STOP: this->update_status(); default: + Serial.print("Wifi Status: "); + Serial.println(WiFi.status()); break; } if (WiFi.status() == WL_CONNECT_FAILED) @@ -74,7 +81,6 @@ wifiHandler::wifiHandler() { this->password = ""; this->SSID = ""; - this->begin(); } void wifiHandler::update_status() @@ -137,6 +143,12 @@ void wifiHandler::update_credentials() void wifiHandler::scan() { Serial.println("scan called"); + /* If the */ + WiFi.status(); + if (WiFi.isConnected() != true) + { + WiFi.disconnect(); + } WiFi.scanNetworks(true); } @@ -164,7 +176,7 @@ void wifiHandler::begin() //strcpy(this->password, password.c_str()); this->SSID = ssid.c_str(); this->password = password.c_str(); - //this->connect(this->SSID, this->password); + //this->connect(std::make_shared(std::string(this->SSID)), std::make_shared(std::string(this->password))); } else { diff --git a/Platformio/HAL/Targets/Simulator/SimulatorMPMCQueue.cpp b/Platformio/HAL/Targets/Simulator/SimulatorMPMCQueue.cpp new file mode 100644 index 0000000..2d6e207 --- /dev/null +++ b/Platformio/HAL/Targets/Simulator/SimulatorMPMCQueue.cpp @@ -0,0 +1,77 @@ +#include "SimulatorMPMCQueue.hpp" + +template +bool SimulatorMPMCQueue::push(T obj) +{ + return this->push(obj, false); +} + +template +bool SimulatorMPMCQueue::push(T obj, bool overwrite) +{ + bool retval = false; + if (this->mtx.try_lock()) + { + if (this->isFull() && overwrite) + { + /* If we should overwrite already written data and the buffer is full, we increment the rd_index as well. + This has to be done in the mutex so we do not overwrite data which is already being read*/ + this->rd_index = this->incrementIndex(this->rd_index); + } + + /* If the buffer is full, we can not write to the buffer. If overwrite is set to true, this check will never + fail as we move the rd_index if the buffer would otherwise be full*/ + if (!this->isFull()) + { + retval = true; + this->data[this->wr_index] = obj; + this->wr_index = this->incrementIndex(this->wr_index); + } + this->mtx.unlock(); + } + return retval; +} + +template +std::optional SimulatorMPMCQueue::pop() +{ + T retval; + if (this->mtx.try_lock()){ + + if (this->isEmpty()) + { + return std::nullopt; + } + + retval = this->data[this->rd_index]; + this->rd_index = this->incrementIndex(this->rd_index); + this->mtx.unlock(); + return retval; + + } + return std::nullopt; +} + +template +std::optional SimulatorMPMCQueue::peek() +{ + T retval; + if (this->mtx.try_lock()) + { + if (this->isEmpty()) + { + return std::nullopt; + } + + retval = this->data[this->rd_index]; + this->mtx.unlock(); + return retval; + } + return std::nullopt; +} + +template +uint32_t SimulatorMPMCQueue::incrementIndex(uint32_t index) +{ + return (index + 1) % this->size; +} \ No newline at end of file diff --git a/Platformio/HAL/Targets/Simulator/SimulatorMPMCQueue.hpp b/Platformio/HAL/Targets/Simulator/SimulatorMPMCQueue.hpp new file mode 100644 index 0000000..947c742 --- /dev/null +++ b/Platformio/HAL/Targets/Simulator/SimulatorMPMCQueue.hpp @@ -0,0 +1,24 @@ +#pragma once +#include "MPMCQueueInterface.hpp" +#include "SimulatorSPSCQueue.hpp" +#include + +template +class SimulatorMPMCQueue: public SimulatorSPSCQueue, public MPMCQueueInterface +{ + public: + SimulatorMPMCQueue(uint32_t size): SimulatorSPSCQueue(size){}; + bool push (T obj); + bool push (T obj, bool overwrite); + std::optional pop(); + std::optional peek(); + bool isFull(); + bool isEmpty(); + private: + T* data; + uint32_t size; + uint32_t rd_index; + uint32_t wr_index; + uint32_t incrementIndex(uint32_t index); + std::mutex mtx; +}; \ No newline at end of file diff --git a/Platformio/HAL/Targets/Simulator/SimulatorSPSCQueue.cpp b/Platformio/HAL/Targets/Simulator/SimulatorSPSCQueue.cpp new file mode 100644 index 0000000..4f8bcd2 --- /dev/null +++ b/Platformio/HAL/Targets/Simulator/SimulatorSPSCQueue.cpp @@ -0,0 +1,68 @@ +#include "SimulatorSPSCQueue.hpp" + +template +SimulatorSPSCQueue::SimulatorSPSCQueue(uint32_t size) +{ + this->size = size; + this->data = new T[](this->size + 1); + this->rd_index = 0; + this->wr_index = 0; +} + +template +SimulatorSPSCQueue::~SimulatorSPSCQueue() +{ + free(this->data); +} + +template +bool SimulatorSPSCQueue::isFull() +{ + return ((this->wr_index + 1) % this->size) == this->rd_index; +} + +template +bool SimulatorSPSCQueue::isEmpty() +{ + return this->rd_index == this->wr_index; +} + +template +bool SimulatorSPSCQueue::push(T obj) +{ + bool retval = false; + if (!this->isFull()) + { + retval = true; + this->data[this->wr_index] = obj; + this->wr_index = this->incrementIndex(this->wr_index); + } + return retval; +} + +template +std::optional SimulatorSPSCQueue::pop() +{ + std::optional retval; + + retval = this->peek(); + + this->rd_index = this->incrementIndex(this->rd_index); + return retval; +} + +template +std::optional SimulatorSPSCQueue::peek() +{ + if (this->isEmpty()) + { + return std::nullopt; + } + return this->data[this->rd_index]; +} + +template +uint32_t SimulatorSPSCQueue::incrementIndex(uint32_t index) +{ + return (index + 1) % this->size; +} \ No newline at end of file diff --git a/Platformio/HAL/Targets/Simulator/SimulatorSPSCQueue.hpp b/Platformio/HAL/Targets/Simulator/SimulatorSPSCQueue.hpp new file mode 100644 index 0000000..21b94e0 --- /dev/null +++ b/Platformio/HAL/Targets/Simulator/SimulatorSPSCQueue.hpp @@ -0,0 +1,22 @@ +#pragma once +#include "SPSCQueueInterface.hpp" +#include + +template +class SimulatorSPSCQueue: public SPSCQueueInterface +{ + public: + SimulatorSPSCQueue(uint32_t size); + ~SimulatorSPSCQueue(); + bool push (T obj); + std::optional pop(); + std::optional peek(); + bool isFull(); + bool isEmpty(); + private: + T* data; + uint32_t size; + uint32_t rd_index; + uint32_t wr_index; + uint32_t incrementIndex(uint32_t index); +}; \ No newline at end of file diff --git a/Platformio/HAL/Targets/Simulator/wifiHandlerSim/wifiHandlerSim.cpp b/Platformio/HAL/Targets/Simulator/wifiHandlerSim/wifiHandlerSim.cpp index 01708ea..7534534 100644 --- a/Platformio/HAL/Targets/Simulator/wifiHandlerSim/wifiHandlerSim.cpp +++ b/Platformio/HAL/Targets/Simulator/wifiHandlerSim/wifiHandlerSim.cpp @@ -16,6 +16,10 @@ wifiHandlerSim::wifiHandlerSim(){ } +void wifiHandlerSim::begin(){ + +} + static wifiStatus status = { .isConnected = true , .IP = "172.0.0.1" diff --git a/Platformio/HAL/Targets/Simulator/wifiHandlerSim/wifiHandlerSim.hpp b/Platformio/HAL/Targets/Simulator/wifiHandlerSim/wifiHandlerSim.hpp index a318637..e994450 100644 --- a/Platformio/HAL/Targets/Simulator/wifiHandlerSim/wifiHandlerSim.hpp +++ b/Platformio/HAL/Targets/Simulator/wifiHandlerSim/wifiHandlerSim.hpp @@ -26,6 +26,7 @@ class wifiHandlerSim: public wifiHandlerInterface { */ void scan(); bool isAvailable(); + void begin(); void onScanDone(std::function>)> function); void onStatusUpdate(std::function)> function); private: diff --git a/Platformio/OmoteUI/OmoteUI.cpp b/Platformio/OmoteUI/OmoteUI.cpp index 39e1d73..703ed39 100644 --- a/Platformio/OmoteUI/OmoteUI.cpp +++ b/Platformio/OmoteUI/OmoteUI.cpp @@ -463,4 +463,5 @@ void OmoteUI::layout_UI() { lv_obj_set_size(img2, 30, 30); this->create_status_bar(); + this->mHardware->wifi()->begin(); }