-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Correct Scheduler and first Tasks
- Loading branch information
Showing
12 changed files
with
199 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,30 @@ | ||
#include "Scheduler.h" | ||
#include <TimerOne.h> | ||
|
||
volatile bool timerFlag = false; | ||
Scheduler::Scheduler() : numTasks(0), lastMillis(0) {} | ||
|
||
void timeHandler(void) | ||
{ | ||
timerFlag = true; | ||
void Scheduler::init(int intervalMillis) { | ||
// Set up the interval for the scheduler | ||
// You may want to use a timer or other mechanisms for more accurate timing | ||
// For simplicity, using millis() in this example | ||
this->intervalMillis = intervalMillis; | ||
lastMillis = millis() - intervalMillis; | ||
} | ||
|
||
void Scheduler::init(int period) | ||
{ | ||
this->period = period; | ||
timerFlag = false; | ||
long u_period = 1000L * period; | ||
Timer1.initialize(u_period); | ||
Timer1.attachInterrupt(timeHandler); | ||
this->numTasks = 0; | ||
} | ||
|
||
bool Scheduler::addTask(Task *task) | ||
{ | ||
if (this->numTasks < MAX_TASKS - 1) | ||
{ | ||
this->tasks[this->numTasks++] = task; | ||
return true; | ||
void Scheduler::addTask(Task *task) { | ||
if (numTasks < sizeof(tasks) / sizeof(tasks[0])) { | ||
tasks[numTasks++] = task; | ||
} | ||
return false; | ||
} | ||
|
||
void Scheduler::schedule() | ||
{ | ||
while (!timerFlag); | ||
timerFlag = false; | ||
|
||
for (int i = 0; i < this->numTasks; i++) | ||
{ | ||
if (this->tasks[i]->isActive()) | ||
{ | ||
if (this->tasks[i]->isPeriodic()) | ||
{ | ||
if (this->tasks[i]->updateAndCheckTime(this->period)) | ||
{ | ||
this->tasks[i]->tick(); | ||
} | ||
} | ||
else | ||
{ | ||
this->tasks[i]->tick(); | ||
} | ||
if (this->tasks[i]->isCompleted()) | ||
{ | ||
this->tasks[i]->setActive(false); | ||
} | ||
} | ||
void Scheduler::schedule() { | ||
unsigned long currentMillis = millis(); | ||
if (currentMillis - lastMillis >= intervalMillis) { // Adjust the interval as needed | ||
for (int i = 0; i < numTasks; ++i) { | ||
if (tasks[i] != nullptr && tasks[i]->isActive()) { | ||
tasks[i]->tick(); | ||
} | ||
} | ||
} | ||
lastMillis = currentMillis; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#ifndef __TASK_WITH_TIMER__ | ||
#define __TASK_WITH_TIMER__ | ||
|
||
#include "Task.h" | ||
#include <Arduino.h> | ||
|
||
class TaskWithTimer : public Task | ||
{ | ||
public: | ||
TaskWithTimer() : Task(){ | ||
this->timerTimestamp = millis(); | ||
}; // Default constructor | ||
|
||
long elapsedTime() | ||
{ | ||
return millis() - timerTimestamp; | ||
} | ||
private: | ||
long timerTimestamp; | ||
}; | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,28 @@ | ||
#include "Arduino.h" | ||
#include "kernel/Scheduler.h" | ||
#include "tasks/BlinkTask.h" | ||
#include "tasks/CheckInTask.h" | ||
#include "config/config.h" | ||
|
||
Scheduler scheduler; | ||
|
||
void setup() | ||
{ | ||
Serial.begin(9600); | ||
scheduler.init(500); | ||
//NOTE: THIS IS JUST A TEST TASK | ||
BlinkTask *blinkTask = new BlinkTask(L3_PIN); | ||
blinkTask->init(500); | ||
blinkTask->setActive(true); | ||
//NOTE: THIS IS THE REAL TASK | ||
CheckInTask *checkInTask = new CheckInTask(); | ||
checkInTask->init(); | ||
checkInTask->setActive(true); | ||
scheduler.addTask(checkInTask); | ||
scheduler.addTask(blinkTask); | ||
} | ||
|
||
void loop() | ||
{ | ||
|
||
scheduler.schedule(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#include "BlinkTask.h" | ||
|
||
BlinkTask::BlinkTask(int pin) | ||
{ | ||
this->pin = pin; | ||
} | ||
|
||
void BlinkTask::init(int period) | ||
{ | ||
Task::init(period); | ||
led = new Led(pin); | ||
state = OFF; | ||
} | ||
|
||
void BlinkTask::tick() | ||
{ | ||
switch (state) | ||
{ | ||
case OFF: | ||
led->switchOn(); | ||
state = ON; | ||
break; | ||
case ON: | ||
led->switchOff(); | ||
state = OFF; | ||
break; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#ifndef __BLINK_TASK__ | ||
#define __BLINK_TASK__ | ||
|
||
#include "kernel/Task.h" | ||
#include "components/api/Led.h" | ||
|
||
class BlinkTask: public Task { | ||
|
||
int pin; | ||
Light* led; | ||
enum { ON, OFF} state; | ||
|
||
public: | ||
|
||
BlinkTask(int pin); | ||
void init(int period); | ||
void tick(); | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#include "CheckInTask.h" | ||
|
||
// Implement the CheckInTask class that executes the checkin process. | ||
// The checkin process is as follows: | ||
// 1. Turn on the L1 led. | ||
// 2. Wait for N1 seconds. | ||
// 3. Turn on the L2 led. | ||
// 4. Open the gate. | ||
|
||
void CheckInTask::tick() | ||
{ | ||
Serial.println("CheckInTask::started"); | ||
switch(this->getState()) { | ||
|
||
case STARTED: | ||
L1->switchOn(); // Turn on L1 | ||
Serial.println("CheckInTask::Turned on L1"); | ||
this->setState(WAITING); // Set the state to STATE1 | ||
break; | ||
case WAITING: | ||
if (this->elapsedTime() >= (N1 * 1000)) // After N1 seconds have elapsed | ||
{ | ||
L1->switchOff(); // Turn off L1 | ||
Serial.println("CheckInTask::Turned off L1"); | ||
L2->switchOn(); // Turn on L2 | ||
Serial.println("CheckInTask::Turned on L2"); | ||
gate->write(90); // Open the gate | ||
Serial.println("CheckInTask::Opened the gate"); | ||
this->setCompleted(); // Mark the task as completed | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#ifndef __CHECK_IN_TASK__ | ||
#define __CHECK_IN_TASK__ | ||
|
||
#include "kernel/TaskWithState.h" | ||
#include "components/api/Led.h" | ||
#include "config/config.h" | ||
#include <Servo.h> | ||
|
||
class CheckInTask : public TaskWithState | ||
{ | ||
public: | ||
CheckInTask() : TaskWithState() | ||
{ | ||
this->L1 = new Led(L1_PIN); | ||
this->L2 = new Led(L2_PIN); | ||
this->gate = new Servo(); | ||
gate->attach(SERVO_PIN); | ||
Serial.println("CheckInTask created"); | ||
this->setState(STARTED); | ||
}; | ||
void tick() override; | ||
|
||
private: | ||
enum states | ||
{ | ||
STARTED, | ||
WAITING | ||
}; | ||
Led *L1; | ||
Led *L2; | ||
Servo *gate; | ||
}; | ||
|
||
#endif |