Skip to content

Commit

Permalink
Merge pull request #34 from aleemont1/Davide
Browse files Browse the repository at this point in the history
Davide
  • Loading branch information
aleemont1 authored Dec 4, 2023
2 parents 5c43327 + c27e9da commit a51d475
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 19 deletions.
4 changes: 2 additions & 2 deletions smart_bridge/src/components/api/LCD.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ class LCD {
/**
* @brief Construct a new LCD object.
*
* @param column the column of the LCD display
* @param row number of row
* @param columns the column of the LCD display
* @param rows number of row
*/
LCD(int address, int columns, int rows);
void write(const char* string, int start_col, int start_rows);
Expand Down
2 changes: 2 additions & 0 deletions smart_bridge/src/components/api/Pir.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ class Pir
* @brief The current detection status of the PIR sensor (true if movement is detected, false otherwise).
*/
bool pirDetectionStatus;


};

#endif // __PIR__
2 changes: 2 additions & 0 deletions smart_bridge/src/components/impl/Pir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ Pir::Pir(int pin) : pirDetectionStatus(OBJECT_NOT_DETECTED)

bool Pir::checkDetectedStatus()
{
updatePirState();
return pirDetectionStatus == OBJECT_DETECTED;
}

bool Pir::checkNotDetectedStatus()
{
updatePirState();
return pirDetectionStatus == OBJECT_NOT_DETECTED;
}

Expand Down
2 changes: 2 additions & 0 deletions smart_bridge/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "tasks/TransitTask.h"
#include "tasks/WaitingTask.h"
#include "tasks/WashingTask.h"
#include "tasks/SleepingTask.h"
#include "tasks/CheckOutTask.h"
#include "tasks/CountDown.h"
#include "tasks/WaitForClickTask.h"
Expand All @@ -20,6 +21,7 @@
Scheduler scheduler;

SerialReceiver *serialReceiver;
LCD *lcd;

void setup()
{
Expand Down
45 changes: 43 additions & 2 deletions smart_bridge/src/tasks/SleepingTask.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,48 @@
#include "SleepingTask.h"

SleepingTask* sleepingTaskInstance = nullptr;

static void staticWakeUp() {
if (sleepingTaskInstance != nullptr) {
sleepingTaskInstance->wakeUp();
}
}

void SleepingTask::tick()
{
/*TODO*/
}
switch (this->getState())
{
case STARTING:
this->goInSleep();
break;

case ALIVE:
if (pir->checkDetectedStatus()) {
Serial.println("OBJECT revealed");
this->setState(OBJECT_DETECTED);
}
break;

case OBJECT_DETECTED:
this->setState(ALIVE);
this->setActive(false);
this->setCompleted();
break;
}
}

void SleepingTask::goInSleep()
{
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleepingTaskInstance = this;
attachInterrupt(digitalPinToInterrupt(2), staticWakeUp, RISING);
sei();
sleep_enable();
sleep_mode();
}

void SleepingTask::wakeUp() {
detachInterrupt(digitalPinToInterrupt(2));
sleep_disable();
this->setState(ALIVE);
}
27 changes: 18 additions & 9 deletions smart_bridge/src/tasks/SleepingTask.h
Original file line number Diff line number Diff line change
@@ -1,38 +1,47 @@
#ifndef __SLEEPING_TASK__
#define __SLEEPING_TASK__

#define _MAX_TIME_BEFORE_SLEEP 10
#define _MAX_TIME_BEFORE_SLEEP 10000 /*ms*/

#include "config/config.h"
#include "components/api/Pir.h"
#include "components/api/LCD.h"
#include "kernel/DependantTaskWithState.h"
#include "avr/interrupt.h"
#include "avr/sleep.h"


/**
* @class SleepingTask
* @brief This task handle the sleeping state using the pir as sensor to detect movement and
* wake up arduino
* @brief This task handle the sleeping state using the pir as sensor to
* detect movement and wake up arduino
*/

class SleepingTask : public DependantTaskWithState
class SleepingTask : public TaskWithState
{
public:
SleepingTask(SleepingTask *sleepingTask) : DependantTaskWithState()
SleepingTask() : TaskWithState()
{
Serial.println("SleepingTask created");
this->pir = new Pir(PIR_PIN);
this->setState(WAITING_FOR_SOMEONE);
this->lcd=new LCD(0x27, 16,2);
this->init();
this->setActive(true);
this->setState(STARTING);
};
void tick() override;
void wakeUp();
void goInSleep();

private:
enum state
{
WAITING_FOR_SOMEONE,
GO_IN_SLEEP,
WAKE_UP,
STARTING,
ALIVE,
OBJECT_DETECTED
};
Pir *pir;
LCD *lcd;
};

#endif
13 changes: 12 additions & 1 deletion smart_bridge/src/tasks/WaitingTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,16 @@

void WaitingTask::tick()
{
/*TODO*/
if (this->getDependency(0) != nullptr)
{
if (this->getDependency(0)->isCompleted())
{
switch (this->getState())
{
case STARTED:
Serial.println("sono qui in waiting");
}

}
}
}
15 changes: 10 additions & 5 deletions smart_bridge/src/tasks/WaitingTask.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
#ifndef __WAITING_TASK__
#define __WAITING_TASK__

#include "kernel/TaskWithState.h"
#include "kernel/DependantTaskWithState.h"
#include "components/api/Pir.h"
#include "SleepingTask.h"
#include "config/config.h"

class WaitingTask : public TaskWithState
class WaitingTask : public DependantTaskWithState
{
public:
WaitingTask() : TaskWithState()
/**
*
*
*/
WaitingTask(SleepingTask *SleepingTask) : DependantTaskWithState()
{
this->pir=new Pir(PIR_PIN); //attached to the INTERRUPT_PIN
this->pir=new Pir(PIR_PIN); //attached to the INTERRUPT_PIN 2
this->init();
this->setState(STARTED);
Serial.println("WaitingTask created");
Expand All @@ -22,7 +27,7 @@ class WaitingTask : public TaskWithState
{
STARTED, //is just left ... so
IN_SUSPENSION, //is going to go in sleep mode
FINSHED //is going to go in checkin state
FINISHED //is going to go in checkin state
};
Pir *pir;
};
Expand Down

0 comments on commit a51d475

Please sign in to comment.