Skip to content

Commit

Permalink
[REFACTOR] Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
aleemont1 committed Dec 2, 2023
1 parent 0ce839d commit 005e2fa
Show file tree
Hide file tree
Showing 13 changed files with 42 additions and 86 deletions.
6 changes: 3 additions & 3 deletions smart_bridge/src/components/impl/Button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
Button::Button(int pin)
{
this->pin = pin;
pinMode(pin, INPUT);
pinMode(this->pin, INPUT);
sync();
}

bool Button::isPressed()
{
return pressed;
return this->pressed;
}

void Button::sync()
{
pressed = digitalRead(pin) == HIGH;
this->pressed = digitalRead(pin) == HIGH;
updateSyncTime(millis());
}
6 changes: 3 additions & 3 deletions smart_bridge/src/components/impl/ServoImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@

ServoImpl::ServoImpl(int pin)
{
servo.attach(pin);
this->servo.attach(pin);
}

void ServoImpl::write(int angle)
{
servo.write(map(angle, 0, 180, 750, 2250));
this->servo.write(map(angle, 0, 180, 750, 2250));
}

void ServoImpl::detach()
{
servo.detach();
this->detach.detach();
}
2 changes: 1 addition & 1 deletion smart_bridge/src/kernel/Scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void Scheduler::schedule()
{

unsigned long now = millis();
if ((now - lastRunTime) >= schedulerPeriod) // It's time for scheduler to tick
if ((now - lastRunTime) >= schedulerPeriod) // It's time for to tick
{
for (register unsigned int i = 0; i < numTasks; ++i) // For each task in the scheduler
{
Expand Down
2 changes: 2 additions & 0 deletions smart_bridge/src/tasks/CheckInTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ void CheckInTask::tick()
#ifdef __LOG
Serial.println("CheckInTask::Turned on L1");
#endif
this->lcd->write("Welcome to the car wash", 0, 0);
this->setState(WAITING); // Set the state to WAITING
break;
case WAITING:
Expand All @@ -23,6 +24,7 @@ void CheckInTask::tick()
#ifdef __LOG
Serial.println("CheckInTask::Opened the gate");
#endif
this->lcd->write("Proceed to the washing area", 0, 0);
this->setCompleted(); // Mark the task as completed
}
}
Expand Down
5 changes: 4 additions & 1 deletion smart_bridge/src/tasks/CheckInTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
#define __CHECK_IN_TASK__

#include "kernel/DependantTaskWithState.h"
#include "components/api/Led.h"
#include "config/config.h"
#include "components/api/Led.h"
#include "components/api/LCD.h"
#include "components/api/ServoImpl.h"

/**
Expand All @@ -19,6 +20,7 @@ class CheckInTask : public DependantTaskWithState
{
this->L1 = new Led(L1_PIN);
this->L2 = new Led(L2_PIN);
this->lcd = new LCD(0x27, 16, 2);
this->gate = new ServoImpl(SERVO_PIN);
this->init();
this->setState(STARTED);
Expand All @@ -34,6 +36,7 @@ class CheckInTask : public DependantTaskWithState
};
Led *L1;
Led *L2;
LCD *lcd;
ServoImpl *gate;
};

Expand Down
7 changes: 4 additions & 3 deletions smart_bridge/src/tasks/CheckOutTask.h
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
#ifndef __CHECK_OUT_TASK__
#define __CHECK_OUT_TASK__

#include "config/config.h"
#include "kernel/DependantTaskWithState.h"
#include "BlinkTask.h"
#include "components/api/Led.h"
#include "components/api/Sonar.h"
#include "config/config.h"
#include "components/api/ServoImpl.h"
#include "BlinkTask.h"


class CheckOutTask : public DependantTaskWithState
{
public:
CheckOutTask(BlinkTask *blinkTask) : DependantTaskWithState()
{
Serial.println("CheckOutTask created");
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);
Expand All @@ -24,6 +24,7 @@ class CheckOutTask : public DependantTaskWithState
this->L3->switchOn();
this->init();
this->setState(OPENING_GATE);
Serial.println("CheckOutTask created");
}
void tick() override;

Expand Down
24 changes: 12 additions & 12 deletions smart_bridge/src/tasks/CountDown.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void CountDown::resetCountDown(int newCountDown)

void CountDown::decreaseCountDown(int decrement)
{
if (getCountDown() - decrement < 0)
if (getCountDown() - decrement <= 0) // NOTE: <= 0 non solo < 0
{
endsCountDown();
}
Expand All @@ -36,13 +36,13 @@ void CountDown::increaseCountDown(int increment)

void CountDown::pauseCountDown()
{
this->pausedCountDown = getCountDown();
this->pausedCountDown = getCountDown(); // NOTE: Credo sia superfluo salvare il valore, puoi disattivare il task e poi riprendere da dove eri, ma andrebbe controllata meglio questa cosa.
this->setActive(false);
}

void CountDown::startCountDown()
void CountDown::startCountDown() // NOTE: Chiama direttamente this->setActive(true) dove ti serve, risparmi spazio sullo stack.
{
this->setActive(true);
this->setActive(true);
}

void CountDown::printCountDown()
Expand All @@ -56,33 +56,33 @@ void CountDown::endsCountDown()
{
if (getCountDown() <= 0)
{
this->isCompleted();
this->printsEndsCountdown();
this->resetCountDown(N3);
this->isCompleted(); // NOTE: non credo serva
this->printsEndsCountdown(); // NOTE: meglio chiamare direttamente la stampa, si usa meno spazio sullo stack
this->resetCountDown(N3);
Serial.println("CountDown::Countdown resetted");
this->setCompleted();
}
}

void CountDown::printsEndsCountdown()
void CountDown::printsEndsCountdown() // NOTE: meglio chiamare direttamente la stampa, si usa meno spazio sullo stack.
{
Serial.println("CountDown::Countdown ended!");
}

bool CountDown::isCountDownActive()
bool CountDown::isCountDownActive() // NOTE: Chiama direttamente this->isActive() dove ti serve, risparmi spazio sullo stack.
{
return this->isActive();
}

void CountDown::stopCountDown()
void CountDown::stopCountDown() // NOTE: Resettalo prima di disattivarlo.
{
this->setActive(false);
this->resetCountDown(N3);
}

void CountDown::resumeCountDown()
{
if (this->pausedCountDown != -1)
if (this->pausedCountDown != -1) // NOTE: Credo che tu possa controllare direttamente this->isActive() invece di usare una variabile aggiuntiva, a meno di altri utilizzi particolari di questa variabile.
{
this->countDown = this->pausedCountDown;
this->pausedCountDown = -1;
Expand All @@ -95,5 +95,5 @@ void CountDown::tick()
startCountDown();
printCountDown();
decreaseCountDown();
endsCountDown();
endsCountDown(); // NOTE: lo chiami già in decreaseCountDown(), non serve chiamarlo qui.
}
22 changes: 0 additions & 22 deletions smart_bridge/src/tasks/ServoTestTask.cpp

This file was deleted.

31 changes: 0 additions & 31 deletions smart_bridge/src/tasks/ServoTestTask.h

This file was deleted.

11 changes: 5 additions & 6 deletions smart_bridge/src/tasks/TransitTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ void TransitTask::tick()
{
case READING_DISTANCE:
this->L2->switchOn();
this->distance = sonar->detectDistance();
#ifdef __LOG
Serial.println("TransitTask::Distance: " + String(this->distance));
#endif
if (this->distance < MINDIST)
if (this->sonar->detectDistance() < MINDIST)
{
this->resetTime();
this->setState(CHECKING_DISTANCE);
Expand All @@ -29,19 +28,18 @@ void TransitTask::tick()
{
this->blinkTask->setActive(true);
}
this->distance = sonar->detectDistance();
#ifdef __LOG
Serial.println("TransitTask::Distance: " + String(this->distance));
Serial.println("TransitTask::Distance: " + String(this->sonar->detectDistance()));
#endif
if (this->distance < MINDIST)
if (this->sonar->detectDistance() < MINDIST)
{
#ifdef __DEBUG
Serial.println("TransitTask::Elapsed: " + String(this->elapsedTime()));
#endif
if (this->elapsedTime() >= (N2 * 1000))
{
#ifdef __LOG
Serial.println("TransitTask::Distance: " + String(this->distance));
Serial.println("TransitTask::Distance: " + String(this->sonar->detectDistance()));
Serial.println("TransitTask::Car entered");
Serial.println("TransitTask::Closing gate");
#endif
Expand All @@ -51,6 +49,7 @@ void TransitTask::tick()
this->blinkTask->setActive(false);
}
this->L2->switchOn();
this->lcd->write("Ready to wash", 0, 0);
this->setCompleted();
}
}
Expand Down
6 changes: 4 additions & 2 deletions smart_bridge/src/tasks/TransitTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "components/api/Sonar.h"
#include "components/api/Led.h"
#include "components/api/ServoImpl.h"
#include "components/api/LCD.h"
/**
* @class TransitTask
* @brief This task reads the distance of the car while entering the washing area.
Expand All @@ -23,16 +24,17 @@ class TransitTask : public DependantTaskWithState
*/
TransitTask(BlinkTask *blinkTask) : DependantTaskWithState()
{
Serial.println("TransitTask created");
this->sonar = new Sonar(SONAR_TRIG_PIN, SONAR_ECHO_PIN, SONAR_MAX_TIME);
this->L2 = new Led(L2_PIN);
this->lcd = new LCD(0x27, 16, 2);
// TODO: FIX SERVO.
this->gate = new ServoImpl(SERVO_PIN);
this->blinkTask = blinkTask;
this->blinkTask->init(100);
this->blinkTask->setActive(false);
this->init();
this->setState(READING_DISTANCE);
Serial.println("TransitTask created");
};

void tick() override;
Expand All @@ -44,9 +46,9 @@ class TransitTask : public DependantTaskWithState
CHECKING_DISTANCE
};
Led *L2;
LCD *lcd;
Sonar *sonar;
ServoImpl *gate;
float distance;
BlinkTask *blinkTask;
};

