From eaff63ac2de05c2adc408736869d812ab95ef64c Mon Sep 17 00:00:00 2001 From: Alessandro Monticelli Date: Fri, 1 Dec 2023 11:26:52 +0100 Subject: [PATCH] Good button and servo impl --- smart_bridge/platformio.ini | 1 + .../src/components/api/AbstractButton.h | 21 +++++ smart_bridge/src/components/api/Button.h | 78 +++---------------- smart_bridge/src/components/api/ServoImpl.h | 20 +++++ .../src/components/impl/AbstractButton.cpp | 13 ++++ smart_bridge/src/components/impl/Button.cpp | 35 ++------- .../src/components/impl/ServoImpl.cpp | 17 ++++ smart_bridge/src/main.cpp | 19 +++-- smart_bridge/src/tasks/ServoTestTask.cpp | 22 ++++++ smart_bridge/src/tasks/ServoTestTask.h | 31 ++++++++ smart_bridge/src/tasks/WaitForClickTask.cpp | 4 +- 11 files changed, 157 insertions(+), 104 deletions(-) create mode 100644 smart_bridge/src/components/api/AbstractButton.h create mode 100644 smart_bridge/src/components/api/ServoImpl.h create mode 100644 smart_bridge/src/components/impl/AbstractButton.cpp create mode 100644 smart_bridge/src/components/impl/ServoImpl.cpp create mode 100644 smart_bridge/src/tasks/ServoTestTask.cpp create mode 100644 smart_bridge/src/tasks/ServoTestTask.h diff --git a/smart_bridge/platformio.ini b/smart_bridge/platformio.ini index 5ed74d5..4365f59 100644 --- a/smart_bridge/platformio.ini +++ b/smart_bridge/platformio.ini @@ -19,3 +19,4 @@ lib_deps = Wire paulstoffregen/TimerOne@^1.1.1 arduino-libraries/Servo@^1.2.1 + nabontra/ServoTimer2@0.0.0-alpha+sha.2bf7fb3c17 diff --git a/smart_bridge/src/components/api/AbstractButton.h b/smart_bridge/src/components/api/AbstractButton.h new file mode 100644 index 0000000..4921370 --- /dev/null +++ b/smart_bridge/src/components/api/AbstractButton.h @@ -0,0 +1,21 @@ +#ifndef __ABSTRACT_BUTTON__ +#define __ABSTRACT_BUTTON__ + +class AbstractButton { + +public: + AbstractButton(); + virtual bool isPressed() = 0; + + virtual void sync(); + long getLastSyncTime(); + +protected: + void updateSyncTime(long time); + +private: + long lastTimeSync; + +}; + +#endif \ No newline at end of file diff --git a/smart_bridge/src/components/api/Button.h b/smart_bridge/src/components/api/Button.h index d9929dc..6d45548 100644 --- a/smart_bridge/src/components/api/Button.h +++ b/smart_bridge/src/components/api/Button.h @@ -1,76 +1,18 @@ #ifndef __BUTTON__ #define __BUTTON__ -constexpr bool BUTTON_PRESSED = true; -constexpr bool BUTTON_RELEASED = false; +#include "AbstractButton.h" -/** - * @class Button - * @brief This class represents a button component. - * - * This class provides the basic functionalities of a button. - * It should be inherited by specific button types. - */ -class Button -{ - -public: - /** - * @brief Default constructor for the Button class. - * - * @param pin The pin number where the button is connected. - */ - Button(int pin); - - /** - * @brief Check if the button is pressed. - * - * @return true if the button is pressed, false otherwise. - */ - bool checkButtonPressStatus(); - - /** - * @brief Check if the button is released. - * - * @return true if the button is released, false otherwise. - */ - bool checkButtonReleaseStatus(); - - /** - * @brief Synchronizes the button state with the actual physical button. - */ - void updateButtonState(); - - /** - * @brief Retrieves the last time the button state was synchronized. - * - * @return The last synchronization time. - */ - long retrieveLastButtonSyncTime(); - -protected: - /** - * @brief Set the last synchronization time. - * - * @param time The new synchronization time. - */ - void setLastButtonSyncTime(long time); +class Button: public AbstractButton { + +public: + Button(int pin); + bool isPressed(); + void sync(); private: - /** - * @brief The last time the button state was synchronized. - */ - long lastTimeButtonWasInSync; - - /** - * @brief The pin number where the button is connected. - */ - int buttonPin; - - /** - * @brief The current state of the button (true if pressed, false otherwise). - */ - bool buttonState; + int pin; + bool pressed; }; -#endif // __BUTTON__ \ No newline at end of file +#endif \ No newline at end of file diff --git a/smart_bridge/src/components/api/ServoImpl.h b/smart_bridge/src/components/api/ServoImpl.h new file mode 100644 index 0000000..0031fa3 --- /dev/null +++ b/smart_bridge/src/components/api/ServoImpl.h @@ -0,0 +1,20 @@ +#ifndef __SERVO_IMPL__ +#define __SERVO_IMPL__ +#include "Arduino.h" +#include "ServoTimer2.h" + +class ServoImpl +{ +public: + ServoImpl(int pin); + + void write(int angle); + + void detach(); + +private: + ServoTimer2 servo; + int pin; +}; + +#endif \ No newline at end of file diff --git a/smart_bridge/src/components/impl/AbstractButton.cpp b/smart_bridge/src/components/impl/AbstractButton.cpp new file mode 100644 index 0000000..b4500a6 --- /dev/null +++ b/smart_bridge/src/components/impl/AbstractButton.cpp @@ -0,0 +1,13 @@ +#include "../api/AbstractButton.h" + +AbstractButton::AbstractButton(){} + +void AbstractButton::updateSyncTime(long time) +{ + lastTimeSync = time; +} + +long AbstractButton::getLastSyncTime() +{ + return lastTimeSync; +} \ No newline at end of file diff --git a/smart_bridge/src/components/impl/Button.cpp b/smart_bridge/src/components/impl/Button.cpp index 8a0ee21..8ecb538 100644 --- a/smart_bridge/src/components/impl/Button.cpp +++ b/smart_bridge/src/components/impl/Button.cpp @@ -1,39 +1,20 @@ #include "../api/Button.h" -#include "config/config.h" #include "Arduino.h" -Button::Button(int pin) : buttonState(BUTTON_RELEASED) +Button::Button(int pin) { - this->buttonPin = BUTTON_PIN; - lastTimeButtonWasInSync = 0; + this->pin = pin; pinMode(pin, INPUT); - updateButtonState(); + sync(); } -bool Button::checkButtonPressStatus() +bool Button::isPressed() { - return buttonState == BUTTON_PRESSED; + return pressed; } -bool Button::checkButtonReleaseStatus() +void Button::sync() { - return buttonState == BUTTON_RELEASED; -} - -void Button::updateButtonState() -{ - // If the button is pressed, the pin will be HIGH. - buttonState = digitalRead(buttonPin) == HIGH ? BUTTON_PRESSED : BUTTON_RELEASED; - // Update the last time the button state was synchronized. - setLastButtonSyncTime(millis()); -} - -void Button::setLastButtonSyncTime(long time) -{ - lastTimeButtonWasInSync = time; -} - -long Button::retrieveLastButtonSyncTime() -{ - return lastTimeButtonWasInSync; + pressed = digitalRead(pin) == HIGH; + updateSyncTime(millis()); } \ No newline at end of file diff --git a/smart_bridge/src/components/impl/ServoImpl.cpp b/smart_bridge/src/components/impl/ServoImpl.cpp new file mode 100644 index 0000000..6cb19c7 --- /dev/null +++ b/smart_bridge/src/components/impl/ServoImpl.cpp @@ -0,0 +1,17 @@ +#include "../api/ServoImpl.h" +#include "Arduino.h" + +ServoImpl::ServoImpl(int pin) +{ + servo.attach(pin); +} + +void ServoImpl::write(int angle) +{ + servo.write(map(angle, 0, 180, 750, 2250)); +} + +void ServoImpl::detach() +{ + servo.detach(); +} \ No newline at end of file diff --git a/smart_bridge/src/main.cpp b/smart_bridge/src/main.cpp index a7f9759..09c9031 100644 --- a/smart_bridge/src/main.cpp +++ b/smart_bridge/src/main.cpp @@ -1,14 +1,15 @@ #include "Arduino.h" +#include "config/config.h" #include "kernel/Scheduler.h" +#include "kernel/SerialReceiver.h" #include "tasks/BlinkTask.h" #include "tasks/CheckInTask.h" -#include "config/config.h" #include "tasks/TransitTask.h" #include "tasks/WaitingTask.h" #include "tasks/WashingTask.h" #include "tasks/CheckOutTask.h" #include "tasks/CountDown.h" -#include "kernel/SerialReceiver.h" +#include "tasks/WaitForClickTask.h" // #include "tasks/ServoTestTask.h" Scheduler scheduler; @@ -27,17 +28,20 @@ void setup() // WaitingTask *waitingTask = new WaitingTask(); CheckInTask *checkInTask = new CheckInTask(); TransitTask *transitTask = new TransitTask(blinkTask); + WaitForClickTask *waitForClickTask = new WaitForClickTask(); // WashingTask *washingTask = new WashingTask(blinkTask, new CountDown(N3)); // TODO: CheckOutTask *checkOutTask = new CheckOutTask(blinkTaskForCheckOutTransit); - CountDown *countDown = new CountDown(N3); // NOTE: This is just a test. - countDown->tick(); // NOTE: This is just a test. + // CountDown *countDown = new CountDown(N3); // NOTE: This is just a test. + // countDown->setActive(true); // NOTE: This is just a test. + + /// serialReceiver = new SerialReceiver(); //test test test test receiver - serialReceiver = new SerialReceiver(); //test test test test receiver /**DEPENDENCIES**/ // checkInTask->addDependency(waitingTask); transitTask->addDependency(checkInTask); + waitForClickTask->addDependency(transitTask); // washingTask->addDependency(transitTask); // checkOutTask->addDependency(washingTask); @@ -46,9 +50,10 @@ void setup() // scheduler.addTask(countDown); // NOTE: This is just a test. // scheduler.addTask(waitingTask); scheduler.addTask(checkInTask); - Serial.println("err:errore"); + //Serial.println("err:errore"); scheduler.addTask(transitTask); scheduler.addTask(blinkTask); + scheduler.addTask(waitForClickTask); // scheduler.addTask(washingTask); @@ -58,7 +63,7 @@ void setup() void loop() { - serialReceiver->readData(); //@EMANUELE this is a test to try the serialReceiver, it must go instantiate when the arduino is in error state + // serialReceiver->readData(); //@EMANUELE this is a test to try the serialReceiver, it must go instantiate when the arduino is in error state //read the class briefs scheduler.schedule(); } \ No newline at end of file diff --git a/smart_bridge/src/tasks/ServoTestTask.cpp b/smart_bridge/src/tasks/ServoTestTask.cpp new file mode 100644 index 0000000..f5c8069 --- /dev/null +++ b/smart_bridge/src/tasks/ServoTestTask.cpp @@ -0,0 +1,22 @@ +#include "ServoTestTask.h" + +void ServoTestTask::tick() +{ + Serial.println("ServoTestTask::tick()"); + switch (this->getState()) + { + case OPEN: + Serial.println("ServoTestTask::OPEN"); + this->servo.writeMicroseconds(1500); + this->setState(CLOSE); + break; + case CLOSE: + // if (this->elapsedTime() >= 1000) + { + Serial.println("ServoTestTask::CLOSE"); + this->servo.writeMicroseconds(0); + this->setState(OPEN); + } + break; + } +} \ No newline at end of file diff --git a/smart_bridge/src/tasks/ServoTestTask.h b/smart_bridge/src/tasks/ServoTestTask.h new file mode 100644 index 0000000..cdd6ede --- /dev/null +++ b/smart_bridge/src/tasks/ServoTestTask.h @@ -0,0 +1,31 @@ +#ifndef __SERVO_TEST_TASK__ +#define __SERVO_TEST_TASK__ + +#include "kernel/TaskWithState.h" +#include "config/config.h" +#include + +class ServoTestTask : public TaskWithState +{ +public: + ServoTestTask() : TaskWithState() + { + // this->servo = new Servo(); + this->servo.detach(); + Serial.println("Servo::attach" + String(this->servo.attach(SERVO_PIN))); + this->init(1000); // Periodic task, executed every 1000ms + this->setActive(true); + this->setState(OPEN); + } + + void tick() override; + +private: + Servo servo; + enum state + { + OPEN, + CLOSE + }; +}; +#endif \ No newline at end of file diff --git a/smart_bridge/src/tasks/WaitForClickTask.cpp b/smart_bridge/src/tasks/WaitForClickTask.cpp index 5f7f8b3..bdcd005 100644 --- a/smart_bridge/src/tasks/WaitForClickTask.cpp +++ b/smart_bridge/src/tasks/WaitForClickTask.cpp @@ -4,10 +4,10 @@ void WaitForClickTask::tick() { if (this->getDependency(0) != nullptr) { - Serial.println("WaitForClickTask::getDependency(0) != nullptr"); + //Serial.println("WaitForClickTask::getDependency(0) != nullptr"); if (this->getDependency(0)->isCompleted()) { - if (this->start->checkButtonReleaseStatus()) + if (this->start->isPressed()) { Serial.println("WaitForClickTask::START pressed"); this->setCompleted();