Skip to content

Commit

Permalink
add scheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
r-shf committed Oct 19, 2015
1 parent 862a535 commit 8e7eb60
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 17 deletions.
19 changes: 5 additions & 14 deletions filters/ehealth/EHealthSensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 32,7 @@ FilterRegister<EHealthSensor> EHealthSensor::reg("ehealthsensor");
EHealthSensor::EHealthSensor(const string & name) :
Filter(name) {
output = createOutputPort<SensorData>("ehealthdata output");
currentTime = 0;
minPeriod = 50;
maxPeriod = 20000;
scheduler = new Scheduler(50,20000);
}

void EHealthSensor::init() {
Expand All @@ -50,7 48,7 @@ void EHealthSensor::init() {
}

void EHealthSensor::readSensor(Sensor * sensor) {
if (currentTime % sensor->getSamplingPeriod() == 0)
if (scheduler->timeToExecute(sensor->getSamplingPeriod()))
{
output->lock();
SensorData * outputData = output->get();
Expand All @@ -60,7 58,8 @@ void EHealthSensor::readSensor(Sensor * sensor) {
}

void EHealthSensor::run() {
high_resolution_clock::time_point start = high_resolution_clock::now();

scheduler->start();

//readSensor(&airflowSensor);
//readSensor(&bloodpressureSensor);
Expand All @@ -72,16 71,8 @@ void EHealthSensor::run() {
readSensor(&pulsioximeterSensor);
readSensor(&temperatureSensor);

high_resolution_clock::time_point end = high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>( end - start ).count();


currentTime =minPeriod;
if (currentTime >= maxPeriod)
currentTime = 0;
scheduler->end();

//cout << "Duration:" << duration << endl;
std::this_thread::sleep_for(std::chrono::microseconds(minPeriod*1000 - duration));

}

Expand Down
7 changes: 4 additions & 3 deletions filters/ehealth/EHealthSensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 25,8 @@
#include "core/Filter.h"
#include "core/Port.h"

#include "filters/ehealth/tools/Scheduler.h"

#include <ehealthsensor/Sensor.h>

struct EHealthSensor: public tmf::Filter {
Expand All @@ -33,9 35,8 @@ struct EHealthSensor: public tmf::Filter {
tmf::OutputPort<ehealthsensor::SensorData> * output;

static tmf::FilterRegister<EHealthSensor> reg;

int currentTime, minPeriod, maxPeriod;


Scheduler * scheduler;

ehealthsensor::AirFlowSensor airflowSensor;
ehealthsensor::BloodPressureSensor bloodpressureSensor;
Expand Down
50 changes: 50 additions & 0 deletions filters/ehealth/tools/Scheduler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 1,50 @@
/*
*
* Tiny Multimedia Framework
* Copyright (C) 2014 Arash Shafiei
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

#include "filters/ehealth/tools/Scheduler.h"


Scheduler::Scheduler(int min, int max): minPeriod(min), maxPeriod(max), currentTime(0) {

}

void Scheduler::start() {
startTime = std::chrono::high_resolution_clock::now();
}

void Scheduler::end() {
endTime = std::chrono::high_resolution_clock::now();

auto duration = std::chrono::duration_cast<std::chrono::microseconds>( endTime - startTime ).count();

currentTime =minPeriod;
if (currentTime >= maxPeriod)
currentTime = 0;

std::this_thread::sleep_for(std::chrono::microseconds(minPeriod*1000 - duration));
}

bool Scheduler::timeToExecute(int period) {
return (currentTime % period == 0);
}

Scheduler::~Scheduler() {

}
42 changes: 42 additions & 0 deletions filters/ehealth/tools/Scheduler.h
Original file line number Diff line number Diff line change
@@ -0,0 1,42 @@
/*
*
* Tiny Multimedia Framework
* Copyright (C) 2014 Arash Shafiei
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/


#ifndef SCHEDULER_H
#define SCHEDULER_H

#include <thread>
#include <chrono>

class Scheduler
{
private:
int minPeriod, maxPeriod, currentTime;
std::chrono::high_resolution_clock::time_point startTime, endTime;

public:
Scheduler(int min, int max);
void start();
void end();
bool timeToExecute(int period);
~Scheduler();
};

#endif // SCHEDULER_H

0 comments on commit 8e7eb60

Please sign in to comment.