100% found this document useful (1 vote)
22 views

Real Time C Efficient Object Oriented and Template Microcontroller Programming 2nd Edition Unknown - Download the ebook now to never miss important content

The document provides information about the second edition of 'Real Time C Efficient Object Oriented and Template Microcontroller Programming', which serves as a practical guide for programming embedded systems using C++. It highlights updates and new features introduced in C++14, corrections made from the first edition, and includes companion code for practical application. Additionally, it lists various related ebooks available for download on ebookfinal.com.

Uploaded by

yumpiomettai79
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
22 views

Real Time C Efficient Object Oriented and Template Microcontroller Programming 2nd Edition Unknown - Download the ebook now to never miss important content

The document provides information about the second edition of 'Real Time C Efficient Object Oriented and Template Microcontroller Programming', which serves as a practical guide for programming embedded systems using C++. It highlights updates and new features introduced in C++14, corrections made from the first edition, and includes companion code for practical application. Additionally, it lists various related ebooks available for download on ebookfinal.com.

Uploaded by

yumpiomettai79
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

Visit ebookfinal.

com to download the full version and


explore more ebooks or textbooks

Real Time C Efficient Object Oriented and Template


Microcontroller Programming 2nd Edition Unknown

_____ Click the link below to download _____


https://ebookfinal.com/download/real-time-c-efficient-
object-oriented-and-template-microcontroller-
programming-2nd-edition-unknown/

Explore and download more ebooks or textbook at ebookfinal.com


Here are some recommended products that we believe you will be
interested in. You can click the link to download.

Beginning C Object Oriented Programming 2nd Edition Dan


Clark

https://ebookfinal.com/download/beginning-c-object-oriented-
programming-2nd-edition-dan-clark/

Beginning C Object Oriented Programming 1st Edition Dan


Clark

https://ebookfinal.com/download/beginning-c-object-oriented-
programming-1st-edition-dan-clark/

Beginning Object Oriented Programming with C 1st Edition


Jack Purdum

https://ebookfinal.com/download/beginning-object-oriented-programming-
with-c-1st-edition-jack-purdum/

Object Oriented Programming and Java Second Edition Danny


Poo

https://ebookfinal.com/download/object-oriented-programming-and-java-
second-edition-danny-poo/
Python 3 Object Oriented Programming 1st Edition Dusty
Phillips

https://ebookfinal.com/download/python-3-object-oriented-
programming-1st-edition-dusty-phillips/

Object oriented Programming with Smalltalk 1st Edition


Harald Wertz

https://ebookfinal.com/download/object-oriented-programming-with-
smalltalk-1st-edition-harald-wertz/

Object Oriented Programming using Java 1st edition Edition


Kendal S.

https://ebookfinal.com/download/object-oriented-programming-using-
java-1st-edition-edition-kendal-s/

Object Oriented Programming in VB Net 1st Edition Alistair


Mcmonnies

https://ebookfinal.com/download/object-oriented-programming-in-vb-
net-1st-edition-alistair-mcmonnies/

Object Oriented JavaScript 2nd Edition Stoyan Stefanov

https://ebookfinal.com/download/object-oriented-javascript-2nd-
edition-stoyan-stefanov/
Real Time C Efficient Object Oriented and Template
Microcontroller Programming 2nd Edition Unknown
Digital Instant Download
Author(s): Unknown
ISBN(s): 9783662478103, 3662478102
File Details: PDF, 5.11 MB
Year: 2015
Language: english
Christopher Kormanyos

Real-Time
C++
Efficient Object-Oriented and Template
Microcontroller Programming
Second Edition

www.allitebooks.com
Real-Time C++

www.allitebooks.com
Christopher Kormanyos

Real-Time C++
Efficient Object-Oriented and Template
Microcontroller Programming
Second Edition

123

www.allitebooks.com
Christopher Kormanyos
Reutlingen
Baden-Württemberg
Germany

ISBN 978-3-662-47809-7 ISBN 978-3-662-47810-3 (eBook)


DOI 10.1007/978-3-662-47810-3

Library of Congress Control Number: 2015944733

Springer Heidelberg New York Dordrecht London


© Springer-Verlag Berlin Heidelberg 2013, 2015
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part
of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations,
recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission
or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar
methodology now known or hereafter developed.
The use of general descriptive names, registered names, trademarks, service marks, etc. in this
publication does not imply, even in the absence of a specific statement, that such names are exempt from
the relevant protective laws and regulations and therefore free for general use.
The publisher, the authors and the editors are safe to assume that the advice and information in this
book are believed to be true and accurate at the date of publication. Neither the publisher nor the
authors or the editors give a warranty, express or implied, with respect to the material contained herein or
for any errors or omissions that may have been made.

Printed on acid-free paper

Springer-Verlag GmbH Berlin Heidelberg is part of Springer Science+Business Media


(www.springer.com)

www.allitebooks.com
To those who pursue the art of technical
creativity

www.allitebooks.com
Preface to the Second Edition

C++ seamlessly blends object-oriented techniques with generic template methods,


creating a modern powerful programming language useful for problem-solving in
countless domains. The most recent evolution of C++ from C++11 to C++14 has
brought yet further improvements to this rich language.1 As C++ becomes even
more expressive, growing numbers of embedded systems developers are discov-
ering new and fascinating ways to utilize its multifaceted capabilities for creating
efficient and effective microcontroller software.
The second edition of this book retains its original purpose to serve as a practical
guide to programming real-time embedded microcontroller systems in C++. New
material has been incorporated predominantly reflecting changes introduced in the
C++14 standard. Various sections have been reworked according to reader sug-
gestions. Selected passages have been reformulated in a continued effort to improve
clarity. In addition, all known errors throughout the text have been corrected.
New sections have been added (in particular for C++14) covering:
• digit separators (Sect. 3.15),
• binary literals (Sect. 3.16),
• user-defined literals (Sect. 3.17),
• variable templates (Sect. 5.12),
• and the chapter09_07 sample project (Sect. 9.7) controlling an
industry-standard seven-segment display.
Two new sample projects, chapter02_03a and chapter09_07, have been
added to the companion code.

1
At the time of writing the second edition of this book, C++14 is brand new. World-class com-
pilers are shipped with support for C++14. Work is in progress on C++1z, the next specification of
C++ (sometimes known as C++17). Experts anticipate that the specification of C++1z could be
finished in 2017.

vii

www.allitebooks.com
viii Preface to the Second Edition

The chapter02_03a sample project implements LED toggling at 1= 2 Hz


with timing provided by a simple multitasking scheduler in combination with a
timer utility.
The chapter09_07 sample project in the newly added Sect. 9.7 uses many
of the advanced programming methods in this book to animate an
industry-standard seven-segment display.
Significantly reworked or corrected parts of this book include:
• corrections and clarifications in Chap. 1 on getting started with C++,
• the description of the chapter02_02 project in Sect. 2.2,
• parts of Chap. 3 on the jump-start in real-time C++,
• corrections and clarifications in Chap. 5 on templates,
• Sects. 6.1 and 6.2 on optimization and performance,
• parts of Chap. 10 on custom memory management,
• parts of Chaps. 12 and 13 on mathematics,
• the literature list in Sect. 17.1,
• parts of Appendix A in the C++ tutorial,
• and repairs and extensions of the citations in some chapter references.

