-
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.
- Loading branch information
1 parent
e94269e
commit dae9ac3
Showing
3 changed files
with
166 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* scheduler.h | ||
* | ||
* Created on: Jul 6, 2018 | ||
* Author: dipsy | ||
*/ | ||
|
||
#ifndef INC_SCHEDULER_H_ | ||
#define INC_SCHEDULER_H_ | ||
|
||
#include <vector> | ||
#include <functional> | ||
#include <libbase/k60/pit.h> | ||
#include <libsc/system.h> | ||
|
||
using std::vector; | ||
using std::function; | ||
using libsc::System; | ||
using libbase::k60::Pit; | ||
|
||
class Scheduler{ | ||
public: | ||
Scheduler(); | ||
Scheduler(uint8_t channel_id, uint32_t count); | ||
|
||
/** | ||
* brief: function $f will be called for every $interval ms, return $job_id | ||
* param: {f} function to be called for every interval | ||
* param: {interval} time interval for f to be called | ||
* return: job_id assigned for f | ||
*/ | ||
uint16_t SetInterval(function<void(void)> f, time_t interval); | ||
|
||
/** | ||
* brief: function $f will be called after $delay ms, return $job_id | ||
* param: {f} function to be called for after some time | ||
* param: {delay} time delay for f to be called | ||
* return: job_id assigned for f | ||
* | ||
*/ | ||
uint16_t SetTimeout(function<void(void)> f, time_t delay); | ||
|
||
/** | ||
* brief: cancel job_id, return success or not | ||
* param: {job_id} interval job to be cleared | ||
* return: success or not | ||
*/ | ||
bool ClearInterval(uint16_t job_id); | ||
|
||
/** | ||
* brief: cancel job_id, return success or not | ||
* param: {job_id} timeout job to be cleared | ||
* return: success or not | ||
*/ | ||
bool ClearTimeout(uint16_t job_id); | ||
|
||
/** | ||
* brief: to be called peroidically, and perform the interval and timeout jobs | ||
*/ | ||
void Peroid(); | ||
private: | ||
|
||
typedef struct{ | ||
uint16_t id; //job id | ||
time_t threshold; //will call at this time | ||
time_t delay; //call interval or delay time | ||
function<void()> job; //the job | ||
} Job; | ||
|
||
uint16_t schedule_sum = 0; | ||
vector<Job> interval_jobs; | ||
vector<Job> timeout_jobs; | ||
|
||
Pit m_pit; | ||
|
||
Pit::Config GetPitConfig(uint8_t pit_channel, uint32_t count){ | ||
Pit::Config config; | ||
config.channel = pit_channel; | ||
config.count = count; | ||
config.isr = [&](Pit*){ | ||
Peroid(); | ||
}; | ||
return config; | ||
} | ||
}; | ||
|
||
|
||
|
||
#endif /* INC_SCHEDULER_H_ */ |
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,57 @@ | ||
/* | ||
* scheduler.cpp | ||
* | ||
* Created on: Jul 6, 2018 | ||
* Author: dipsy | ||
*/ | ||
|
||
#include "scheduler.h" | ||
|
||
Scheduler::Scheduler(uint8_t channel_id, uint32_t count):m_pit(GetPitConfig(channel_id, count)){ | ||
} | ||
|
||
uint16_t Scheduler::SetInterval(function<void(void)> f, time_t interval){ | ||
interval_jobs.push_back({++schedule_sum,System::Time()+interval,interval, f}); | ||
return schedule_sum; | ||
} | ||
|
||
uint16_t Scheduler::SetTimeout(function<void(void)> f, time_t delay){ | ||
timeout_jobs.push_back({++schedule_sum,System::Time()+delay, delay, f}); | ||
return schedule_sum; | ||
} | ||
|
||
bool Scheduler::ClearInterval(uint16_t job_id){ | ||
for(int i = 0; i < interval_jobs.size(); i++){ | ||
if(interval_jobs[i].id == job_id){ | ||
interval_jobs.erase(interval_jobs.begin()+job_id); | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
bool Scheduler::ClearTimeout(uint16_t job_id){ | ||
for(int i = 0; i < timeout_jobs.size(); i++){ | ||
if(timeout_jobs[i].id == job_id){ | ||
timeout_jobs.erase(timeout_jobs.begin()+job_id); | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
void Scheduler::Peroid(){ | ||
time_t now = System::Time(); | ||
for(int i = 0; i < interval_jobs.size(); ++i){ | ||
if(now > interval_jobs[i].threshold){ | ||
interval_jobs[i].threshold = now + interval_jobs[i].delay; | ||
interval_jobs[i].job(); | ||
} | ||
} | ||
for(int i = timeout_jobs.size(); i >=0; --i){ | ||
if(now > timeout_jobs[i].threshold){ | ||
timeout_jobs[i].job(); | ||
ClearTimeout(i); | ||
} | ||
} | ||
} |