diff --git a/src/config.cpp b/src/config.cpp index 359031a..60a8803 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -16,7 +16,6 @@ const uint8_t LOADCELL_DOUT_PIN = 16; //16; const uint8_t LOADCELL_SCK_PIN = 17; //17; const uint8_t calVal_eepromAdress = 0; const uint16_t SCALE_LEVEL_WEIGHT = 500; -uint16_t defaultScaleCalibrationValue = 430; // ***** HX711 // ***** TTP223 (Touch Sensor) diff --git a/src/config.h b/src/config.h index 88c57c0..a6468a7 100644 --- a/src/config.h +++ b/src/config.h @@ -22,6 +22,7 @@ #define NVS_NAMESPACE_SCALE "scale" #define NVS_KEY_CALIBRATION "cal_value" #define NVS_KEY_AUTOTARE "auto_tare" +#define SCALE_DEFAULT_CALIBRATION_VALUE 430.0f; #define BAMBU_USERNAME "bblp" diff --git a/src/main.cpp b/src/main.cpp index f160e95..d301583 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -156,94 +156,93 @@ void loop() { } } - // Wenn Waage nicht Kalibriert - if (scaleCalibrated == 3) + // If scale is not calibrated, only show a warning + if (!scaleCalibrated) { - oledShowMessage("Scale not calibrated!"); - vTaskDelay(5000 / portTICK_PERIOD_MS); - yield(); - esp_task_wdt_reset(); - - return; - } - - // Ausgabe der Waage auf Display - if(pauseMainTask == 0) - { - if (mainTaskWasPaused || (weight != lastWeight && nfcReaderState == NFC_IDLE && (!bambuCredentials.autosend_enable || autoSetToBambuSpoolId == 0))) - { - (weight < 2) ? ((weight < -2) ? oledShowMessage("!! -0") : oledShowWeight(0)) : oledShowWeight(weight); + // Do not show the warning if the calibratin process is onging + if(!scaleCalibrationActive){ + oledShowMessage("Scale not calibrated"); + vTaskDelay(1000 / portTICK_PERIOD_MS); } - mainTaskWasPaused = false; - } - else - { - mainTaskWasPaused = true; - } - - - // Wenn Timer abgelaufen und nicht gerade ein RFID-Tag geschrieben wird - if (currentMillis - lastWeightReadTime >= weightReadInterval && nfcReaderState < NFC_WRITING) - { - lastWeightReadTime = currentMillis; - - // Prüfen ob die Waage korrekt genullt ist - // Abweichung von 2g ignorieren - if (autoTare && (weight > 2 && weight < 7) || weight < -2) + }else{ + // Ausgabe der Waage auf Display + if(pauseMainTask == 0) { - scale_tare_counter++; + if (mainTaskWasPaused || (weight != lastWeight && nfcReaderState == NFC_IDLE && (!bambuCredentials.autosend_enable || autoSetToBambuSpoolId == 0))) + { + (weight < 2) ? ((weight < -2) ? oledShowMessage("!! -0") : oledShowWeight(0)) : oledShowWeight(weight); + } + mainTaskWasPaused = false; } else { - scale_tare_counter = 0; + mainTaskWasPaused = true; } - // Prüfen ob das Gewicht gleich bleibt und dann senden - if (abs(weight - lastWeight) <= 2 && weight > 5) + + // Wenn Timer abgelaufen und nicht gerade ein RFID-Tag geschrieben wird + if (currentMillis - lastWeightReadTime >= weightReadInterval && nfcReaderState < NFC_WRITING) { - weigthCouterToApi++; - } - else + lastWeightReadTime = currentMillis; + + // Prüfen ob die Waage korrekt genullt ist + // Abweichung von 2g ignorieren + if (autoTare && (weight > 2 && weight < 7) || weight < -2) + { + scale_tare_counter++; + } + else + { + scale_tare_counter = 0; + } + + // Prüfen ob das Gewicht gleich bleibt und dann senden + if (abs(weight - lastWeight) <= 2 && weight > 5) + { + weigthCouterToApi++; + } + else + { + weigthCouterToApi = 0; + weightSend = 0; + } + } + + // reset weight counter after writing tag + // TBD: what exactly is the logic behind this? + if (currentMillis - lastWeightReadTime >= weightReadInterval && nfcReaderState != NFC_IDLE && nfcReaderState != NFC_READ_SUCCESS) { weigthCouterToApi = 0; - weightSend = 0; } - } + + lastWeight = weight; - // reset weight counter after writing tag - // TBD: what exactly is the logic behind this? - if (currentMillis - lastWeightReadTime >= weightReadInterval && nfcReaderState != NFC_IDLE && nfcReaderState != NFC_READ_SUCCESS) - { - weigthCouterToApi = 0; - } - - lastWeight = weight; + // Wenn ein Tag mit SM id erkannte wurde und der Waage Counter anspricht an SM Senden + if (activeSpoolId != "" && weigthCouterToApi > 3 && weightSend == 0 && nfcReaderState == NFC_READ_SUCCESS && tagProcessed == false && spoolmanApiState == API_IDLE) { + // set the current tag as processed to prevent it beeing processed again + tagProcessed = true; - // Wenn ein Tag mit SM id erkannte wurde und der Waage Counter anspricht an SM Senden - if (activeSpoolId != "" && weigthCouterToApi > 3 && weightSend == 0 && nfcReaderState == NFC_READ_SUCCESS && tagProcessed == false && spoolmanApiState == API_IDLE) { - // set the current tag as processed to prevent it beeing processed again - tagProcessed = true; - - if (updateSpoolWeight(activeSpoolId, weight)) - { - weightSend = 1; - + if (updateSpoolWeight(activeSpoolId, weight)) + { + weightSend = 1; + + } + else + { + oledShowIcon("failed"); + vTaskDelay(2000 / portTICK_PERIOD_MS); + } } - else - { - oledShowIcon("failed"); - vTaskDelay(2000 / portTICK_PERIOD_MS); - } - } - if(sendOctoUpdate && spoolmanApiState == API_IDLE){ - autoSetToBambuSpoolId = activeSpoolId.toInt(); + if(sendOctoUpdate && spoolmanApiState == API_IDLE){ + autoSetToBambuSpoolId = activeSpoolId.toInt(); - if(octoEnabled) - { - updateSpoolOcto(autoSetToBambuSpoolId); + if(octoEnabled) + { + updateSpoolOcto(autoSetToBambuSpoolId); + } + sendOctoUpdate = false; } - sendOctoUpdate = false; } esp_task_wdt_reset(); diff --git a/src/scale.cpp b/src/scale.cpp index 4be30c7..201618d 100644 --- a/src/scale.cpp +++ b/src/scale.cpp @@ -17,8 +17,9 @@ uint8_t weigthCouterToApi = 0; uint8_t scale_tare_counter = 0; bool scaleTareRequest = false; uint8_t pauseMainTask = 0; -uint8_t scaleCalibrated = 1; +bool scaleCalibrated; bool autoTare = true; +bool scaleCalibrationActive = false; // ##### Funktionen für Waage ##### uint8_t setAutoTare(bool autoTareValue) { @@ -88,7 +89,13 @@ void start_scale(bool touchSensorConnected) { // NVS lesen Preferences preferences; preferences.begin(NVS_NAMESPACE_SCALE, true); // true = readonly - calibrationValue = preferences.getFloat(NVS_KEY_CALIBRATION, defaultScaleCalibrationValue); + if(preferences.isKey(NVS_KEY_CALIBRATION)){ + calibrationValue = preferences.getFloat(NVS_KEY_CALIBRATION); + scaleCalibrated = true; + }else{ + calibrationValue = SCALE_DEFAULT_CALIBRATION_VALUE; + scaleCalibrated = false; + } // auto Tare // Wenn Touch Sensor verbunden, dann autoTare auf false setzen @@ -103,18 +110,6 @@ void start_scale(bool touchSensorConnected) { scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN); - if (isnan(calibrationValue) || calibrationValue < 1) { - calibrationValue = defaultScaleCalibrationValue; - scaleCalibrated = 0; - - oledShowMessage("Scale not calibrated!"); - for (uint16_t i = 0; i < 50000; i++) { - yield(); - vTaskDelay(pdMS_TO_TICKS(1)); - esp_task_wdt_reset(); - } - } - oledShowProgressBar(6, 7, DISPLAY_BOOT_TEXT, "Tare scale"); for (uint16_t i = 0; i < 2000; i++) { yield(); @@ -152,6 +147,8 @@ uint8_t calibrate_scale() { uint8_t returnState = 0; float newCalibrationValue; + scaleCalibrationActive = true; + vTaskSuspend(RfidReaderTask); vTaskSuspend(ScaleTask); @@ -228,6 +225,7 @@ uint8_t calibrate_scale() { esp_task_wdt_reset(); } + scaleCalibrated = true; returnState = 1; } else @@ -262,6 +260,7 @@ uint8_t calibrate_scale() { vTaskResume(ScaleTask); pauseBambuMqttTask = false; pauseMainTask = 0; + scaleCalibrationActive = false; return returnState; } diff --git a/src/scale.h b/src/scale.h index 96f59a4..c38583a 100644 --- a/src/scale.h +++ b/src/scale.h @@ -15,8 +15,9 @@ extern uint8_t weigthCouterToApi; extern uint8_t scale_tare_counter; extern uint8_t scaleTareRequest; extern uint8_t pauseMainTask; -extern uint8_t scaleCalibrated; +extern bool scaleCalibrated; extern bool autoTare; +extern bool scaleCalibrationActive; extern TaskHandle_t ScaleTask;