Companion Code

The companion code continues to be supported and numerous developers have


successfully worked with it on various cross-development platforms. The scope
of the companion code has been expanded to include a much wider range of target
microcontrollers. In addition, the chapter02_03a and chapter09_07 sample
projects that are mentioned above have been added to the companion code.
The companion code is available at:
http://github.com/ckormanyos/real-time-cpp

More Notes on Coding Style

The second edition of this book features slight changes in coding style. These can
be encountered in the code samples throughout the text.
Compiler support for standard C99 and C++11 macros of the form UINT8_C(),
UINT16_C(), UINT32_C(), etc., and corresponding macros for signed types in
the <stdint.h> and <cstdint> headers has become more prevalent (see also
Sect. 3.2). Consequently, these macros are used more frequently throughout the code
samples.

www.allitebooks.com
Preface to the Second Edition ix

These macros are useful for creating integer numeric literal values having
specified widths. The code below, for example, utilizes UINT8_C() to initialize an
8-bit integer variable with a numeric literal value.

Digit separators have become available with C++14 (Sect. 3.15). These are used
in selected code samples to improve the clarity of long numeric literals. Digit
separators are shown in the code sample below.

Other than these minor changes, however, the coding style in the second edition
of this book remains consistent with that of the first edition and is intended to be
clean and clear.

Reutlingen, Germany Christopher Kormanyos


Seattle, Washington
May 2015

www.allitebooks.com
Preface to the First Edition

This book is a practical guide to programming real-time embedded microcontroller


systems in C++. The C++ language has powerful object-oriented and template
features that can improve software design and portability, while simultaneously
reducing code complexity and the risk of error. At the same time, C++ compiles
highly efficient native code. This unique and effective combination makes C++
well-suited for programming microcontroller systems that require compact size,
high performance, and safety-critical reliability.
The target audience of this book includes hobbyists, students, and professionals
interested in real-time C++. The reader should be familiar with C or another pro-
gramming language and should ideally have had some exposure to microcontroller
electronics and the performance and size issues prevalent in embedded systems
programming.

About This Book

This is an interdisciplinary book that includes a broad range of topics. Real-world


examples have been combined with brief descriptions in an effort to provide an
intuitive and straightforward methodology for microcontroller programming in
C++. Efficiency is always in focus and numerous examples are backed up with
real-time performance measurements and size analyses that quantify the true costs
of the code down to the very last byte and microsecond.
Throughout the chapters, C++ is used in a bare-bones, no-frills fashion without
relying on any libraries other than those specified in the language standard itself.
This approach facilitates portability.
This book has three parts and several appendices. The three parts generally build
on each other with the combined goal of providing a coherent and effective set of
C++ methods that can be used with a wide range of embedded microcontrollers.

xi

www.allitebooks.com
xii Preface to the First Edition

• Part I provides a foundation for real-time C++ by covering language technolo-


gies. Topics include getting started in real-time C++, object-oriented methods,
template programming, and optimization. The first three chapters have a par-
ticularly hands-on nature and are intended to boost competence in real-time C++.
Chapter 6 has a unique and important role in that it is wholly dedicated to
optimization techniques appropriate for microcontroller programming in C++.
• Part II presents detailed descriptions of a variety of C++ components that are
widely used in microcontroller programming. These components can be either
used as presented, or adapted for other projects. This part of the book uses some
of C++’s most powerful language elements, such as class types, templates, and
the STL, to develop components for microcontroller register access, low-level
drivers, custom memory management, embedded containers, multitasking, etc.
• Part III describes mathematical methods and generic utilities that can be
employed to solve recurring problems in real-time C++.
• The appendices include a C++ language tutorial, information on the real-time
C++ development environment and instructions for building GNU GCC
cross-compilers and a microcontroller circuit.
C++ is a rich language with many features and details, the description of which
can fill entire bookshelves. This book, however, primarily concentrates on how to
use C++ in a real-time microcontroller environment. Along these lines, C++ lan-
guage tutorials have been held terse, and information on microcontroller hardware
and compilers is included only insofar as it is needed for the examples. A suggested
list of additional reading material is given in Chap. 17 for those seeking supple-
mentary information on C++, the C++ standard library and STL, software design,
C++ coding guidelines, the embedded systems toolchain, and microcontroller
hardware.
When units are needed to express physical quantities, the MKS (meter, kilogram,
second) system of units is used.

Companion Code, Targets, and Tools

The companion code includes three introductory projects and one reference project.
The introductory projects treat various aspects of the material presented in Chaps.
1 and 2. The reference project is larger in scope and exercises many of the methods
from all the chapters.
The companion code is available at:
http://github.com/ckormanyos/real-time-cpp
The C++ techniques in this book specifically target microcontrollers in the
small-to-medium size range. Here, small-to-medium spans the following approxi-
mate size and performance ranges.
• 4 kB … 1-MB program code
• 256 byte … 128-kB RAM

www.allitebooks.com
Preface to the First Edition xiii

• 8–bit … 32–bit CPU


• 8 MHz … 200-MHz CPU frequency
Most of the methods described in this book are, however, scalable. As such, they
can be used equally well on larger or smaller devices, even on PCs and worksta-
tions. In particular, they can be employed if the application has strict performance
and size constraints.
A popular 8–bit microcontroller clocked with a frequency of 16 MHz has been
used as the primary target for benchmarking and testing the code samples in this
book. Certain benchmarks have also been performed with a well-known 32–bit
microcontroller clocked at 24 MHz. An 8–bit microcontroller and a 32–bit micro-
controller have been selected in order to exercise the C++ methods over a wide range
of microcontroller performance.
All the C++ examples and benchmarks in the book and the companion code
have been compiled with GNU GCC versions 4.6.2 and 4.7.0. Certain examples
and benchmarks have also been compiled with other PC-based compilers.
The most recent specification of C++11 in ISO/IEC 14882:2011 is used
throughout the text. At the time this book is written, the specification of C++11 is
brand new. The advent of C++11 has made C++ significantly more effective and
easy-to-use. This will profoundly influence C++ programming. The well-informed
reader will, therefore, want to keep in touch with C++11 best-practice as it evolves
in the development community.

Notes on Coding Style

A consistent coding style is used throughout the examples in this book and in the
companion code.
Code samples are written with a fixed-width font. C++ language keywords
and built-in types use the same font, but they are in boldface. For instance,

In general, the names of all symbols such as variables, class types, members, and
subroutines are written in lower-case. A single underscore ( _ ) is used to separate
words and abbreviations in names. For instance, a system-tick variable expressed
with this style is shown in the code sample below:

Using prefixes, suffixes, or abbreviations to incorporate type information in a


name, sometimes known as Hungarian notation, is not done. Superfluous prefixes,
xiv Preface to the First Edition

suffixes, and abbreviations in Hungarian notation may obscure the name of a


symbol and symbol names can be more intuitive and clear without them. For
example,

Names that are intended for use in public domains are preferentially long and
descriptive rather than short and abbreviated. Here, clarity of expression is preferred
over terseness. Symbols used for local subroutine parameters or private imple-
mentation details with obvious meanings, however, often have terse or abbreviated
names.
The global subroutine below, for example, uses this naming style. It returns the
float value of the squared Euclidean distance from the origin of a point in
two-dimensional Cartesian space R2 :

C++ references are heavily used because this can be advantageous for small
microcontrollers. Consider an 8–bit microcontroller. The work of copying sub-
routine parameters or the work of pushing them onto the stack for anything wider
than 8 bits can be significant. This workload can potentially be reduced by using
references. In the previous code sample, for instance, the floating-point subroutine
parameters x and y, each 4 bytes wide, have been passed to the subroutine by
reference (i.e., const float&).
Fixed-size integer types defined in the std namespace of the C++ standard
library such as std::uint8_t, std::uint16_t, and std::uint32_t, and
the like are preferentially used instead of plain built-in types such as char, short,
and int. This improves clarity and portability. An unsigned login response with
exactly 8 bits, for instance, is shown below.

Code samples often rely on one or more of the C++ standard library headers
such as <algorithm>, <array>, <cstdint>, <limits>, <tuple>,
<vector>, etc. In general, code samples requiring library headers do not
explicitly include their necessary library headers.
Preface to the First Edition xv

The declaration of login_response above, for example, actually requires


<cstdint> for the definition of std::uint8_t. The library file is, however,
not included. In general, the code samples focus on the core of the code, not on the
inclusion of library headers.
It is easy to guess or remember, for example, that std::array can be found in
<array> and that std::vector is located <vector>. It can, however, be
more difficult to guess or remember that std::size_t is in <cstddef> or that
std::accumulate() is in <numeric>. With assistance from online help and
other resources and with a little practice, though, it becomes routine to identify what
standard library parts can be found in which headers.
In cases for which particular emphasis is placed on the inclusion of a header file,
the relevant #include line(s) may be explicitly written. For instance,

Namespaces are used frequently. In general, though, the using directive is not
used to inject symbols in namespaces into the global namespace. This means that
the entire namespace must be typed with the name of a symbol in it. This, again,
favors non-ambiguity over brevity.
The unsigned 16–bit counter below, for example, uses a type from the std
namespace. Since the “using namespace std” directive is not used, the name
of the namespace (std) is explicitly included in the type.

Suffixes are generally appended to literal constant values. When a suffix is


appended to a literal constant value, its optional case is upper-case. For example,

Certain established C++ coding guidelines have strongly influenced the coding
style. For the sake of terseness and clarity, however, not every guideline has been
followed all the time.
xvi Preface to the First Edition

One clearly recognizable influence of the coding guidelines is the diligent use of
C++-style casts when converting built-in types. The following code, for instance,
explicitly casts from float to an unsigned integer type.

Even though explicit casts like these are not always mandatory, they can resolve
ambiguity and eliminate potential misinterpretation caused by integer promotion.
Another influence of the coding guidelines on the code is the ordering of class
members according to their access level in the class. The communication class
below, for example, represents the base class in a hierarchy of communication
objects. The members in the class definition are ordered according to access level.
In particular,

C-style preprocessor macros are used occasionally. Preprocessor macros are


written entirely in upper case letters. Underscores separate the words in the names
of preprocessor macros. The MAKE_WORD() preprocessor macro below, for
example, creates an unsigned 16–bit word from two unsigned 8–bit constituents.
Preface to the First Edition xvii

Acknowledgments

First and foremost, I would like to thank my wife and my daughter for encouraging
me to write this book and also for creating a peaceful, caring atmosphere in which I
could work productively. Thank you for your support and your time. You have my
gratitude.
I would also like to express appreciation to family, friends, and associates, too
numerous to list, who contributed to this project with their innovative ideas, sup-
port, friendship, and companionship.
Thanks go to the members of the C++ standards committee, Boost, the volun-
teers at GCC, and all the developers in the vibrant C++ and embedded systems
communities. Through your efforts, often times for no pay whatsoever, C++ has
evolved to an unprecedented level of expressiveness, making object-oriented and
generic programming more effective and easier than ever.
Working with Springer Verlag was a delightful experience. I thank my editor,
who first identified the merit of this work and supported me throughout the writing
process. I also thank the copy editing team and all the staff at Springer Verlag for
their professionalism and capable assistance.
• ATMEL® and AVR® are registered trademarks of Atmel Corporation or its
subsidiaries, in the US and other countries.
• Real-Time C++: Efficient Object-Oriented and Template Microcontroller
Programming is a book authored by Christopher Kormanyos and published by
Springer Verlag and has not been authorized, sponsored, or otherwise approved
of by Atmel Corporation.
• ARDUINO® is a registered trademark of the Arduino Group.
• SPITM is a trademark of Motorola Corporation.
• The circuit of the target hardware described in this book and depicted in Chapter
2 and Appendix D was designed and assembled on a solderless prototyping
breadboard by Christopher Kormanyos.
• The photographs of the target hardware described in this book and depicted in
Chapter 2 and Appendix D were taken by Christopher Kormanyos.

Reutlingen, Germany Christopher Kormanyos


Seattle, Washington
September 2012
Contents

Part I Language Technologies for Real-Time C++

1 Getting Started with Real-Time C++ . . . . . . . . . . . . . . . . . . . . . . 3


1.1 The LED Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 The Syntax of C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.3 Class Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.4 Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.5 Objects and Instances . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.6 #include . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.7 Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
1.8 C++ Standard Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
1.9 The main() Subroutine . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
1.10 Low-Level Register Access . . . . . . . . . . . . . . . . . . . . . . . . . 16
1.11 Compile-Time Constant . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
References. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

2 Working with a Real-Time C++ Program on a Board . . . . . . . . . 19


2.1 The Target Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.2 Build and Flash the LED Program . . . . . . . . . . . . . . . . . . . . 20
2.3 Adding Timing for Visible LED Toggling . . . . . . . . . . . . . . . 24
2.4 Run and Reset the LED Program . . . . . . . . . . . . . . . . . . . . . 26
2.5 Recognizing and Handling Errors and Warnings . . . . . . . . . . . 27
2.6 Reaching the Right Efficiency . . . . . . . . . . . . . . . . . . . . . . . 29
References. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31

3 An Easy Jump-Start in Real-Time C++ . . . . . . . . . . . . . . . . . . . . 33


3.1 Declare Locals When Used . . . . . . . . . . . . . . . . . . . . . . . . . 33
3.2 Fixed-Size Integer Types . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
3.3 The bool Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
3.4 Organization with Namespaces . . . . . . . . . . . . . . . . . . . . . . . 37

xix
xx Contents

3.5 Basic Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38


3.6 Basic Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
3.7 nullptr Replaces NULL . . . . . . . . . . . . . . . . . . . . . . . . . . 40
3.8 Generalized Constant Expressions with constexpr . . . . . . . 41
3.9 static_assert . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
3.10 Using <limits>. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
3.11 std::array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
3.12 Basic STL Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
3.13 <numeric> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
3.14 atomic_load() and atomic_store() . . . . . . . . . . . . . 46
3.15 Digit Separators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
3.16 Binary Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
3.17 User-Defined Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49

4 Object-Oriented Techniques for Microcontrollers . . . . . . . . . . . . . 51


