Skip to content

Commit

Permalink
[SCHEDULER] Implement basic scheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
aleemont1 committed Nov 24, 2023
1 parent ba99ac0 commit 41f0abe
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions smart_bridge/src/kernel/Scheduler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include "Scheduler.h"
#include <TimerOne.h>

volatile bool timerFlag = false;

void timeHandler(void)
{
timerFlag = true;
}

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;
}
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);
}
}
}
}

0 comments on commit 41f0abe

Please sign in to comment.