11546| 6
|
[入门] Metro库的使用说明(草稿) |
本帖最后由 Rockets 于 2017-2-22 17:01 编辑 众所周知,Arduino是没有办法进行多线程的操作的。但是我们可以用一些技巧来实现,其中metro库就是其中的一个。 简单来说Metro库是一个用于进行多线程的库。 相关使用方法如下: 方法Metro() Instantiates a Metro object with a default interval of 1000 milliseconds. 实例一个Metro对象,并且默认间隔为1000毫秒。Metro(unsigned long interval) Instantiates a Metro object with a set interval in milliseconds. 实例一个Metro对象,并设定一个无符号长整数以毫秒为单位。byte check() Because Metro does not use interrupts, you have to "check" the Metro regularly (as often as possible). check() returns true if the interval has lapsed. Returns false if not. 由于Metro不采用中断,你必须经常检查Metro对象,返回true为失效,false为非失效。void interval(unsigned long interval) Changes the interval in milliseconds. 改变间隔时间以毫秒为单位。void reset() Restarts/resets the Metro. Often a good idea if you change the interval. 重设Metro对象。样例代码: [mw_shl_code=applescript,true]/* This code will blink an LED attached to pin 13 on and off. It will stay on for 0.25 seconds. It will stay off for 1 second. */ #include <Metro.h> //Include Metro library #define LED 13 // Define the led's pin //Create a variable to hold theled's current state int state = HIGH; // Instanciate a metro object and set the interval to 250 milliseconds (0.25 seconds). Metro ledMetro = Metro(250); void setup() { pinMode(LED,OUTPUT); digitalWrite(LED,state); } void loop() { if (ledMetro.check() == 1) { // check if the metro has passed its interval . if (state==HIGH) state=LOW; else state=HIGH; digitalWrite(LED,state); } }[/mw_shl_code] 参考链接: 官方github的wiki https://github.com/thomasfredericks/Metro-Arduino-Wiring/wiki Arduino上有关事件驱动库的评估 http://blog.ardublock.com/zh/201 ... braries-on-arduino/ |
© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed