Readers' feedback

This book and the free QP download are your entry into the 21st century of embedded systems programming. Because the code is provided in C, porting is rather easy and running on embedded systems and desktop systems is practical and easy. I was able to build and run the code as provided without modifications on my Linux, Mac and Windows systems and on the H8S and a Microchip PIC 24 with only a minimalist porting effort of about a half a day in each case."

Rob Wehrli (Knoxville, TN), review

...the author brings such clarity and insight to the value and application of UML Statecharts, that this book is an essential read for anyone who is even thinking of using finite state or hierarchical state machines."

Jonathan Kaye (Philadelphia, PA USA), review

The QP framework is simplified to the point where the developer only focuses on the actual application and need not worry about the underlying kernel, queuing mechanisms, tasks, etc. However, if the developer decides to understand what is executing 'under the hood' per se, the book goes through in detail all the intricacies of the infrastructure."

Thanh Tieu (Los Angeles, CA USA), review

This book took me from being a C programming novice, to writing 1000s of lines of embedded control systems code, that has been running reliably for several years, with just one bug - my own!"

Dr. Haitham Hindi, (Palo Alto, CA), review

For guys like me, who have spent years searching for a way to generate powefull real-time, scalable, elegant, cost effctive (especially in memory size) and maintenable source code, this book is a real PEARL."

Ricardo D. Raupp, review

Framework-based software tracing generates the true, precisely timestamped records of what's happening in the system, even without adding any instrumentation to the application. This is possible because the framework controls the application."

Robert Berger, (Athens, Attika Greece), review

The strong point of the book are many figures which clearly illustrate the author's ideas... The C/C++ code itself is clean, nicely structured and intertwined with the explanations... Because of these features I use the book as an educational aid for the graduates who join my team and need some introduction to real commercial programming."

Cezary Zieniewicz (Wroclaw, Poland), review

more reviews

Choose the best online deal...

Buy from Elsevier

Practical UML Statecharts in C/C++, Second Edition

Practical UML Statecharts in C/C++, Second Edition

Practical UML Statecharts in C/C++, Second Edition: Event-Driven Programming for Embedded Systems

The most popular book about UML statecharts and event-driven programming for embedded systems. This ultimate resource describes all the related concepts and provides a very detailed design study of the QP™ frameworks.

Author: Miro M. Samek
ISBN-10: 0750687061
ISBN-13: 978-0750687065
Category: Embedded Systems / Programming / Software
Publisher: Newnes (imprint of Elsevier)
Published: October 1, 2008
Pages: 728 pages (paperback)
Practical UML Statecharts in C/C++, Second Edition
Chinese Translation
Practical UML Statecharts in C/C++, Chinese Translation

Related Resources and Tools

The following table provides the links for downloading all the tools used in the book as well as other resources.

Updates and Errata Updates and Errata for the book provides corss-referenced error corrections and changes made to the QP frameworks since the publication of the book. The PDF document is searchable by page number, section number, and update type. Last updated for QP 4.3.00 on 09-Jan-2012.

download Updates and Errata (PDF 0.8MB) 
QM tool QP™ is is a family of lightweight, open source, state machine frameworks described in the book, which are like modern real-time operating systems for executing event-driven hierarchical state machines (UML statecharts). The downloads below contain the QP framework source code and all examples described in the book.
download the latest version of QP/C™ from 
download the latest version of QP/C++™ from 
download the latest version of QP-nano™ from 

QM tool QM™ (QP™ Modeler) is a free graphical modeling UML tool for designing and implementing real-time embedded applications based on the QP™ state machine frameworks. QM™ provides best-in-class, intuitive diagramming environment and generates very compact C or C++ code.

download the latest version of QM™ from 
QTools QTools™ Collection contains various open source tools for working with the QP state machine frameworks, such as: (1) QSPY host application, (2) QCLEAN for cleanup of source code files, (3) QFSGEN for generating ROM-based file systems for embedded web servers, and (4) QUDP for testing UDP connectivity. The Qtools for Windows provides also the latest GNU make for Windows and realated file utilities (rm, touch, etc.)

