From 308381cfb2d2b7370326f3505a92223fbb2d94e2 Mon Sep 17 00:00:00 2001 From: zzzz Date: Fri, 21 Jun 2024 15:17:18 +0200 Subject: [PATCH] Implement SmartFeedback adjustments --- src/SmartFeedback.cpp | 44 +++++++++++++++++++++++++++++++++++++++++++ src/SmartFeedback.hpp | 13 +++++++++++++ src/main.cpp | 38 ++++++++++++++++++++++++++----------- src/main.hpp | 1 + 4 files changed, 85 insertions(+), 11 deletions(-) create mode 100644 src/SmartFeedback.cpp create mode 100644 src/SmartFeedback.hpp diff --git a/src/SmartFeedback.cpp b/src/SmartFeedback.cpp new file mode 100644 index 0000000..52b3f48 --- /dev/null +++ b/src/SmartFeedback.cpp @@ -0,0 +1,44 @@ +#include "SmartFeedback.hpp" + +// bool: false => feedback less brewtime; true => feedback more brewtime +int SmartFeedback::calculateNewTime(bool feedback, JSONVar steepingLog, int currentSteepingTime) +{ + int numLogEntries = steepingLog.length(); + int steepingTimes[numLogEntries]; + for (int i = 0; i < numLogEntries; i++) + { + steepingTimes[i] = steepingLog[i]["steeping_seconds"]; + } + + // if there is no log yet, we just add/substract MAX_STEP + if (numLogEntries == 0) + return feedback ? currentSteepingTime + MAX_STEP : currentSteepingTime - MAX_STEP; + + // check the last brewing time + int lastSteepingTime = steepingTimes[numLogEntries-1]; + Serial.print("last steeping time: "); + Serial.println(lastSteepingTime); + if (feedback) + { + // we want a longer brewing time + + if (lastSteepingTime >= currentSteepingTime) + return currentSteepingTime + MIDDLE_STEP; + + if (lastSteepingTime < currentSteepingTime) + return currentSteepingTime + MAX_STEP; + } + else + { + // we want a shorter brewing time + + if (lastSteepingTime > currentSteepingTime) + return currentSteepingTime - MAX_STEP; + + if (lastSteepingTime <= currentSteepingTime) + return currentSteepingTime - MIDDLE_STEP; + } + + // something went wrong + return 0; +} diff --git a/src/SmartFeedback.hpp b/src/SmartFeedback.hpp new file mode 100644 index 0000000..2474698 --- /dev/null +++ b/src/SmartFeedback.hpp @@ -0,0 +1,13 @@ +#pragma once +#include + +class SmartFeedback +{ +public: + // bool: false => feedback less brewtime; true => feedback more brewtime + int calculateNewTime(bool feedback, JSONVar steepingLog, int currentBrewtime); +private: + int MAX_STEP = 20; + int MIDDLE_STEP = 10; + int MIN_STEP = 5; +}; \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 66b1c2f..cd0423f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -31,6 +31,7 @@ SmartDisplay smartDisplay(&display); TeaNetworking teaNet; SmartSound sound(BUZZER_PIN); +SmartFeedback smartFeedback; SmartButton buttonLeft(TACTILE_BTN_LEFT); SmartButton buttonRight(TACTILE_BTN_RIGHT); @@ -48,7 +49,13 @@ int defaultSteepingTime = 120; unsigned long timeoutTime; // macros -#define CATCH_NET_FAILURE(netResult, msg) if (netResult == JSON.parse("{}")) {smartDisplay.printMsg(msg);delay(500);break;} +#define CATCH_NET_FAILURE(netResult, msg) \ + if (netResult == JSON.parse("{}")) \ + { \ + smartDisplay.printMsg(msg); \ + delay(500); \ + break; \ + } enum State { @@ -285,7 +292,7 @@ void loop() CATCH_NET_FAILURE(res, "TRY AGAIN") CATCH_NET_FAILURE(res2, "TRY AGAIN") - + state = State::WAIT_FOR_RFID_SCAN; smartStrip.reset(); break; @@ -317,15 +324,15 @@ void loop() if (isTouchBtnPressed()) { + smartDisplay.playTeaAnimationPartOne(32, "Accepted!", "(perfect)", "", "saving..."); // keep steeping time for next brew String steeepingTimePayload = teaNet.bundleSteepingTimeLogEntry(currentTea.m_steepingSeconds); String teaMetaPayload = teaNet.bundleTeaMetaChange(currentTea.m_teaName, currentTea.m_teaNotes, currentTea.m_waterTemp, currentTea.m_steepingSeconds); Serial.println(steeepingTimePayload); - smartDisplay.playTeaAnimationPartOne(32, "Accepted!", "(perfect)", "", "saving..."); JSONVar res = teaNet.httpsPOSTRequest(shuttleServer + "/tea-steeping-time-log/" + currentTea.m_id, steeepingTimePayload); JSONVar res2 = teaNet.httpsPUTRequest(shuttleServer + "/types-of-tea/" + currentTea.m_id, teaMetaPayload); - + CATCH_NET_FAILURE(res, "TRY AGAIN") CATCH_NET_FAILURE(res2, "TRY AGAIN") @@ -335,16 +342,21 @@ void loop() } if (buttonLeft.isJustPressed()) { - // decrease steeping time for next brew // TODO: this should be smarter + // decrease steeping time for next brew + smartDisplay.playTeaAnimationPartOne(32, "Accepted!", "(shorter)", "", "saving..."); + + // request steeping log + JSONVar steepingLog = teaNet.httpsGETRequest(shuttleServer + "/brewing-events/" + currentTea.m_id); + CATCH_NET_FAILURE(steepingLog, "TRY AGAIN") + String steeepingTimePayload = teaNet.bundleSteepingTimeLogEntry(currentTea.m_steepingSeconds); Serial.println(steeepingTimePayload); - smartDisplay.playTeaAnimationPartOne(32, "Accepted!", "(shorter)", "", "saving..."); // log old steeping time JSONVar res = teaNet.httpsPOSTRequest(shuttleServer + "/tea-steeping-time-log/" + currentTea.m_id, steeepingTimePayload); // now update steeping time for next use - currentTea.m_steepingSeconds -= 10; + currentTea.m_steepingSeconds = smartFeedback.calculateNewTime(false, steepingLog, currentTea.m_steepingSeconds); String teaMetaPayload = teaNet.bundleTeaMetaChange(currentTea.m_teaName, currentTea.m_teaNotes, currentTea.m_waterTemp, currentTea.m_steepingSeconds); JSONVar res2 = teaNet.httpsPUTRequest(shuttleServer + "/types-of-tea/" + currentTea.m_id, teaMetaPayload); @@ -357,15 +369,19 @@ void loop() } if (buttonRight.isJustPressed()) { - // increase steeping time for next brew // TODO: this should be smarter + // increase steeping time for next brew + smartDisplay.playTeaAnimationPartOne(32, "Accepted!", "(longer)", "", "saving..."); + + // request steeping log + JSONVar steepingLog = teaNet.httpsGETRequest(shuttleServer + "/brewing-events/" + currentTea.m_id); + CATCH_NET_FAILURE(steepingLog, "TRY AGAIN") String steeepingTimePayload = teaNet.bundleSteepingTimeLogEntry(currentTea.m_steepingSeconds); Serial.println(steeepingTimePayload); - smartDisplay.playTeaAnimationPartOne(32, "Accepted!", "(longer)", "", "saving..."); JSONVar res = teaNet.httpsPOSTRequest(shuttleServer + "/tea-steeping-time-log/" + currentTea.m_id, steeepingTimePayload); // now update steeping time for next use - currentTea.m_steepingSeconds += 10; + currentTea.m_steepingSeconds = smartFeedback.calculateNewTime(true, steepingLog, currentTea.m_steepingSeconds); String teaMetaPayload = teaNet.bundleTeaMetaChange(currentTea.m_teaName, currentTea.m_teaNotes, currentTea.m_waterTemp, currentTea.m_steepingSeconds); JSONVar res2 = teaNet.httpsPUTRequest(shuttleServer + "/types-of-tea/" + currentTea.m_id, teaMetaPayload); @@ -411,7 +427,7 @@ void loop() String payload = teaNet.bundleNewTeaInfo(currentTea.m_rfidCode, currentTea.m_steepingSeconds); JSONVar result = teaNet.httpsPOSTRequest(shuttleServer + "/add-tea", payload); - + CATCH_NET_FAILURE(result, "POST FAILED") smartDisplay.playTeaAnimationPartTwo(32, "", "Done!", "", ""); diff --git a/src/main.hpp b/src/main.hpp index e2e7684..d5d747d 100644 --- a/src/main.hpp +++ b/src/main.hpp @@ -19,5 +19,6 @@ #include "TeaData.hpp" #include "SmartButton.hpp" #include "SmartSound.hpp" +#include "SmartFeedback.hpp" bool isTouchBtnPressed(); \ No newline at end of file