4.1 Object Oriented Programming . . . . . . . . . . . . . . . . . . . . . . . 51
4.2 Objects and Encapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . 56
4.3 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
4.4 Dynamic Polymorphism. . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
4.5 The Real Overhead of Dynamic Polymorphism . . . . . . . . . . . 60
4.6 Pure Virtual and Abstract. . . . . . . . . . . . . . . . . . . . . . . . . . . 61
4.7 Class Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
4.8 Non-Copyable Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
4.9 Constant Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
4.10 Static Constant Integral Members . . . . . . . . . . . . . . . . . . . . . 68
4.11 Class Friends . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
4.12 Virtual Is Unavailable in the Base Class Constructor . . . . . . . . 71
References. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74

5 C++ Templates for Microcontrollers . . . . . . . . . . . . . . . . . . . . . . 75


5.1 Template Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
5.2 Template Scalability, Code Re-Use and Efficiency . . . . . . . . . 77
5.3 Template Member Functions . . . . . . . . . . . . . . . . . . . . . . . . 79
5.4 Template Class Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
5.5 Template Default Parameters . . . . . . . . . . . . . . . . . . . . . . . . 83
5.6 Template Specialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
5.7 Static Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
5.8 Using the STL with Microcontrollers. . . . . . . . . . . . . . . . . . . 89
5.9 Variadic Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
5.10 Template Metaprogramming . . . . . . . . . . . . . . . . . . . . . . . . . 93
5.11 Tuples and Generic Metaprogramming . . . . . . . . . . . . . . . . . 96
5.12 Variable Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
References. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Contents xxi

6 Optimized C++ Programming for Microcontrollers . . . . . . . . . . . 103


6.1 Use Compiler Optimization Settings . . . . . . . . . . . . . . . . . . . 103
6.2 Know the Microcontroller’s Performance . . . . . . . . . . . . . . . . 106
6.3 Know an Algorithm’s Complexity. . . . . . . . . . . . . . . . . . . . . 108
6.4 Use Assembly Listings . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
6.5 Use Map Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
6.6 Understand Name Mangling and De-Mangling . . . . . . . . . . . . 111
6.7 Know When to Use Assembly and When Not to . . . . . . . . . . 112
6.8 Use Comments Sparingly. . . . . . . . . . . . . . . . . . . . . . . . . . . 114
6.9 Simplify Code with typedef . . . . . . . . . . . . . . . . . . . . . . . 114
6.10 Use Native Integer Types. . . . . . . . . . . . . . . . . . . . . . . . . . . 116
6.11 Use Scaling with Powers of Two . . . . . . . . . . . . . . . . . . . . . 118
6.12 Potentially Replace Multiply with Shift-and-Add . . . . . . . . . . 119
6.13 Consider Advantageous Hardware Dimensioning . . . . . . . . . . 120
6.14 Consider ROM-Ability . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
6.15 Minimize the Interrupt Frame . . . . . . . . . . . . . . . . . . . . . . . . 123
6.16 Use Custom Memory Management . . . . . . . . . . . . . . . . . . . . 126
6.17 Use the STL Consistently . . . . . . . . . . . . . . . . . . . . . . . . . . 126
6.18 Use Lambda Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
6.19 Use Templates and Scalability . . . . . . . . . . . . . . . . . . . . . . . 129
6.20 Use Metaprogramming to Unroll Loops . . . . . . . . . . . . . . . . . 130
References. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130

Part II Components for Real-Time C++

7 Accessing Microcontroller Registers. . . . . . . . . . . . . . . . . . . . . . . 133


7.1 Defining Constant Register Addresses . . . . . . . . . . . . . . . . . . 133
7.2 Using Templates for Register Access. . . . . . . . . . . . . . . . . . . 135
7.3 Generic Templates for Register Access . . . . . . . . . . . . . . . . . 137
7.4 Bit-Mapped Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142

8 The Right Start . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143


8.1 The Startup Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
8.2 Initializing RAM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
8.3 Initializing the Static Constructors. . . . . . . . . . . . . . . . . . . . . 147
8.4 The Connection between the Linker and Startup . . . . . . . . . . . 149
8.5 Understand Static Initialization Rules . . . . . . . . . . . . . . . . . . 151
8.6 Avoid Using Uninitialized Objects . . . . . . . . . . . . . . . . . . . . 152
8.7 Jump to main() and Never return. . . . . . . . . . . . . . . . . . 154
8.8 When in main(), What Comes Next? . . . . . . . . . . . . . . . . . 155
References. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
xxii Contents

9 Low-Level Hardware Drivers in C++. . . . . . . . . . . . . . . . . . . . . . 157


9.1 An I/O Port Pin Driver Template Class . . . . . . . . . . . . . . . . . 157
9.2 Programming Interrupts in C++ . . . . . . . . . . . . . . . . . . . . . . 160
9.3 Implementing a System-Tick . . . . . . . . . . . . . . . . . . . . . . . . 164
9.4 A Software PWM Template Class. . . . . . . . . . . . . . . . . . . . . 167
9.5 A Serial SPITM Driver Class . . . . . . . . . . . . . . . . . . . . . . . . 171
9.6 CPU-Load Monitors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
9.7 Controlling a Seven-Segment Display . . . . . . . . . . . . . . . . . . 178
References. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183

10 Custom Memory Management . . . . . . . . . . . . . . . . . . . . . . . . . . 185


10.1 Dynamic Memory Considerations . . . . . . . . . . . . . . . . . . . . . 185
10.2 Using Placement-new . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
10.3 Allocators and STL Containers . . . . . . . . . . . . . . . . . . . . . . . 188
10.4 The Standard Allocator . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
10.5 Writing a Specialized ring_allocator. . . . . . . . . . . . . . . 190
10.6 Using ring_allocator and Other Allocators . . . . . . . . . . 193
10.7 Recognizing and Handling Memory Limitations . . . . . . . . . . . 195
References. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197

11 C++ Multitasking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199


11.1 Multitasking Schedulers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
11.2 Task Timing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
11.3 The Task Control Block . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
11.4 The Task List. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
11.5 The Scheduler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
11.6 Extended Multitasking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
11.7 Preemptive Multitasking . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
11.8 The C++ Thread Support Library . . . . . . . . . . . . . . . . . . . . . 209
References. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210

Part III Mathematics and Utilities for Real-Time C++

12 Floating-Point Mathematics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213


12.1 Floating-Point Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
12.2 Mathematical Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
12.3 Elementary Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
12.4 Special Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
12.5 Complex-Valued Mathematics . . . . . . . . . . . . . . . . . . . . . . . 225
12.6 Compile-Time Evaluation of Functions with constexpr . . . . 228
12.7 Generic Numeric Programming. . . . . . . . . . . . . . . . . . . . . . . 231
References. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
Contents xxiii

13 Fixed-Point Mathematics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241


13.1 Fixed-Point Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
13.2 A Scalable Fixed-Point Template Class . . . . . . . . . . . . . . . . . 244
13.3 Using the fixed_point Class. . . . . . . . . . . . . . . . . . . . . . 247
13.4 Fixed-Point Elementary Transcendental Functions . . . . . . . . . . 250
13.5 A Specialization of std::numeric_limits . . . . . . . . . . . 260
References. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262

14 High-Performance Digital Filters . . . . . . . . . . . . . . . . . . . . . . . . . 263


