导言:以封装一个简单的LED库为例,当然这个库是没有必要的,这是测试!! OK ,Let's Start!
1、新建一个文件夹:LED
在LED文件夹下创建examples文件夹、keywords.txt文件、LED.cpp文件和LED.h文件
2、编写LED.h 。。。。。。其实就是创建一个LED的类:
#ifndef LED_H
#define LED_H
#include "arduino.h"
class LED
{
public:
LED(int pin);
void on();
void off();
private:
int pin;
};
#endif
3、编写LED.cpp 。。。。。。填充那个类
#include"led.h"
LED::LED(int pin)
{
pinMode(pin,OUTPUT);
this->pin=pin;
}
void LED::on()
{
digitaWrite(pin,HIGH);
}
void LED::off()
{
digitaWrite(pin,LOW);
}
4、在examples文件夹下创建LED文件夹然后在LED文件夹下创建LED.ino文件、然后编写此文件。。。。。。。。。测试自己的程序。。。。一个demo
#include "LED.h"
LED led(13);
void setup()
{
}
void loop()
{
led.on();
delay(1000);
led.off();
delay(1000);
}
5、文件编写完成。。。。。。少了点东西好像,,,,那个keywords.txt没写,,,随便填充吧。。。。。。哈哈哈
最后将整个文件夹丢到arduino IDE 的libraries目录下,OK。。。打开IDE,可以看到我们编写的工程了,打开测试一下吧!!