diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 3a84a22..78a82f6 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -9,4 +9,4 @@ build: script: - cd firmware - platformio run - #- platformio test -v -e native + - platformio test -v -e native diff --git a/firmware/dat/.dont_remove b/firmware/dat/.dont_remove new file mode 100644 index 0000000..e69de29 diff --git a/firmware/lib/basic/Dtypes.h b/firmware/lib/basic/Dtypes.h index 3fd8b21..5d83f8f 100644 --- a/firmware/lib/basic/Dtypes.h +++ b/firmware/lib/basic/Dtypes.h @@ -1,6 +1,12 @@ #pragma once -inline void _assert(const char* expression, const char* message, const char* file, int line) +#ifndef PLATFORM_NATIVE + +// ---------------------------------- Arduino ------------------------------------------------------------- + +#include + +inline void _assert(const char *expression, const char *message, const char *file, int line) { Serial.print("Assert "); Serial.print(file); @@ -12,9 +18,58 @@ inline void _assert(const char* expression, const char* message, const char* fil Serial.println(message); } -template< typename T> -inline String toString(const T & t) { +template +inline String toString(const T &t) +{ return String(t); } #define assert_msg(EXPRESSION, MSG) ((EXPRESSION) ? (void)0 : _assert(#EXPRESSION, #MSG, __FILE__, __LINE__)) + +#else +// ---------------------------------- Native ----------------------------------------------------------------- + +#include +#include +#include +#include +#include +#include +#include +#include + +typedef uint32_t uint_t; + +typedef std::string String; +typedef uint8_t byte; +typedef const char *PGM_P; + +using std::max; +using std::min; + +inline uint32_t strlen_P(PGM_P str) +{ + return std::strlen(str); +} + +inline const char *F(const char *in) { return in; } + +inline void _assert(const char *expression, const char *message, const char *file, int line) +{ + std::cerr << "Assert " << file << ":" << line << " '" << expression << "' failed." << std::endl; + std::cerr << message << std::endl; + + abort(); +} + +template +inline std::string toString(const T &t) +{ + std::stringstream stream; + stream << t; + return stream.str(); +} + +#define assert_msg(EXPRESSION, MSG) ((EXPRESSION) ? (void)0 : _assert(#EXPRESSION, #MSG, __FILE__, __LINE__)) + +#endif \ No newline at end of file diff --git a/firmware/lib/basic/FilesystemAbstraction.h b/firmware/lib/basic/FilesystemAbstraction.h index e89b898..790f5b8 100644 --- a/firmware/lib/basic/FilesystemAbstraction.h +++ b/firmware/lib/basic/FilesystemAbstraction.h @@ -1,5 +1,7 @@ #pragma once +#include "Dtypes.h" + #ifdef PLATFORM_ESP32 #include "SPIFFS.h" @@ -93,7 +95,6 @@ namespace portablefs #include #include #include -#include "MockDtypes.h" namespace fs = std::filesystem; diff --git a/firmware/lib/basic/MockDtypes.h b/firmware/lib/basic/MockDtypes.h deleted file mode 100644 index 857d1e1..0000000 --- a/firmware/lib/basic/MockDtypes.h +++ /dev/null @@ -1,46 +0,0 @@ -#pragma once - -#include -#include -#include -#include -#include -#include -#include -#include - -typedef uint32_t uint_t; - -typedef std::string String; -typedef uint8_t byte; -typedef const char * PGM_P; - -using std::min; -using std::max; - -inline uint32_t strlen_P(PGM_P str) { - return std::strlen(str); -} - -inline const char * F( const char * in) { return in; } - - -inline void _assert(const char* expression, const char* message, const char* file, int line) -{ - std::cerr << "Assert " << file << ":" << line << " '" << expression << "' failed." << std::endl; - std::cerr << message << std::endl; - - abort(); -} - -template< typename T> -inline std::string toString(const T & t) { - std::stringstream stream; - stream << t; - return stream.str(); -} - -class String : public std::string -{}; - -#define assert(EXPRESSION, MSG) ((EXPRESSION) ? (void)0 : _assert(#EXPRESSION, #MSG, __FILE__, __LINE__)) \ No newline at end of file diff --git a/firmware/lib/scale/Scale.h b/firmware/lib/scale/Scale.h index 109723e..55748d7 100644 --- a/firmware/lib/scale/Scale.h +++ b/firmware/lib/scale/Scale.h @@ -11,8 +11,8 @@ public: if (hx711_.is_ready()) { long value = hx711_.read_average(CONFIG_MEASUREMENT_AVG_COUNT) - offset_; - if(value > 0) - measurementOut = (int16_t)(value / DIVIDER); + if(value < 0) + measurementOut = (int16_t)(-value / DIVIDER); else measurementOut = 0; return true; diff --git a/firmware/lib/session/MeasurementSession.h b/firmware/lib/session/MeasurementSession.h index ef99e1b..a28e38b 100644 --- a/firmware/lib/session/MeasurementSession.h +++ b/firmware/lib/session/MeasurementSession.h @@ -126,7 +126,7 @@ private: } static String chunkFileName(uint32_t chunkNr, uint32_t startTime) { - return("/dat/" + toString(startTime) + "_" + toString(chunkNr)); + return("/dat/" + toString(startTime) + String("_") + toString(chunkNr)); } Chunk_T chunks[2]; diff --git a/firmware/lib/userdb/UserDB.h b/firmware/lib/userdb/UserDB.h index cd866d9..30b5247 100644 --- a/firmware/lib/userdb/UserDB.h +++ b/firmware/lib/userdb/UserDB.h @@ -1,9 +1,6 @@ #pragma once -#ifndef PLATFORM_NATIVE -#include -#endif - +#include "Dtypes.h" using SessionIdType = uint32_t; constexpr size_t MAX_USERS = 64; diff --git a/firmware/lib/webdav/AsyncWebDav.h b/firmware/lib/webdav/AsyncWebDav.h index e2a9e5a..a45fbd4 100644 --- a/firmware/lib/webdav/AsyncWebDav.h +++ b/firmware/lib/webdav/AsyncWebDav.h @@ -41,8 +41,7 @@ public: size_t operator()(uint8_t *buffer, size_t maxLen, size_t index) { - Serial.print("index "); - Serial.println(index); + Serial.printf("%ld index %u\n", millis(), index); using namespace webdav_constants; uint8_t *bufferStart = buffer; @@ -57,6 +56,7 @@ public: } bool fileFound = false; + Serial.printf("%ld (0)\n", millis()); while (dir_.next()) { if (isFirstFileOfTrainingGroup()) @@ -71,10 +71,12 @@ public: //toBuffer(buffer, path_.c_str()); toBuffer(buffer, RESPONSE_START); toBuffer(buffer, HREF_START); + Serial.printf("%ld (1)\n", millis()); const auto fileName = dir_.fileName(); const auto fileNameWithoutDir = fileName.substring(fileName.lastIndexOf("/") + 1); String fileBaseName = fileNameWithoutDir.substring(0, fileNameWithoutDir.indexOf('_')); fileBaseName += ".st"; + Serial.printf("%ld (2)\n", millis()); toBuffer(buffer, fileBaseName.c_str()); toBuffer(buffer, HREF_END); toBuffer(buffer, PROPSTAT_START); @@ -89,9 +91,11 @@ public: { toBuffer(buffer, CONTENTLEN_START); String fileSizeStr(getFileSize(fileName)); - toBuffer(buffer, fileSizeStr.c_str()); + //toBuffer(buffer, fileSizeStr.c_str()); + toBuffer(buffer, "1"); toBuffer(buffer, CONTENTLEN_END); } + Serial.printf("%ld (3)\n", millis()); toBuffer(buffer, PROP_END); toBuffer(buffer, STATUS_OK); @@ -104,10 +108,10 @@ public: finished_ = true; } + size_t bytesWritten = buffer - bufferStart; assert_msg(bytesWritten < maxLen, "Written too much!"); - //Serial.print("Bytes written "); - //Serial.println(bytesWritten); + Serial.printf("%ld Bytes written %u\n", millis(), bytesWritten); //Serial.print("Max bytes "); //Serial.println(maxLen); return bytesWritten; diff --git a/firmware/src/ConfigHardware.h b/firmware/src/ConfigHardware.h index ff6978f..c32eda7 100644 --- a/firmware/src/ConfigHardware.h +++ b/firmware/src/ConfigHardware.h @@ -3,10 +3,10 @@ #include -//#define _HW_V_20 +#define _HW_V_20 // HX711 load cell -#ifdef USE_ESP32 +#ifdef PLATFORM_ESP32 #ifdef _HW_V_20 const int CONFIG_SCALE_DOUT_PIN = 23; @@ -20,10 +20,10 @@ const int CONFIG_SCALE_SCK_PIN = 23; const int CONFIG_SCALE_DOUT_PIN = D2; const int CONFIG_SCALE_SCK_PIN = D3; #endif -const uint8_t CONFIG_MEASUREMENT_AVG_COUNT = 1; // number of measurements in normal phase -const uint8_t CONFIG_TARE_AVG_COUNT = 6; // number of measurements in tare-phase (to find 0 ) -const int CONFIG_MEASURE_DELAY = 100; // interval in ms between measurements +const uint8_t CONFIG_MEASUREMENT_AVG_COUNT = 1; // number of measurements in normal phase +const uint8_t CONFIG_TARE_AVG_COUNT = 6; // number of measurements in tare-phase (to find 0 ) +const int CONFIG_MEASURE_DELAY = 100; // interval in ms between measurements //const int CONFIG_VALUE_DIVIDER = 8; // uint32 measurements are divided by this factor, before stored in uint16_t -const int CONFIG_VALUE_DIVIDER = 256; // uint32 measurements are divided by this factor, before stored in uint16_t +const int CONFIG_VALUE_DIVIDER = 128; // uint32 measurements are divided by this factor, before stored in uint16_t const uint32_t CONFIG_SESSION_CHUNK_SIZE = 1024; //1024*8 - 16 * sizeof(uint32_t); diff --git a/firmware/src/firmware_main.cpp b/firmware/src/firmware_main.cpp index 1081287..ef25943 100644 --- a/firmware/src/firmware_main.cpp +++ b/firmware/src/firmware_main.cpp @@ -1,8 +1,8 @@ -#define USE_ESP32 // Arduino & ESP headers -#include -#ifdef USE_ESP32 +#include "Dtypes.h" + +#ifdef PLATFORM_ESP32 #include #else #include @@ -13,7 +13,6 @@ #include // for NTP // Own libs -#include "Dtypes.h" #include "MockScale.h" #include "Scale.h" #include "MeasurementSession.h" @@ -207,7 +206,7 @@ void setup() // WiFi WiFi.mode(WIFI_STA); WiFi.begin(CONFIG_WIFI_SSID, CONFIG_WIFI_PASSWORD); - #ifdef USE_ESP32 + #ifdef PLATFORM_ESP32 WiFi.setHostname(CONFIG_HOSTNAME); #else WIFI.hostname(CONFIG_HOSTNAME); @@ -232,6 +231,10 @@ void setup() Serial.println("Spiffs listing:"); listDir(SPIFFS, "/", 3); + + // + Serial.print("Free Heap"); + Serial.println(ESP.getFreeHeap()); } void loop() { diff --git a/firmware/test/test_common/sessiontest.cpp b/firmware/test/test_common/sessiontest.cpp index bfceac3..6ea066e 100644 --- a/firmware/test/test_common/sessiontest.cpp +++ b/firmware/test/test_common/sessiontest.cpp @@ -1,4 +1,4 @@ -#include "MockDtypes.h" +#include "Dtypes.h" #include "MockSerial.h" #include "MeasurementSession.h" #include "MockStorage.h" diff --git a/hardware/calibration.txt b/hardware/calibration.txt new file mode 100644 index 0000000..ed30f93 --- /dev/null +++ b/hardware/calibration.txt @@ -0,0 +1,6 @@ +Old load cell + const int CONFIG_VALUE_DIVIDER = 128; + 1kg ~= 700 + => overflow at 93 kg + +