From b61c79f4d563e8d4aee2ae3f585e4a01f33e82fc Mon Sep 17 00:00:00 2001 From: zzzz Date: Sat, 27 Apr 2024 01:00:18 +0200 Subject: [PATCH] Added steeping timer --- platformio.ini | 1 - src/SmartServo.cpp | 72 ++++++++++++++++------------------------------ src/SmartServo.hpp | 8 ++++-- src/TeaTimer.cpp | 37 ++++++++++++++++++++++++ src/TeaTimer.hpp | 26 +++++++++++++++++ src/main.cpp | 63 ++++++++++++++++++++++++++-------------- 6 files changed, 135 insertions(+), 72 deletions(-) create mode 100644 src/TeaTimer.cpp create mode 100644 src/TeaTimer.hpp diff --git a/platformio.ini b/platformio.ini index f8041db..a4d875e 100644 --- a/platformio.ini +++ b/platformio.ini @@ -13,5 +13,4 @@ platform = espressif8266 board = d1 framework = arduino lib_deps = - contrem/arduino-timer@^3.0.1 adafruit/Adafruit SSD1306@^2.5.9 diff --git a/src/SmartServo.cpp b/src/SmartServo.cpp index 43d1217..44e3da6 100644 --- a/src/SmartServo.cpp +++ b/src/SmartServo.cpp @@ -1,54 +1,32 @@ -#include -#include +#include "SmartServo.hpp" -class SmartServo +SmartServo::SmartServo(int pin) { -public: - SmartServo(int pin) - { - myservo.attach(pin); - } + myservo.attach(pin); +} - static bool bounceServo(SmartServo *servo) +// Note: This blocks execution until servo finished moving. +void SmartServo::moveServoTo(int pos, int stepSize) +{ + int currentPos = myservo.read(); + for (int p = currentPos; p < pos; p += stepSize) { - if (servo->toggle) - { - servo->moveServoToZero(); - } - else - { - servo->moveServoTo180(); - } - servo->toggle = !servo->toggle; - return true; + myservo.write(p); + delay(10); } + for (int p = currentPos; p > pos; p -= stepSize) + { + myservo.write(p); + delay(10); + } +} - void moveServoToZero() - { - moveServoTo(0, 10); - } - void moveServoTo180() - { - moveServoTo(180, 5); - } +void SmartServo::moveServoToZero() +{ + moveServoTo(0, 10); +} - // Note: This blocks execution until servo finished moving. - void moveServoTo(int pos, int stepSize) - { - int currentPos = myservo.read(); - for (int p = currentPos; p < pos; p += stepSize) - { - myservo.write(p); - delay(10); - } - for (int p = currentPos; p > pos; p -= stepSize) - { - myservo.write(p); - delay(10); - } - } - -private: - Servo myservo; - bool toggle = false; -}; \ No newline at end of file +void SmartServo::moveServoTo180() +{ + moveServoTo(180, 5); +} diff --git a/src/SmartServo.hpp b/src/SmartServo.hpp index c168202..3cb45dd 100644 --- a/src/SmartServo.hpp +++ b/src/SmartServo.hpp @@ -1,4 +1,5 @@ #pragma once +#include class SmartServo { @@ -7,5 +8,8 @@ public: void moveServoTo(int pos, int stepSize); void moveServoToZero(); void moveServoTo180(); - bool bounceServo(void *); -}; \ No newline at end of file + +private: + Servo myservo; + bool toggle = false; +}; diff --git a/src/TeaTimer.cpp b/src/TeaTimer.cpp new file mode 100644 index 0000000..f8b5f94 --- /dev/null +++ b/src/TeaTimer.cpp @@ -0,0 +1,37 @@ +#include "TeaTimer.hpp" + +void TeaTimer::init(SmartServo *t_servo) +{ + m_servo = t_servo; +} + +// Move servo down, start timer. +void TeaTimer::beginSteeping(unsigned long t_steepingDuration) +{ + if (m_isSteeping) + return; + m_servo->moveServoTo180(); + m_endSteepingTime = millis() + t_steepingDuration; + m_isSteeping = true; +} + +// Move servo up, stop timer immediately. +void TeaTimer::end_steeping() +{ + m_servo->moveServoToZero(); + m_isSteeping = false; +} + +// Called at start of update. +void TeaTimer::tick() +{ + if (millis() >= m_endSteepingTime) + { + end_steeping(); + } +} + +unsigned long TeaTimer::remainingSteepTimeSeconds() +{ + return (m_endSteepingTime - millis()) / 1000; +} \ No newline at end of file diff --git a/src/TeaTimer.hpp b/src/TeaTimer.hpp new file mode 100644 index 0000000..84b1adf --- /dev/null +++ b/src/TeaTimer.hpp @@ -0,0 +1,26 @@ +#pragma once +#include +#include "SmartServo.hpp" + +class TeaTimer +{ +public: + void init(SmartServo *t_servo); + // Move servo down, start timer. + void beginSteeping(unsigned long t_steepingDuration); + // Move servo up, stop timer immediately. + void end_steeping(); + // Called at start of update. + void tick(); + unsigned long remainingSteepTimeSeconds(); + bool isSteeping() + { + return m_isSteeping; + }; + +private: + bool m_isSteeping = false; + // The time in millis when steeping is done. + unsigned long m_endSteepingTime; + SmartServo *m_servo; +}; \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index b19c0c8..082988e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,14 +1,11 @@ #include -#include -#include - #include #include #include #include -#include "main.hpp" -#include "SmartServo.cpp" +#include "TeaTimer.hpp" +#include "SmartServo.hpp" #define WIRE Wire @@ -18,14 +15,15 @@ auto display = Adafruit_SSD1306(128, 32, &WIRE); -auto timer = timer_create_default(); - SmartServo smartServo(SERVO_PIN); +TeaTimer teaTimer; void setup() { // Serial.begin(9600); + teaTimer.init(&smartServo); + pinMode(LED_BUILTIN, OUTPUT); pinMode(TOUCH_BTN_PIN, INPUT); @@ -34,35 +32,56 @@ void setup() // Since the buffer is intialized with an Adafruit splashscreen // internally, this will display the splashscreen. display.display(); - - // Clear the buffer. - // display.clearDisplay(); - // display.display(); + delay(1000); smartServo.moveServoToZero(); - - // timer.every(1000, toggle_led); - // timer.every(2000, reinterpret_cast::handler_t>(SmartServo::bounceServo), &smartServo); } +void printMsg(const String &s) +{ + display.clearDisplay(); + display.setTextColor(WHITE); + display.setTextSize(2); + display.setCursor(0, 10); + display.println(s); + display.display(); +} +void printMsg(unsigned long l) +{ + display.clearDisplay(); + display.setTextColor(WHITE); + display.setTextSize(2); + display.setCursor(0, 10); + display.println(l); + display.display(); +} + +bool isBtnPressed() +{ + if (millis() < 10000) return false; + return digitalRead(TOUCH_BTN_PIN) == HIGH; +} void loop() { - timer.tick(); + teaTimer.tick(); - if (digitalRead(TOUCH_BTN_PIN) == HIGH) + if (isBtnPressed()) { - digitalWrite(LED_BUILTIN, HIGH); + printMsg("BUTTON"); + teaTimer.beginSteeping(5000); + } + else if (!teaTimer.isSteeping()) + { + printMsg("Default"); } else { - digitalWrite(LED_BUILTIN, LOW); + auto r = teaTimer.remainingSteepTimeSeconds(); + printMsg(r); } - - // display.display(); } -bool toggle_led(void *) +void toggle_led() { digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); - return true; // repeat? true } \ No newline at end of file