download the latest version of QTools™ from 
QP Development Kits QP Development Kits contain QP ports to various CPUs, operating systems, and compilers. Please keep checking this webpage, as new ports are added frequently.
check available QDKs
QP state machine frameworks for ARM-Cortex The book describes the QP port to ARM Cortex-M with the cooperative kernel in Chapter 8, but the port to the preemptive QK kernel is not explained. The following Quantum Leaps Application Notes describe in detail all aspects of using QP with ARM Cortex-M processors.
download Application Note "QP and ARM Cortex-M with GNU" download Application Note "QP and ARM Cortex-M with IAR" download Application Note "QP and ARM Cortex-M with ARM/Keil"
EK-TM4C123GXL kit To demonstrate modern embedded systems programming with QP, the accompanying code includes examples for the very inexpensive ARM Corterx-M4F-based EK-TM4C123GXL evaluation kit (also known as Tiva-C Series Launchpad) form Texas Instruments. The ARM Cortex-M examples use the exact same source code as the Windows (and DOS) counterparts, and differ only in the Board Support Package (BSP).

NOTE NOTE: The EK-TM4C123GXL board is the replacement for the EK-LM3S811 board, which is nearing its end-of-production.

The complete EK-TM4C123GXL development kit with cables and development tools on CD-ROM is available for $12.99 from many online distributors, such as ARROW or MOUSER. Once on the distributor's website, you need to search for "EK-TM4C123GXL".
EK-TM4C123GXL Board User's Manual (0.5MB PDF)
EK-TM4C123GXL kit web-page
EKI-LM3S811 kit NOTE NOTE: The book describes the EK-LM3S811 development kit, which now is nearing its end-of-production. The recommended replacement is the ARM Cortex-M4F-based Stellaris LaunchPad described above.
EKI-LM3S811 Board User's Manual (0.6MB PDF)
EKI-LM3S811 kit web-page
Download free IAR EWARM KickStart The ARM Cortex examples for IAR require the the IAR EWARM toolset. The 32KB-limited KickStart edition of IAR toolset for ARM is included in the EKI-LM3S811 Stellaris kit and is also available for a free download from IAR.

NOTE: The IAR EWARM 5.11 described in the book has now been superseded by the new version 6.60. download IAR EWARM
App Note: "Installing IAR EWARM KickStart for developing QP Applications with the EK-LM3S811 Board" (0.7MB PDF)

Sourcery G++ The ARM Cortex examples for GNU require the GNU-based Sourcery CodeBench toolset. To dowload the code to the target board and debug it, you need the Sourcery CodeBench Professional Edition, which includes the Sourcery Debug Sprite for ARM. The 30-day evaluation version of Sourcery CodeBench for ARM is included in the EKC-LM3S811 Stellaris kit.
download Sourcery CodeBench
Download free Open Watcom Much of the code accompanying the book is highly portable C or C++ independent really on any particular CPU, operating system, or compiler. However, to focus the discussion, many of the examples provided in the code are available for x86 PC running DOS or DOS emulation. The legacy 16-bit DOS has been chosen as a demonstration platform because it allows programming a standard x86-based PC at the bare-metal level. Without leaving your desktop, you can work with interrupts, directly manipulate CPU registers, and directly access the I/O space. No other modern 32-bit development environment for the standard PC allows this much so easily. The additional advantage of the legacy DOS platform is the availability of mature and free tools and DOS emulators. To that end, the examples are compiled with the Open Watcom toolchain, which is available for a free download. The DOS examples can also execute in the DOSBox x86 emulator on platforms that don't support DOS.

NOTE NOTE: The Open Watcom toolchain now replaces the Turbo C++ 1.01 toolchain referenced in the book, because the Trubo C++ 1.01 compiler is no longer available for a free download. In contrast, Open Watcom is licensed under an OSI-certified open source license, which allows free commercial and non-commercial use.