14.1 A Floating-Point Order-1 Filter. . . . . . . . . . . . . . . . . . . . . . . 263
14.2 An Order-1 Integer Filter . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
14.3 Order-N Integer FIR Filters . . . . . . . . . . . . . . . . . . . . . . . . . 269
14.4 Some Worked-Out Filter Examples . . . . . . . . . . . . . . . . . . . . 274
References. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279

15 C++ Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281


15.1 The nothing Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
15.2 The noncopyable Class . . . . . . . . . . . . . . . . . . . . . . . . . . 284
15.3 A Template timer Class . . . . . . . . . . . . . . . . . . . . . . . . . . 286
15.4 Linear Interpolation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
15.5 A circular_buffer Template Class . . . . . . . . . . . . . . . . 292
15.6 The Boost Library. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
References. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297

16 Extending the C++ Standard Library and the STL . . . . . . . . . . . 299


16.1 Defining the Custom dynamic_array Container . . . . . . . . . 299
16.2 Implementing and Using dynamic_array . . . . . . . . . . . . . 301
16.3 Writing Parts of the C++ Library if None Is Available . . . . . . 305
16.4 Implementation Notes for Parts of the C++ Library
and STL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ...... 306
16.5 Providing now() for <chrono>’s
High-Resolution Clock . . . . . . . . . . . . . . . . . . . . . . ...... 312
Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ...... 313

17 Additional Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315


17.1 Literature List. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
References. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316

Appendix A: A Tutorial for Real-Time C++ . . . . . . . . . . . . . . . . . . . . 319

Appendix B: A Robust Real-Time C++ Environment . . . . . . . . . . . . . 345

Appendix C: Building and Installing GNU GCC Cross Compilers . . . . 351

www.allitebooks.com
Discovering Diverse Content Through
Random Scribd Documents
It was very quiet. They had no difficulty in breathing, as the locker
had a number of air spaces that they had bored in the top and sides,
invisible to a casual glance.
Once in a while they could hear a car speeding past on the Shore
Road.
Minute after minute went by. They were becoming cramped.
Presently Joe yawned loud and long.
"I guess it's no use," said Frank, at last. "We're out of luck to-night."
"Can't expect to be lucky the first time," replied his brother
philosophically.
"We might as well go home."
Frank raised the lid of the locker and peeped out. It was quite dark.
The Shore Road was deserted.
"Coast is clear," he said.
They got quickly out of the locker. They lost no time, for there was a
possibility that one of the auto thieves might be in the neighborhood,
watching the roadster, and if their trap was discovered it would be
useless to make a second attempt.
They got back into the car, Joe taking the wheel this time. He drove
the roadster back onto the highway, turned it around, and they set
out back for Bayport.
Both lads were disappointed, although they had not yet given up
hope. They had been so confident that their plan would be
successful that this failure took some of the wind out of their sails, so
to speak.
"We'll just try again to-morrow night," said Frank.
"Perhaps the auto thieves have quit."
"Not them! They'll fall for our trap yet."
"I'm glad we didn't tell any of the fellows. We'll look mighty foolish if it
doesn't work."
The car sped along the Shore Road, the headlights casting a brilliant
beam of illumination. As they rounded a curve they caught a glimpse
of a dark figure trudging along in the shadow of the trees bordering
the ditch.
"Wonder who that is," Frank remarked, peering at the man.
Joe bore down on the wheel, swinging the car around so that the
headlights fell full on the man beside the road. Then he swung the
car back into its course again.
The fellow had flung up his arm to shield his face from the glare, but
he had not been quick enough to hide his features altogether. Frank
had recognized him at once.
"So!" he remarked thoughtfully. "Our friend again."
"I didn't get a good look at him," Joe said. "Somehow, he seemed
familiar."
"He was. I'd recognize that face anywhere now."
"Who was it?"
"Gus Montrose."
Joe whistled.
"I wonder what he's doing, skulking along here at this time of night."
"I have an idea that we'll find out before long."
"Do you think he has anything to do with the car thefts?"
"Shouldn't be surprised. He seems a rather suspicious sort of
character."
They sped past the dark figure, who went on, head down, hands
thrust deep in his coat pockets.
"I'd like to know more about that chap," mused Frank. "I'll bet he's
not hanging around here for any good reason."
CHAPTER XIV
Montrose Again
The Hardy boys were not discouraged by this failure. They realized
that it was too much to hope for success in their venture at the first
trial and resolved to lay their trap again.
If their parents were curious as to why they had remained out so
late, they gave no sign of it, and the following night Frank and Joe
again drove out along the Shore Road in their new car. This time
they went to another parking place, not far from the spot where Isaac
Fussy's automobile had been stolen.
Again they turned out the lights, again they crawled into the locker at
the back, and again they remained in hiding, while car after car went
by on the Shore Road.
An hour passed.
"Looks as if we're out of luck again," whispered Joe.
"We'll stay with it a while longer."
Frank switched on his flashlight and glanced at his watch. It was
almost ten o'clock. They heard an automobile roar past at
tremendous speed, and a few moments later there was the heavy
rumble of a truck.
"Funny time of night for a truck to be out," Frank remarked.
"That first car was sure breaking all speed laws."
After a long time, Frank again looked at his watch.
"Half-past ten."
"Another evening wasted."
"Are you getting tired?"
"My legs are so cramped I don't think I'll ever be able to walk straight
again."
Joe had inadvertently raised his voice. Suddenly Frank gripped his
arm.
"Shh!"
They listened. They heard footsteps coming along the road. The
steps sounded clear and distinct on the hard highway. Then they
became soft and muffled as the pedestrian turned out onto the
grassy slope.
"Coming this way," whispered Frank.
Some one approached the roadster cautiously. The boys could hear
him moving around the car. After a moment or so, one of the doors
was opened and some one clambered into the seat.
The boys were breathless with excitement. Was this one of the auto
thieves?
But the intruder made no move to drive the car away. Instead, when
he had snapped the lights on and off, he got out, closed the door
behind him and strode off through the grass.
The first impulse of the two brothers was to clamber out, but they
realized that this would be folly. They remained quiet, as the
footsteps receded into the distance. The man gained the road again
and walked slowly away. Finally, they heard the footsteps no more.
Frank sighed with disappointment.
"I thought sure we had a bite that time," he said.
"It was only a nibble."
When the lads were quite sure their unknown visitor had gone, Frank
raised the lid of the locker and the boys got out.
"I guess it was only some farmer on the way home. He probably just
got into the car out of curiosity."
"He wasn't an auto thief, that's certain, or he would have driven off
with it."
"Not much use staying around any longer."
They got back into the seat. Nothing had been disturbed. Beyond
turning the lights on and off, the stranger had tampered with nothing.
Frank started up the engine, and drove the car back onto the Shore
Road. There was not much room in which to turn around, so he
drove on down the road for about a quarter of a mile until he came to
a lane which offered sufficient space.
Just as he was bringing the car around to head back toward Bayport,
the headlights shone on two figures coming up the road. In the glare,
the men were clearly revealed.
"There's our friend Gus again," remarked Frank quietly.
He was right. There was no mistaking the surly visage of the ex-
farmhand. The man with him was unknown to the boys, but he was
no more prepossessing than his companion. Broad of build,
unshaven of face, he was not the sort of fellow one would care to
meet alone on a dark night.
"Handsome-looking pair," Joe commented.
The car swung out into the road and the two men stepped out into
the ditch, turning their faces away. Frank stepped on the accelerator,
and the roadster shot ahead.
"This seems to be Gus Montrose's beat," he said, when they had
driven beyond hearing distance.
"Wonder what takes him out along here every night."
"Perhaps he was the chap who got into the roadster."
But Frank shook his head.
"That fellow went away in the direction Montrose is coming from," he
pointed out. "And, besides, he was alone."
"That's true, too."
Wondering what brought Montrose and his villainous-looking
companion out the Shore Road on foot at that hour, the Hardy boys
drove back into Bayport.
"Better luck next time," said Frank, cheering up.
"We won't give up yet. Third time's luck, you know."
"Let's hope so. To-morrow night may tell."
They drove back into the city without incident, and when they
reached their home they saw that there was a light in their father's
study. Frank's face lengthened.
"I'll bet we're in for it now. He doesn't often stay up this late."
"He's likely sitting up to lecture us."
They put the car into the garage. The light in the study seemed
ominous just then.
"Well," said Joe, "I guess we might as well go in and face the music.
If the worst comes to the worst we'll tell him just what we were up
to."
They went into the house. It would have been easy for them to have
gained their room by the back stairs, but the boys had too much
principle to dodge any unpleasantness in this manner, so they made
a point of passing by their father's study. The door was open and
they saw Fenton Hardy sitting at his desk.
He was not writing, but was gazing in front of him with a fixed
expression on his face. A telephone was at his elbow.
To their relief, he smiled when he saw them.
"Come in," he invited.
Frank and Joe entered the study.
"Did you catch any auto thieves?" asked their father.
The boys were astonished.
"How did you know we were after auto thieves, Dad?" asked Frank.
"It doesn't take a great deal of perception to find that out," their father
answered. "All these mysterious doings can have only one reason."
"Well, we didn't catch any," Joe admitted.
"I didn't think so. They've been busy to-night."
"Again!"
Fenton Hardy nodded.
"I've just been talking to the secretary of the Automobile Club. He
telephoned me a short time ago. The thieves cut loose in earnest
this evening."
"Did they steal another car?"
"Two. They made off with a new Buick that was parked down on Oak
Street, and then they stole a truck from one of the wholesale
companies."
"Can you beat that!" breathed Joe. "Two more gone!"
"They were taken within a few minutes of each other, evidently. The
reports reached the police station almost at the same time. The truck
mightn't have been missed until morning, but one of the wholesale
company employees was coming home and he recognized it as it
was driven away. He thought it rather suspicious, so he went on up
to the company garage and found the truck had disappeared."
The brothers looked at one another.
"A truck and a pleasure car!" exclaimed Frank. "Why, that must have
been—"
The same thought had struck Joe.
"The two cars that passed us on the Shore Road! What time were
they stolen, Dad?"
"Some time between half-past nine and ten o'clock. Why? Did you
see them?"
"Two cars went out the Shore Road a little before ten o'clock. They
were both going at a fast clip. I remember we remarked at the time
that it was a funny hour of the night for a truck to be out."
"The Shore Road, eh? Did you get a good look at them?"
The boys were embarrassed.
"Well, to tell the truth," said Frank hesitatingly, "we didn't exactly see
them. We heard them."
"Hm! You didn't see them, but you heard them, and you were on the
Shore Road. That's a little mystery in itself," remarked their father,
with a smile.
He reached for the telephone and asked for a number. In a short
time his party answered.
"Hello, Chief. This is Fenton Hardy speaking.... Yes.... I've just had
information that the big car and the truck went out the Shore Road
way a few minutes before ten o'clock.... Yes.... You've made
inquiries?... I see.... That's strange, isn't it?... Yes, my information is
quite reliable.... All right.... Let me know if you hear anything.... Don't
mention it.... Thank you, Chief.... Good-bye."
He put down the telephone.
"I was talking to Chief Collig. He says the three towns at the other
end of the Shore Road were notified immediately after the thefts
were discovered and that they had officers watching the roads from
ten o'clock on."
"And they didn't see the cars?"
Fenton Hardy shook his head.
"Not the slightest trace of either of them."
Frank and Joe looked at one another blankly.
"Well, if that don't beat the Dutch!" Frank exclaimed.
"You're quite sure of the time?"
"Positive. I had just looked at my watch."
"Well," said Fenton Hardy, "since the cars haven't been seen in any
of the other towns and since there aren't any other roads, the Shore
Road must hold the solution. I think I'll do a little prospecting around
the farms out that way to-morrow."
"We've been doing a little prospecting ourselves," admitted Joe, "but
we haven't been very successful so far."
"Keep at it," their father said encouragingly. "And good luck to you
both!"

