CMP EMBEDDED.COM

Login | Register     Welcome Guest  
HOME DESIGN PRODUCTS COLUMNS E-LEARNING CONFERENCES CODE FORUMS/BLOGS NEWSLETTERS CONTACT FEATURES RSS RSS

A crash course in UML state machines: Part 3
Designing a UML state machine



Embedded.com

Executing the example code
The actual C and C++ implementations of the calculator hierarchical state machine from Figure 2.18 are available from the Website accompanying the book on which this series is based -"Practical UML Statecharts in C/C++" - at www.state-machine.com/psicc2/. The calculator example in C is found in the directory <qp>\qpc\examples\80x86\dos\tcpp101\l\calc\ and the C++ version is found in the directory <qp>\qpcpp\examples\80x86\dos\tcpp101\l\calc\. The source code consists of the following files:

calc.h contains the declaration of signals, events, and the Calc state machine structure.

calc.c (calc.cpp in the C++ version) contains the implementation of the Calc state machine.

bsp.h contains the board support package interface.

bsp.c (bsp.cpp in the C++ version) contains the implementation of the board-specific functions.

main.c (main.cpp in the C++ version) contains the main() function and the event loop.

CALC.PRJ is the Turbo C++ project file for building the application.

As always, the code I provide is executable and I encourage you to try it out. You can run the example on any Windows PC by either double-clicking on the executable located in the directory <qp>\qpc\examples\80x86\dos\tcpp101\l\calc\dbg\CALC.EXE.

The calculator example is interactive and you can perform computations with it. You use the keyboard to send keypress events to the application; the state of the calculator display is shown at the command prompt. The calculator recognizes keys: 0, 1, . . ., 9, ., +, –, *, /, =, C, and E (cancel entry, CE). The Esc key terminates the application. All other keys are ignored.

Figure 2.19: The calculator HSM running in a Windows console.

Figure 2.19 shows a screen shot in which you can see how the calculator handles the expression 2, –, –, –, 2, = that has crashed the Visual Basic calculator discussed in part 1 of this article series. I'd like to challenge you to crash the state machine-based calculator. The calculator starts with displaying zero aligned at the right edge of the display [ 0]. To the right of the display, you can see the key sent to the calculator. For example, the first key is 2. The key event is followed by the sequence of actions that the calculator HSM performs in response to the key event. I recommend that you correlate this output with the calculator state diagram from Figure 2.18.

1 | 2 | 3 | 4 | 5

Rate this article: Low High
Current rating
  • .
Embedded.com Career Center
Looking for a new job?
SEARCH JOBS

Browse all jobs

SPONSOR
RECENT JOB POSTINGS



WEBINAR
WEBINAR
TECH PAPER
WEBINAR




 :