Practical Statecharts in C/C++

Miro Samek

Mentioned 5

'Downright revolutionary... the title is a major understatement... 'Quantum Programming' may ultimately change the way embedded software is designed.' -- Michael Barr, Editor-in-Chief, Embedded Systems Programming magazine (Click here

More on Amazon.com

Mentioned in questions and answers.

We need to implement a simple state machine in C.
Is a standard switch statement the best way to go?
We have a current state (state) and a trigger for the transition.


switch(state)
{
  case STATE_1:
     state = DoState1(transition);
     break;
  case STATE_2:
     state = DoState2(transition);
     break;
}
...
DoState2(int transition)
{
   // Do State Work
   ...
   if(transition == FROM_STATE_2) {
     // New state when doing STATE 2 -> STATE 2
   }
   if(transition == FROM_STATE_1) {
    // New State when moving STATE 1 -> STATE 2
   }
   return new_state;
}

Is there a better way for simple state machines

EDIT: For C++, I think the Boost Statechart library might be the way to go. However, it does not help with C. Lets concentrate on the C use case.

This article is a good one for the state pattern (though it is C++, not specifically C).

If you can put your hands on the book "Head First Design Patterns", the explanation and example are very clear.

There is a book titled Practical Statecharts in C/C++. However, it is way too heavyweight for what we need.

I loved the course I took in Automata Theory and Formal Languages, so naturally I started looking around the interwebs to learn what happened since the time the books on which the course was based were written.

What I discovered was that the list of stuff I wasn't familiar with seemed to be very short. For example, from the list of automatons in the Wikipedia entry for the subject, half were covered by the course, and the other half were mostly related to the one language not covered by the course.

Further, when researching about the applications of the theory, I got mostly the same results: programming language syntax, compilers, text search, and.... that's about it.

So is it really that dead? Or does it continues to evolve? Are there new applications for the theory?

Rather than looking at the theory as dead, instead consider that it has become so practical for applications that we've moved beyond the theory. An excellent book to consider that bridges between the theory and application is Miro Samek's "Practical Statecharts in C/C++". There is now a second edition available, which I've not read. But I found nothing lacking in the first edition; to this day I find it one of the most valuable texts I have ever read.

I am trying to build a board game ... And looks like it has to be implemented using a state machine..

I know of the State pattern from GoF, but I am sure there must be other ways to implement state machine. Please let me know.. if you know of any articles or books that contains details about different implementation (trade off of each of them), please direct me.. thanks

We've used Harel's statecharts (similar/equivalent to state machines but somewhat easier to think about), there's a good book called Practical Statecharts in C/C++.

Does anyone know of any kind of UML variation which can map events in a GUI system?

I have a quite complex event driven GUI system and I would like to create a diagram so new developers can easily get an overview of events fired by various views and what models/controllers or view are instantiated/effected by these events.

I was looking for something that would look more like a mind map which branches all over the place rather than linear scenarios.

Does anyone have any ideas?

Regards,

Chris

State diagrams.

Old and pricey, but here is a great text on modeling user interfaces with state charts. I've worn the pages out on mine....

the book that finally helped me really understand UML statecharts is Samek's Practical Statecharts in C/C++ which has a few chapters on them as well as an impressive implementation framework for building state-based systems. I think it's worth getting just for the explanations of how to think with statecharts.

For actually creating quick statecharts or similar rough diagrams, I do a lot of work with the textual DOT language in GraphViz and often then include those diagrams directly into documentation generated with Doxygen.

My company is transitioning from assembly coded microcontrollers to C. We are considering Microchip, Atmel, Renasas, et. al. for future projects using C code. Are there good training resources to bring our engineers up to speed with C? Seminars, instructors, classes, etc. The experience level of the engineers varies from no training to some knowledge.

Welcome to the 80's. :) Start with the either classic K&R C book or my favorite C: a Reference Manual by Harbinson & Steel if you can find one. I work with PICs and there are several useful books about PIC programming. I was already familiar with C from using PCs I didn't find them useful in that regard.

C allows you to be close to assembly and most compilers to include it inline, so it's as close to assembler as any high level language. You should happy and at home pretty quickly, I think.

If you are interested in jumping forward a few decades ;>) you might want to consider a Microchip C complier. I'm going to be looking into this soon and there is a good book on Practical Statecharts in C/C++.

The good news is that if your engineers have a background in assembly, some of the 'C' langugage concepts, such as pointers and arrays, will not be difficult to grasp (and hopefully master.)

Regarding your specific question about embedded C training, Netrino has a bunch of courses on embedded training, including writing embedded code in C and C++.

Also, the Embedded Systems Conferences (just had one in Boston a couple weeks ago, next one is in Silicon Valley next spring I think) are always full of courses on embedded development & different languages. But you probably don't have the luxury of waiting 6 months.

If your staff are more of the "book learner" types, these books might be of interest:

That should give you enough to chew on for a little while.

For 16-bit Microchip pics, there is

http://www.amazon.com/Programming-16-Bit-PIC-Microcontrollers-Technology/dp/0750682922

IIRC it was decent, but they focus pretty heavily on doing one thing per chip. Our apps multiplex a lot of I/O, and for that the examples/casestudies in the book (that assumed exclusive use) were hard to bring to practice.

When I started with this architecture, having a readily made example board (from Microchip, $50-$100 range) really helped, specially if you target roughly the same variant as the example board.

This because it offered an easy check if a problem was hardware or software related by running it on the example board.