Skip to content

Commit

Permalink
k60 scheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
dipsywong98 committed Jul 6, 2018
1 parent e94269e commit dae9ac3
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 3 deletions.
89 changes: 89 additions & 0 deletions k60/inc/scheduler.h
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_ */
23 changes: 20 additions & 3 deletions k60/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "debug_console.h"
#include "test.h"
#include "wheelbase.h"
#include "scheduler.h"

namespace libbase
{
Expand Down Expand Up @@ -89,8 +90,8 @@ void master(){

encoder_value0 =wheelbase.EncoderGetCount(0);
encoder_value1 =wheelbase.EncoderGetCount(1);
// encoder_value2 =wheelbase.EncoderGetCount(2);
// wheelbase.UpdateEncoders();
encoder_value2 =wheelbase.EncoderGetCount(2);
wheelbase.UpdateEncoders();

if(System::Time()>nextRender){
led0.Switch();
Expand Down Expand Up @@ -179,15 +180,31 @@ void test(){
// }
}

void testScheduler(){
Led::Config led_config;
led_config.id = 0;
Led led0(led_config);
led_config.id = 1;
Led led1(led_config);
Scheduler scheduler(0,75000*100);
int x = scheduler.SetInterval([&]{
led0.Switch();
},250);
scheduler.ClearInterval(x);
while(1);
}

int main(void)
{
System::Init();


// test();
master();
// master();
// slave();
// test();

testScheduler();

return 0;
}
57 changes: 57 additions & 0 deletions k60/src/scheduler.cpp
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);
}
}
}

0 comments on commit dae9ac3

Please sign in to comment.