From fd16803d2aab8628d513862bd9bc539915de02e2 Mon Sep 17 00:00:00 2001 From: zzzz Date: Thu, 2 May 2024 21:53:47 +0200 Subject: [PATCH] Basic process loop working --- src/SmartDisplay.cpp | 32 +++++++++++- src/SmartDisplay.hpp | 5 +- src/TeaTimer.cpp | 7 ++- src/TeaTimer.hpp | 3 +- src/main.cpp | 122 +++++++++++++++++++++++-------------------- 5 files changed, 102 insertions(+), 67 deletions(-) diff --git a/src/SmartDisplay.cpp b/src/SmartDisplay.cpp index 773bfbf..921e200 100644 --- a/src/SmartDisplay.cpp +++ b/src/SmartDisplay.cpp @@ -5,14 +5,14 @@ SmartDisplay::SmartDisplay(Adafruit_SSD1306 *display) m_display = display; } -void SmartDisplay::printTeaConfigScreen(int steepingSeconds, int editDir) +void SmartDisplay::printTeaConfigScreen(const char *teaName, int steepingSeconds, int editDir) { m_display->clearDisplay(); m_display->setTextColor(WHITE); m_display->setTextSize(1); m_display->setCursor(0, 0); - m_display->println("Premium Earl Grey"); + m_display->println(teaName); m_display->println(" "); m_display->setTextSize(2); @@ -33,5 +33,33 @@ void SmartDisplay::printTeaConfigScreen(int steepingSeconds, int editDir) m_display->print("-"); } } + m_display->display(); +} + +void SmartDisplay::printTeaSteepingProgressScreen(const char *teaName, int steepingSeconds) +{ + m_display->clearDisplay(); + m_display->setTextColor(WHITE); + m_display->setTextSize(1); + m_display->setCursor(0, 0); + + m_display->println(teaName); + m_display->println("(Steeping)"); + + m_display->setTextSize(2); + m_display->println(steepingSeconds); + + m_display->display(); +} + +void SmartDisplay::printWaitForRFIDScreen() +{ + m_display->clearDisplay(); + m_display->setTextColor(WHITE); + m_display->setTextSize(1); + m_display->setCursor(0, 0); + + m_display->println("Waiting for RFID scan..."); + m_display->display(); } \ No newline at end of file diff --git a/src/SmartDisplay.hpp b/src/SmartDisplay.hpp index 7400ac4..16834bc 100644 --- a/src/SmartDisplay.hpp +++ b/src/SmartDisplay.hpp @@ -5,9 +5,10 @@ class SmartDisplay { public: SmartDisplay(Adafruit_SSD1306 *display); - void printTeaConfigScreen(int steepingSeconds, int editDir); + void printTeaConfigScreen(const char* teaName, int steepingSeconds, int editDir); void printAddNewTeaScreen(); - void printSteepingInProgressScreen(); + void printTeaSteepingProgressScreen(const char *teaName, int steepingSeconds); + void printWaitForRFIDScreen(); private: Adafruit_SSD1306 *m_display; }; \ No newline at end of file diff --git a/src/TeaTimer.cpp b/src/TeaTimer.cpp index 4660722..e57e1cc 100644 --- a/src/TeaTimer.cpp +++ b/src/TeaTimer.cpp @@ -1,6 +1,6 @@ #include "TeaTimer.hpp" -void TeaTimer::init(ContinuousServo *t_servo) +TeaTimer::TeaTimer(ContinuousServo *t_servo) { m_servo = t_servo; } @@ -10,7 +10,7 @@ void TeaTimer::beginSteeping(unsigned long t_steepingDuration) { if (m_isSteeping) return; - m_servo->moveServoForMillisClockwise(2000); + m_servo->moveServoForMillisClockwise(m_servoTurningTime); m_endSteepingTime = millis() + t_steepingDuration; m_isSteeping = true; } @@ -18,14 +18,13 @@ void TeaTimer::beginSteeping(unsigned long t_steepingDuration) // Move servo up, stop timer immediately. void TeaTimer::end_steeping() { - m_servo->moveServoForMillisCounterClockwise(2000); + m_servo->moveServoForMillisCounterClockwise(m_servoTurningTime); m_isSteeping = false; } // Called at start of update. void TeaTimer::tick() { - m_servo->tick(); if (millis() >= m_endSteepingTime && m_isSteeping) { end_steeping(); diff --git a/src/TeaTimer.hpp b/src/TeaTimer.hpp index 931b59e..d6f2307 100644 --- a/src/TeaTimer.hpp +++ b/src/TeaTimer.hpp @@ -6,7 +6,7 @@ class TeaTimer { public: - void init(ContinuousServo *t_servo); + TeaTimer(ContinuousServo *t_servo); // Move servo down, start timer. void beginSteeping(unsigned long t_steepingDuration); // Move servo up, stop timer immediately. @@ -20,5 +20,6 @@ private: bool m_isSteeping = false; // The time in millis when steeping is done. unsigned long m_endSteepingTime; + unsigned long m_servoTurningTime = 1000; ContinuousServo *m_servo; }; \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 148b235..eed09e3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,6 +4,9 @@ #include #include #include +#include +#include +#include #include "TeaTimer.hpp" #include "ContinuousServo.hpp" @@ -27,7 +30,7 @@ MFRC522::MIFARE_Key key; auto display = Adafruit_SSD1306(128, 32, &WIRE); ContinuousServo contServo(SERVO_PIN); -TeaTimer teaTimer; +TeaTimer teaTimer(&contServo); SmartDial smartDial(POTENTIOMETER_PIN); SmartDisplay smartDisplay(&display); @@ -54,14 +57,16 @@ void setup() Serial.println("Initializing RFID..."); mfrc522.PCD_Init(); - Serial.print(F("Ver: 0x")); + /*Serial.print(F("Ver: 0x")); byte readReg = mfrc522.PCD_ReadRegister(mfrc522.VersionReg); - Serial.println(readReg, HEX); + Serial.println(readReg, HEX);*/ + mfrc522.PICC_HaltA(); Serial.println("Initializing OLED..."); display.begin(SSD1306_SWITCHCAPVCC, 0x3C, false); // Address 0x3C for 128x32 printMsg("Tea is Great!"); - delay(3000); + + delay(1000); // Prepare the key (used both as key A and key B) for (byte i = 0; i < 6; i++) @@ -101,8 +106,6 @@ void printMsg(unsigned long l) bool isBtnPressed() { - if (millis() < 10000) - return false; return digitalRead(TOUCH_BTN_PIN) == HIGH; } @@ -110,16 +113,55 @@ int steepingSeconds = 60; void loop() { + contServo.tick(); + switch (state) { case WAIT_FOR_RFID_SCAN: { + smartDisplay.printWaitForRFIDScreen(); + // Look for new cards + if (!mfrc522.PICC_IsNewCardPresent()) + return; + + // Select one of the cards + if (!mfrc522.PICC_ReadCardSerial()) + return; + + // Show some details of the PICC (that is: the tag/card) + // UID is a unique four hex pairs, e.g. E6 67 2A 12 + Serial.print(F("Card UID:")); + oled_dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size); + display.println(); + display.display(); + delay(1000); + + // PICC type, e.g. "MIFARE 1K0" + // Serial.print(F("PICC type: ")); + // MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak); + // Serial.println(mfrc522.PICC_GetTypeName(piccType)); + + // Halt PICC + mfrc522.PICC_HaltA(); + + display.println("Success!"); + display.display(); + delay(1000); + + state = State::DISPLAY_SCANNED_TEA_CONFIG; break; } case DISPLAY_SCANNED_TEA_CONFIG: { int dir = smartDial.smoothEdit(&steepingSeconds, 0, 120); - smartDisplay.printTeaConfigScreen(steepingSeconds, dir); + smartDisplay.printTeaConfigScreen("Premium Earl Grey", steepingSeconds, dir); + + // change state if start pressed + if (isBtnPressed()) + { + teaTimer.beginSteeping(10000); + state = State::STEEPING_IN_PROGRESS; + } break; } case ADD_NEW_TEA_CONFIG: @@ -128,60 +170,24 @@ void loop() } case STEEPING_IN_PROGRESS: { + teaTimer.tick(); + if (teaTimer.isSteeping()) + { + auto r = teaTimer.remainingSteepTimeSeconds(); + smartDisplay.printTeaSteepingProgressScreen("Premium Earl Grey", r); + } + else + { + printMsg("DONE"); + if (!contServo.isTurning()) + { + delay(3000); + state = State::WAIT_FOR_RFID_SCAN; + } + } break; } }; - - // Look for new cards - if (!mfrc522.PICC_IsNewCardPresent()) - return; - - // Select one of the cards - if (!mfrc522.PICC_ReadCardSerial()) - return; - - // Show some details of the PICC (that is: the tag/card) - // UID is a unique four hex pairs, e.g. E6 67 2A 12 - Serial.print(F("Card UID:")); - serial_dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size); - - display.clearDisplay(); - display.setTextColor(WHITE); - display.setTextSize(1); - display.setCursor(0, 10); - display.println("Card UID: "); - // oled_dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size); - // display.println(); - // display.display(); - - // PICC type, e.g. "MIFARE 1K0" - Serial.print(F("PICC type: ")); - MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak); - Serial.println(mfrc522.PICC_GetTypeName(piccType)); - - // Halt PICC - mfrc522.PICC_HaltA(); - - Serial.print("Delay scanning..."); - delay(scanDelay * 1000); // Turn seconds into milliseconds - Serial.println(" ...Ready to scan again!"); - - /* -teaTimer.tick(); - -if (isBtnPressed() && !teaTimer.isSteeping()) -{ -teaTimer.beginSteeping(5000); -} -if (teaTimer.isSteeping()) -{ -auto r = teaTimer.remainingSteepTimeSeconds(); -printMsg(r); -} -else -{ -printMsg("Default"); -}*/ } void toggle_led()