Skip to content

Commit

Permalink
Merge pull request #8 from aleemont1/scheduler
Browse files Browse the repository at this point in the history
Implement scheduler
  • Loading branch information
aleemont1 authored Nov 24, 2023
2 parents 5826ec6 + 41f0abe commit c061051
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 18 deletions.
37 changes: 19 additions & 18 deletions smart_bridge/platformio.ini
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[env:uno]
platform = atmelavr
board = uno
framework = arduino
lib_deps =
arduino-libraries/Servo@^1.2.1
marcoschwartz/LiquidCrystal_I2C@^1.1.4
Wire
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[env:uno]
platform = atmelavr
board = uno
framework = arduino
lib_deps =
arduino-libraries/Servo@^1.2.1
marcoschwartz/LiquidCrystal_I2C@^1.1.4
Wire
paulstoffregen/TimerOne@^1.1.1
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);
}
}
}
}
19 changes: 19 additions & 0 deletions smart_bridge/src/kernel/Scheduler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef __SCHEDULER__
#define __SCHEDULER__

#include "Task.h"

#define MAX_TASKS 30
class Scheduler {
//A simple cooperative scheduler to execute tasks
Task *tasks[MAX_TASKS];
int numTasks;
int period;

public:
void init(int period);
virtual bool addTask(Task *task);
virtual void schedule();
};

#endif

0 comments on commit c061051

Please sign in to comment.