CHAPTER XV
The Suspect
It was late before the Hardy boys got to sleep that night.
The events of the evening, culminating in the discovery that the auto
thieves had been at work in Bayport while they were lying in wait for
them on the Shore Road, gave the lads plenty to talk about before
they were finally claimed by slumber.
In the morning, it required two calls to arouse them. They dressed
sleepily and had to hurry downstairs in order to be in time for
breakfast. This did not escape the notice of ever-watchful Aunt
Gertrude.
"When I was a girl," she said pointedly, "young people went to bed at
a reasonable hour and didn't go gallivanting all over the country half
the night. Every growing boy and girl needs eight or nine hours'
sleep. I'd be ashamed to come down to breakfast rubbing my eyes
and gaping."
"It isn't very often they get up late," said Mrs. Hardy. "We can
overlook it once in a while, I suppose."
"Overlook it!" snorted Aunt Gertrude. "Mark my words, Laura, those
boys will come to no good end if you encourage them in coming in at
all hours of the night. Goodness knows what mischief they were up
to." She glared severely at them.
Frank and Joe realized that their aunt was curious as to where they
had been the past two evenings and was using this roundabout
method of tempting them into an explanation. However, as Joe
expressed it later, they "refused to bite."
Instead, they hastily consumed their breakfast, drawing from the
good lady a lecture on the dreadful consequences of eating in a
hurry, illustrated by an anecdote concerning a little boy named
Hector, who met a lamentable and untimely death by choking himself
on a piece of steak and passed away surrounded by weeping
relatives.
The boys, however, were evidently not impressed by the fate of the
unfortunate Hector, for they gulped down their meal, snatched up
their books, and rushed off to school without waiting for Aunt
Gertrude's account of the funeral. They were crossing the school
yard when the bell rang and they reached the classroom just in time.
"I feel like a stewed owl," was Joe's comment.
"Never ate stewed owl," returned his brother promptly. "How does it
taste?"
"I said I felt, I didn't say I ate," retorted Joe. "Gee, but your eyes do
look bunged up."
"What about your own?"
"Oh, if only I had had just one more hour's sleep!"
"I could go two or three."
"Aunt Gertrude was onto us."
"Yes, but she didn't get anywhere with it."
"Hope I don't fall asleep over my desk."
"Same here."
The morning dragged. They were very sleepy. Once or twice, Joe
yawned openly and Miss Petty, who taught history, accused him of
lack of interest in the proceedings.
"You may keep yourself awake by telling us what you know of the
Roman system of government under Julius Cæsar," she said.
Joe got to his feet. He floundered through a more or less acceptable
account of Roman government. It was dreary stuff, and Frank,
listening to the droning voice, became drowsier and drowsier. His
head nodded, and finally he went to sleep altogether and had a vivid
dream in which he chased Julius Cæsar, attired in a toga and with a
laurel wreath on his head, along the Shore Road in a steam-roller.
Miss Petty left the Romans and began comparing ancient and
modern systems of government, which led her into a discourse on
the life of Abraham Lincoln. She was just reaching Lincoln's death
when there was a loud snore.
Miss Petty looked up.
"Who made that noise?"
Another snore.
Joe dug his brother in the ribs with a ruler and Frank looked up, with
an expression of surprise on his face.
"Frank Hardy, are you paying attention?"
"Yes, ma'am," replied Frank, now wide awake. In his dream he
imagined Julius Cæsar had turned on him and had poked him in the
ribs with a spear.
"Do you know who we were talking about?"
"Oh, yes, ma'am."
"Do you know anything about his death?"
"Yes, ma'am," said Frank, under the impression that the lesson still
dealt with Cæsar.
"How did he die?"
"He was stabbed."
"He was stabbed, was he? Where?"
"In—in the Forum. He was murdered by some of the senators, led by
Cassius and Brutus, and Marc Antony made a speech."
The class could contain itself no longer. Snickers burst out, and
these welled into a wave of laughter in which even Miss Petty was
forced to join. Frank looked around in vast surprise.
"This," said the teacher, "is an interesting fact about Lincoln. I don't
remember having heard of it before. So he was stabbed to death by
the senators and Marc Antony made a speech?"
"I—I was talking about Cæsar, Miss Petty."
"And I was talking about Abraham Lincoln. Will you be good enough
to stay awake for the remainder of the lesson, Hardy?"
Frank looked sheepishly at his book, while Chet Morton doubled up
in his seat and gave vent to a series of explosive chuckles that soon
brought the teacher's attention to him and he was required to recite
the Gettysburg Address, stalling completely before he had gone a
dozen words. By the time the teacher had finished her comments on
his poor memory, Chet had other things to occupy his mind.
Frank and Joe Hardy were wide awake for the rest of the morning.
After lunch, they were on their way back to school, resolving to cut
out the late hours, so as not to risk a repetition of the ridicule they
had suffered that morning, when Frank suddenly caught sight of a
familiar figure not far ahead.
"Why, there's Gus Montrose again," he said. "Wonder what he's
doing in town?"
"Let's trail him," Joe suggested.
"Good idea. We'll find out what he does with his time."
The former hired man of the Dodds was shambling down the street
at a lazy gait, apparently wrapped up in his own concerns. Frank and
Joe followed, at a respectful distance. When Montrose reached a
busy corner he turned down a side street and here his demeanor
changed. His shoulders were straighter and his step more
purposeful.
Taking the opposite side of the street, the boys strolled along,
keeping well behind Montrose but not letting him out of sight. They
followed him for about two blocks and then, leaning against a
telegraph pole at the next corner, they saw Montrose's companion of
the previous night. He looked up as Montrose approached, and then
the pair met and joined in earnest conversation.
There was something peculiarly furtive about the two men. Not
wishing to be observed, the Hardy boys stepped into a soft drink
place near by and bought some ginger ale, which they drank in the
store, keeping an eye on the pair across the street, through the
window.
Finally, Montrose's companion moved slowly away, and Montrose
himself shambled across the road. He was lost to sight for a
moment.
"We'll trail him a little while longer," said Frank. "We have about a
quarter of an hour before school opens."
They paid for the ginger ale and stepped out of the shop. To their
astonishment, Gus Montrose was coming directly toward them. They
had lost sight of him in the window and had assumed that he had
gone on down the street. Instead he had turned back.
They affected not to notice him, and were starting back up the street
when Montrose overtook them and brushed against Frank rudely.
"Look here," he said, in a gruff voice. "What's the idea of followin'
me, hey?"
"Following you!" said Frank, in tones of simulated surprise.
"Yes—followin' me. I saw you. What do you mean by it?"
"Can't we walk down the same street?" inquired Joe.
"You didn't walk down here by accident. You followed me here."
"You must have something on your conscience if you think that,"
Frank told him. "This is a free country. We can walk where we like."
"Is that so? Well, I'm not goin' to put up with havin' a pair of young
whippersnappers trailin' me around town," snarled Gus Montrose.
"Hear that?"
"We hear you."
"Well, remember it, then. You just mind your own business after this,
see?"
"If you think we were following you, that's your own affair," returned
Frank. "We're on our way to school, if you'd like to know."
"Well, see that you go there. You're better off in school than
monkeyin' in my affairs, let me tell you. And a sight safer, too."
The man's tone was truculent.
"Oh, I think you're pretty harmless," laughed Joe.
"You'll find out how harmless I am if I catch you followin' me around
again. Just mind your own business after this and keep goin' in the
opposite direction when you see me comin'."
The man's insulting tone annoyed Frank.
"Look here," he said, sharply, facing Montrose. "If you don't start off
in the opposite direction right now, I'll call a policeman. Now, get out
of here."
Somewhat taken aback, Gus Montrose halted.
"You were followin' me—" he growled.
"You heard what I said. Clear out of here and stop annoying us."
If Montrose had hoped to frighten the lads, he was disappointed.
Like most cowardly men, he backed down readily when confronted
with opposition. Grumbling to himself, he turned away and crossed
the street.
The Hardy boys went on toward school.
"That'll give him something to think about," remarked Frank.
"You hit the right note when you said he must have something on his
conscience or he wouldn't have thought we were following him."
"I'm sure he has. A man with a clear conscience would never
suspect he was being trailed. There's something mighty fishy about
Gus Montrose and his queer-looking friend."
"Too bad he saw us. He'll be on his guard against us now."
"That doesn't matter. We can keep an eye on him just the same. I'd
give a farm to know what the pair of them were talking about."
"And I'd give a five-dollar bill just to know if he put that fishing pole in
the car up at the Dodds' and got Jack into trouble."
"So would I."
The boys were greatly puzzled. They were convinced that Gus
Montrose was up to no good and this conviction had only been
strengthened by their encounter. They reasoned that a law-abiding
man would scarcely have shown such resentment as Montrose had
evidenced.
"Well, whether he's one of the thieving party or not, we'll take
another whirl at the Shore Road to-night," said Frank, as the two
brothers entered the school yard.
Joe glanced at the sky. Massed clouds were gathering and the air
was close.
"Looks as if we'll have to call it off. There's going to be a storm."
"Storm or no storm, I have a hunch that we'll get some action before
the day is out."
Both Frank and Joe were right.
There was a storm, and before midnight they had more action than
they had ever bargained for.

