Hardware Reference
In-Depth Information
size_t write(value, size)
: Sends data in binary bytes instead of ASCII.
write()
can send
a single byte value, in which case
size
is not needed. A string and buffer are sent as a series of
bytes.
size
declares the buffer's number of bytes to send. Returns the number of bytes sent.
•
void serialEvent(){ }
: Can be added to a sketch that is called any time there is incoming
serial activity.
•
openFrameworks Setup
With the Arduino code outputting and accepting input from the serial monitor, other programs can be developed
using C, C++, Java, or any other computer language from scratch to connect the Arduino to the computer. Coding
from scratch, however, can be a bit tedious for a proof-of-concept project. openFrameworks provides a nice prebuilt
interface for programming serial with C++ and also adds many other useful tidbits for audio and graphics. The next
set of examples will show how the openFrameworks libraries can be used to connect to the Arduino and the sketch
from Listing 3-1.
To verify that openFrameworks is working properly and the library is compiled, make a copy of the empty
example folder in the openFrameworks distribution; keep the folder in the same directory and rename it to
ch3
.
■
the examples are located in the openFrameworks
examples
directory. an empty example to start with is
located in the
apps/myApps
subdirectory of the openFrameworks main directory.
Note
Open the workspace for Code::Blocks or the VC++ project file in the renamed folder, and then compile. Two
things should compile:
emptyexample
and
libopenFrameworks
. After both parts are compiled successfully,
openFrameworks is ready for new code. Coding the examples in this chapter for openFrameworks applications is
done in the Code::Blocks project file or the Microsoft Visual Studio solution (
not
the one named
libopenFrameworks
,
but the one named after the project—e.g.,
emptyexample.workspace
). The files for the examples in this chapter are
available for download from
www.apress.com/9781430239390
and are contained in three files:
main.cpp
,
testapp.cpp
,
and
testapp.h
. These three files can replace the files with the same name that are in the renamed folder
src
directory.
■
moving and compiling projects outside of the openFrameworks
apps/examples
directory may cause
dependencies issues. to solve this, point all the dependencies to the location of the openFrameworks main directory.
Caution
in 32-bit Gnu/Linux, the workspace file for Code::Blocks may point to the 64-bit libraries. solve this by opening the
workspace file in a generic text editor and change
linux64
to
linux
.
Connecting to the Arduino from openFrameworks
Listings 3-2 through 3-4 make up the three files to create and run a basic openFrameworks program to connect to
an Arduino to send and receive data without having to use the serial monitor or console, while also providing extra
computing power by allowing the Arduino to connect to a C++ program.