Arduino 扩展库的制作(官网原文件精细翻译)--附详细实例教程
来源:互联网 发布:微耕门禁数据库表结构 编辑:程序博客网 时间:2024/04/28 08:42
Writing a Library for Arduino
为Arduino编个扩展库
This document explains how to create a library for Arduino. It starts with a sketch with a sketch
for flashing Morse code and explains how to convert its functions into a library. This allows
other people to easily use the code that you've written and to easily update it as you improve
the library.
本文档介绍了如何创建一个Arduino扩展库。它将向你描述如何将一个摩尔斯代码信号的功能做成一个扩展
库。这将会使别人很容易使用你的代码,并且你也可以很方便的编辑和修改你的扩展库。
We start with a sketch that does simple Morse code:
我们从写一个简单的摩尔斯码开始:
int pin = 13;
void setup()
{
pinMode(pin, OUTPUT);
}
void loop()
{
dot(); dot(); dot();
dash(); dash(); dash();
dot(); dot(); dot();
delay(3000);
}
void dot()
{
digitalWrite(pin, HIGH);
delay(250);
digitalWrite(pin, LOW);
delay(250);
}
void dash()
{
digitalWrite(pin, HIGH);
delay(1000);
digitalWrite(pin, LOW);
delay(250);
}
If you run this sketch, it will flash out the code for SOS (a distress call) on pin 13.
运行这段代码,会使Pin 13脚发出SOS紧急呼救的莫尔斯代码信号。
The sketch has a few different parts that we'll need to bring into our library. First, of course,
we have the dot() and dash() functions that do the actual blinking. Second, there's the ledPin
variable which the functions use to determine which pin to use. Finally, there's the call to
pinMode() that initializes the pin as an output.
代码有几个不同的地方,我们将其写入我们的库。首先,我们显然要有dot()函数和dash()函数功能,它们
提供闪烁的功能。其次,还要用“ledpin”变量来确定使用哪个针脚。最后,要用pinmode()函数来初始化引脚
输出。
Let's start turning the sketch into a library!
让我们开始把程序写成扩展库!
You need at least two files for a library: a header file (w/ the extension .h) and the source
file (w/ extension .cpp). The header file has definitions for the library: basically a listing of
everything that's inside; while the source file has the actual code. We'll call our library
"Morse", so our header file will be Morse.h. Let's take a look at what goes in it. It might seem
a bit strange at first, but it will make more sense once you see the source file that goes with
it.
你至少需要两个文件:一个头文件(扩展名为.h)和一个源文件(扩展名为.cpp)。头文件定义扩展库:
基本上是一个原代码中所有元素的列表。我们引用我们的扩展库“Morse”,所以我们把头文件名写为
“Morse.h”,让它看上去一目了然。它看上去有点奇怪,但它与源文件一起运行时将有更多的功能。
The core of the header file consists of a line for each function in the library, wrapped up in a
class along with any variables you need:
头文件的核心是一个扩展库中所有函数的列表,这个列表以及你所需要的所有的变量写在一个类里面:
class Morse
{
public:
Morse(int pin);
void dot();
void dash();
private:
int _pin;
};
A class is simply a collection of functions and variables that are all kept together in one
place. These functions and variables can be public, meaning that they can be accessed by people
using your library, or private, meaning they can only be accessed from within the class itself.
Each class has a special function known as a constructor, which is used to create an instance of
the class. The constructor has the same name as the class, and no return type.
类是一个函数和变量的简单集合。这些函数和变量可以是公开的,以使别人可以使用你的扩展库。或者,
他们只能从类内部访问。每个类有一个特殊的函数,称为构造函数,它用来创建一个类的过程。构造函数与类
具有相同的名字,但它没有返回类型。
You need a couple of other things in the header file. One is an #include statement that gives you
access to the standard types and constants of the Arduino language (this is automatically added
to normal sketches, but not to libraries). It looks like this (and goes above the class
definition given previously):
在头文件里你还需要一些其他的东西。用一个#include声明让你访问Arduino语言中的标准变量和常量(它
自动添加,但不在扩展库中)。就像这样(它将在最开始运行):
#include "WProgram.h"
Finally, it's common to wrap the whole header file up in a weird looking construct:
最后,它将把整个头文件打包进一个特殊的构造里:
#ifndef Morse_h
#define Morse_h
// the #include statment and code go here...
// 把声明和代码写在这里
#endif
Basically, this prevents problems if someone accidently #include's your library twice.
基本上,这可以防止别人不小心重复引用库。
Finally, you usually put a comment at the top of the library with its name, a short description
of what it does, who wrote it, the date, and the license.
最后,我们通常会在顶部加入一些自己的信息,比如库的名字、简短的描述、作者的名字、日期和许可等
等。
Let's take a look at the complete header file:
下面,让我们看一下完整的头文件:
/*
Morse.h - Library for flashing Morse code.
Created by David A. Mellis, November 2, 2007.
Released into the public domain.
*/
#ifndef Morse_h
#define Morse_h
#include "WProgram.h"
class Morse
{
public:
Morse(int pin);
void dot();
void dash();
private:
int _pin;
};
#endif
Now let's go through the various parts of the source file, Morse.cpp.
现在让我们看看.cpp文件的组成部分。
First comes a couple of #include statements. These give the rest of the code access to the
standard Arduino functions, and to the definitions in your header file:
首先是一组#include报表,要把它写在文件开头,提供其余代码使用标准Arduino功能:
#include "WProgram.h"
#include "Morse.h"
Then comes the constructor. Again, this explains what should happen when someone creates an
instance of your class. In this case, the user specifies which pin they would like to use. We
configure the pin as an output save it into a private variable for use in the other functions:
然后是构造函数,再次声明当有人使用你的类的过程时会发生什么。在本例中,用户可以指定要使用的针
脚。我们将输出引脚的配置保存到一个私有变量用于其他功能:
Morse::Morse(int pin)
{
pinMode(pin, OUTPUT);
_pin = pin;
}
There are a couple of strange things in this code. First is the Morse:: before the name of the
function. This says that the function is part of the Morse class. You'll see this again in the
other functions in the class. The second unusual thing is the underscore in the name of our
private variable, _pin. This variable can actually have any name you want, as long as it matches
the definition in the header file. Adding an underscore to the start of the name is a common
convention to make it clear which variables are private, and also to distinguish the name from
that of the argument to the function (pin in this case).
有一些特殊的东西在这个代码里。首先是在这个函数名字前的“Morse::”。这表示,此函数属于“Morse
”类,你会在这个类的其它函数里多次看到。其二是在私有变量名称前的下划线,“_pin”。这个变量可以是
其它名字,只要可以与头文件匹配。加下划线开始的名字是私有变量的一个约定俗成的写法,同时也能从函数
的功能段区分名字(在本例中)。
Next comes the actual code from the sketch that you're turning into a library (finally!). It
looks pretty much the same, except with Morse:: in front of the names of the functions, and _pin
instead of pin:
扩展库的代码和源代码看起来非常类似,除了函数名前有“Morse::”和用“_pin”代替了“pin”:
void Morse::dot()
{
digitalWrite(_pin, HIGH);
delay(250);
digitalWrite(_pin, LOW);
delay(250);
}
void Morse::dash()
{
digitalWrite(_pin, HIGH);
delay(1000);
digitalWrite(_pin, LOW);
delay(250);
}
Finally, it's typical to include the comment header at the top of the source file as well.Let's
see the whole thing:
最后,在源文件的顶部同样会有注释部分。让我们来看看整个扩展库源文件:
/*
Morse.cpp - Library for flashing Morse code.
Created by David A. Mellis, November 2, 2007.
Released into the public domain.
*/
#include "WProgram.h"
#include "Morse.h"
Morse::Morse(int pin)
{
pinMode(pin, OUTPUT);
_pin = pin;
}
void Morse::dot()
{
digitalWrite(_pin, HIGH);
delay(250);
digitalWrite(_pin, LOW);
delay(250);
}
void Morse::dash()
{
digitalWrite(_pin, HIGH);
delay(1000);
digitalWrite(_pin, LOW);
delay(250);
}
And that's all you need (there's some other nice optional stuff, but we'll talk about that
later). Let's see how you use the library.
这就是所有必要的部分(其它一些可选的东西,我们将在以后再谈)。让我们来看看如何使用扩展库。
First, make a Morse directory inside of the libraries sub-directory of your sketchbook
directory.Copy or move the Morse.h and Morse.cpp files into that directory.Now launch the Arduino
environment.If you open the Sketch > Import Library menu, you should see Morse inside.The library
will be compiled with sketches that use it.If the library doesn't seem to build, make sure that
the files really end in .cpp and .h (with no extra .pde or .txt extension, for example).
首先,在扩展库的子目录建一个莫尔斯扩展库目录。把Morse.h和Morse.cpp文件复制或移动到该目录中。
现在打开Arduino编译器。打开“Sketch”>“Import Library”菜单,你应该看到有“Morse”选项。在使用的
时候该扩展库将同时被编译。如果没有看到这个扩展库,请确保文件名为正常的“.cpp”和“h”(没有其它多
余的如“.pde”或“.txt”这样的扩展名)。
原文地址:http://www.geek-workshop.com/thread-192-1-1.html
- Arduino 扩展库的制作(官网原文件精细翻译)--附详细实例教程
- JNI实例教程,附详细步骤
- Arduino的详细介绍(基于Mega2560)
- Arduino的详细介绍(基于Mega2560)
- Arduino的ADXL345库文件(附下载链接)
- 基于WIZ820io的以太网关(兼容XBEE、Arduino扩展)
- NumPy的详细教程(官网手册翻译)
- NumPy的详细教程(官网手册翻译)
- 使用Excel制作图表的实例教程 Excel图表制作大全
- esp32的arduino扩展开发方式
- DVD-lab PRO2.5 制作DVD图文实例教程(附:配套素材及工程文件)
- 用于Arduino的无线网插板WizFi shield(基于WizFi210和WIZ820io,集以太网、WiFi扩展于一身)
- JSON数据格式(附翻译)
- GO 语言写的加密算法(附 java 版翻译)
- GAN眼中的图像翻译(附神奇歌单)
- GAN眼中的图像翻译(附神奇歌单)
- JSP扩展标签实例教程
- 微信开发中使用curl实现http与https的post级get请求实例教程(附工程代码)
- 设置图标按钮
- Stanford机器学习---第三讲. 逻辑回归和过拟合问题的解决 logistic Regression & Regularization
- ASP.NET MVC案例教程(基于ASP.NET MVC beta)——第一篇:准备工作
- 设计API的五大规则
- SQL 入门的一些资料
- Arduino 扩展库的制作(官网原文件精细翻译)--附详细实例教程
- ASP.NET MVC案例教程(基于ASP.NET MVC beta)——第二篇:第一个页面
- 深入理解Oracle的并行操作
- cocos2d-x学习日志(3) --动画类
- sqlite数据库使用
- 改变 WID 的位置
- ASP.NET MVC案例教程(基于ASP.NET MVC beta)——第三篇:ASP.NET MVC全局观
- Stanford机器学习---第四讲. 神经网络的表示 Neural Networks representation
- ASP.NET MVC案例教程(基于ASP.NET MVC beta)——第四篇:传递表单数据