Skip to content

Commit

Permalink
Merge pull request #27 from aleemont1/alessandro
Browse files Browse the repository at this point in the history
Alessandro
  • Loading branch information
aleemont1 authored Dec 2, 2023
2 parents 18a07ed + a1e5721 commit 0ce839d
Show file tree
Hide file tree
Showing 16 changed files with 169 additions and 124 deletions.
2 changes: 1 addition & 1 deletion smart_bridge/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ lib_deps =
marcoschwartz/LiquidCrystal_I2C@^1.1.4
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();
}
2 changes: 1 addition & 1 deletion smart_bridge/src/kernel/Scheduler.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "Scheduler.h"
#include <TimerOne.h>
#include "Arduino.h"

Scheduler::Scheduler() : numTasks(0), lastRunTime(0) {}

Expand Down
21 changes: 13 additions & 8 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 @@ -18,7 +19,7 @@ SerialReceiver *serialReceiver;
void setup()
{
Serial.begin(9600);
scheduler.init(50); // NOTE: Might be set higher to use less power, needs testing.
scheduler.init(100); // NOTE: Might be set higher to use less power, needs testing.
/**CREATE TASKS**/
// ServoTestTask *servoTask = new ServoTestTask();
BlinkTask *blinkTask = new BlinkTask(L2_PIN);
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();
}
7 changes: 3 additions & 4 deletions smart_bridge/src/tasks/CheckInTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "kernel/DependantTaskWithState.h"
#include "components/api/Led.h"
#include "config/config.h"
#include <Servo.h>
#include "components/api/ServoImpl.h"

/**
* @class CheckInTask
Expand All @@ -19,8 +19,7 @@ class CheckInTask : public DependantTaskWithState
{
this->L1 = new Led(L1_PIN);
this->L2 = new Led(L2_PIN);
this->gate = new Servo();
this->gate->attach(SERVO_PIN);
this->gate = new ServoImpl(SERVO_PIN);
this->init();
this->setState(STARTED);
Serial.println("CheckInTask created");
Expand All @@ -35,7 +34,7 @@ class CheckInTask : public DependantTaskWithState
};
Led *L1;
Led *L2;
Servo *gate;
ServoImpl *gate;
};

#endif
1 change: 0 additions & 1 deletion smart_bridge/src/tasks/CheckOutTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ void CheckOutTask::tick()
case OPENING_GATE:
Serial.println("CheckOutTask::Opening the gate");
gate->write(90);
delay(2000); // Simulates the time needed to open the gate
Serial.println("CheckOutTask::Opened the gate");
this->setState(CHECKING_DISTANCE);
break;
Expand Down
8 changes: 3 additions & 5 deletions smart_bridge/src/tasks/CheckOutTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "components/api/Led.h"
#include "components/api/Sonar.h"
#include "config/config.h"
#include <Servo.h>
#include "components/api/ServoImpl.h"

class CheckOutTask : public DependantTaskWithState
{
Expand All @@ -17,9 +17,7 @@ class CheckOutTask : public DependantTaskWithState
this->L2 = new Led(L2_PIN);
this->L3 = new Led(L3_PIN);
this->sonar = new Sonar(SONAR_TRIG_PIN, SONAR_ECHO_PIN, SONAR_MAX_TIME);
this->gate = new Servo();
this->gate->detach();
this->gate->attach(SERVO_PIN);
this->gate = new ServoImpl(SERVO_PIN);
this->blinkTask = blinkTask;
this->blinkTask->isCompleted();
this->blinkTask->setActive(false);
Expand All @@ -40,7 +38,7 @@ class CheckOutTask : public DependantTaskWithState

Led *L2;
Led *L3;
Servo *gate;
ServoImpl *gate;
Sonar *sonar;
Task *blinkTask;
float detectedDistance; // Distance of the car from the sensor
Expand Down
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;
// }
// }
Loading

0 comments on commit 0ce839d

Please sign in to comment.