Searching \ for 'Help with C state machine' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/language/index.htm?key=c
Search entire site for: 'Help with C state machine'.

Truncated match.
PICList Thread
'Help with C state machine'
1996\05\04@020812 by Doug Manzer

picon face
Can anyone help me write some C code to implement a state machine?
I'm using the PCM compiler from CCS and want code that compiles to
a computed jump. I could write in-line assembler of course, but
need the code to be portable.

The following works ok on a PC, compiled with Borland C++ 3.1,
where I use the number of command-line arguments to derive a
state number:

---------------------------
#include <stdio.h>

void state0( void ) ; // state function prototypes
void state1( void ) ;
void state2( void ) ;

const void (*state_pt[])() = { state0 , state1 , state2 } ;
// array of pointers to void functions

void main( int argc )
{
   int state ;
   state = argc - 1 ;
   (*state_pt[state])() ;
}

void state0(void)
{
   printf( "This is state 0 \n" ) ;
}

void state1(void)
{
   printf( "This is state 1 \n" ) ;
}

void state2(void)
{
   printf( "This is state 2 \n" ) ;
}
---------------------------

Although this runs fine in Borland C, the compiler gives me a
warning "Call to function without prototype" for:

   (*state_pt[state])() ;

I don't know why I get this warning, as I already declared state_pt
(as a global).

However,the real problem occurs when I try to compile similar code
under PCM (adapted for the PIC environment -- no argc, etc). This
compiler doesn't like...

 const void (*state_pt[])() = { state0 , state1 , state2 } ;

... and gives error msg: "Line  9   Error # 34  Unknown type."

Does anyone know what's going wrong? Any help appreciated.

Regards, Doug Manzer

1996\05\04@021846 by Clyde Smith-Stubbs

flavicon
face
dmanzer@wimsey.com (Doug Manzer) wrote:

> Although this runs fine in Borland C, the compiler gives me a
> warning "Call to function without prototype" for:
>
>     (*state_pt[state])() ;
>
> I don't know why I get this warning, as I already declared state_pt
> (as a global).

Yes, but you didn't declare the arguments, so your declaration is
not a prototype.

> However,the real problem occurs when I try to compile similar code
> under PCM (adapted for the PIC environment -- no argc, etc). This
> compiler doesn't like...
>
>   const void (*state_pt[])() = { state0 , state1 , state2 } ;
>
> ... and gives error msg: "Line  9   Error # 34  Unknown type."

It's not a very helpful message, but your declaration is in error.
What you have declared is an array of pointers to functions returning
"const void". Now there is no such thing as "const void". I suspect you
meant to declare a const array of pointers, so your declaration should
read:

void (* const state_pt[])(void) = { state0 , state1 , state2 } ;

Note that the 'const' is on the right of the '*' and so qualifies the
array, not the function return type. Also note the (void) function parameter
spec, making this a prototype.


--
Clyde Smith-Stubbs       | HI-TECH Software,       | Voice: +61 7 3300 5011
spam_OUTclydeTakeThisOuTspamhitech.com.au      | P.O. Box 103, Alderley, | Fax:   +61 7 3300 5246
http://www.hitech.com.au | QLD, 4051, AUSTRALIA.   | BBS:   +61 7 3300 5235
----------------------------------------------------------------------------
For info on the World's best C cross compilers for embedded systems, point
your WWW browser at http://www.hitech.com.au, or email .....infoKILLspamspam@spam@hitech.com.au

1996\05\04@031348 by Doug Manzer

picon face
Further to my query, I got an email reply (seemed like just a few
minutes later!) saying that my declaration should have read:

  void (* const state_pt[])(void) = { state0 , state1 , state2 } ;


Thanks for the help -- however, when I tried...

  void (* const state_pt[])() = { state0 , state1 , state2 } ;

...I got error message # 81, "Expecting a basic type". (The
CCS compilers don't like "void" as function arguments).

Regards, D.M.

1996\05\04@064109 by Clyde Smith-Stubbs

flavicon
face
dmanzer@wimsey.com (Doug Manzer) wrote:

> Thanks for the help -- however, when I tried...
>
>    void (* const state_pt[])() = { state0 , state1 , state2 } ;
>
> ...I got error message # 81, "Expecting a basic type". (The
> CCS compilers don't like "void" as function arguments).

Well, then I guess you had better get onto the compiler vendor - that
line is a perfectly good declaration. The woes of non-ANSI compilers ;-(

Clyde

--
Clyde Smith-Stubbs       | HI-TECH Software,       | Voice: +61 7 3300 5011
clydespamKILLspamhitech.com.au      | P.O. Box 103, Alderley, | Fax:   +61 7 3300 5246
http://www.hitech.com.au | QLD, 4051, AUSTRALIA.   | BBS:   +61 7 3300 5235
----------------------------------------------------------------------------
For info on the World's best C cross compilers for embedded systems, point
your WWW browser at http://www.hitech.com.au, or email .....infoKILLspamspam.....hitech.com.au

1996\05\04@140654 by Ben L Wirz
flavicon
face
Doug,

       The PCM complier doesn't allow pointers to functions.  See the
third paragraph of the first page of the manual.  I'm not sure if the
ByteCraft version does or not, but this may be one reaons why PCM is $600
cheaper.

Ben,

Ben,

Ben Wirz                Check out My Home Page for Great Deals on Bulk Buy's
                       Nitinol Wire, LMD 18200 H Bridge and 10 Mhz PIC 16C84
EraseMEblw2spam_OUTspamTakeThisOuTcec.wustl.edu      http://cec.wustl.edu/~blw2/index.html

On Fri, 3 May 1996, Doug Manzer wrote:

{Quote hidden}

1996\05\04@164706 by Eric T. Brewer

flavicon
face
Instead of having an array of pointers to functions, you can have a dispatch
function or case table at the point where you want the state dispatch. Not
as sexy, but it's still C!

Something like the following will work:
#include <stdio.h>

void state0( void ) ; // state function prototypes
void state1( void ) ;
void state2( void ) ;

enum { state0, state1, state2 } state;

void stateDispatch(void)
{
       switch (state)
       {
               case state0:    state0 ();      break;
               case state1:    state1 ();      break;
               case state2:    state2 ();      break;
       }
}

void main( int argc )
{
   state = argc - 1 ;

       stateDispatch ();
       // OR
       switch (state)
       {
               case state0:    state0 ();      break;
               case state1:    state1 ();      break;
               case state2:    state2 ();      break;
       }
}

void state0(void)
{
   printf( "This is state 0 \n" ) ;
}

void state1(void)
{
   printf( "This is state 1 \n" ) ;
}

void state2(void)
{
   printf( "This is state 2 \n" ) ;
}


cheers,
eric


At 11:17 PM 5/3/96, Doug Manzer wrote:
{Quote hidden}

More... (looser matching)
- Last day of these posts
- In 1996 , 1997 only
- Today
- New search...