From 49f89e94fd0faec3fecb3a8b06687d1b7f30fde8 Mon Sep 17 00:00:00 2001 From: Miroslav Pivovarsky Date: Mon, 13 May 2024 21:53:56 +0200 Subject: [PATCH] added test parts of the code for various errors --- ESP32_PrusaConnectCam/camera.cpp | 4 ++- ESP32_PrusaConnectCam/cfg.cpp | 48 ++++++++++++++++++++++++---- ESP32_PrusaConnectCam/connect.cpp | 4 +-- ESP32_PrusaConnectCam/connect.h | 2 +- ESP32_PrusaConnectCam/log.cpp | 2 +- ESP32_PrusaConnectCam/serial_cfg.cpp | 2 +- 6 files changed, 49 insertions(+), 13 deletions(-) diff --git a/ESP32_PrusaConnectCam/camera.cpp b/ESP32_PrusaConnectCam/camera.cpp index 8cf76b6..7cdb30f 100644 --- a/ESP32_PrusaConnectCam/camera.cpp +++ b/ESP32_PrusaConnectCam/camera.cpp @@ -277,6 +277,7 @@ void Camera::CapturePhoto() { log->AddEvent(LogLevel_Info, F("Taking photo...")); /* capture final photo */ + delay(5); // delay for camera stabilization. test it FrameBuffer = esp_camera_fb_get(); if (!FrameBuffer) { log->AddEvent(LogLevel_Error, F("Camera capture failed! photo")); @@ -476,7 +477,8 @@ void Camera::CopyPhoto(String* i_data, int i_from, int i_to) { * @return int - photo size */ int Camera::GetPhotoSize() { - return FrameBuffer->len; + log->AddEvent(LogLevel_Verbose, "Photo size: " + String(FrameBuffer->len)); + return (int) FrameBuffer->len; } /** diff --git a/ESP32_PrusaConnectCam/cfg.cpp b/ESP32_PrusaConnectCam/cfg.cpp index 029cee3..9a594d4 100644 --- a/ESP32_PrusaConnectCam/cfg.cpp +++ b/ESP32_PrusaConnectCam/cfg.cpp @@ -258,7 +258,13 @@ void Configuration::GetFingerprint() { */ void Configuration::SaveUint8(uint16_t address, uint8_t data) { EEPROM.write(address, data); - EEPROM.commit(); + + if (EEPROM.commit()) { + Log->AddEvent(LogLevel_Verbose, F("Write uint8_t done")); + } else { + Log->AddEvent(LogLevel_Error, F("Failed to write uint8_t")); + EEPROM.commit(); // try again + } } /** @info Function for save int8_t to EEPROM @@ -268,7 +274,13 @@ void Configuration::SaveUint8(uint16_t address, uint8_t data) { */ void Configuration::SaveInt8(uint16_t address, int8_t data) { EEPROM.write(address, data); - EEPROM.commit(); + + if (EEPROM.commit()) { + Log->AddEvent(LogLevel_Verbose, F("Write int8_t done")); + } else { + Log->AddEvent(LogLevel_Error, F("Failed to write int8_t")); + EEPROM.commit(); // try again + } } /** @info Function for save bool to EEPROM @@ -278,7 +290,13 @@ void Configuration::SaveInt8(uint16_t address, int8_t data) { */ void Configuration::SaveBool(uint16_t address, bool data) { EEPROM.write(address, data); - EEPROM.commit(); + + if (EEPROM.commit()) { + Log->AddEvent(LogLevel_Verbose, F("Write bool done")); + } else { + Log->AddEvent(LogLevel_Error, F("Failed to write bool")); + EEPROM.commit(); // try again + } } /** @info Function for save uint16_t to EEPROM @@ -292,7 +310,13 @@ void Configuration::SaveUint16(uint16_t address, uint16_t data) { EEPROM.write(address, highByte); EEPROM.write(address + 1, lowByte); - EEPROM.commit(); + + if (EEPROM.commit()) { + Log->AddEvent(LogLevel_Verbose, F("Write uint16_t done")); + } else { + Log->AddEvent(LogLevel_Error, F("Failed to write uint16_t")); + EEPROM.commit(); // try again + } } /** @@ -311,8 +335,13 @@ void Configuration::SaveString(uint16_t address, uint16_t max_length, String dat for (uint16_t i = address + 1, j = 0; j < data.length(); i++, j++) { EEPROM.write(i, data.charAt(j)); } - EEPROM.commit(); - Log->AddEvent(LogLevel_Verbose, F("Write string done")); + + if (EEPROM.commit()) { + Log->AddEvent(LogLevel_Verbose, F("Write string done")); + } else { + Log->AddEvent(LogLevel_Error, F("Failed to write string")); + EEPROM.commit(); // try again + } } else { Log->AddEvent(LogLevel_Verbose, F("Skip write string")); } @@ -331,7 +360,12 @@ void Configuration::SaveIpAddress(uint16_t address, String data) { EEPROM.write(address + 1, ip[1]); EEPROM.write(address + 2, ip[2]); EEPROM.write(address + 3, ip[3]); - EEPROM.commit(); + + if (EEPROM.commit()) { + Log->AddEvent(LogLevel_Verbose, F("Write IP address done")); + } else { + Log->AddEvent(LogLevel_Error, F("Failed to write IP address")); + } } } diff --git a/ESP32_PrusaConnectCam/connect.cpp b/ESP32_PrusaConnectCam/connect.cpp index 1077fd4..2eb98bf 100644 --- a/ESP32_PrusaConnectCam/connect.cpp +++ b/ESP32_PrusaConnectCam/connect.cpp @@ -78,7 +78,7 @@ bool PrusaConnect::SendDataToBackend(String *i_data, int i_data_length, String i WiFiClientSecure client; BackendReceivedStatus = ""; bool ret = false; - log->AddEvent(LogLevel_Info, "Sending " + i_type + " to PrusaConnect"); + log->AddEvent(LogLevel_Info, "Sending " + i_type + " to PrusaConnect, " + String(i_data_length) + " bytes"); /* check fingerprint and token length */ if ((Fingerprint.length() > 0) && (Token.length() > 0)) { @@ -119,7 +119,7 @@ bool PrusaConnect::SendDataToBackend(String *i_data, int i_data_length, String i log->AddEvent(LogLevel_Verbose, F("Send data photo")); int index = 0; /* send data in fragments */ - for (index = 0; index < i_data_length; index = index + PHOTO_FRAGMENT_SIZE) { + for (index = 0; index < i_data_length; index += PHOTO_FRAGMENT_SIZE) { camera->CopyPhoto(i_data, index, index + PHOTO_FRAGMENT_SIZE); client.print(*i_data); log->AddEvent(LogLevel_Verbose, String(i_data_length) + "/" + String(index)); diff --git a/ESP32_PrusaConnectCam/connect.h b/ESP32_PrusaConnectCam/connect.h index 0e0ac70..f55cc84 100644 --- a/ESP32_PrusaConnectCam/connect.h +++ b/ESP32_PrusaConnectCam/connect.h @@ -62,7 +62,7 @@ private: Logs *log; ///< pointer to logs object Camera *camera; ///< pointer to camera object - bool SendDataToBackend(String *,int, String, String, String, SendDataToBackendType); + bool SendDataToBackend(String *, int, String, String, String, SendDataToBackendType); public: PrusaConnect(Configuration*, Logs*, Camera*); diff --git a/ESP32_PrusaConnectCam/log.cpp b/ESP32_PrusaConnectCam/log.cpp index 3a46ea7..b44eeaa 100644 --- a/ESP32_PrusaConnectCam/log.cpp +++ b/ESP32_PrusaConnectCam/log.cpp @@ -253,7 +253,7 @@ String Logs::GetSystemTime() { struct tm timeinfo; if (!getLocalTime(&timeinfo)) { #if (true == CONSOLE_VERBOSE_DEBUG) - Serial.println("Failed to obtain time"); + Serial.println(F("Failed to obtain time")); #endif return ret; } diff --git a/ESP32_PrusaConnectCam/serial_cfg.cpp b/ESP32_PrusaConnectCam/serial_cfg.cpp index bff9c91..ca1147e 100644 --- a/ESP32_PrusaConnectCam/serial_cfg.cpp +++ b/ESP32_PrusaConnectCam/serial_cfg.cpp @@ -146,7 +146,7 @@ String lastTwoChars = command.substring(command.length() - 2); ESP.restart(); } else if (command.startsWith("commandslist") && command.endsWith(";")) { - log->AddEvent(LogLevel_Warning, "--> Available commands"); + log->AddEvent(LogLevel_Warning, F("--> Available commands")); PrintAvailableCommands(); } else {