Quantum Programming - Embedded.com

Quantum Programming

Relatively few of the embedded systems books I receive are worthy of permanent space on my bookshelf . That's why I want to tell you about one recent standout.

I don't buy many books about embedded systems. Most of the relevant ones find their way to me as review copies. I try to read, or at least skim, those that look promising. Unfortunately, I've found that relatively few are worthy of permanent space on my bookshelf or recommendation to others. That's why I want to tell you about one recent standout.

Perhaps you recall the article “State-Oriented Programming,” which appeared precisely two years ago in this magazine. It described a simple manual technique for turning hierarchical state machines into compact C or C++ code. Though I was certainly impressed by the authors' approach, I didn't then recognize the brilliance of the underlying ideas. The primary author's recent publication of a book-length treatment of the topic has helped me see the light.

The book, by Miro Samek, is titled Practical Statecharts in C/C++ (CMP Books). The title of the book is a major understatement. What stands out in my mind is not the practical nature of the solution it provides to a common recurring problem, but the downright revolutionary ideas behind that solution.

The author's so-called “Quantum Programming” may ultimately change the way embedded software is designed. Never before has there been a viable alternative to the traditional main()+ISR vs. RTOS problem. Preemptive multitasking only works well in specific scenarios, while main()+ISR comes with its own set of problems when you try to scale it. A third way is needed.

This book offers a solution, based around state machines, that is compact (5KB is all that's typically required), realized in C and C++ (no fee or royalties), of theoretical value in any language, and can support multiple state machines running in parallel if necessary. It's also the first good way I've seen to deal with the inheritance of state behavior in hierarchical state machines.

Before quantum programming, there were basically three approaches to state machine implementation: switch statements, tables of function pointers, and object-oriented programming constructs. The issue of substates in hierarchical state machines was complex in all of these approaches. Hierarchical state machines are common, with part of each state's behavior being determined by its parent state and the rest by the substate itself. This is difficult to implement in all of the traditional approaches, as it either requires duplication of code or additional function/method calls. At best, the results tend toward spaghetti code.

In a nutshell, quantum programming is a design pattern for direct and efficient implementation of (hierarchical) state machines. It uses the popular and proven UML statecharts as its graphical specification language, and leaves the choice of implementation programming language up to the developer. Hierarchical states are implemented via an externally driven “event processor,” the use of which ensures that substates need not duplicate the functionality of their parents (and grandparents). Dr. Samek calls his implementation the “Quantum Framework.”

I believe that these innovative techniques will quickly become popular, and have already put them to good use. If you read only one book about embedded systems this year, make it Practical Statecharts in C/C++.

Return to the August 2002 Table of Contents

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.