Browse Source

Merge branch 'main' into fix/checkdigitconsistency-double-precision

SybexXx 1 month ago
parent
commit
59da553a5d

+ 3 - 15
code/components/jomjol_controlcamera/ClassControllCamera.cpp

@@ -710,8 +710,6 @@ esp_err_t CCamera::CaptureToBasisImage(CImageBasis *_Image, int delay)
         return ESP_OK;
     }
 
-    stbi_uc *p_target;
-    stbi_uc *p_source;
     int channels = 3;
     int width = CCstatus.ImageWidth;
     int height = CCstatus.ImageHeight;
@@ -722,19 +720,9 @@ esp_err_t CCamera::CaptureToBasisImage(CImageBasis *_Image, int delay)
     LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, _zw);
 #endif
 
-    for (int x = 0; x < width; ++x)
-    {
-        for (int y = 0; y < height; ++y)
-        {
-            p_target = _Image->rgb_image + (channels * (y * width + x));
-            p_source = _zwImage->rgb_image + (channels * (y * width + x));
-
-            for (int c = 0; c < channels; c++)
-            {
-                p_target[c] = p_source[c];
-            }
-        }
-    }
+    // Both images are packed RGB888 with the same dimensions: copy the whole frame at once
+    // (the former per-pixel, column-by-column loop took ~2 s on an ESP32-CAM)
+    memcpy(_Image->rgb_image, _zwImage->rgb_image, width * height * channels);
 
     delete _zwImage;
 

+ 2 - 8
code/components/jomjol_fileserver_ota/server_file.cpp

@@ -421,15 +421,12 @@ static esp_err_t send_datafile(httpd_req_t *req, bool send_full_file)
             return ESP_FAIL;
         }
 
-        /* Keep looping till the whole file is sent */
+        /* Keep looping till the whole file is sent (the last, empty chunk terminates the response) */
     } while (chunksize != 0);
 
     /* Close file after sending complete */
     fclose(fd);
     ESP_LOGI(TAG, "File sending complete");
-
-    /* Respond with an empty chunk to signal HTTP response completion */
-    httpd_resp_send_chunk(req, NULL, 0);
     return ESP_OK;
 }
 
@@ -515,15 +512,12 @@ static esp_err_t send_logfile(httpd_req_t *req, bool send_full_file)
             return ESP_FAIL;
         }
 
-        /* Keep looping till the whole file is sent */
+        /* Keep looping till the whole file is sent (the last, empty chunk terminates the response) */
     } while (chunksize != 0);
 
     /* Close file after sending complete */
     fclose(fd);
     ESP_LOGD(TAG, "File sending complete");
-
-    /* Respond with an empty chunk to signal HTTP response completion */
-    httpd_resp_send_chunk(req, NULL, 0);
     return ESP_OK;
 }
 

+ 0 - 5
code/components/jomjol_fileserver_ota/server_ota.cpp

@@ -385,8 +385,6 @@ esp_err_t handler_ota_update(httpd_req_t *req)
 
         httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
         httpd_resp_send(req, zw.c_str(), strlen(zw.c_str()));
-        /* Respond with an empty chunk to signal HTTP response completion */
-        httpd_resp_send_chunk(req, NULL, 0);
 
         ESP_LOGD(TAG, "Done empty directory /firmware");
         return ESP_OK;
@@ -448,7 +446,6 @@ esp_err_t handler_ota_update(httpd_req_t *req)
         unzip_file(in, out + "/");
         std::string zw = "Web Interface Update Successfull!\nNo reboot necessary";
         httpd_resp_send(req, zw.c_str(), strlen(zw.c_str()));
-        httpd_resp_sendstr_chunk(req, NULL);
         return ESP_OK;
     }
 
@@ -474,14 +471,12 @@ esp_err_t handler_ota_update(httpd_req_t *req)
         std::string zw = "file deleted\n";
         ESP_LOGD(TAG, "%s", zw.c_str());
         httpd_resp_send(req, zw.c_str(), strlen(zw.c_str()));
-        httpd_resp_send_chunk(req, NULL, 0);
         return ESP_OK;
     }
 
     std::string zw = "ota without parameter - should not be the case!";
     httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
     httpd_resp_send(req, zw.c_str(), strlen(zw.c_str()));
-    httpd_resp_send_chunk(req, NULL, 0);
 
     LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "ota without parameter - should not be the case!");
 

+ 4 - 3
code/components/jomjol_flowcontroll/ClassFlowCNNGeneral.cpp

@@ -8,7 +8,9 @@
 #include "CTfLiteClass.h"
 #include "ClassLogFile.h"
 #include "esp_log.h"
+
 #include "../../include/defines.h"
+#include "Helper.h"
 
 static const char* TAG = "CNN";
 
