Skip to content

Commit 54dc501

Browse files
authored
Merge pull request #849 from jasonacox/patch-1
Add begin() function to class example as best practice
2 parents 161f8f7 + 6965645 commit 54dc501

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

content/learn/08.contributions/03.arduino-creating-library-guide/arduino-creating-library-guide.md

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class Morse
6161
{
6262
public:
6363
Morse(int pin);
64+
void begin();
6465
void dot();
6566
void dash();
6667
private:
@@ -107,6 +108,7 @@ class Morse
107108
{
108109
public:
109110
Morse(int pin);
111+
void begin();
110112
void dot();
111113
void dash();
112114
private:
@@ -125,19 +127,27 @@ First comes a couple of #include statements. These give the rest of the code acc
125127
#include "Morse.h"
126128
```
127129

128-
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:
130+
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. The constructor records that in a private variable for use in the other functions:
129131

130132
```arduino
131133
Morse::Morse(int pin)
132134
{
133-
pinMode(pin, OUTPUT);
134135
_pin = pin;
135136
}
136137
```
137138

138139
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).
139140

140-
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**:
141+
Next, you'll create a `begin()` function to handle hardware configuration. This will be called from the `setup()` function of the sketch. Hardware configuration is done in a dedicated function instead of the constructor because the hardware has not yet been initialized at the time the constructor code is executed. In our library, we need to set the pin as an output:
142+
143+
```arduino
144+
void Morse::begin()
145+
{
146+
pinMode(_pin, OUTPUT);
147+
}
148+
```
149+
150+
Then 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**:
141151

142152
```arduino
143153
void Morse::dot()
@@ -163,6 +173,7 @@ Finally, it's typical to include the comment header at the top of the source fil
163173
/*
164174
Morse.cpp - Library for flashing Morse code.
165175
Created by David A. Mellis, November 2, 2007.
176+
Updated by Jason A. Cox, February 18, 2023.
166177
Released into the public domain.
167178
*/
168179
@@ -171,10 +182,14 @@ Finally, it's typical to include the comment header at the top of the source fil
171182
172183
Morse::Morse(int pin)
173184
{
174-
pinMode(pin, OUTPUT);
175185
_pin = pin;
176186
}
177187
188+
void Morse::begin()
189+
{
190+
pinMode(_pin, OUTPUT);
191+
}
192+
178193
void Morse::dot()
179194
{
180195
digitalWrite(_pin, HIGH);
@@ -194,7 +209,7 @@ void Morse::dash()
194209

195210
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.
196211

197-
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).
212+
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 .ino, .pde or .txt extension, for example).
198213

199214
Let's see how we can replicate our old SOS sketch using the new library:
200215

@@ -205,6 +220,7 @@ Morse morse(13);
205220
206221
void setup()
207222
{
223+
morse.begin();
208224
}
209225
210226
void loop()
@@ -228,7 +244,7 @@ Morse morse(13);
228244

229245
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).
230246

231-
Notice that our `setup()` is now empty; that's because the call to `pinMode()` happens inside the library (when the instance is constructed).
247+
Notice that our `setup()` now has a call to `morse.begin()` which configures the pin that was set in the constructor.
232248

233249
Finally, to call the `dot()` and `dash()` 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:
234250

@@ -243,6 +259,7 @@ If you tried the new sketch, you probably noticed that nothing from our library
243259

244260
```arduino
245261
Morse KEYWORD1
262+
begin KEYWORD2
246263
dash KEYWORD2
247264
dot KEYWORD2
248265
```

0 commit comments

Comments
 (0)