{"id":16611,"date":"2022-11-11T20:36:08","date_gmt":"2022-11-11T20:36:08","guid":{"rendered":"https:\/\/wolles-elektronikkiste.de\/?p=16611"},"modified":"2022-11-29T20:44:21","modified_gmt":"2022-11-29T20:44:21","slug":"creating-libraries-and-classes-part-i","status":"publish","type":"post","link":"https:\/\/wolles-elektronikkiste.de\/en\/creating-libraries-and-classes-part-i","title":{"rendered":"Creating Libraries and Classes &#8211; Part I"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">About this post<\/h2>\n\n<p>Having already presented a number of my libraries on this blog (see also on <a href=\"https:\/\/github.com\/wollewald\" target=\"_blank\" rel=\"noopener\">GitHub<\/a>), I have been asked several times how to actually create them. It&#8217;s a bit like being asked how to write an Arduino sketch &#8211; there are many answers to that in any length. At least, I will not manage to cover the topic in all its facets, although I spend two posts on it.<\/p>\r\n<p>But I hope this crash course will help you get started. And even if you don&#8217;t want to create your own libraries, some knowledge is still very useful. Being able to &#8220;read&#8221; libraries will help you to use them better or to understand error messages if necessary.<\/p>\r\n<p>In this first part, I explain the basics. The grammar, so to speak. I have deliberately chosen an example that has nothing to do with microcontrollers. In the second part, I will show you how to develop a library for a sensor and what typical issues arise.<\/p>\r\n\n<h2 class=\"wp-block-heading\">Libraries &#8211; basic terms<\/h2>\n\n<p>I don&#8217;t want to get too deep into general theory, but make sure &#8220;we&#8217;re speaking the same language&#8221;. I will keep this part about the basic concepts short because I want to focus on the practical application. If you think it&#8217;s <em>too<\/em> short, do a little googling. There is already so much on the net about this that I can&#8217;t offer any added value.<\/p>\r\n\n<h3 class=\"wp-block-heading\">Libraries vs. classes<\/h3>\n\n<p>A library is actually just a collection of program parts that is not executable on its own. The library can contain classes, but it does not have to. For the class, the library is a kind of container in which it is kept &#8211; alongside other components, if necessary.<\/p>\r\n<p>The Arduino libraries that are not part of the &#8220;basic equipment&#8221; are located in the folder &#8220;Arduino\/libraries&#8221; as subfolders. The subfolders have the name of the library.<\/p>\r\n\n<h3 class=\"wp-block-heading\">Objects and object-oriented programming <\/h3>\n\n<p>In object-oriented programming (OOP), we think of the world as a collection of objects that are related to each other. Classes are the blueprints for objects, just as circuit diagrams are for circuits, for example. Just as you can create virtually an infinite number of circuits from a circuit diagram, you can create an unlimited number of objects from a class. At least as many as the memory allows you.&nbsp;<\/p>\r\n<p>The objects or the classes are designed in such a way that they reveal only what is necessary to the outside world (principle of encapsulation). Basically, the object properties should only be able to be changed via defined methods. This initially means increased effort, but it pays off later. Who does not know this: You extend a program, which then no longer works because the program parts affect each other unintentionally. Using OOP, these problems can be minimized.&nbsp;<\/p>\r\n<p>In addition to encapsulation, there are other fundamental principles of OOP. Above all, these are inheritance, polymorphism and abstraction. However, I will not go into it any further. Interested parties can look <a href=\"https:\/\/en.wikipedia.org\/wiki\/Object-oriented_programming\" target=\"_blank\" rel=\"noopener\">here<\/a>, for example.<\/p>\r\n\n<h3 class=\"wp-block-heading\">Attributes and methods <\/h3>\n\n<p>Objects have certain attributes (properties) and methods (functions). An example that is often used to illustrate this is a car:<\/p>\r\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/11\/car_class_eng_lang-1024x535.png\"><img decoding=\"async\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/11\/car_class_eng_lang-1024x535.png\" alt=\"Writing libraries: the car as a class\" class=\"wp-image-16026\"\/><\/a><figcaption class=\"wp-element-caption\">The car as a class<\/figcaption><\/figure>\n<p>Some properties are invariant, such as the model or the color. Other properties, such as speed, are variable and are changed via methods. Methods, on the other hand, do not necessarily have to control a property, such as the &#8220;hoot&#8221; method here.<\/p>\r\n\n<h3 class=\"wp-block-heading\">Header and source files<\/h3>\n\n<p>The library folders contain the corresponding header (extension &#8220;.h&#8221;) and source files (extension &#8220;.cpp&#8221;). Among other things, the header files contain the class declarations. If necessary, further elements are placed there, such as <code>#define<\/code> and <code>#include<\/code> statements or enum definitions. The source files mainly contain the functions.<\/p>\r\n<p>For simple libraries that contain only a single class, the library, the class, and the header and source files often have the same name.<\/p>\r\n<p>If you are designing a library yourself, make sure that you use individual names. Header files or classes whose names appear twice in the library directory can cause problems. That&#8217;s why (almost) all my libraries and classes that I publish on GitHub have my initials &#8220;WE&#8221; in their names, so you can see that this doesn&#8217;t (only \ud83d\ude42 ) serve my ego.<\/p>\r\n\n<h2 class=\"wp-block-heading\">Preparations<\/h2>\n\n<p>You can download the CoolCarLib exercise library used in this post from GitHub. Follow <a href=\"https:\/\/github.com\/wollewald\/CoolCarLib\" target=\"_blank\" rel=\"noopener\">this link<\/a>, then click on the &#8220;Code&#8221; button and select &#8220;Download ZIP&#8221;. Save the ZIP file in your &#8220;Arduino\/libraries&#8221; folder and unzip it there. You should then find a folder called &#8220;CoolCarLib-main&#8221;. You don&#8217;t need the ZIP file anymore and can delete it. The easiest way to get to the library example sketches is via File \u2192 Examples \u2192 CoolCarLib-main in the Arduino IDE.<\/p>\r\n\n<p>For the creation and editing of libraries, the Arduino IDE is not particularly well suited as an editor. I recommend the free <a href=\"https:\/\/notepad-plus-plus.org\/\" target=\"_blank\" rel=\"noopener\">Notepad++<\/a>. For smaller projects, the program is absolutely sufficient and requires hardly any introduction. <\/p>\r\n\n<h2 class=\"wp-block-heading\">A minimal class &#8211; CoolCarBasic<\/h2>\n\n<p>The first class we are looking at describes a car and is named CoolCarBasic. The car is very minimalistic, having only the following attributes and methods:<\/p>\r\n<ul>\r\n<li><strong>Attributes:<\/strong>\r\n<ul>\r\n<li>Maximum number of passengers (maxPassengers)<\/li>\r\n<li>Speed (speed)<\/li>\r\n<\/ul>\r\n<\/li>\r\n<li><strong>Methods<\/strong>\r\n<ul>\r\n<li>Return the maximum number of passengers (getMaxPassengers):<\/li>\r\n<li>Set the speed (setSpeed)<\/li>\r\n<li>Return the current speed (getSpeed)<\/li>\r\n<li>Hoot! \/li>\r\n<\/ul>\r\n<\/li>\r\n<\/ul>\r\n\n<h3 class=\"wp-block-heading\">CoolCarBasic &#8211; header file<\/h3>\n\n<p>CoolCarLib contains the two classes CoolCarBasic and CoolCar. The following excerpt of the header file CoolCarLib.h contains a general part and the code of the CoolCarBasic class declaration:<\/p>\r\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-group=\"CoolCarLib.h - CoolCarBasic\" data-enlighter-title=\"CoolCarLib.h - CoolCarBasic\">#ifndef COOL_CAR_LIB_H_\r\n#define COOL_CAR_LIB_H_\r\n\r\n#include &lt;Arduino.h&gt;\r\n\r\n\/* ###############  CoolCarBasic ############### *\/\r\n\r\nclass CoolCarBasic  \/\/ Class Declaration\r\n{\r\n    public: \r\n        CoolCarBasic(uint8_t mP);  \/\/ Constructor\r\n    \r\n        uint8_t getMaxPassengers();\r\n        uint16_t getSpeed();\r\n        void setSpeed(uint16_t speed);\r\n        void hoot();\r\n                 \r\n    protected:\r\n        uint8_t maxPassengers;\r\n        uint16_t speed;\r\n};\r\n\r\n\/* #################  CoolCar ################ \r\n....\r\n....\r\n....\r\n*\/\r\n\r\n#endif<\/pre>\r\n\n<h4 class=\"wp-block-heading\">Explanation of the header file<\/h4>\n\n<p>The content of the header file is framed by:<\/p>\r\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-linenumbers=\"false\">#ifndef COOL_CAR_LIB_H_ \r\n#define COOL_CAR_LIB_H_ \r\n.......\r\n\r\n#endif<\/pre>\r\n\n<p>The statements beginning with the hash <code>#<\/code> are preprocessor directives. That is, they are not part of the actual code, but determine what is read as code. Only if <code>COOL_CAR_LIB_H_<\/code> has not been defined yet, the code between <code>#ifndef<\/code> (= if not defined) and <code>#endif<\/code> is included. <code>COOL_CAR_LIB_H_<\/code> would be defined then. This prevents the code from being read in twice.&nbsp;<\/p>\r\n<p><code>#include &lt;Arduino.h&gt;<\/code> embeds &#8211; not surprisingly &#8211; the Arduino library. What is surprising, however, is <em>that<\/em> you have to include Arduino.h at all. If you write a simple sketch, Arduino.h will be included automatically even without this directive. This necessity results from the order in which the program parts are read. If your library is read before the Arduino library, the compiler will stop with an error message when it encounters an Arduino-specific function such as <code>digitalWrite()<\/code> or <code>Serial.print()<\/code>.<\/p>\r\n<p>Then follows the class declaration, started with the keyword <code>class<\/code>. The content of the class declaration is enclosed in curly brackets. It ends with a semicolon. If the semicolon is missing, there are error messages, which unfortunately do not point directly to the cause.<\/p>\r\n<p>All functions (methods) and variables (attributes) after <code>public:<\/code> are publicly accessible. You access them via the object name and the point operator. Everything after <code>protected:<\/code> is available only to the class itself. That means, if you create a CoolCarBasic object with the name myCar, then the statement <code>myCar.setSpeed(50)<\/code> is allowed. <code>myCar.speed = 50<\/code>, on the other hand, would not be allowed. An alternative to <code>protected<\/code> is <code>private<\/code>. The difference is that private functions and variables cannot be accessed in inherited classes. <\/p>\r\n<p>The first function of the class is <code>CoolCarBasic(uint8_t);<\/code>. This is the constructor, which we will discuss below.<\/p>\r\n<p>For variable definitions of integers, you should specify the variable type in the notation <code>(u)int<\/code><em><code>x<\/code><\/em><code>_t<\/code> . &#8220;int&#8221; stands for integer, &#8220;u&#8221; for unsigned and &#8220;x&#8221; for the size in bits. For example, on an Arduino, a <code>uint16_t<\/code> corresponds to <code>unsigned int<\/code>. However, there are MCUs, such as the ESP32, on which an integer is larger than 16 bits. Therefore, <code>uint16_t<\/code> is a clearer definition. &nbsp;<\/p>\r\n\n<h3 class=\"wp-block-heading\">CoolCarBasic &#8211; source file<\/h3>\n\n<p>Now we come to the source file CoolCarLib.cpp. First, here is the code relevant for CoolCarBasic:<\/p>\r\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-group=\"CoolCarLib.cpp - CoolCarBasic\" data-enlighter-title=\"CoolCarLib.cpp - CoolCarBasic\">#include &lt;CoolCarLib.h&gt;\r\n\r\n\/* ###############  CoolCarBasic ############### *\/\r\n\r\n\/************  Constructor ************\/\r\n\r\nCoolCarBasic::CoolCarBasic(uint8_t mP){\r\n  maxPassengers = mP;\r\n  speed = 0;\r\n}\r\n\r\n\/**********  Public Functions **********\/\r\n\r\nuint8_t CoolCarBasic::getMaxPassengers(){    \r\n    return maxPassengers;\r\n}\r\n\r\nuint16_t CoolCarBasic::getSpeed(){    \r\n    return speed;\r\n}\r\n\r\nvoid CoolCarBasic::setSpeed(uint16_t sp){    \r\n    speed = sp;\r\n}\r\n\r\nvoid CoolCarBasic::hoot(){\r\n  Serial.println(\"beep! beep! beep!\");\r\n}\r\n\r\n\/* #################  CoolCar ################ \r\n............\r\n*\/<\/pre>\r\n\n<h4 class=\"wp-block-heading\">Explanation of the source file <\/h4>\n\n<p>In the first line CoolCarBasic.h is included. The statement seems superfluous because we include the file in the sketches right away. But you can&#8217;t do without it &#8211; these are the peculiarities of the preprocessor.<\/p>\r\n<p>This is followed by the definitions of the functions previously declared in the header file. What stands out is that all function names are preceded by the class name, separated by the double colon <code>::<\/code>. This character has the bulky name &#8220;scope resolution operator&#8221;. <code>CoolCarBasic::xxx()<\/code> means that the function xxx() belongs to the CoolCarBasic class. How else should the compiler know which class to assign the function to!<\/p>\r\n<p>Now we come to the constructor <code>CoolCarBasic::CoolCarBasic(uint8_t){...}<\/code>. The constructor is the function you use to create your objects. As a function, the constructor is special in that it has no return value. However, it is allowed to pass parameters to the constructor. In our example, the constructor gets the maximum number of passengers and assigns it to the maxPassengers private variable. In addition, <code>speed<\/code> is set to the initial value 0. Typically, however, this is something you don&#8217;t put in the constructor, but in a <code>init()<\/code> or <code>begin()<\/code> function.<\/p>\r\n<p>You query the maximum number of passengers with <code>getMaxPassengers()<\/code>. Since maxPassengers is a constant value, there is no <code>setMaxPassengers()<\/code> function. This is different for <code>speed<\/code>. Here we define a get and a set function.<\/p>\r\n<p>The function <code>hoot()<\/code> is a method that does not change any variable. When it is called, it will trigger a <code>Serial.println()<\/code> instruction. Normally, I would not define the output medium in a class, but only return values &#8211; how they are output is decided by the user.<\/p>\r\n\n<h3 class=\"wp-block-heading\">CoolCarBasic &#8211; Usage<\/h3>\n\n<h4 class=\"wp-block-heading\">cool_car_basic_test.ino &#8211; test sketch<\/h4>\n\n<p>And this is what a sketch using CoolCarBasic might look like:<\/p>\r\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-group=\"cool_car_basic_test.ino\" data-enlighter-title=\"cool_car_basic_test.ino\">#include &lt;CoolCarBasic.h&gt;\r\n\r\nCoolCarBasic myCar = CoolCarBasic(7); \/\/ 7 passengers is maximum\r\n\r\nvoid setup() {\r\n  Serial.begin(9600);\r\n  \/\/delay(200); \/\/ uncomment for ESP32 \/ ESP8266 Boards\r\n  \r\n  byte passengerLimit = myCar.getMaxPassengers();\r\n  unsigned int currentSpeed = myCar.getSpeed();\r\n  \r\n  Serial.print(\"Max. number of passengers: \");\r\n  Serial.println(passengerLimit);\r\n  \r\n  Serial.print(\"CurrentSpeed [km\/h]: \");\r\n  Serial.println(currentSpeed);\r\n  \r\n  myCar.setSpeed(50);\r\n  currentSpeed = myCar.getSpeed();\r\n  Serial.print(\"CurrentSpeed [km\/h]: \");\r\n  Serial.println(currentSpeed);\r\n  \r\n  myCar.hoot();  \r\n}\r\n\r\nvoid loop() {}<\/pre>\r\n\n<p>Line 3 calls the constructor and creates the object <code>myCar<\/code>. You can also say: An instance of the CoolCarBasic class is created. In doing so, you pass the value for <code>maxPassengers<\/code> to the object.<\/p>\r\n<p>If you use an ESP32 or ESP8266 based board, you should uncomment<code>delay(200);<\/code> in line 7. Otherwise, you might see an empty serial monitor after uploading the sketch.<\/p>\r\n<p>The rest of the sketch should be self-explanatory.<\/p>\r\n\n<h4 class=\"wp-block-heading\">Output of cool_car_basic_test.ino<\/h4>\n\n<p>The output is not particularly surprising:<\/p>\r\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/09\/output_cool_car_basic.png\"><img loading=\"lazy\" decoding=\"async\" width=\"763\" height=\"349\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/09\/output_cool_car_basic.png\" alt=\"Output of cool_car_basic_test.ino\" class=\"wp-image-16004\" srcset=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/09\/output_cool_car_basic.png 763w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/09\/output_cool_car_basic-300x137.png 300w\" sizes=\"auto, (max-width: 763px) 100vw, 763px\" \/><\/a><figcaption class=\"wp-element-caption\">Output of cool_car_basic_test.ino<\/figcaption><\/figure>\n<p>One more thing you could try: Turn the variable <code>speed<\/code> into a public variable by moving its declaration to the &#8220;public&#8221; area. You will see that you can then make an assignment such as <code>myCar.speed = 150<\/code>. Or you can display the value using <code>Serial.print(myCar.speed)<\/code>. And why don&#8217;t you do that, but go to the trouble of writing set and get functions? The answer is: Because of the principle of encapsulation. And why is encapsulation important? You have control! For example, checks can be built into the set function to prevent the entry of invalid values.<\/p>\r\n\n<h2 class=\"wp-block-heading\">An advanced class &#8211; CoolCar<\/h2>\n\n<p>With CoolCarBasic, you got to know the basic framework for a class. However, a few important things that you typically need are still missing. I would like to explain this using the second class of CoolCarLib, namely CoolCar. Actually, this would also be a good opportunity to deal with the topic of inheritance, but I don&#8217;t want this post to become too long.<\/p>\r\n<p>CoolCar has the following attributes and methods:<\/p>\r\n<ul>\r\n<li><strong>Attributes:<\/strong><br>\r\n<ul>\r\n<li>Maximum number of passengers<\/li>\r\n<li>Maximum speed<\/li>\r\n<li>Current speed<\/li>\r\n<li>Length<\/li>\r\n<li>Air conditioner level<\/li>\r\n<\/ul>\r\n<\/li>\r\n<li><strong>Methods:<\/strong>\r\n<ul>\r\n<li>Initialization<\/li>\r\n<li>Get the maximum number of passengers \/ maximum speed \/ length \/ current speed<\/li>\r\n<li>Accelerate \/ brake by the value x<\/li>\r\n<li>Hoot!<\/li>\r\n<li>Get \/ set the level of air conditioning<\/li>\r\n<\/ul>\r\n<\/li>\r\n<\/ul>\r\n\n<h3 class=\"wp-block-heading\">CoolCar &#8211; header file <\/h3>\n\n<p>Here is the relevant part of the header file:<\/p>\r\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-group=\"CoolCarLib.h - CoolCar\" data-enlighter-title=\"CoolCarLib.h - CoolCar\">\/* #################  CoolCar ################ *\/\r\n\r\nenum cc_ac_level{   \r\n    CC_AC_OFF, CC_AC_LOW, CC_AC_MEDIUM, CC_AC_HIGH, CC_AC_MAX\r\n};\r\n\r\nclass CoolCar\r\n{\r\n    public: \r\n        CoolCar(const uint8_t mP, const uint16_t mSp, const float len = 4.2)\r\n        : maxPassengers{mP}, maxSpeed{mSp}, length{len} { \/*empty *\/ }\r\n        \r\n        void init();\r\n        uint8_t getMaxPassengers();\r\n        uint16_t getMaxSpeed();\r\n        float getLengthInMeters();\r\n        void hoot();\r\n        bool accelerate(uint16_t accVal);\r\n        void brake(uint16_t brakeVal);\r\n        uint16_t getCurrentSpeed();\r\n        void setAirConLevel(cc_ac_level acLevel);\r\n        cc_ac_level getAirConLevel();\r\n            \r\n    protected:\r\n        uint8_t maxPassengers;\r\n        uint16_t maxSpeed;\r\n        float length; \r\n        int16_t currentSpeed;\r\n        cc_ac_level airConLevel;\r\n        \r\n        int16_t calculateNewSpeed(int16_t value);\r\n};<\/pre>\r\n\n<h3 class=\"wp-block-heading\">Explanations to the header file<\/h3>\n\n<h4 class=\"wp-block-heading\">Use of Enum Definitions<\/h4>\n\n<p>Our CoolCar car now has an air conditioning system that is supposed to be adjustable in five levels, from &#8220;Off&#8221; (<code>CC_AC_OFF<\/code>) to &#8220;Maximum&#8221; (<code>CC_AC MAX<\/code>). To define the levels, we use the private variable <code>airConLevel<\/code>, which is an enumeration. The setting is made via <code>setAirConLevel()<\/code>. Of course, you could simply use integer values from 0 to 4 for the levels. But &#8220;MEDIUM&#8221;, for example, is more understandable than &#8220;Level 2&#8221;.<\/p>\r\n<p>The names of the enumeration elements should be individual. That&#8217;s why I prefixed them with &#8220;CC_AC_&#8221; (Cool Car Air Condition). If you do not follow this rule and the identifier has already been used elsewhere for a global definition, you will get an error message. You can provoke this issue by trying to rename <code>CC_AC_HIGH<\/code> to a simple <code>HIGH<\/code>, which as you know is already assigned for the logic level.<\/p>\r\n\n<h4 class=\"wp-block-heading\">Recommended: Enum classes<\/h4>\n\n<p>Actually, it is recommended to use <code>enum class<\/code> instead of the simple enumerations:<\/p>\r\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">enum class cc_ac_level : uint8_t {   \r\n    CC_AC_OFF, CC_AC_LOW, CC_AC_MEDIUM, CC_AC_HIGH, CC_AC_MAX\r\n};<\/pre>\r\n\n<p>If you want to access the elements, you must prefix them with the name of the enum class separated by the scope resolution operator <code>::<\/code>, for example like this: <code>cc_ac_level::CC_AC_MEDIUM<\/code>. The advantage is on the one hand less risk of name collisions, on the other hand implicit type conversions, which could lead to unhappy surprises, are avoided. For more details, look e.g. <a href=\"https:\/\/stackoverflow.com\/questions\/18335861\/why-is-enum-class-preferred-over-plain-enum\" target=\"_blank\" rel=\"noopener\">here<\/a>.<\/p>\r\n<p>In most Arduino libraries you can find the simple enumerations, so also in mine, I have to confess.<\/p>\r\n\n<h4 class=\"wp-block-heading\">Passing multiple parameters to the constructor<\/h4>\n\n<p>The constructor of the CoolCar class is passed the following parameters:<\/p>\r\n<ul>\r\n<li>Maximum number of passengers<\/li>\r\n<li>Maximum speed<\/li>\r\n<li>Car length<\/li>\r\n<\/ul>\r\n<p>Since these properties do not change, it is best to pass them as constants.<\/p>\r\n<p>The larger the number of parameters, the higher the probability that the user will make a mistake in sequence. Therefore, you should not overdo it.<\/p>\r\n<p>As with ordinary functions, parameters for the constructor can also be set up as optional by predefining them. In this example, we do that for the length <code>len<\/code>. If the length is not passed, the default setting &#8220;4.2&#8221; is used.<\/p>\r\n<p>By the notation I use here, the object properties here are already assigned the passed parameters in the header file.<\/p>\r\n<p>It is also possible to overload the constructor, i.e. to declare it multiple times with different parameters.<\/p>\r\n\n<h3 class=\"wp-block-heading\">CoolCar &#8211; source file<\/h3>\n\n<p>Here is the relevant part of the source file:<\/p>\r\n\n<div class=\"scroll-paragraph-long\">\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-group=\"CoolCarLib.cpp - CoolCar\" data-enlighter-title=\"CoolCarLib.cpp - CoolCar\">\/**********  Public Functions **********\/\r\n\r\nvoid CoolCar::init(){    \r\n    currentSpeed = 0;\r\n    airConLevel = CC_AC_OFF;\r\n}\r\n\r\nuint8_t CoolCar::getMaxPassengers(){    \r\n    return maxPassengers;\r\n}\r\n\r\nuint16_t CoolCar::getMaxSpeed(){    \r\n    return maxSpeed;\r\n}\r\n\r\nfloat CoolCar::getLengthInMeters(){\r\n    return length;\r\n}\r\n\r\nuint16_t CoolCar::getCurrentSpeed(){\r\n    return currentSpeed;\r\n}\r\n\r\nvoid CoolCar::hoot(){\r\n    Serial.println(\"beep! beep! beep!\");\r\n}\r\n\r\nbool CoolCar::accelerate(uint16_t accVal){\r\n    bool noLimitViolation = true;\r\n    uint16_t newSpeed = static_cast&lt;uint16_t&gt;(calculateNewSpeed(accVal));\r\n    if(newSpeed &gt; maxSpeed){\r\n        currentSpeed = maxSpeed;\r\n        noLimitViolation = false;\r\n    }\r\n    else{\r\n        currentSpeed = newSpeed;\r\n    }\r\n    return noLimitViolation;\r\n}\r\n\r\nvoid CoolCar::brake(uint16_t brakeVal){\r\n    int16_t newSpeed = calculateNewSpeed(brakeVal * (-1));\r\n    if(newSpeed &lt;= 0){\r\n        currentSpeed = 0;\r\n    }\r\n    else{\r\n        currentSpeed = (uint16_t)newSpeed;\r\n    }\r\n}\r\n\r\nvoid CoolCar::setAirConLevel(cc_ac_level level){\r\n    airConLevel = level;\r\n}\r\n\r\ncc_ac_level CoolCar::getAirConLevel(){\r\n    return airConLevel;\r\n}\r\n        \r\n\/*********  Private Functions *********\/\r\n\r\nint16_t CoolCar::calculateNewSpeed(int16_t value){\r\n    int16_t speed = currentSpeed + value;\r\n    return speed;   \r\n}<\/pre>\r\n<p>&nbsp;<\/p>\r\n<\/div>\r\n\n<h3 class=\"wp-block-heading\">Explanations to the source file<\/h3>\n\n<h4 class=\"wp-block-heading\">Constructor<\/h4>\n\n<p>The constructor does not need to be listed again in the source file this time. The parameters have already been passed.<\/p>\r\n\n<h4 class=\"wp-block-heading\">Init() function<\/h4>\n\n<p>Many Arduino classes have a <code>init()<\/code> or <code>begin()<\/code> function in which object properties are assigned a certain default value. In our case, this concerns the air conditioner level and the current speed.<\/p>\r\n<p>If the classes represent components such as sensors or controllers, <code>init()<\/code> also typically checks whether the components have been connected correctly.<\/p>\r\n\n<h4 class=\"wp-block-heading\">Speed control<\/h4>\n\n<p>In contrast to the last example, we control the speed via acceleration <code>accelerate()<\/code> and via braking <code>brake()<\/code>. If you try to exceed the maximum speed, then the speed will be limited to the maximum speed. Moreover, the function <code>accelerate()<\/code> has a return value (<code>noLimitViolation<\/code>). If it is <code>true<\/code>, then everything is fine. If, on the other hand, it is <code>false<\/code>, you have tried to accelerate beyond the maximum speed. <\/p>\r\n<p>The rule for braking is that it must not lead to negative speeds. <code>brake()<\/code> contains a corresponding control function. However, I have dispensed with a return value.<\/p>\r\n\n<h4 class=\"wp-block-heading\">Be careful with the signs <\/h4>\n\n<p>Our car can only drive forward. So, the speed should always be positive. Still, <code>calculateNewSpeed()<\/code> returns a <code>int16_t<\/code>. The reason is easy to see: The check for negative values only takes place after the return to <code>brake()<\/code>. The return value must be explicitly converted to a <code>uint16_t<\/code>. Especially when working with registers (see part 2 of the article), it is easy to forget that they may also contain negative values.&nbsp;<\/p>\r\n<p>Many authors use the so-called C-type cast for type conversion, for example <code>(uint16_t)value<\/code>. You should use <code>static_cast&lt;uint16_t&gt;(value)<\/code> instead &#8211; but I often don&#8217;t stick to that either.<\/p>\r\n\n<h3 class=\"wp-block-heading\">CoolCar &#8211; Usage<\/h3>\n\n<h4 class=\"wp-block-heading\">cool_car_test.ino &#8211; Test Sketch<\/h4>\n\n<p>Here&#8217;s a little example sketch:<\/p>\r\n\n<div class=\"scroll-paragraph-long\">\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-group=\"cool_car_test.ino\" data-enlighter-title=\"cool_car_test.ino\">#include &lt;CoolCarLib.h&gt;\r\n\r\nCoolCar myCar = CoolCar(5, 180, 3.5); \r\n\/\/ Alternative: CoolCar myCar(5, 180); \r\n\r\nvoid setup() {\r\n  Serial.begin(9600);\r\n  \/\/delay(200); \/\/ uncomment for ESP32 \/ ESP8266\r\n  myCar.init();\r\n  Serial.print(\"Max. number of passengers: \");\r\n  Serial.println(myCar.getMaxPassengers());\r\n  Serial.print(\"Max. Speed [km\/h]: \");\r\n  Serial.println(myCar.getMaxSpeed());\r\n  Serial.print(\"Length [meters]: \");\r\n  Serial.println(myCar.getLengthInMeters());\r\n  myCar.hoot();\r\n  Serial.print(\"Speed: \");\r\n  Serial.println(myCar.getCurrentSpeed());\r\n\r\n  if(!myCar.accelerate(50)){\r\n    Serial.println(\"Speed Limit Warning!\"); \r\n  }\r\n  Serial.print(\"New Speed [km\/h]: \");\r\n  Serial.println(myCar.getCurrentSpeed());\r\n\r\n  if(!myCar.accelerate(150)){\r\n    Serial.println(\"Acceleration warning!!\"); \r\n  }\r\n  Serial.print(\"New Speed [km\/h]: \");\r\n  Serial.println(myCar.getCurrentSpeed());\r\n\r\n  Serial.print(\"Air Conditioning Level: \");\r\n  Serial.println(myCar.getAirConLevel());\r\n\r\n  myCar.brake(60);\r\n  Serial.print(\"New Speed [km\/h]: \");\r\n  Serial.println(myCar.getCurrentSpeed());\r\n\r\n  myCar.setAirConLevel(CC_AC_MEDIUM);\r\n  Serial.print(\"Air Con Level [num]: \");\r\n  Serial.println(myCar.getAirConLevel());\r\n  printAirConLevel();\r\n}\r\n\r\nvoid loop() {} \r\n\r\nvoid printAirConLevel(){\r\n  cc_ac_level acLevel = myCar.getAirConLevel(); \r\n  Serial.print(\"Air Con Level [level]: \");\r\n  switch(acLevel){\r\n    case CC_AC_OFF:\r\n      Serial.println(\"off\");\r\n      break;\r\n    case CC_AC_LOW:\r\n      Serial.println(\"low\");\r\n      break;\r\n    case CC_AC_MEDIUM:\r\n      Serial.println(\"medium\");\r\n      break;\r\n    case CC_AC_HIGH:\r\n      Serial.println(\"high\");\r\n      break;\r\n    case CC_AC_MAX:\r\n      Serial.println(\"maximum\");\r\n      break; \r\n    default:\r\n      Serial.print(\"couldn't detect\");\r\n  }\r\n}<\/pre>\r\n<p>&nbsp;<\/p>\r\n<\/div>\r\n\n<h4 class=\"wp-block-heading\">Output of cool_car_test.ino<\/h4>\n\n<p>This output should not be unexpected either:<\/p>\r\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/output_cool_car_example.png\"><img loading=\"lazy\" decoding=\"async\" width=\"755\" height=\"397\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/output_cool_car_example.png\" alt=\"Output of cool_car_test.ino\" class=\"wp-image-16423\" srcset=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/output_cool_car_example.png 755w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/output_cool_car_example-300x158.png 300w\" sizes=\"auto, (max-width: 755px) 100vw, 755px\" \/><\/a><figcaption class=\"wp-element-caption\">Output of cool_car_test.ino<\/figcaption><\/figure><\/div>\n<p>The example shows a disadvantage of the simple enumerations and enum classes. If you query <code>airConLevel<\/code> with <code>getAirConLevel()<\/code>, you will only get the bare number, but not the name of the enumeration element. Only a function like <code>printAirConLevel()<\/code> translates the return value into something understandable.<\/p>\r\n\n<h2 class=\"wp-block-heading\">Keyword Highlighting with keywords.txt<\/h2>\n\n<p>To avoid typos or to recognize them more easily, it is helpful if the keywords, i.e. functions, variables, enumeration elements, etc. are highlighted in color by the Arduino IDE. To do this, create a file called keywords.txt and copy it to the library directory. You list the names to be highlighted in the file. Behind the names you put, separated by a tab(!), KEYWORD1, KEYWORD2 or LITERAL. The # sign indicates comments. Other IDEs have smarter solutions!<\/p>\r\n\n<div class=\"scroll-paragraph\">\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-group=\"keywords.txt\" data-enlighter-title=\"keywords.txt\">#######################################\r\n# Syntax Coloring Map For CoolCarLib\r\n#######################################\r\n\r\n#######################################\r\n# Datatypes (KEYWORD1)\r\n#######################################\r\n\r\nCoolCar\tKEYWORD1\r\nCoolCarBasic\tKEYWORD1\r\n\r\n# ENUM TYPES\r\ncc_ac_level\tKEYWORD1\r\n\r\n#######################################\r\n# Methods and Functions (KEYWORD2)\r\n#######################################\r\n\r\ngetMaxPassengers\tKEYWORD2\r\ngetSpeed\tKEYWORD2\r\nsetSpeed\tKEYWORD2\r\nhoot\tKEYWORD2\r\nmaxPassengers\tKEYWORD2\r\nspeed\tKEYWORD2\r\ninit\tKEYWORD2\r\ngetMaxSpeed\tKEYWORD2\r\ngetLengthInMeters\tKEYWORD2\r\naccelerate\tKEYWORD2\r\nbrake\tKEYWORD2\r\ngetCurrentSpeed\tKEYWORD2\r\nsetAirConLevel\tKEYWORD2\r\ngetAirConLevel\tKEYWORD2\r\nmaxSpeed\tKEYWORD2\r\nlength\tKEYWORD2\r\ncurrentSpeed\tKEYWORD2\r\ncalculateNewSpeed\tKEYWORD2\r\n\r\n#######################################\r\n# Constants (LITERAL1)\r\n#######################################\r\n\r\n# ENUM VALUES\r\nCC_AC_OFF\tLITERAL1\r\nCC_AC_LOW\tLITERAL1\r\nCC_AC_MEDIUM\tLITERAL1\r\nCC_AC_HIGH\tLITERAL1\r\nCC_AC_MAX\tLITERAL1<\/pre>\r\n<p>&nbsp;<\/p>\r\n<\/div>\r\n\n<h2 class=\"wp-block-heading\">Conclusion and outlook<\/h2>\n\n<p>With this post, you have learned basics of creating libraries. However, this example had no real relation to microcontrollers yet. If you want to create a library for typical Arduino components, further questions arise. For example: How do I selectively change certain bits in a register? Or: How do I read values that span multiple registers? Or: how do I pass an SPI or Wire object? These and other questions will be answered in the second part of this article using a practical example.<\/p>\r\n","protected":false},"excerpt":{"rendered":"<p>This post is part 1 of a crash course on libraries and classes. It is designed to help you get started with this complex topic.<\/p>\n","protected":false},"author":1,"featured_media":16049,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[575],"tags":[1933,1934,556,1930,1925,1927,1928,1935,1937,694,1931,1936,1926,558,1929,1932],"class_list":["post-16611","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software-and-tools","tag-cpp-en","tag-h-en","tag-arduino-en-2","tag-attributes","tag-class","tag-classes","tag-classes-vs-libraries","tag-constructor","tag-enum-en","tag-example-en","tag-header-file","tag-keywords-txt-en","tag-libraries","tag-library-en-2","tag-methods","tag-source-file"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Creating Libraries and Classes - Part I &#8226; Wolles Elektronikkiste<\/title>\n<meta name=\"description\" content=\"This post is part 1 of a crash course on libraries and classes. I wrote it i to help you get started with this complex topic.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/wolles-elektronikkiste.de\/en\/creating-libraries-and-classes-part-i\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Creating Libraries and Classes - Part I &#8226; Wolles Elektronikkiste\" \/>\n<meta property=\"og:description\" content=\"This post is part 1 of a crash course on libraries and classes. I wrote it i to help you get started with this complex topic.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/wolles-elektronikkiste.de\/en\/creating-libraries-and-classes-part-i\" \/>\n<meta property=\"og:site_name\" content=\"Wolles Elektronikkiste\" \/>\n<meta property=\"article:published_time\" content=\"2022-11-11T20:36:08+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-11-29T20:44:21+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/09\/Beitragsbild_Bibliotheken.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1100\" \/>\n\t<meta property=\"og:image:height\" content=\"1100\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Wolfgang Ewald\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Wolfgang Ewald\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"18 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en\\\/creating-libraries-and-classes-part-i#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en\\\/creating-libraries-and-classes-part-i\"},\"author\":{\"name\":\"Wolfgang Ewald\",\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en#\\\/schema\\\/person\\\/b774e4d64b4766889a2f7c6e5ec85b46\"},\"headline\":\"Creating Libraries and Classes &#8211; Part I\",\"datePublished\":\"2022-11-11T20:36:08+00:00\",\"dateModified\":\"2022-11-29T20:44:21+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en\\\/creating-libraries-and-classes-part-i\"},\"wordCount\":2772,\"commentCount\":4,\"publisher\":{\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en#\\\/schema\\\/person\\\/b774e4d64b4766889a2f7c6e5ec85b46\"},\"image\":{\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en\\\/creating-libraries-and-classes-part-i#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/wp-content\\\/uploads\\\/2022\\\/09\\\/Beitragsbild_Bibliotheken.jpg\",\"keywords\":[\".cpp\",\".h\",\"Arduino\",\"attributes\",\"class\",\"classes\",\"classes vs. libraries\",\"constructor\",\"enum\",\"Example\",\"header file\",\"keywords.txt\",\"libraries\",\"Library\",\"methods\",\"source file\"],\"articleSection\":[\"Software and tools\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en\\\/creating-libraries-and-classes-part-i#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en\\\/creating-libraries-and-classes-part-i\",\"url\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en\\\/creating-libraries-and-classes-part-i\",\"name\":\"Creating Libraries and Classes - Part I &#8226; Wolles Elektronikkiste\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en\\\/creating-libraries-and-classes-part-i#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en\\\/creating-libraries-and-classes-part-i#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/wp-content\\\/uploads\\\/2022\\\/09\\\/Beitragsbild_Bibliotheken.jpg\",\"datePublished\":\"2022-11-11T20:36:08+00:00\",\"dateModified\":\"2022-11-29T20:44:21+00:00\",\"description\":\"This post is part 1 of a crash course on libraries and classes. I wrote it i to help you get started with this complex topic.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en\\\/creating-libraries-and-classes-part-i#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en\\\/creating-libraries-and-classes-part-i\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en\\\/creating-libraries-and-classes-part-i#primaryimage\",\"url\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/wp-content\\\/uploads\\\/2022\\\/09\\\/Beitragsbild_Bibliotheken.jpg\",\"contentUrl\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/wp-content\\\/uploads\\\/2022\\\/09\\\/Beitragsbild_Bibliotheken.jpg\",\"width\":1100,\"height\":1100},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en\\\/creating-libraries-and-classes-part-i#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Startseite\",\"item\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Creating Libraries and Classes &#8211; Part I\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en#website\",\"url\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en\",\"name\":\"Wolles Elektronikkiste\",\"description\":\"Die wunderbare Welt der Elektronik\",\"publisher\":{\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en#\\\/schema\\\/person\\\/b774e4d64b4766889a2f7c6e5ec85b46\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en#\\\/schema\\\/person\\\/b774e4d64b4766889a2f7c6e5ec85b46\",\"name\":\"Wolfgang Ewald\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/wp-content\\\/uploads\\\/2019\\\/03\\\/cropped-Logo-1.png\",\"url\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/wp-content\\\/uploads\\\/2019\\\/03\\\/cropped-Logo-1.png\",\"contentUrl\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/wp-content\\\/uploads\\\/2019\\\/03\\\/cropped-Logo-1.png\",\"width\":512,\"height\":512,\"caption\":\"Wolfgang Ewald\"},\"logo\":{\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/wp-content\\\/uploads\\\/2019\\\/03\\\/cropped-Logo-1.png\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Creating Libraries and Classes - Part I &#8226; Wolles Elektronikkiste","description":"This post is part 1 of a crash course on libraries and classes. I wrote it i to help you get started with this complex topic.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/wolles-elektronikkiste.de\/en\/creating-libraries-and-classes-part-i","og_locale":"en_US","og_type":"article","og_title":"Creating Libraries and Classes - Part I &#8226; Wolles Elektronikkiste","og_description":"This post is part 1 of a crash course on libraries and classes. I wrote it i to help you get started with this complex topic.","og_url":"https:\/\/wolles-elektronikkiste.de\/en\/creating-libraries-and-classes-part-i","og_site_name":"Wolles Elektronikkiste","article_published_time":"2022-11-11T20:36:08+00:00","article_modified_time":"2022-11-29T20:44:21+00:00","og_image":[{"width":1100,"height":1100,"url":"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/09\/Beitragsbild_Bibliotheken.jpg","type":"image\/jpeg"}],"author":"Wolfgang Ewald","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Wolfgang Ewald","Est. reading time":"18 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/wolles-elektronikkiste.de\/en\/creating-libraries-and-classes-part-i#article","isPartOf":{"@id":"https:\/\/wolles-elektronikkiste.de\/en\/creating-libraries-and-classes-part-i"},"author":{"name":"Wolfgang Ewald","@id":"https:\/\/wolles-elektronikkiste.de\/en#\/schema\/person\/b774e4d64b4766889a2f7c6e5ec85b46"},"headline":"Creating Libraries and Classes &#8211; Part I","datePublished":"2022-11-11T20:36:08+00:00","dateModified":"2022-11-29T20:44:21+00:00","mainEntityOfPage":{"@id":"https:\/\/wolles-elektronikkiste.de\/en\/creating-libraries-and-classes-part-i"},"wordCount":2772,"commentCount":4,"publisher":{"@id":"https:\/\/wolles-elektronikkiste.de\/en#\/schema\/person\/b774e4d64b4766889a2f7c6e5ec85b46"},"image":{"@id":"https:\/\/wolles-elektronikkiste.de\/en\/creating-libraries-and-classes-part-i#primaryimage"},"thumbnailUrl":"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/09\/Beitragsbild_Bibliotheken.jpg","keywords":[".cpp",".h","Arduino","attributes","class","classes","classes vs. libraries","constructor","enum","Example","header file","keywords.txt","libraries","Library","methods","source file"],"articleSection":["Software and tools"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/wolles-elektronikkiste.de\/en\/creating-libraries-and-classes-part-i#respond"]}]},{"@type":"WebPage","@id":"https:\/\/wolles-elektronikkiste.de\/en\/creating-libraries-and-classes-part-i","url":"https:\/\/wolles-elektronikkiste.de\/en\/creating-libraries-and-classes-part-i","name":"Creating Libraries and Classes - Part I &#8226; Wolles Elektronikkiste","isPartOf":{"@id":"https:\/\/wolles-elektronikkiste.de\/en#website"},"primaryImageOfPage":{"@id":"https:\/\/wolles-elektronikkiste.de\/en\/creating-libraries-and-classes-part-i#primaryimage"},"image":{"@id":"https:\/\/wolles-elektronikkiste.de\/en\/creating-libraries-and-classes-part-i#primaryimage"},"thumbnailUrl":"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/09\/Beitragsbild_Bibliotheken.jpg","datePublished":"2022-11-11T20:36:08+00:00","dateModified":"2022-11-29T20:44:21+00:00","description":"This post is part 1 of a crash course on libraries and classes. I wrote it i to help you get started with this complex topic.","breadcrumb":{"@id":"https:\/\/wolles-elektronikkiste.de\/en\/creating-libraries-and-classes-part-i#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/wolles-elektronikkiste.de\/en\/creating-libraries-and-classes-part-i"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/wolles-elektronikkiste.de\/en\/creating-libraries-and-classes-part-i#primaryimage","url":"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/09\/Beitragsbild_Bibliotheken.jpg","contentUrl":"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/09\/Beitragsbild_Bibliotheken.jpg","width":1100,"height":1100},{"@type":"BreadcrumbList","@id":"https:\/\/wolles-elektronikkiste.de\/en\/creating-libraries-and-classes-part-i#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Startseite","item":"https:\/\/wolles-elektronikkiste.de\/en"},{"@type":"ListItem","position":2,"name":"Creating Libraries and Classes &#8211; Part I"}]},{"@type":"WebSite","@id":"https:\/\/wolles-elektronikkiste.de\/en#website","url":"https:\/\/wolles-elektronikkiste.de\/en","name":"Wolles Elektronikkiste","description":"Die wunderbare Welt der Elektronik","publisher":{"@id":"https:\/\/wolles-elektronikkiste.de\/en#\/schema\/person\/b774e4d64b4766889a2f7c6e5ec85b46"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/wolles-elektronikkiste.de\/en?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/wolles-elektronikkiste.de\/en#\/schema\/person\/b774e4d64b4766889a2f7c6e5ec85b46","name":"Wolfgang Ewald","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/03\/cropped-Logo-1.png","url":"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/03\/cropped-Logo-1.png","contentUrl":"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/03\/cropped-Logo-1.png","width":512,"height":512,"caption":"Wolfgang Ewald"},"logo":{"@id":"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/03\/cropped-Logo-1.png"}}]}},"_links":{"self":[{"href":"https:\/\/wolles-elektronikkiste.de\/en\/wp-json\/wp\/v2\/posts\/16611","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wolles-elektronikkiste.de\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wolles-elektronikkiste.de\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wolles-elektronikkiste.de\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wolles-elektronikkiste.de\/en\/wp-json\/wp\/v2\/comments?post=16611"}],"version-history":[{"count":0,"href":"https:\/\/wolles-elektronikkiste.de\/en\/wp-json\/wp\/v2\/posts\/16611\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wolles-elektronikkiste.de\/en\/wp-json\/wp\/v2\/media\/16049"}],"wp:attachment":[{"href":"https:\/\/wolles-elektronikkiste.de\/en\/wp-json\/wp\/v2\/media?parent=16611"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wolles-elektronikkiste.de\/en\/wp-json\/wp\/v2\/categories?post=16611"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wolles-elektronikkiste.de\/en\/wp-json\/wp\/v2\/tags?post=16611"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}