CHAPTER XVI
Kidnaped
Rain threatened throughout the afternoon, but although the sky
darkened and there was an ominous calm, the storm held off. After
supper the Hardy boys went outside and looked at the clouds.
"It's sure going to be a jim-dandy," declared Joe. "Do you think we
really should go out to-night?"
"A little thing like a storm won't hold the car thieves back. They'll
operate in any weather."
"Won't they think it queer to see a car parked out in the rain?"
"They'll probably think it was stalled and that the owner went to get
help."
"That's right, too," Joe agreed. "I guess we can chance it."
"We'll put the top up to protect ourselves. And, anyway, it's dry in the
locker."
"The rain will be the least of our worries in there," said Joe, with a
grin. "Let's be going."
They went out to the garage and put up the top of the roadster, then
got in. As they drove down High Street there was a low rumble of
thunder and a splash of rain against the windshield.
"Storm's coming, right enough," Frank said. "Still, I have a hunch."
Ever since the previous night he had been possessed by a feeling
that their next venture would be crowned with success. He could not
explain it, but the feeling was there nevertheless.
They spied Con Riley, in oilskins against the approaching downpour,
patrolling his beat, and drew up at the curb.
"New car, eh?" said Riley, surveying the roadster grimly. "I'll be
runnin' you in for speeding some of these days, I'll be bound."
"Not in this boat," Frank assured him. "If we ever hit higher than thirty
the engine would fly out."
"Thirty!" scoffed the constable. "That looks like a real racin' car. You
mean ninety."
"We'll take you for a drive some time when you're off duty. We just
stopped to ask if there was anything new about the auto thieves."
Riley looked very grave, as he always did when any one asked him
questions pertaining to police matters.
"Well," he said, "there is and there isn't."
"That means there isn't."
"We ain't found 'em yet. But that don't mean they won't be found,"
said the officer darkly. "We're followin' up clues."
"What kind of clues?"
"Oh, just clues," said the officer vaguely. "We'll have 'em behind the
bars before long. But you'd better keep an eye on that car of yours.
It's just the kind somebody would steal."
"Trust us. There's been no trace of the other cars, then?"
Riley shook his head.
"Not a sign. But them thieves will go too far some of these fine days,
and then we'll catch 'em."
"Well, we hope you're the man who lands them," said Frank
cheerfully, as he edged the car out from the curb again. "So long."
The boys drove away, and Con Riley patiently resumed his beat.
"The game is still open," remarked Joe. "If the police had learned
anything new, Riley would have heard about it."
"Whenever he says they're following up clues, you can be certain
that they're up against it. The thieves are just as much at large as
they ever were."
It was beginning to rain heavily before they reached the outskirts of
Bayport and by the time they were well out on the Shore Road the
storm was upon them. Thunder rolled and rumbled in the blackening
sky and jagged streaks of lightning flickered through the clouds. Rain
streamed down in the glare of the headlights.
As the downpour grew in violence, the road became more
treacherous. Without chains, the rear wheels of the car skidded and
slithered on the greasy surface.
One of the numerous defects of the roadster's mechanism was a
loose steering wheel. Under ordinary circumstances it gave little
trouble, but on this treacherous road, Frank experienced difficulty in
keeping the car on its course.
Just outside Bayport was a steep hill, dipping to the bluffs that
overhung the bay. Under the influence of the rain, the sloping road
had become wet and sticky, and as the roadster began the descent
Frank knew he was in for trouble.
The car skidded wildly, and the faulty brakes did not readily respond.
Once, the nose of the roadster appeared to be heading directly
toward the steep bluff, where only a narrow ledge separated the
boys from a terrible plunge onto the rocks of the beach below. Joe
gave a gasp of apprehension, but Frank bore down on the wheel and
managed to swing the car back onto the road again in the nick of
time.
But the danger was not yet over.
The car was tobogganing down the slope as though entirely out of
control. The rear wheels skidded crazily and several times the car
was almost directly across the road, sliding sideways, and when it
did regain the ruts it shot ahead with breath-taking speed.
Almost any second the boys expected the roadster would leave the
slippery clay and either shoot across the ledge into space or crash
into the rocky wall at the left.
Somehow, luck was with them. Luck and Frank's quick work at the
unreliable wheel saved them from disaster.
The car gained the level ground, settled into the ruts, and went
speeding on at a more reasonable rate. The lads now breathed more
easily.
"Looked like our finish, that time," observed Joe.
"I'll say it did! I wouldn't have given a nickel for our chances when we
were about half way down the hill."
"Well, a miss is as good as a mile. We're still alive."
"And the old boat is still rolling along. When we get back I'm going to
have that steering wheel fixed. It very nearly cost us our lives."
On through the storm the Hardy boys drove, until at last they
reached the place where they had parked on the previous night.
There was no one in sight as they drove out onto the grass, and
Frank turned off the engine and switched out the lights. Quickly, they
scrambled out, raised the lid of the locker, and got inside.
The locker was warm and dry. The boys were comfortable enough,
aside from being somewhat cramped, and they could hear the rain
roaring down on the top of the roadster as the storm grew in
violence.
Warned by their former experience, the boys had made themselves
more comfortable than they had previously been. On the floor of the
locker they had spread a soft rug and they had also supplied
themselves with two small but comfortable pillows.
"I am not going to wear out my knees and elbows," Frank had said.
"The last time we were out my left elbow was black and blue."
"We'll fix it up as comfortable as a bed," Joe had answered.
In addition to the rug and pillows the boys had brought along a small
box of fancy crackers and also a bottle of cold water, for hiding in the
locker for hours had made them both hungry and thirsty.
"I could eat a few crackers right now," remarked Joe, shortly after
they had settled down to their vigil.
"Same here," answered his brother. "Pass the box over."
Each lad had several crackers and followed them with a swallow of
water. As they munched the crackers the thunder rolled and rolled in
the distance and they could see an occasional flash of lightning
through a crack of the locker door.
"It sure is a dirty night," Frank whispered, as they crouched in the
darkness of their voluntary prison.
"Even for auto thieves."
Thunder rolled and grumbled and the rain poured down in drenching
torrents. They could hear the beating of the surf on the distant shore
of Barmet Bay, far below.
Minutes passed, with only the monotonous roar of the storm.
"What's the time?" asked Joe finally.
Frank switched on the flashlight and glanced at his watch.
"Half-past nine."
"Time enough yet."
They settled down to wait. Scarcely five minutes had passed before
they heard a new sound above the clamor of the rain and wind.
Some one stepped up on the running board of the roadster, flung
open the door, and sat down behind the wheel. The boys had not
heard the intruder's approach, owing to the noise of the storm, and
they sat up, startled.
The newcomer lost no time.
In a moment, the engine roared, and then the car started forward
with a jerk.
It lurched across the grassy ground, then climbed up onto the Shore
Road. Back in the locker, the lads were bounced and jolted against
one another. They did not mind this, for there was wild joy in their
hearts. At last their patient vigil had been rewarded.
"Kidnaped!" whispered Frank exultantly.
Once on the road, the car set off at rapidly increasing speed through
the storm. The man at the wheel was evidently an expert driver, for
he got every ounce of power the engine was capable of, and held
the roadster to the highway. The roar of the motor could be heard
high above the drumming of the rain.
In the darkness of the locker, the boys sat tight, not knowing where
the car was going, not knowing how long this wild journey might last.
They kept alert for any turns from the Shore Road, realizing that they
might have to find their way back by memory.
For above five minutes, the car held to the Shore Road, and then
suddenly swerved to the right.
Neither of the boys had any recollection of a side road in this part of
the country, and they were immediately surprised. However, by the
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.

Let us accompany you on the journey of exploring knowledge and


personal growth!

ebookfinal.com

You might also like