4
0
Fork 0
mirror of https://github.com/zzzzDev4/IAS-Better-Tea.git synced 2025-04-21 07:31:20 +02:00

Added steeping timer

This commit is contained in:
zzzz 2024-04-27 01:00:18 +02:00
parent e75dc39664
commit b61c79f4d5
6 changed files with 135 additions and 72 deletions

View file

@ -13,5 +13,4 @@ platform = espressif8266
board = d1
framework = arduino
lib_deps =
contrem/arduino-timer@^3.0.1
adafruit/Adafruit SSD1306@^2.5.9

View file

@ -1,54 +1,32 @@
#include <Servo.h>
#include <arduino-timer.h>
#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;
};
void SmartServo::moveServoTo180()
{
moveServoTo(180, 5);
}

View file

@ -1,4 +1,5 @@
#pragma once
#include <Servo.h>
class SmartServo
{
@ -7,5 +8,8 @@ public:
void moveServoTo(int pos, int stepSize);
void moveServoToZero();
void moveServoTo180();
bool bounceServo(void *);
};
private:
Servo myservo;
bool toggle = false;
};

37
src/TeaTimer.cpp Normal file
View file

@ -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;
}

26
src/TeaTimer.hpp Normal file
View file

@ -0,0 +1,26 @@
#pragma once
#include <Arduino.h>
#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;
};

View file

@ -1,14 +1,11 @@
#include <Arduino.h>
#include <arduino-timer.h>
#include <Servo.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#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<Timer<>::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
}