Expand Down
2 changes: 1 addition & 1 deletion smart_bridge/src/tasks/WaitForClickTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ void WaitForClickTask::tick()
{
if (this->getDependency(0) != nullptr)
{
//Serial.println("WaitForClickTask::getDependency(0) != nullptr");
if (this->getDependency(0)->isCompleted())
{
this->lcd->write("Ready to wash, press START to continue",0,0);
if (this->start->isPressed())
{
Serial.println("WaitForClickTask::START pressed");
Expand Down
4 changes: 3 additions & 1 deletion smart_bridge/src/tasks/WaitForClickTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
#define __WAIT_FOR_CLICK_TASK__

#include "kernel/DependantTask.h"
#include "components/api/Button.h"
#include "config/config.h"
#include "components/api/Button.h"
#include "components/api/LCD.h"

#include "Arduino.h"
Expand All @@ -20,6 +20,7 @@ class WaitForClickTask : public DependantTask
WaitForClickTask() : DependantTask()
{
this->start = new Button(BUTTON_PIN);
this->lcd = new LCD(0x27,16,2);
this->init();
Serial.println("WaitForClickTask created");
};
Expand All @@ -28,5 +29,6 @@ class WaitForClickTask : public DependantTask

private:
Button *start;
LCD *lcd;
};
#endif

0 comments on commit 005e2fa

Please sign in to comment.