arduino` yield()`函数的秘密是什么?

and*_*dig 16 arduino arduino-ide esp8266

关于到期yield()日,Arduino文档在https://www.arduino.cc/en/Reference/Scheduler上解释.显然它是Scheduler库的一部分:

#include <Scheduler.h>
Run Code Online (Sandbox Code Playgroud)

但是,我可以调用yield()我的Nano或ESP8266而不包括Scheduler lib - 但仅限于我的主程序,而不是包含文件.此外,包含不适用于我的非会费.

我错过了yield()什么秘密,或者yield()除了Due之外在Arduino平台上做了什么?

Dan*_*_ds 29

但是,我可以在Nano或ESP8266上调用yield()而不包括Scheduler lib

yield()功能也在ESP8266库中实现:

生产

这是ESP8266与更经典的Arduino微控制器之间最重要的差异之一.ESP8266在后台运行许多实用功能 - 保持WiFi连接,管理TCP/IP堆栈以及执行其他任务.阻止这些功能运行可能导致ESP8266崩溃并自行重置.为了避免这些神秘的重置,请避免在草图中使用长的阻塞循环.

ESP8266 Arduino库的惊人创建者还实现了yield()函数,该函数调用后台函数以允许它们执行其操作.

这就是为什么你可以yield()在主程序中调用包含ESP8266标头的原因.

参见ESP8266事情连接指南.

更新:

yield() 在Arduino.h中定义为:

void yield(void);
Run Code Online (Sandbox Code Playgroud)

yield()也声明hooks.h如下:

/**
 * Empty yield() hook.
 *
 * This function is intended to be used by library writers to build
 * libraries or sketches that supports cooperative threads.
 *
 * Its defined as a weak symbol and it can be redefined to implement a
 * real cooperative scheduler.
 */
static void __empty() {
    // Empty
}
void yield(void) __attribute__ ((weak, alias("__empty")));
Run Code Online (Sandbox Code Playgroud)

所以,在它上面Nano,它可能什么都不做(除非你有其他库#included).

  • 这是否意味着 ESP 上的“延迟”不是真实的? (2认同)

Yah*_*wil 5

Yield 是 AVR Arduino 核心的一个“弱”函数。我在 Wiring.c 中看到一个对它的调用。

void delay(unsigned long ms)
{
    uint32_t start = micros();

    while (ms > 0) {
        yield();
        while ( ms > 0 && (micros() - start) >= 1000) {
            ms--;
            start += 1000;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这意味着yield()函数将在delay函数的循环期间执行。因此,yield 将用于延迟结束时的某些后台处理或用于执行具有超时功能的功能。

注意:yield 必须在 application/sketch 中定义

更新:这个问题让我很兴奋地发表一篇关于 arduino core 的产量和其他隐藏功能的小文章