实例介绍
官方网站整理的库函数,对学习Arduino并进行开发的人十分有用
Writing a Library for 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 We start with a sketch that does simple Morse code int pin = 13; roid setup () pinMode(pin, OUTPUT) void1o°p doto doto; doto dash()i dash()i dash( dot(i dot)i doto delay (3000); id dot( digitalWrite(pin, HiGH); de1ay(250); digitalWrite(pin, lOW); delay(250)i id dash() digitalWrite(pin, High)i deay(1000) digitalWrite (pin, LOW); delay (250)i [Get Code] If 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 that we'l need to bring into our library. First, of course, we have the doto and dashOfunctions that do the actual blinking. Second there's the led Pin variable which the functions use to determine which pin to use. Finally, there s the call to pin Modeo that initializes the pin as an output. 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 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)i void doto void dashi private int pini [Get Code] 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 aninstance 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. h" [Get Code Finally, it's common to wrap the whole header file up in a weird looking construct H ifndef Morse h t define morse h / the #include statment and code go here. #endif [Get Code] 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 "Arduino. h class Morse public: Morse(int pin) void doto void dashi private int pi fendi [Get Code Now let's 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 functions, and to the definitions in your header file: #include "Arduino. h'n #include "Morse. h" [Get Code] 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)i oIn pini [Get Code] 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 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: void morse:: dot digitalWrite( pin, HIGH); delay (250); digitalWrite( pin, LoW)i delay (250)i void Morse: dash() digitalWrite( pin, HIGH)i delay(1000)i digitalWrite( pin, LoW)i delay(250)i [Get Codel 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 "Arduino. h #include "Morse. h" Morse:: Morse(int pin) pinMode (pin, OUTPUT)i n void Morse: dot( digitalWrite( pin, HiGH)i delay(250)i digitalWrite( pin, LOw) lelay (250) void Morse: dash( digitalWrite( pin, HIGH)i delay(1000) digitalWrite( pin, low)i delay(250)i [Get Code] 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) Let' s see how we can replicate our old SoS sketch using the new library: #include <Morse. h> Morse morse(13)i void setup () void loop() morse. dot(: morse. dot(: morse. dot(; morse. dash(); morse. dash(): morse. dash(; morse.dot(): morse. dot(): morse. dot(); deay(3000); [Get Code] There 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 sketch 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 called morse Morse morse(13)i [Get Code] When this line gets executed (which actually happens even before the setup function), the constructor for the Morse class will be called, and passed the argument you've given here (in this case, just 13) Notice that our setup is now empty; that's because the call to pin Modeo happens inside the library (when the instance is constructed) Finally, to call the doto and dasho functions, we need to prefix them with morse. -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 instance 's variables should be used during that call to a function. That is, if we had both Morse morse(13)i Morse morse(12)i [Get Code] then inside a call to morse doto, pin would be 12 If you tried the new sketch, you probably noticed that nothing from our library was recognized by the environment and highlighted in color. Unfortunately, the arduino software can't automatically figure out what you' ve 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 called keywords txtin the morse directory It should look like this Morse KEYWORD1 dash KEYWORD2 dot KEYWORD2 [Get Code] Each line has the name of the keyword, followed by a tab (not spaces) followed by the kind of keyword. Classes should beKEYWORDi and are colored orange; functions should be KeyWord2 and will be brown. You' ll have to restart the arduino environment to get it to recognize the new keywords It's also nice to provide people with an example sketch that uses your library. To do this, create an examples directory inside the Morse directory. Then, move or copy the directory containing the sketch (let's call it soS)we wrote above into the examples directory. (You can find the sketch using the Sketch> Show Sketch Folder command. If you restart the arduino environment(this is the last time, I promise)-you'll see a Library-Morse item inside the File >Sketchbook Examples menu containing your example. You might want to add some comments that better explain how to use your library If you'd 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, please post them to the Software Development forum More Sharing Services Share Share on emailShare on favorites Share on printShare on facebook Share on twitter EEPROM Library The microcontroller on the arduino board has EEPROM: memory whose values are kept when the board is turned off (like a tiny hard drive). This library enables you to read and write those bytes The microcontrollers on the various arduino boards have different amounts of eeprom 1024 bytes on the ATmega328, 512 bytes on the ATmega168 and aTmega8, 4 KB(4096 bytes )on the aTmega1280 and ATmega2560 Functions reado write read 4( Description Reads a byte from the eeProm. locations that have never been written to have the value f255 Syntax EEPROM read(address) Parameters address: the location to read from, starting from o(int) Returns the value stored in that location(byte) Example #inc lude <eeprom. h int a =0 int value void setup Serial begin (9600 void loop o value= EEPROM read (a) Serial print(a) Serial print('\t Serial print(value) Serial println a=a+1 if(a==512) a=0; delay(500) write Description Write a byte to the EEPROM Syntax EEPROM write(address, value Parameters address: the location to write to, starting from o(int) value: the value to write, from o to 255 (byte) Returns none Note An EEPROM write takes 3.3 ms to complete The EEPROM memory has a specified life of 100,000 write/erase cycles, so you may need to be careful about how often you write to it. Example #inc lude <eeprom. h) void setup for (int i=0: i< 512: i++) EEPROM write(i, i) void loop 10 【实例截图】
【核心代码】
标签:
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论