LCDOTA Scan

This commit is contained in:
Siwat Sirichai 2024-01-16 22:08:13 +07:00
parent 9b44d1653a
commit 9c25fca3c3
4 changed files with 41 additions and 9 deletions

View File

@ -507,8 +507,12 @@ void ESPMegaDisplay::reset()
* @brief Constructor for the ESPMegaDisplay class. * @brief Constructor for the ESPMegaDisplay class.
* @param displayAdapter The serial adapter connected to the display. * @param displayAdapter The serial adapter connected to the display.
*/ */
ESPMegaDisplay::ESPMegaDisplay(HardwareSerial *displayAdapter) ESPMegaDisplay::ESPMegaDisplay(HardwareSerial *displayAdapter, uint16_t baudRate, uint16_t uploadBaudRate, uint8_t txPin, uint8_t rxPin)
{ {
this->baudRate = baudRate;
this->uploadBaudRate = uploadBaudRate;
this->txPin = txPin;
this->rxPin = rxPin;
this->serialMutex = xSemaphoreCreateMutex(); this->serialMutex = xSemaphoreCreateMutex();
this->otaBytesWritten = 0; this->otaBytesWritten = 0;
this->displayAdapter = displayAdapter; this->displayAdapter = displayAdapter;
@ -526,6 +530,7 @@ void ESPMegaDisplay::begin()
ESP_LOGE("ESPMegaDisplay", "Failed to take serial mutex"); ESP_LOGE("ESPMegaDisplay", "Failed to take serial mutex");
return; return;
} }
this->displayAdapter->begin(this->baudRate, SERIAL_8N1, this->rxPin, this->txPin);
this->displayAdapter->setTimeout(100); this->displayAdapter->setTimeout(100);
this->displayAdapter->flush(); this->displayAdapter->flush();
xSemaphoreGive(this->serialMutex); xSemaphoreGive(this->serialMutex);
@ -629,12 +634,30 @@ void ESPMegaDisplay::giveSerialMutex()
xSemaphoreGive(this->serialMutex); xSemaphoreGive(this->serialMutex);
} }
bool ESPMegaDisplay::beginUpdate(size_t size)
{
// The display's baudrate might be stuck at 9600 if the display is not initialized
// We try to initiate the display at the user specified baud rate first, if it fails, we try again at 9600
if (!beginUpdate(size, uploadBaudRate))
{
ESP_LOGW("ESPMegaDisplay", "Failed to initiate LCD update at %d baud, retrying at 9600 baud.", uploadBaudRate);
if (!beginUpdate(size, 9600))
{
ESP_LOGE("ESPMegaDisplay", "Failed to initiate LCD update at 9600 baud.");
return false;
}
}
return true;
}
/** /**
* @brief Starts an OTA update. * @brief Starts an OTA update.
* @param size The size of the update. * @param size The size of the update.
* @param baudRate The baud rate use to connect to the display to initiate the update.
* @note The baud rate that is used to transfer the data is defined by the uploadBaudRate parameter in the constructor.
* @return True if the OTA update is started, false otherwise. * @return True if the OTA update is started, false otherwise.
*/ */
bool ESPMegaDisplay::beginUpdate(size_t size) bool ESPMegaDisplay::beginUpdate(size_t size, uint16_t baudRate)
{ {
if (xSemaphoreTake(this->serialMutex, DISPLAY_MUTEX_TAKE_TIMEOUT) == pdFALSE) if (xSemaphoreTake(this->serialMutex, DISPLAY_MUTEX_TAKE_TIMEOUT) == pdFALSE)
{ {
@ -644,6 +667,7 @@ bool ESPMegaDisplay::beginUpdate(size_t size)
ESP_LOGD("ESPMegaDisplay", "LCD OTA Subroutine has taken the serial mutex, all other tasks will be blocked until the OTA update is complete."); ESP_LOGD("ESPMegaDisplay", "LCD OTA Subroutine has taken the serial mutex, all other tasks will be blocked until the OTA update is complete.");
// We have taken the serial mutex, all helper functions will be blocked until the OTA update is complete // We have taken the serial mutex, all helper functions will be blocked until the OTA update is complete
// Thus, we have to interact directly with the display adapter // Thus, we have to interact directly with the display adapter
this->displayAdapter->begin(baudRate, SERIAL_8N1, this->rxPin, this->txPin);
this->sendStopBytes(); this->sendStopBytes();
this->displayAdapter->print("rest"); this->displayAdapter->print("rest");
this->sendStopBytes(); this->sendStopBytes();
@ -656,9 +680,11 @@ bool ESPMegaDisplay::beginUpdate(size_t size)
this->displayAdapter->read(); this->displayAdapter->read();
this->displayAdapter->print("whmi-wri "); this->displayAdapter->print("whmi-wri ");
this->displayAdapter->print(size); this->displayAdapter->print(size);
this->displayAdapter->print(",921600,res0"); this->displayAdapter->print(",");
this->displayAdapter->print(uploadBaudRate);
this->displayAdapter->print(",res0");
this->sendStopBytes(); this->sendStopBytes();
this->displayAdapter->begin(921600); this->displayAdapter->begin(uploadBaudRate, SERIAL_8N1, this->rxPin, this->txPin);
delay(1000); delay(1000);
// If the display is ready, it will send a 0x05 byte // If the display is ready, it will send a 0x05 byte
// If it does, return true, otherwise return false // If it does, return true, otherwise return false
@ -678,6 +704,7 @@ bool ESPMegaDisplay::beginUpdate(size_t size)
while (this->displayAdapter->available()) while (this->displayAdapter->available())
this->displayAdapter->read(); this->displayAdapter->read();
ESP_LOGE("ESPMegaDisplay", "LCD Update Subroutine failed to initialize."); ESP_LOGE("ESPMegaDisplay", "LCD Update Subroutine failed to initialize.");
xSemaphoreGive(this->serialMutex);
return false; return false;
} }
@ -745,7 +772,7 @@ void ESPMegaDisplay::endUpdate()
* @brief Gets the number of bytes written during an OTA update. * @brief Gets the number of bytes written during an OTA update.
* @return The number of bytes written. * @return The number of bytes written.
*/ */
size_t ESPMegaDisplay::getOtaBytesWritten() size_t ESPMegaDisplay::getUpdateBytesWritten()
{ {
return this->otaBytesWritten; return this->otaBytesWritten;
} }

View File

@ -17,7 +17,7 @@
class ESPMegaDisplay class ESPMegaDisplay
{ {
public: public:
ESPMegaDisplay(HardwareSerial *displayAdapter); ESPMegaDisplay(HardwareSerial *displayAdapter, uint16_t baudRate, uint16_t uploadBaudRate, uint8_t txPin, uint8_t rxPin);
void begin(); void begin();
void loop(); void loop();
void reset(); void reset();
@ -39,10 +39,15 @@ class ESPMegaDisplay
void giveSerialMutex(); void giveSerialMutex();
SemaphoreHandle_t serialMutex; SemaphoreHandle_t serialMutex;
bool beginUpdate(size_t size); bool beginUpdate(size_t size);
bool beginUpdate(size_t size, uint16_t baudRate);
bool writeUpdate(uint8_t* data, size_t size); bool writeUpdate(uint8_t* data, size_t size);
void endUpdate(); void endUpdate();
size_t getOtaBytesWritten(); size_t getUpdateBytesWritten();
protected: protected:
uint16_t baudRate;
uint16_t uploadBaudRate;
uint8_t txPin;
uint8_t rxPin;
size_t otaBytesWritten; size_t otaBytesWritten;
uint8_t currentPage; uint8_t currentPage;
uint8_t rx_buffer_index; uint8_t rx_buffer_index;

View File

@ -86,7 +86,7 @@ void ESPMegaDisplayOTA::otaUpdateWriteHandler(AsyncWebServerRequest *request, Js
} }
// Write the data to the display // Write the data to the display
display->writeUpdate(data_array, size); display->writeUpdate(data_array, size);
request->send(200, "application/json", "{\"status\": \"success\",\"bytes_written\": "+String(this->display->getOtaBytesWritten())+"}"); request->send(200, "application/json", "{\"status\": \"success\",\"bytes_written\": "+String(this->display->getUpdateBytesWritten())+"}");
} }
void ESPMegaDisplayOTA::otaUpdateEndHandler(AsyncWebServerRequest *request, JsonVariant &json) { void ESPMegaDisplayOTA::otaUpdateEndHandler(AsyncWebServerRequest *request, JsonVariant &json) {
this->webServer->checkAuthentication(request); this->webServer->checkAuthentication(request);

View File

@ -479,7 +479,7 @@ void InternalDisplay::setInputMarker(uint8_t pin, bool state)
* *
* @param displayAdapter The HardwareSerial object that is connected to the display * @param displayAdapter The HardwareSerial object that is connected to the display
*/ */
InternalDisplay::InternalDisplay(HardwareSerial *displayAdapter) : ESPMegaDisplay(displayAdapter) InternalDisplay::InternalDisplay(HardwareSerial *displayAdapter) : ESPMegaDisplay(displayAdapter, 115200, 921600, 1, 3)
{ {
this->currentPage = INTERNAL_DISPLAY_DASHBOARD_PAGE; this->currentPage = INTERNAL_DISPLAY_DASHBOARD_PAGE;
this->iot = nullptr; this->iot = nullptr;