asymt / blog Goto Github PK
View Code? Open in Web Editor NEW这是一个专门用来定博客的项目
Home Page: http://www.asymt.com
这是一个专门用来定博客的项目
Home Page: http://www.asymt.com
processSchedule是一个可以应用于Arduino平台的多任务库,这是个开源项目,项目地址https://github.com/wizard97/ArduinoProcessScheduler在我刚开始使用这个库的时候碰到了一点小问题,我相信很多第一次使用这个库的人都会碰到我这个问题:“就是在使用这个库的时候有没有一种像在loop方法里面使用delay设置延迟的方式?”,答案是否定的,但是有变通的方式,本篇文章就是向大家介绍这种方式的。
我先引用一下这个项目介绍的部分内容,然后再来说我选择的理由
Basic
- Control Over How Often a Process Runs (Periodically, Iterations, or as Often as Possible)
- Process Priority Levels (Easily make custom levels as well)
- Dynamically Add/Remove and Enable/Disable Processes
- Interrupt safe (add, disable, destroy, etc.. processes from interrupt routines)
- Process concurrency protection (Process will always be in a valid state)
Advanced
- Spawn new processes from within running processes
- Automatic Process Monitoring Statistics (calculates % CPU time for process)
- Truly object oriented (a Process is its own object)
- Exception Handling (wait what?!)
- Scheduler can automatically interrupt stuck processes
上述引用是这个多任务库的一些特点,我把每条都翻译一下
基本的
在开始描述怎么做之前,我默认正在读这篇文章的人都是对这个多任务库有过初步了解通读过这个项目的wiki页面的。如果你还没有进入这个开源项目了解过,可以先去这个项目的主页了解后再回来读这篇文章。
现在开始正式介绍在Process是怎么实现延迟,其实方法也很简单,就是在你想要设置延迟之前先记录一下你当前Process的运行状态,然后调用Process对象的setPeriod(delay_ms)方法重新设置当前Process对象的循环周期,然后return等待下次循环。如果我的描述无法让你理解,那么下面的一个小例子应该能帮助你。
virtual void service()
{
int lengh=sizeof(tune)/sizeof(tune[0]);
noTone(_tonePin);
for(int i=state;i<lengh;i++){
tone(_tonePin,tune[i]);
state=x+1;
/**设置该Process对象新的执行周期,
这样在设置的这个新周期时间之后都会再次调用该对象的service()方法**/
setPeriod(400*durations[i]);
return;
}
state=0;
setPeriod(5000);
}
对于这种所有过程代码都写在service()方法的案例应该比较容易理解,然而我在实际的应用中可能 会在service()方法中顺序执行多个方法,这些被调用的方法可能会有不同的延迟需要,这样想保存运行状态就会比较复杂了,我们需要给不同的方法定义一种状态,这样当service()方法再次被调用的时候我就能知道该从哪个方法开始执行了,就像下面这个例子一样处理:
//可以先定义一个状态的枚举类,定义出所有的状态
typedef enum processState
{
STATE_F1,
STATE_F2,
STATE_F3,
...etc
} processState_t;
class toneProcess : public process
{
toneProcess(Scheduler &manager, ProcPriority pr, unsigned int period)
: Process(manager, pr, period)
{
state = STATE_F1; //initialize the current state
}
//在每次进入到service()方法的时候都检查一下运行状态,确定本次需要执行哪个方法
virtual void service() override {
switch(state) {
case STATE_F1:
function1();
break;
case STATE_F2:
function2();
break;
case STATE_F3:
function3();
break;
}
void function1(){
Serial.println("this is function1");
//start delay
setPeriod(500); // Will resume at function 2 in 500ms
state = STATE_F2; //state transition to state 2
}
}
private:
processState_t state;
};
好了,本文到这里也就可以结束了,以上内容如果有什么说得不清楚或者有错误的地方请大家一定要留言告诉我,我会及时修正。
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.