[ragel-users] Best practice: using ragel for bit-based state machines on microcontroller?

Jan Sporbeck ich+lists at slashhome.de
Sat May 16 12:40:21 UTC 2009

Adrian, thanks for pointing me to that and providing an example.
I'll look into that and use it to at last verify the code I have been
making up manually. (I ended up with 40 states superpositioning four
state machines..)

Just to make clear what I meant by packing the bits into a bigger
structure. (Yes, I know it doesn't provide the most compact code)

union {
  struct {
    unsigned char  s1:1;
    unsigned char  s2:1;
    unsigned char  s3:1;
    unsigned char  s4:1;
  } bits;
  unsigned char byte;
} bit_byte;

bit_byte.bits.s1 = 1;

PORTD = bit_byte.byte;


Am Donnerstag, den 14.05.2009, 23:10 -0400 schrieb Adrian Thurston:
> Ah, you can process at the bit level. See attached code. You'll probably 
> want to scrutinize the generated and compiled code to see if it's good 
> enough for your needs. One thing to note about using the getchar 
> directive is that the expression can be evaluated more than once per 
> value of p and if the compiler doesn't optimize that duplicate work away 
> then your code won't be optimal.
> By the way, I'm not sure if I just figured this out now, or if somebody 
> told me about this technique a while back and I forgot. (Bob maybe?)
> -Adrian
> Jan Sporbeck wrote:
> > Hello there!
> > 
> > I'd like to use ragel to design the state machine for a microcontroller
> > project (avr atmega8535) written in C.
> > 
> > I think I understand how to use it for streams of char, int, etc. But
> > what's the best practice to use for bit based decisions?
> > 
> > I could use union..struct to pack all bits in one datatype and process
> > that. But I doubt that'll yield a performant implementation.
> > 
> > I read messages on this list that ragel is used for uC projects, but no
> > details how.
> > 
> > So any hints or example code to get me started is appreciated.
> > 
> > Thanks in advance,
> > /Jan.

More information about the ragel-users mailing list