Timeout
Timeout 类层次结构
使用 Timeout 接口设置中断以在指定的延迟后调用函数。
您可以创建任意数量的 Timeout 对象,同时允许多个未完成的中断。
警告和说明
-
定时器基于 32 位 int 微秒计数器,因此它们的时间最长可达 2^31-1 微秒(30 分钟)。它们的设计时间介于微秒和秒之间。 在较长时间内,您应该考虑 time() 实时时钟。
-
ISR 中没有阻塞代码:避免任何调用等待,无限循环或阻塞调用。
-
在 ISR 中没有 printf,malloc 或 new:避免调用庞大的库函数。特别是,某些库函数(例如 printf,malloc 和 new)不可重入,并且当从 ISR 调用时它们的行为可能会被破坏。
Timeout 类参考
受保护的成员函数 | |
virtual void | handler () |
受保护的成员函数继承自 mbed::Ticker | |
void | setup (us_timestamp_t t) |
受保护的成员函数继承自 mbed::TimerEvent | |
void | insert (timestamp_t timestamp) |
void | insert_absolute (us_timestamp_t timestamp) |
void | remove () |
其他继承成员 | |
公共成员函数继承自 mbed::Ticker | |
Ticker (const ticker_data_t *data) | |
void | attach (Callback< void()> func, float t) |
template<typename T , typename M > | |
void | attach (T *obj, M method, float t) |
void | attach_us (Callback< void()> func, us_timestamp_t t) |
template<typename T , typename M > | |
void | attach_us (T *obj, M method, us_timestamp_t t) |
void | detach () |
公共成员函数继承自 mbed::TimerEvent | |
TimerEvent (const ticker_data_t *data) | |
virtual | ~TimerEvent () |
静态公共成员函数继承自 mbed::TimerEvent | |
static void | irq (uint32_t id) |
受保护的属性继承自 mbed::Ticker | |
us_timestamp_t | _delay |
Callback< void()> | _function |
bool | _lock_deepsleep |
受保护的属性继承自 mbed::TimerEvent | |
ticker_event_t | event |
const ticker_data_t * | _ticker_data |
Timeout hello, world
设置超时以在给定超时后反转 LED:
/* mbed Example Program
* Copyright (c) 2006-2014 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "mbed.h"
Timeout flipper;
DigitalOut led1(LED1);
DigitalOut led2(LED2);
void flip() {
led2 = !led2;
}
int main() {
led2 = 1;
flipper.attach(&flip, 2.0); // setup flipper to call flip after 2 seconds
// spin in a main loop. flipper will interrupt it to call flip
while(1) {
led1 = !led1;
wait(0.2);
}
}
Timeout 示例
试试这个例子来附加一个成员函数:
#include "mbed.h"
// A class for flip()-ing a DigitalOut
class Flipper {
public:
Flipper(PinName pin) : _pin(pin) {
_pin = 0;
}
void flip() {
_pin = !_pin;
}
private:
DigitalOut _pin;
};
DigitalOut led1(LED1);
Flipper f(LED2);
Timeout t;
int main() {
// the address of the object, member function, and interval
t.attach(callback(&f, &Flipper::flip), 2.0);
// spin in a main loop. flipper will interrupt it to call flip
while(1) {
led1 = !led1;
wait(0.2);
}
}