NOTE: The Open Watcom for DOS installer is itself a DOS application and will not run on systems that cannot execute 16-bit DOS applications. For those systems you can execute the installer in the DOSbox described below.
download open-watcom-c-dos-1.9.exe (80MB)
DOSBox x86 emulator DOSBox emulates a full x86 PC with DOS, graphics, and sound on platforms which don't have DOS, such as 64-bit Windows, Linux/FreeBSD, and Mac OS X. Starting from release 4.1.04, all QP examples for DOS are compatible with DOSBox, which means that all DOS examples not only run in DOSBox, but you can use the provided make scripts to build the examples in DOSBox (requires the Open Watcom compiler for DOS).

NOTE: DOSBox uses a keyboard mapping in which the backslash '\' is not always available. In this case, you typically can use the forward slash '/' as the directory separator. Also, to use DOSBox for executing and building QP examples, it is recommended to place the following commands in the [autoexec] section of the DOSBox configuration file (on Windows 7, DOSBox configuration file is located in C:\Users\<user>\AppData\Local\DOSBox\dosbox-0.73.conf, on Windows XP, it is located in C:\Documents and Settings\<user>\Local Settings\Application Data\DOSBox\dosbox-0.73.conf):

mount c c:\
mount d d:\
path c:\tools\WATCOM\BINW
download DOSBox for your platform
Linux Cygwin Some examples in this book run on Linux, as well as any other POSIX-compliant operating system, such as BSD, QNX, Max OS X, or Solaris. You can also build the Linux examples on Windows under Cygwin.
download QP port to Linux
eZ430-F2013 kit To demonstrate really small, ultra-low power embedded system, Chapter 12 of the book describes QP-nano examples for the inexpensive MSP430-based eZ430-F2013 USB Stick from Texas Instruments. The MSP430 examples use the free 4KB-limited KickStart edition of the IAR EW430 toolset.
visit eZ430 kit website at TI
Micium uC/OS-II The book uses the Micrium µC/OS-II as an example for porting QP to a conventional RTOS. µC/OS-II has been chosen, because it is superbly documented in the excellent book "MicroC-OS-II The Real Time Kernel" by Jean J. Labrosse.
visit >µC/OS-II website at Micrium
Visual Basic Calculator Example Chapter 2 of the book discusses the Visual Basic calculator sample application that shipped in millions of copies with Microsoft Visual Basic. The provided ZIP file contains the calculator source code, the executable, and the Visual Basic DLL required to run the example.
download the VB calculator example
Free support and discussion Search our free support discussion forum on for answers or post your questions to the QP community.

Visit free support discussion forum

About the examples

Compared to the first edition, this book presents more examples of event-driven systems and the examples are more complete. Significant effort has been made to come up with examples that are not utterly trivial yet don't obscure the general principles in too many details. The examples have been carefully chosen so that they don't require any specific domain knowledge.

DOS version ARM Cortex version "Fly 'n' Shoot" game example (Chapters 1, 12)
The "Fly 'n' Shoot" game demonstrates a complete, non-trivial, real-time project implemented with the event-driven paradigm and UML state machines. Several version of the game are provided for QP/C, QP/C++, and QP-nano (versions for non-preemptive "vanilla" kernel and the preemptive QK kernel are provided).
DOS version QHsmTst example (Chapter 2)
The semantics of Hierarchical State Machines (UML statecharts) is rich and can raise many questions. The exhaustive QHsmTst example can answer all such questions in an interactive and fun way. To use the example, you launch the qhsmtst.exe executable (included in the accompanying code) and inject events to the state machine by typing letters a..i on your keyboard (the Esc key terminates the application). You correlate the instrumented printouts from every event action, entry/exit actions, initial transitions, and regular transitions with the state diagram in Figure 2.11 (page 88).
DOS version Calculator example (Chapters 2, 4)
The Calculator example demonstrates how to design quite a complex behavior with a hierarchical state machine and how to implement it with QEP.
DOS version Time Bomb examples (Chapter 3)
The series of four Time Bomb examples demonstrate various standard state machine implementation techniques in C and C++. The example techniques include: "Nested-switch Statement", "State-Table", the object-oriented "State" design pattern (C++ version only), and the FSM implementation with QEP.
DOS version State pattern examples (Chapter 5)
The series of five examples demonstrate all five state design patterns discussed in Chaper 5, which are: "Ultimate Hook", "Reminder", "Deferred Event", "Orthogonal Component", and "Transition to History". Executable versions for each design pattern for QP/C, QP/C++, and QP-nano are provided.
DOS version ARM Cortex version Dining Philosopher Problem (DPP) example (Chapter 9)
The DPP example shows relatively simple application used for testing various QP ports. The DPP example requires minimal target resources, such as a few LEDs to show the status of the Dining Philosophers. Executable versions of this example application are available in every QP Development Kit (QDK).
DOS version MSP430 version PEdestrian LIght CONtrolled (PELICAN) crossing example (Chapter 12)
The PELICAN crossing example demonstrates a non-trivial, but very lightweight hierarchical state machine that is ideal for testing QP-nano applications on very small MCUs. In Chapter 12, the PELICAN crossing example runs on the eZ430-F2013 USB-stick evaluation platform with just 128 bytes of RAM and 2KB of ROM.

