diff --git a/smart_bridge/src/components/api/LCD.h b/smart_bridge/src/components/api/LCD.h index e69724d..d9a50e3 100644 --- a/smart_bridge/src/components/api/LCD.h +++ b/smart_bridge/src/components/api/LCD.h @@ -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); diff --git a/smart_bridge/src/components/api/Pir.h b/smart_bridge/src/components/api/Pir.h index 3b2d4dc..69a8161 100644 --- a/smart_bridge/src/components/api/Pir.h +++ b/smart_bridge/src/components/api/Pir.h @@ -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__ \ No newline at end of file diff --git a/smart_bridge/src/components/impl/Pir.cpp b/smart_bridge/src/components/impl/Pir.cpp index 63b117e..c556b95 100644 --- a/smart_bridge/src/components/impl/Pir.cpp +++ b/smart_bridge/src/components/impl/Pir.cpp @@ -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; } diff --git a/smart_bridge/src/main.cpp b/smart_bridge/src/main.cpp index 31afead..a3b86ee 100644 --- a/smart_bridge/src/main.cpp +++ b/smart_bridge/src/main.cpp @@ -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" @@ -20,6 +21,7 @@ Scheduler scheduler; SerialReceiver *serialReceiver; +LCD *lcd; void setup() { diff --git a/smart_bridge/src/tasks/SleepingTask.cpp b/smart_bridge/src/tasks/SleepingTask.cpp index c7b0822..d8e58c9 100644 --- a/smart_bridge/src/tasks/SleepingTask.cpp +++ b/smart_bridge/src/tasks/SleepingTask.cpp @@ -1,7 +1,48 @@ #include "SleepingTask.h" +SleepingTask* sleepingTaskInstance = nullptr; + +static void staticWakeUp() { + if (sleepingTaskInstance != nullptr) { + sleepingTaskInstance->wakeUp(); + } +} void SleepingTask::tick() { - /*TODO*/ -} \ No newline at end of file + 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); +} diff --git a/smart_bridge/src/tasks/SleepingTask.h b/smart_bridge/src/tasks/SleepingTask.h index 1990ce8..ab954ab 100644 --- a/smart_bridge/src/tasks/SleepingTask.h +++ b/smart_bridge/src/tasks/SleepingTask.h @@ -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 diff --git a/smart_bridge/src/tasks/WaitingTask.cpp b/smart_bridge/src/tasks/WaitingTask.cpp index 47c4168..757cc09 100644 --- a/smart_bridge/src/tasks/WaitingTask.cpp +++ b/smart_bridge/src/tasks/WaitingTask.cpp @@ -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"); + } + + } + } } \ No newline at end of file diff --git a/smart_bridge/src/tasks/WaitingTask.h b/smart_bridge/src/tasks/WaitingTask.h index 8fe61ef..a9ab900 100644 --- a/smart_bridge/src/tasks/WaitingTask.h +++ b/smart_bridge/src/tasks/WaitingTask.h @@ -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"); @@ -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; };