diff --git a/src/SmartDisplay.cpp b/src/SmartDisplay.cpp index 5df5780..6601110 100644 --- a/src/SmartDisplay.cpp +++ b/src/SmartDisplay.cpp @@ -90,9 +90,17 @@ void SmartDisplay::printTeaConfigScreen(TeaData tea) m_display->println(tea.m_teaName); m_display->setCursor(128 - 35, m_display->getCursorY()); - m_display->print(tea.m_waterTemp); - m_display->drawCircle(m_display->getCursorX() + 4, m_display->getCursorY(), 1, WHITE); - m_display->println(" C"); + + if (tea.m_waterTemp > 0) + { + m_display->print(tea.m_waterTemp); + m_display->drawCircle(m_display->getCursorX() + 4, m_display->getCursorY(), 1, WHITE); + m_display->println(" C"); + } + else + { + m_display->println(""); + } m_display->setTextSize(2); String steepingTime = SmartTime::formatSeconds(tea.m_steepingSeconds); @@ -130,23 +138,38 @@ void SmartDisplay::printRequestFeedbackScreen(TeaData tea) { resetDisplay(); - m_display->println("Feedback on brew intensity"); - m_display->println(); - m_display->println("|less| perfect |more|"); + m_display->println(" Feedback "); + m_display->setCursor(0, m_display->getCursorY() + 4); + m_display->println("< >"); + m_display->setCursor(0, m_display->getCursorY() + 4); + m_display->println("shorter ok longer"); m_display->display(); } -void SmartDisplay::printAddNewTeaConfigScreen(TeaData tea) +void SmartDisplay::printAddNewTeaConfigScreen(TeaData tea, u_int *flicker) { resetDisplay(); - m_display->println("Add initial config for new tea"); - m_display->println(); - m_display->print("Steeping time:"); + m_display->println(" New Tea"); + m_display->println("Steeping time:"); + + if (*flicker % 12 == 0) + { + toggle = !toggle; + } + if (toggle) + { + m_display->display(); + *flicker += 3; + return; + } + + m_display->setTextSize(2); String steepingTime = SmartTime::formatSeconds(tea.m_steepingSeconds); m_display->print(steepingTime); m_display->setTextSize(1); + m_display->setCursor(m_display->getCursorX(), m_display->getCursorY() + 7); m_display->println(" min"); m_display->display(); diff --git a/src/SmartDisplay.hpp b/src/SmartDisplay.hpp index f108d0a..55430db 100644 --- a/src/SmartDisplay.hpp +++ b/src/SmartDisplay.hpp @@ -17,7 +17,7 @@ public: public: // special screens - void printAddNewTeaConfigScreen(TeaData tea); + void printAddNewTeaConfigScreen(TeaData tea, u_int *flicker); // animations void playTeaAnimation(); @@ -37,4 +37,5 @@ public: private: Adafruit_SSD1306 *m_display; + bool toggle = false; }; \ No newline at end of file diff --git a/src/SmartStrip.cpp b/src/SmartStrip.cpp index c0fb46a..25e41c5 100644 --- a/src/SmartStrip.cpp +++ b/src/SmartStrip.cpp @@ -12,6 +12,7 @@ void SmartStrip::reset() { FastLED.clear(true); FastLED.show(); + FastLED.show(); } void SmartStrip::setGreen(int led) @@ -21,6 +22,7 @@ void SmartStrip::setGreen(int led) FastLED.show(); } + void SmartStrip::animateBottomToTop(int r, int g, int b) { for (int i = 0; i <= 14; i++) @@ -28,9 +30,8 @@ void SmartStrip::animateBottomToTop(int r, int g, int b) leds[i].setRGB(r, g, b); FastLED.show(); FastLED.show(); - delay(100); + delay(50); } - FastLED.clear(true); FastLED.show(); } @@ -48,6 +49,7 @@ void SmartStrip::progressBar(int min, int max, int val) leds[i].setRGB(3, 3, 3); FastLED.show(); FastLED.show(); + FastLED.show(); } } } \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index c06a001..567c675 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -93,6 +93,7 @@ void setup() } u_int frame = 0; +u_int flicker = 0; void loop() { @@ -147,15 +148,7 @@ void loop() } case PROCESS_RFID_SCAN: { - smartDisplay.resetDisplay(); - display.println("Detected!"); - display.display(); - delay(200); - display.println(currentTea.m_rfidCode); - delay(200); - - display.println("Sync..."); - display.display(); + smartDisplay.playTeaAnimationPartOne(32, "", "Loading...", "", ""); JSONVar result = teaNet.httpsGETRequest(shuttleServer + "/teabyrfid/" + currentTea.m_rfidCode); Serial.print("Result:"); @@ -165,30 +158,25 @@ void loop() if (result == JSON.parse("{}")) { smartDisplay.printMsg("GET FAILED"); - delay(1000); + delay(500); break; } if (JSONVar::stringify(result) == "null") { Serial.print("TEA DOES NOT EXIST YET!"); - String payload = teaNet.createTeaData("Unnamed Tea", currentTea.m_rfidCode, 90, 180); - result = teaNet.httpsPOSTRequest(shuttleServer + "/addtea", payload); - // catch possible failure - if (result == JSON.parse("{}")) - { - smartDisplay.printMsg("POST FAILED"); - delay(1000); - break; - } state = State::DISPLAY_ADD_NEW_TEA_CONFIG; + currentTea = TeaData("New Tea", currentTea.m_rfidCode, -1, 120); + smartDisplay.playTeaAnimationPartTwo(32, "", "Done!", "", ""); + break; } else { state = State::DISPLAY_SCANNED_TEA_CONFIG; + currentTea = TeaData(result["teaname"], currentTea.m_rfidCode, result["watertemp"], result["steepingseconds"]); + smartDisplay.playTeaAnimationPartTwo(32, "", "Done!", "", ""); } - currentTea = TeaData(result["teaname"], currentTea.m_rfidCode, result["watertemp"], result["steepingseconds"]); break; } case DISPLAY_SCANNED_TEA_CONFIG: @@ -213,7 +201,7 @@ void loop() } smartDisplay.printTeaConfigScreen(currentTea); - if (isBtnPressed() && currentTea.m_steepingSeconds <= 0) + if (isTouchBtnPressed() && currentTea.m_steepingSeconds <= 0) { // rickroll Serial.println("Easter"); @@ -222,21 +210,8 @@ void loop() } // change state if start pressed - if (isBtnPressed()) + if (isTouchBtnPressed()) { - String payload = teaNet.createTeaData(currentTea); - Serial.println(payload); - smartDisplay.playTeaAnimationPartOne(32, "", "Sync...", "", ""); - JSONVar res = teaNet.httpsPOSTRequest(shuttleServer + "/updatetea", payload); - // catch possible failure - if (res == JSON.parse("{}")) - { - smartDisplay.printMsg("POST FAILED"); - delay(1000); - break; - } - smartDisplay.playTeaAnimationPartTwo(32, "", "Done!", "", ""); - delay(250); teaTimer.beginSteeping(currentTea.m_steepingSeconds * 1000); state = State::STEEPING_IN_PROGRESS; smartStrip.reset(); @@ -252,7 +227,7 @@ void loop() if (buttonLeft.isJustPressed()) { teaTimer.endSteeping(); - smartDisplay.printMsg("DONE"); + smartDisplay.printMsg("Ready!"); smartStrip.animateBottomToTop(0, 1, 0); delay(1000); state = State::REQUEST_FEEDBACK; @@ -267,9 +242,8 @@ void loop() } else { - smartDisplay.printMsg("DONE"); + smartDisplay.printMsg("Ready!"); smartStrip.animateBottomToTop(0, 1, 0); - delay(1000); state = State::REQUEST_FEEDBACK; } break; @@ -281,38 +255,89 @@ void loop() String uid = smartRFID.readTag(50); // just enough time to detect tag if (uid != "no_tag_detected" && uid != "") { + // we still want to save the last configuration (treated as perfect, no steeping time change) + String payload = teaNet.createTeaData(currentTea); + Serial.println(payload); + smartDisplay.playTeaAnimationPartOne(32, "", "Loading...", "", ""); + JSONVar res = teaNet.httpsPOSTRequest(shuttleServer + "/updatetea", payload); + // catch possible failure + if (res == JSON.parse("{}")) + { + smartDisplay.printMsg("TRY AGAIN"); + delay(500); + break; + } + currentTea.m_rfidCode = uid; smartDisplay.resetDisplay(); + smartStrip.reset(); state = PROCESS_RFID_SCAN; break; } - if (isBtnPressed()) + if (isTouchBtnPressed()) { // keep steeping time for next brew - smartDisplay.printMsg("Feedback accepted (perfect)"); - delay(1000); state = State::WAIT_FOR_RFID_SCAN; + + String payload = teaNet.createTeaData(currentTea); + Serial.println(payload); + smartDisplay.playTeaAnimationPartOne(32, "Accepted!", "(perfect)", "", "saving..."); + JSONVar res = teaNet.httpsPOSTRequest(shuttleServer + "/updatetea", payload); + // catch possible failure + if (res == JSON.parse("{}")) + { + smartDisplay.printMsg("POST FAILED"); + delay(1000); + break; + } + smartDisplay.playTeaAnimationPartTwo(32, "", "Done!", "", ""); + smartStrip.reset(); } if (buttonLeft.isJustPressed()) { // decrease steeping time for next brew - smartDisplay.printMsg("Feedback accepted (-)"); - delay(1000); state = State::WAIT_FOR_RFID_SCAN; + + String payload = teaNet.createTeaData(currentTea); + Serial.println(payload); + smartDisplay.playTeaAnimationPartOne(32, "Accepted!", "(shorter)", "", "saving..."); + JSONVar res = teaNet.httpsPOSTRequest(shuttleServer + "/updatetea", payload); + // catch possible failure + if (res == JSON.parse("{}")) + { + smartDisplay.printMsg("POST FAILED"); + delay(1000); + break; + } + smartDisplay.playTeaAnimationPartTwo(32, "", "Done!", "", ""); + smartStrip.reset(); } if (buttonRight.isJustPressed()) { // increase steeping time for next brew - smartDisplay.printMsg("Feedback accepted (+)"); - delay(1000); state = State::WAIT_FOR_RFID_SCAN; + + String payload = teaNet.createTeaData(currentTea); + Serial.println(payload); + smartDisplay.playTeaAnimationPartOne(32, "Accepted!", "(longer)", "", "saving..."); + JSONVar res = teaNet.httpsPOSTRequest(shuttleServer + "/updatetea", payload); + // catch possible failure + if (res == JSON.parse("{}")) + { + smartDisplay.printMsg("POST FAILED"); + delay(1000); + break; + } + smartDisplay.playTeaAnimationPartTwo(32, "", "Done!", "", ""); + smartStrip.reset(); } break; } case DISPLAY_ADD_NEW_TEA_CONFIG: { - smartDisplay.printAddNewTeaConfigScreen(currentTea); + flicker++; + smartDisplay.printAddNewTeaConfigScreen(currentTea, &flicker); String uid = smartRFID.readTag(50); // just enough time to detect tag if (uid != "no_tag_detected" && uid != "") @@ -332,19 +357,21 @@ void loop() currentTea.m_steepingSeconds += 10; } - if (isBtnPressed()) + if (isTouchBtnPressed()) { - smartDisplay.printMsg("Accepted"); - delay(500); - smartDisplay.printMsg("sync..."); - JSONVar res = teaNet.httpsPOSTRequest(shuttleServer + "/updatetea", teaNet.createTeaData(currentTea)); + smartDisplay.playTeaAnimationPartOne(32, "Accepted!", "", "", "saving..."); + + String payload = teaNet.createTeaData("Unnamed Tea", currentTea.m_rfidCode, -1, currentTea.m_steepingSeconds); + JSONVar result = teaNet.httpsPOSTRequest(shuttleServer + "/addtea", payload); // catch possible failure - if (res == JSON.parse("{}")) + if (result == JSON.parse("{}")) { smartDisplay.printMsg("POST FAILED"); - delay(1000); + delay(500); break; } + + smartDisplay.playTeaAnimationPartTwo(32, "", "Done!", "", ""); state = State::DISPLAY_SCANNED_TEA_CONFIG; } @@ -353,7 +380,7 @@ void loop() }; } -bool isBtnPressed() +bool isTouchBtnPressed() { return digitalRead(TOUCH_BTN_PIN) == HIGH; } \ No newline at end of file diff --git a/src/main.hpp b/src/main.hpp index 668673c..e2e7684 100644 --- a/src/main.hpp +++ b/src/main.hpp @@ -20,4 +20,4 @@ #include "SmartButton.hpp" #include "SmartSound.hpp" -bool isBtnPressed(); \ No newline at end of file +bool isTouchBtnPressed(); \ No newline at end of file