About the book

PSiCC2 and Development Kits from Luminary Micro and TI

Practical UML Statecharts in C/C++, Second Edition bridges the gap between high-level abstract concepts of the Unified Modeling Language (UML) and the actual programming aspects of modern hierarchical state machines (UML statecharts). The book describes a lightweight, open source, event-driven infrastructure, called QP that enables direct manual coding UML statecharts and concurrent event-driven applications in C or C++ without big tools.

The book is presented in two parts. In Part I, you get a practical description of the relevant state machine concepts starting from traditional finite state automata to modern UML state machines followed by state machine coding techniques and state-machine design patterns, all illustrated with executable examples. In Part II, you find a detailed design study of a generic real-time framework indispensable for combining concurrent, event-driven state machines into robust applications. Part II begins with a clear explanation of the key event-driven programming concepts such as inversion of control (Hollywood Principle), blocking versus non-blocking code, run-to-completion (RTC) execution semantics, the importance of event queues, dealing with time, and the role of state machines to maintain the context from one event to the next. This background is designed to help software developers in making the transition from the traditional sequential to the modern event-driven programming, which can be one of the trickiest paradigm shifts.

The lightweight QP event-driven infrastructure goes several steps beyond the traditional real-time operating system (RTOS). In the simplest configuration, QP runs on bare-metal microprocessor, microcontroller, or DSP completely replacing the RTOS. QP can also work with almost any OS/RTOS to take advantage of the existing device drivers, communication stacks, and other middleware. The accompanying website to this book contains complete open source code for QP, ports to popular processors and operating systems, including 80x86, ARM Cortex-M3, MSP430, and Linux, as well as all examples described in the book.

About the audience

Practical UML Statecharts in C/C++, Second Edition is intended for the following software developers interested in event-driven programming and modern state machines:

Due to the code-centric approach, this book will primarily appeal to software developers tasked with creating actual, working code, as opposed to just modeling. This book does not provide yet another CASE tool. Instead, this book is about practical, maintainable coding techniques for UML state machines, and about combining concurrent state machines into robust event-driven systems by means of a real-time framework.

About the author

Miro Samek

Dr. Miro M. Samek is founder and president of Quantum Leaps, an open source company providing lightweight, state machine-based, event-driven application frameworks for embedded systems. He is the author of Practical Statecharts in C/C++ (CMP Books, 2002), has written numerous articles for magazines, including a column for C/C++ Users Journal, is a regular speaker at the Embedded Systems Conferences, and serves on the editorial review board of the Embedded Systems Design magazine. For a number of years, he worked in various Silicon Valley companies as an embedded software architect and before that he worked as an embedded software engineer at GE Medical Systems (now GE Healthcare). Dr. Samek earned his Ph.D. in nuclear physics at GSI (Darmstadt, Germany).

Last updated: November 03, 2013