In the first line, we create a new
Telegraph
object that communicates on pin
13 and emits dits that are 200 milliseconds long. Then we emit the message
“Hello, world!” as Morse code. This way, we are able to send whatever message
we want, and we can change easily the pin and the length of a dit.
Now that we have defined the interface, we will implement it.
Fleshing Out the Morse Code Generator’s Interface
Declaring interfaces is important, but it’s as important to implement them.
Create a new tab and enter the filename
telegraph.cpp
.
This is the right place to explain why we’ve used
TelegraphLibrary
and not
Telegraph
as the sketch’ name, even though it’d be a more natural choice. The reason
is that the Arduino IDE turns every sketch (ending with
.ino
) into a C++ file
(ending with
.cpp
). For a sketch named
Telegraph.ino
it generates a C++ file named
Telegraph.cpp
. In a case-insensitive file system, this conflicts with a file named
telegraph.cpp
, and it leads to some strange error messages.
Enter the following code now in the newly created tab:
TelegraphLibrary/telegraph.cpp
#include <ctype.h>
#include <Arduino.h>
#include "telegraph.h"
const char* LETTERS[] = {
".-", "-...", "-.-.", "-..", ".", // A-E
"..-.", "--.", "....", "..", ".---", // F-J
"-.-", ".-..", "--", "-.", "---", // K-O
".--.", "--.-", ".-.", "...", "-", // P-T
"..-", "...-", ".--", "-..-", "-.--", // U-Y
"--.." // Z
};
const char* DIGITS[] = {
"-----", ".----", "..---", "...--", // 0-3
"....-", ".....", "-....", "--...", // 4-7
"---..", "----." // 8-9
};
Like most C++ programs, ours imports some libraries first. Because we need
functions such as
toupper
later, we include
ctype.h
, and we have to include
telegraph.h
to make our class declaration and its corresponding function decla-
rations available. But what is
Arduino.h
good for?
Until now we haven’t thought about where constants such as
HIGH
,
LOW
, or
OUTPUT
came from. They are defined in several header files that come with the
Chapter 4. Building a Morse Code Generator Library • 64
report erratum • discuss
www.it-ebooks.info