ImageVerifierCode 换一换
格式:DOCX , 页数:8 ,大小:17.78KB ,
资源ID:24261699      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/24261699.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(arduino库文件建立方法.docx)为本站会员(b****4)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

arduino库文件建立方法.docx

1、arduino库文件建立方法Writing a Library for ArduinoThis document explains how to create a library for Arduino. It starts 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 youve written and to easily update it

2、as you improve the library.For more information, see theAPI Style Guidefor information on making a good Arduino-style API for your library.We start with a sketch that does simple Morse code:intpin=13;voidsetup()pinMode(pin,OUTPUT);voidloop() dot();dot();dot(); dash();dash();dash(); dot();dot();dot()

3、;delay(3000);voiddot()digitalWrite(pin,HIGH);delay(250);digitalWrite(pin,LOW);delay(250);voiddash()digitalWrite(pin,HIGH);delay(1000);digitalWrite(pin,LOW);delay(250);Get CodeIf you run this sketch, it will flash out the code for SOS (a distress call) on pin 13.The sketch has a few different parts t

4、hat well need to bring into our library. First, of course, we have thedot()anddash()functions that do the actual blinking. Second, theres theledPinvariable which the functions use to determine which pin to use. Finally, theres the call topinMode()that initializes the pin as an output.Lets start turn

5、ing 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 thats inside; while the source file has the actual code. Well call

6、our library Morse, so our header file will be Morse.h. Lets 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.The core of the header file consists of a line for each function in the library, wrapped up in

7、a class along with any variables you need:classMorsepublic: Morse(intpin); voiddot(); voiddash(); private: int_pin;Get CodeA class is simply a collection of functions and variables that are all kept together in one place. These functions and variables can bepublic, meaning that they can be accessed

8、by people using your library, orprivate, meaning they can only be accessed from within the class itself. Each class has a special function known as aconstructor, which is used to create aninstanceof the class. The constructor has the same name as the class, and no return type.You need a couple of ot

9、her 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 Ard

10、uino.hGet CodeFinally, its 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.#endifGet CodeBasically, this prevents problems if someone accidently #includes your library twice.Finally, you usually put a comment

11、 at the top of the library with its name, a short description of what it does, who wrote it, the date, and the license.Lets 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 Mo

12、rse_h#define Morse_h#include Arduino.hclassMorsepublic: Morse(intpin); voiddot(); voiddash(); private: int_pin;#endifGet CodeNow lets go through the various parts of the source file, Morse.cpp.First comes a couple of #include statements. These give the rest of the code access to the standard Arduino

13、 functions, and to the definitions in your header file:#include Arduino.h#include Morse.hGet CodeThen 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

14、 as an output save it into a private variable for use in the other functions:Morse:Morse(intpin)pinMode(pin,OUTPUT); _pin=pin;Get CodeThere are a couple of strange things in this code. First is theMorse:before the name of the function. This says that the function is part of theMorseclass. Youll see

15、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 c

16、onvention to make it clear which variables are private, and also to distinguish the name from that of the argument to the function (pinin this case).Next comes the actual code from the sketch that youre turning into a library (finally!). It looks pretty much the same, except withMorse:in front of th

17、e names of the functions, and_pininstead ofpin:voidMorse:dot()digitalWrite(_pin,HIGH);delay(250);digitalWrite(_pin,LOW);delay(250);voidMorse:dash()digitalWrite(_pin,HIGH);delay(1000);digitalWrite(_pin,LOW);delay(250);Get CodeFinally, its typical to include the comment header at the top of the source

18、 file as well. Lets 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 Arduino.h#include Morse.hMorse:Morse(intpin)pinMode(pin,OUTPUT); _pin=pin;voidMorse:dot()digitalWrite(_pin,HIGH);delay(250)

19、;digitalWrite(_pin,LOW);delay(250);voidMorse:dash()digitalWrite(_pin,HIGH);delay(1000);digitalWrite(_pin,LOW);delay(250);Get CodeAnd thats all you need (theres some other nice optional stuff, but well talk about that later). Lets see how you use the library.First, make aMorsedirectory inside of thel

20、ibrariessub-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 theSketch Import Librarymenu, you should see Morse inside. The library will be compiled with sketches that use it. If the library does

21、nt seem to build, make sure that the files really end in .cpp and .h (with no extra .pde or .txt extension, for example).Lets see how we can replicate our old SOS sketch using the new library:#include Morse morse(13);voidsetup()voidloop() morse.dot();morse.dot();morse.dot(); morse.dash();morse.dash(

22、);morse.dash(); morse.dot();morse.dot();morse.dot();delay(3000);Get CodeThere are a few differences from the old sketch (besides the fact that some of the code has moved to a library).First, weve added an #include statement to the top of the sketch. This makes the Morse library available to the sket

23、ch and includes it in the code sent to the board. That means if you no longer need a library in a sketch, you should delete the #include statement to save space.Second, we now create an instance of the Morse class calledmorse:Morse morse(13);Get CodeWhen this line gets executed (which actually happe

24、ns even before thesetup()function), the constructor for the Morse class will be called, and passed the argument youve given here (in this case, just13).Notice that oursetup()is now empty; thats because the call topinMode()happens inside the library (when the instance is constructed).Finally, to call

25、 thedot()anddash()functions, we need to prefix them withmorse.- the name of the instance we want to use. We could have multiple instances of the Morse class, each on their own pin stored in the _pin private variable of that instance. By calling a function on a particular instance, we specify which i

26、nstances variables should be used during that call to a function. That is, if we had both:Morse morse(13);Morse morse2(12);Get Codethen inside a call tomorse2.dot(),_pinwould be 12.If you tried the new sketch, you probably noticed that nothing from our library was recognized by the environment and h

27、ighlighted in color. Unfortunately, the Arduino software cant automatically figure out what youve define in your library (though it would be a nice feature to have), so you have to give it a little help. To do this, create a file calledkeywords.txtin the Morse directory. It should look like this:Mor

28、se KEYWORD1dash KEYWORD2dot KEYWORD2Get CodeEach line has the name of the keyword, followed by a tab (not spaces), followed by the kind of keyword. Classes should beKEYWORD1and are colored orange; functions should beKEYWORD2and will be brown. Youll have to restart the Arduino environment to get it t

29、o recognize the new keywords.Its also nice to provide people with an example sketch that uses your library. To do this, create anexamplesdirectory inside theMorsedirectory. Then, move or copy the directory containing the sketch (lets call itSOS) we wrote above into the examples directory. (You can f

30、ind the sketch using theSketch Show Sketch Foldercommand.) If you restart the Arduino environment (this is the last time, I promise) - youll see aLibrary-Morseitem inside theFile Sketchbook Examplesmenu containing your example. You might want to add some comments that better explain how to use your library.If youd like to check out the complete library (with keywords and example), you can download it:Morse.zip.Thats all for now but Ill probably write an advanced library tutorial soon. In the meantime, if you have any problems or suggestions, pleas

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1