Mbed BK Ed2 CH 6
Mbed BK Ed2 CH 6
If you use or reference these slides or the associated textbook, please cite the original authors’ work as follows:
Toulson, R. & Wilmshurst, T. (2016). Fast and Effective Embedded Systems Design - Applying the ARM mbed
(2nd edition), Newnes, Oxford, ISBN: 978-0-08-100880-5.
www.embedded-knowhow.co.uk
The benefits of considered program design
There are a number of C/C++ programming techniques which enable these design
requirements to be achieved, as discussed in this chapter.
Functions
A function is a portion of code within a larger program. The function performs a
specific task and is relatively independent of the main code. Functions can be used to
manipulate data; this is particularly useful if a number of similar data manipulations
are required in the program. We can input data values to the function and the
function can return the result to the main program. It is also possible to use functions
with no input or output data.
A function call
Program design
It is often useful to use a flowchart to indicate the operation of program flow and the
use of functions. We can design code flow using a flowchart prior to coding. Figure 6.2
shows some of the flowchart symbols that are used.
The C/C++ pre-processor modifies code before the program is compiled. Pre-processor
directives are denoted with a “#” symbol.
The #include directive is used to tell the pre-processor to include any code or
statements contained within an external header file; #include essentially just acts as a
copy and paste feature.
The #define directive allows use of meaningful names for specific numerical constants,
for example:
#define SAMPLEFREQUENCY 44100
#define PI 3.141592
The #ifndef directive means “if not defined”, and helps to avoid multiple definition of a
variable in different files, for example:
#ifndef VARIABLE_H // if VARIABLE_H has not previously been defined
#define VARIABLE_H // define it now
The #endif directive is used to indicate the end of the #ifndef conditional
Using mbed objects globally
All mbed objects must be defined in an “owner” source file. But we may want to use
those objects in other files in the project, i.e. “globally”. This can be done by defining
the mbed object in the owner’s header file. When an mbed object is defined for global
use, the extern specifier should be used. For example, in Program Example 6.6, the file
SegDisplay.cpp defines Seg1 as follows:
BusOut Seg1(p5,p6,p7,p8,p9,p10,p11,p12);
As other source files need to manipulate Seg1, it is also declared in the SegDisplay.h
header file (Program Example 6.7) using the extern specifier, as follows:
extern BusOut Seg1;
The specific mbed pins don’t need to be redefined in the header file, as these will have
already been specified in the original object declaration.
Modular program example
Complex programs can now be built up from multiple files. Actual program code for
this example can be found as Program Example 6.5 to 6.9, in the book or on the
support web site.
Working with bespoke libraries
Many libraries exist for implementing additional mbed features. Some of these
libraries are provided by the mbed official website, whereas others have been
created by advanced developers who have allowed their code to be shared through
the online mbed community. We call these the “bespoke” libraries.
Select ‘Import this library’ option and you will then be asked to choose the mbed
program which you want to import the library in to.
Updating libraries
Since libraries (including the mbed official ones) are often “works in progress”, you
may sometimes need to update libraries to the most recent version.
The easiest way to do this is to select the library in your program folder and look at
its current status on the right hand side of the compiler.