Skip to content

Commit

Permalink
Good button and servo impl
Browse files Browse the repository at this point in the history
  • Loading branch information
aleemont1 committed Dec 1, 2023
1 parent d1df622 commit eaff63a
Show file tree
Hide file tree
Showing 11 changed files with 157 additions and 104 deletions.
1 change: 1 addition & 0 deletions smart_bridge/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
21 changes: 21 additions & 0 deletions smart_bridge/src/components/api/AbstractButton.h
Original file line number Diff line number Diff line change
@@ -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
78 changes: 10 additions & 68 deletions smart_bridge/src/components/api/Button.h
Original file line number Diff line number Diff line change
@@ -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__
#endif
20 changes: 20 additions & 0 deletions smart_bridge/src/components/api/ServoImpl.h
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions smart_bridge/src/components/impl/AbstractButton.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "../api/AbstractButton.h"

AbstractButton::AbstractButton(){}

void AbstractButton::updateSyncTime(long time)
{
lastTimeSync = time;
}

long AbstractButton::getLastSyncTime()
{
return lastTimeSync;
}
35 changes: 8 additions & 27 deletions smart_bridge/src/components/impl/Button.cpp
Original file line number Diff line number Diff line change
@@ -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());
}
17 changes: 17 additions & 0 deletions smart_bridge/src/components/impl/ServoImpl.cpp
Original file line number Diff line number Diff line change
@@ -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();
}
19 changes: 12 additions & 7 deletions smart_bridge/src/main.cpp
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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();
}
22 changes: 22 additions & 0 deletions smart_bridge/src/tasks/ServoTestTask.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
}
31 changes: 31 additions & 0 deletions smart_bridge/src/tasks/ServoTestTask.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef __SERVO_TEST_TASK__
#define __SERVO_TEST_TASK__

#include "kernel/TaskWithState.h"
#include "config/config.h"
#include <Servo.h>

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
4 changes: 2 additions & 2 deletions smart_bridge/src/tasks/WaitForClickTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit eaff63a

Please sign in to comment.