版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_33894122/article/details/84195322
背景
最近在瞎鼓捣一些开源硬件 ,arduino便是其中之一,之前就听说他把底层都封装了,对小白用户很友好,很好奇他的封装形式。
代码分析
IDE初始代码
我们在一开始安装arduinoIDE之后,打开之后的初始界面如下,有两个空的函数等着玩家去填充,其他什么代码都没有。
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
然后打开arduino安装目录,找到核心代码终于找到了其中关联部分,在main.cpp中,如下
#include <Arduino.h>
//Declared weak in Arduino.h to allow user redefinitions.
int atexit(void (*func)()) { return 0; }
// Weak empty variant initialization function.
// May be redefined by variant files.
void initVariant() __attribute__((weak));
void initVariant() { }
int main(void)
{
init();
initVariant();
#if defined(USBCON)
USBDevice.attach();
#endif
setup();
for (;;) {
loop();
if (serialEventRun) serialEventRun();
}
return 0;
}
其他相关代码
void setup(void);
void loop(void);
在main.cpp中可以看出setup()函数只会执行一遍,而loop()函数会一直执行,在arduino源码中只是声明了这两个函数而已,函数实体需要玩家去填充任务代码。
其实这样看来整个框架还是很清晰的,十分简单,做底层工作久了,如果仅仅看到arduinoIDE中的代码难免会有疑惑,和好奇心。
本来还想分析他具体硬件封装代码,看了一下是C++代码,以后有时间再来补充。