2017-2-22 17:01:58 [显示全部楼层]
10118浏览
查看: 10118|回复: 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/

iooops  中级技匠

发表于 2017-2-28 19:33:31

沙发
回复

使用道具 举报

iooops  中级技匠

发表于 2017-2-28 19:33:34

板凳
回复

使用道具 举报

iooops  中级技匠

发表于 2017-2-28 19:33:38

地板
回复

使用道具 举报

iooops  中级技匠

发表于 2017-2-28 19:33:49

所以它可以用来干啥。。
回复

使用道具 举报

Rockets  NPC
 楼主|

发表于 2017-3-6 11:34:57

iooops 发表于 2017-2-28 19:33
所以它可以用来干啥。。

在需要多线程操作的情况下,可以使用该库进行开发。
回复

使用道具 举报

iooops  中级技匠

发表于 2017-3-7 12:41:43

Rockets 发表于 2017-3-6 11:34
在需要多线程操作的情况下,可以使用该库进行开发。

好像很实用啊~
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail