Arduino问答实验室

protothread 和 arduino 第一个简单的范例0条评论

作者:学长   发布于:2013年9月10日 14:44:19 周二    分类: Arduino    4413 人浏览

protothread and arduino, a first easy example

I wrote a little example to demonstrate the possibilities of protothread and an avr µc. I decided to use some arduino specific functions like pinMode(), digitalWrite() and digitalRead() because i think this way it is very easy to understand what's happening in the code. But you should always keep in mind that these functions are up to 50 times slower than direct port access. It's easy to change the read and write functions to direct port access later on in a project if more I/O power is needed. For me arduino is a rapid prototyping platform...


protothread 和 arduino 第一个简单的范例

我写了一个小范例来展示 protothread 的可能性以及 avr µc.
我决定使用某些 arduino 指定的函数, 像是 pinMode(), digitalWrite() 和 digitalRead() 
因为我认为这方式比较容易了解程序代码里面发生了什么事.
但你在心里应该清楚的知道,这些函数是比直接操作端口慢了50次以上
这些函数比直接操作端口 更容易改变 读写功能, 在以后需要更多 I/O 的项目更为明显。
对我来说,arduino 就是一个快速开发原型的平台...

What the code does, is that it toggles an LED every n ms using two independent protothreads for it. One pt toggles every 1000ms, the other one every 900ms. The result is an erratic blinking pattern. Download the protothread library and unpack it into your library directory, This example is already included as pde file. Restart your arduino IDE after unpacking and you will find it listed under "examples -> pt" in the "File" menue of the IDE.
我写的小范例做了些什么呢?它使用了2个 protothreads  分别每格n ms 触发 LED 。
一个 pt 每格 1000 ms 触发 , 另一个900ms . 
这样的结果就是时闪时灭的模式。
下载 protothread 链接库 解压到你的 library 目录里面,这个范例已经包括了 pde 文件. 
在解压完之后重开 arduino IDE , 你会看到在 file 的选单里 example ->pt 下有份范例列表 。


#include <pt.h>   // include protothread library  //加载 protothread 链接库

#define LEDPIN 13  // LEDPIN is a constant  //LEDPIN 是个常数

static struct pt pt1, pt2; // each protothread needs one of these // 每个 protothread 需要一个

void setup() {
  pinMode(LEDPIN, OUTPUT); // LED init //LED 初始化
  PT_INIT(&pt1);  // initialise the two       //初始化这两个 protothread 变数
  PT_INIT(&pt2);  // protothread variables
}

void toggleLED() {
  boolean ledstate = digitalRead(LEDPIN); // get LED state //读取 LED 状态
  ledstate ^= 1;   // toggle LED state using xor //使用xor 来改变 LED 状态
  digitalWrite(LEDPIN, ledstate); // write inversed state back //写入改变后的状态
}

/* This function toggles the LED after 'interval' ms passed */
/* 这个函数在 interval  ms 经过后改变 LED */
static int protothread1(struct pt *pt, int interval) {
  static unsigned long timestamp = 0;
  PT_BEGIN(pt);
  while(1) { // never stop  // 永不停止
    /* each time the function is called the second boolean
    *  argument "millis() - timestamp > interval" is re-evaluated
    *  and if false the function exits after that. 
    *  每次这个函数被呼叫时, 第二个 boolean 参数 "millis()-timestamp > interval " 就被执行
    *  如果计算结果为 false , 就跳离 */
    PT_WAIT_UNTIL(pt, millis() - timestamp > interval );
    timestamp = millis(); // take a new timestamp
    toggleLED();
  }
  PT_END(pt);
}
/* exactly the same as the protothread1 function */
/*跟 protothread1 函数一样*/
static int protothread2(struct pt *pt, int interval) {
  static unsigned long timestamp = 0;
  PT_BEGIN(pt);
  while(1) {
    PT_WAIT_UNTIL(pt, millis() - timestamp > interval );
    timestamp = millis();
    toggleLED();
  }
  PT_END(pt);
}

void loop() {
  protothread1(&pt1, 900); // schedule the two protothreads //放入两个 protothread
  protothread2(&pt2, 1000); // by calling them infinitely //无限次数旳被呼叫
} 



I hope this example is easy to understand. I will post a more complex one the next days, dealing with input and output via the serial connection and some sort of calculation, all taking place in 'quasi'parallel. Maybe some sort of clock with the possibility to set it and some periodic actions like blinking a led and printing the actual time on the console. 
希望这个范例很容易了解。我将在未来几天写更复杂, 由串行端口连接的输入和输出,以及一些排序的计算, 
这些都类似并行的运算。


Maybe some sort of clock with the possibility to set it and some periodic actions like blinking a led and printing the actual time on the console.



学长译注:

protothread 的确是一个很好的程式库,让 Arduino 的多执行绪成为可能。

但是又有多少 Arduino 应用的开发者,会愿意改变写作习惯来榨干 Arduino 的运行能力?我愿意!你呢?

Me.png


原文:http://harteware.blogspot.tw/2010/11/protothread-and-arduino-first-easy.html

另一篇翻译文章:protothread, a powerfull library protothread 一个厉害的 arduino 多线程链接库

Arduino问答实验室- 本站版权
1、本主题由文章作者发表,文章作者与Arduino问答实验室享有文章相关版权
2、转载或引用本文时请同时征得文章作者和Arduino问答实验室的同意
3、本站部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责
4、如本帖侵犯到任何版权问题,请立即告知本站即时予与以处理
5、原文链接:www.985z.com/?post=27
二维码
继续查看有关 arduino pt protothread 的文章

我来说说

*

*