@@ -671,8 +673,7 @@ bool ClassFlowCNNGeneral::doNeuralNetwork(string time) {
                 case Analogue:
                     LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "CNN Type: Analogue");
                     {
-                        float f1, f2;
-                        f1 = 0; f2 = 0;
+                        float f1 = 0, f2 = 0;
 
                         tflite->LoadInputImageBasis(GENERAL[n]->ROI[roi]->image);        
                         tflite->Invoke();
@@ -680,7 +681,7 @@ bool ClassFlowCNNGeneral::doNeuralNetwork(string time) {
 
                         f1 = tflite->GetOutputValue(0);
                         f2 = tflite->GetOutputValue(1);
-                        float result = fmod(atan2(f1, f2) / (M_PI * 2) + 2, 1);
+                        float result = fmod(fast_atan2(f1, f2) / (M_PI * 2) + 2, 1);
                               
                         if(GENERAL[n]->ROI[roi]->CCW) {
                             GENERAL[n]->ROI[roi]->result_float = 10 - (result * 10);

+ 12 - 11
code/components/jomjol_flowcontroll/ClassFlowPostProcessing.cpp

@@ -1,5 +1,4 @@
 #include "ClassFlowPostProcessing.h"
-#include "Helper.h"
 #include "ClassFlowTakeImage.h"
 #include "ClassLogFile.h"
 
@@ -11,7 +10,9 @@
 #include "time_sntp.h"
 
 #include "esp_log.h"
+
 #include "../../include/defines.h"
+#include "Helper.h"
 
 static const char* TAG = "POSTPROC";
 
@@ -894,8 +895,8 @@ bool ClassFlowPostProcessing::doFlow(string zwtime) {
 
         if (PreValueUse && NUMBERS[j]->PreValueOkay) {
             if ((NUMBERS[j]->Nachkomma > 0) && (NUMBERS[j]->ChangeRateThreshold > 0)) {
-                double _difference1 = (NUMBERS[j]->PreValue - (NUMBERS[j]->ChangeRateThreshold / pow(10, NUMBERS[j]->Nachkomma)));
-                double _difference2 = (NUMBERS[j]->PreValue + (NUMBERS[j]->ChangeRateThreshold / pow(10, NUMBERS[j]->Nachkomma)));
+                double _difference1 = (NUMBERS[j]->PreValue - (NUMBERS[j]->ChangeRateThreshold / pow10_double(NUMBERS[j]->Nachkomma)));
+                double _difference2 = (NUMBERS[j]->PreValue + (NUMBERS[j]->ChangeRateThreshold / pow10_double(NUMBERS[j]->Nachkomma)));
 
                 if ((NUMBERS[j]->Value >= _difference1) && (NUMBERS[j]->Value <= _difference2)) {
                     NUMBERS[j]->Value = NUMBERS[j]->PreValue;
@@ -911,7 +912,7 @@ bool ClassFlowPostProcessing::doFlow(string zwtime) {
                     if (NUMBERS[j]->isExtendedResolution) {
                         LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Neg: value=" + std::to_string(NUMBERS[j]->Value) 
                                                     + ", preValue=" + std::to_string(NUMBERS[j]->PreValue) 
-                                                    + ", preToll=" + std::to_string(NUMBERS[j]->PreValue-(2/pow(10, NUMBERS[j]->Nachkomma))));
+                                                    + ", preToll=" + std::to_string(NUMBERS[j]->PreValue - (2 / pow10_double(NUMBERS[j]->Nachkomma))));
                     } 
 
                     NUMBERS[j]->ErrorMessageText = NUMBERS[j]->ErrorMessageText + "Neg. Rate - Read: " + zwvalue + " - Raw: " + NUMBERS[j]->ReturnRawValue + " - Pre: " + RundeOutput(NUMBERS[j]->PreValue, NUMBERS[j]->Nachkomma) + " "; 
@@ -1096,7 +1097,7 @@ string ClassFlowPostProcessing::ErsetzteN(string input, double _prevalue) {
             pot = posPunkt - posN;
         }
 
-        zw =_prevalue / pow(10, pot);
+        zw =_prevalue / pow10_double(pot);
         ziffer = ((int) zw) % 10;
         input[posN] = ziffer + 48;
 
@@ -1128,27 +1129,27 @@ double ClassFlowPostProcessing::checkDigitConsistency(double input, int _decilam
     pot_max = ((int) log10(input)) + 1;
 	
     while (pot <= pot_max) {
-        zw = input / pow(10, pot-1);
+        zw = input / pow10_double(pot-1);
         aktdigit_before = ((int) zw) % 10;
-        zw = _preValue / pow(10, pot-1);
+        zw = _preValue / pow10_double(pot-1);
         olddigit_before = ((int) zw) % 10;
 
-        zw = input / pow(10, pot);
+        zw = input / pow10_double(pot);
         aktdigit = ((int) zw) % 10;
-        zw = _preValue / pow(10, pot);
+        zw = _preValue / pow10_double(pot);
         olddigit = ((int) zw) % 10;
 
         no_nulldurchgang = (olddigit_before <= aktdigit_before);
 
         if (no_nulldurchgang) {
             if (aktdigit != olddigit) {
-                input = input + ((double) (olddigit - aktdigit)) * pow(10, pot);     // New Digit is replaced by old Digit;
+                input = input + ((double)(olddigit - aktdigit)) * pow10_double(pot);     // New Digit is replaced by old Digit;
             }
         }
         else {
             // despite zero crossing, digit was not incremented --> add 1
             if (aktdigit == olddigit) {
-                input = input + ((double) (1)) * pow(10, pot);   // add 1 at the point
+                input = input + ((double)(1)) * pow10_double(pot);   // add 1 at the point
             }
         }
 			

+ 0 - 3
code/components/jomjol_flowcontroll/MainFlowControl.cpp

@@ -1514,9 +1514,6 @@ esp_err_t handler_current_date(httpd_req_t *req)
     httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
     httpd_resp_send(req, formatedDateAndTime.c_str(), formatedDateAndTime.length());
 
-    /* Respond with an empty chunk to signal HTTP response completion */
-    httpd_resp_sendstr_chunk(req, NULL);
-
 #ifdef DEBUG_DETAIL_ON
     LogFile.WriteHeapInfo("handler_uptime - End");
 #endif

+ 126 - 0
code/components/jomjol_helper/Helper.cpp

@@ -180,6 +180,132 @@ string getSDCardSectorSize()
 
 ///////////////////////////////////////////////////////////////////////////////////////////////
 
+bool copy_string(char *dst, size_t dst_size, const char *src)
+{
+	if (dst == nullptr || src == nullptr || dst_size == 0)
+	{
+		return false;
+	}
+
+	size_t len = strlen(src);
+
+	if (len >= dst_size)
+	{
+		dst[0] = '\0';
+		return false; // Target too small
+	}
+
+	memcpy(dst, src, len + 1);
+	return true;
+}
+
+void mem_copy(uint8_t *_target, uint8_t *_source, int _size)
+{
+	if ((_target != nullptr) && (_source != nullptr) && (_size > 0))
+	{
+		uint8_t *dst = _target;
+		uint8_t *src = _source;
+
+		for (int i = 0; i < _size; ++i)
+		{
+			*dst++ = *src++;
+		}
+	}
+}
+
+void mem_copy32(void *_target, const void *_source, size_t _size)
+{
+	if ((_target != nullptr) && (_source != nullptr) && (_size > 0))
+	{
+		uint32_t *d32 = (uint32_t *)_target;
+		const uint32_t *s32 = (const uint32_t *)_source;
+
+		size_t words = _size / 4;
+
+		for (size_t i = 0; i < words; i++)
+		{
+			d32[i] = s32[i];
+		}
+
+		// Copy remaining bytes
+		uint8_t *d8 = (uint8_t *)(d32 + words);
+		const uint8_t *s8 = (const uint8_t *)(s32 + words);
+
+		for (size_t i = 0; i < (_size % 4); i++)
+		{
+			d8[i] = s8[i];
+		}
+	}
+}
+
+float fast_atan2(float y, float x)
+{
+	float abs_y = fabs(y) + 1e-10;
+	float r = (x - abs_y) / (x + abs_y);
+	float angle = 0.78539816339 - 0.78539816339 * r;
+	return (y < 0) ? -angle : angle;
+}
+
+double pow10_double(int pot)
+{
+	static const double pow10_table[] = {
+		1.0,
+		10.0,
+		100.0,
+		1000.0,
+		10000.0,
+		100000.0,
+		1000000.0,
+		10000000.0,
+		100000000.0,
+		1000000000.0};
+
+	if (pot >= 0 && pot < (int)(sizeof(pow10_table) / sizeof(pow10_table[0])))
+	{
+		return pow10_table[pot];
+	}
+
+	// Fallback if pot is larger
+	double result = 1.0;
+	for (int i = 0; i < pot; ++i)
+	{
+		result *= 10.0;
+	}
+
+	return result;
+}
+
+uint32_t pow10_int(int pot)
+{
+	// Lookup table for small exponents 0-9
+	static const uint32_t table[] = {
+		1,		   // 10^0
+		10,		   // 10^1
+		100,	   // 10^2
+		1000,	   // 10^3
+		10000,	   // 10^4
+		100000,	   // 10^5
+		1000000,   // 10^6
+		10000000,  // 10^7
+		100000000, // 10^8
+		1000000000 // 10^9
+	};
+
+	if (pot >= 0 && pot < (int)(sizeof(table) / sizeof(table[0])))
+	{
+		return table[pot];
+	}
+
+	// Fallback for larger pot
+	uint32_t result = 1;
+	for (int i = 0; i < pot; ++i)
+	{
+		result *= 10;
+	}
+
+	return result;
+}
+
 void memCopyGen(uint8_t *_source, uint8_t *_target, int _size)
 {
 	for (int i = 0; i < _size; ++i)

+ 8 - 0
code/components/jomjol_helper/Helper.h

@@ -45,6 +45,14 @@ float temperatureRead();
 std::string intToHexString(int _valueInt);
 time_t addDays(time_t startTime, int days);
 
+bool copy_string(char *dst, size_t dst_size, const char *src);
+void mem_copy(uint8_t *_target, uint8_t *_source, int _size);
+void mem_copy32(void *_target, const void *_source, size_t size);
+float fast_atan2(float y, float x);
+
+double pow10_double(int pot); // faster than pow(10, pot)
+uint32_t pow10_int(int pot);  // faster than pow(10, pot)
+
 void memCopyGen(uint8_t* _source, uint8_t* _target, int _size);
 
 std::vector<string> HelperZerlegeZeile(std::string input, std::string _delimiter);

+ 4 - 2
code/components/jomjol_image_proc/CAlignAndCutImage.cpp

@@ -6,7 +6,9 @@
 #include <algorithm>
 #include <esp_log.h>
 #include "psram.h"
+
 #include "../../include/defines.h"
+#include "Helper.h"
 
 static const char* TAG = "c_align_and_cut_image";
 
@@ -69,8 +71,8 @@ bool CAlignAndCutImage::Align(RefInfo *_temp1, RefInfo *_temp2)
 
     float w_org, w_ist, d_winkel;
 
-    w_org = atan2(_temp2->found_y - _temp1->found_y, _temp2->found_x - _temp1->found_x);
-    w_ist = atan2(r1_y - r0_y, r1_x - r0_x);
+    w_org = fast_atan2((float)(_temp2->found_y - _temp1->found_y), (float)(_temp2->found_x - _temp1->found_x));
+    w_ist = fast_atan2((float)(r1_y - r0_y), (float)(r1_x - r0_x));
 
     d_winkel = (w_ist - w_org) * 180 / M_PI;
 

+ 4 - 18
code/components/jomjol_image_proc/CImageBasis.cpp

@@ -398,15 +398,8 @@ void CImageBasis::CreateEmptyImage(int _width, int _height, int _channels)
         return;
     }
 
-    stbi_uc* p_source;    
-
-    for (int x = 0; x < width; ++x)
-        for (int y = 0; y < height; ++y)
-        {
-            p_source = rgb_image + (channels * (y * width + x));
-            for (int _channels = 0; _channels < channels; ++_channels)
-                p_source[_channels] = (uint8_t) 0;
-        }
+    // One contiguous memset instead of a per-pixel, column-by-column loop through PSRAM
+    memset(rgb_image, 0, width * height * channels);
 
     RGBImageRelease();
 }
@@ -418,17 +411,10 @@ void CImageBasis::EmptyImage()
         LogFile.WriteHeapInfo("EmptyImage");
     #endif
 
-    stbi_uc* p_source;
-
     RGBImageLock();
 
-    for (int x = 0; x < width; ++x)
-        for (int y = 0; y < height; ++y)
-        {
-            p_source = rgb_image + (channels * (y * width + x));
-            for (int _channels = 0; _channels < channels; ++_channels)
-                p_source[_channels] = (uint8_t) 0;
-        }
+    // One contiguous memset instead of a per-pixel, column-by-column loop through PSRAM
+    memset(rgb_image, 0, width * height * channels);
 
     RGBImageRelease();
 }

+ 3 - 10
code/main/server_main.cpp

@@ -290,9 +290,7 @@ esp_err_t hello_main_handler(httpd_req_t *req)
         return ESP_FAIL;
     }
 
-    res = send_file(req, filetosend);
-    /* Respond with an empty chunk to signal HTTP response completion */
-    httpd_resp_send_chunk(req, NULL, 0);
+    res = send_file(req, filetosend);   // send_file() already terminates the chunked response
 
     if (res != ESP_OK)
         return res;
@@ -324,13 +322,8 @@ esp_err_t img_tmp_handler(httpd_req_t *req)
     filetosend = filetosend + "/img_tmp/" + std::string(filename);
     ESP_LOGD(TAG, "File to upload: %s", filetosend.c_str());
 
-    esp_err_t res = send_file(req, filetosend); 
-    if (res != ESP_OK)
-        return res;
-
-    /* Respond with an empty chunk to signal HTTP response completion */
-    httpd_resp_send_chunk(req, NULL, 0);
-    return ESP_OK;
+    // send_file() already terminates the chunked response
+    return send_file(req, filetosend);
 }