CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

pointers to functions
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Trampas



Joined: 04 Sep 2004
Posts: 89
Location: NC

View user's profile Send private message MSN Messenger

PostPosted: Tue Sep 07, 2004 2:22 pm     Reply with quote

OK I got a funny error...

Code:

typedef int (*pFunc)(void);
typedef struct {
   CHAR name[16];
   pFunc ptrFunc;
} MenuItem;


int one(void)
{
   return 0;
}

MenuItem  testMenu[2]={{"one", 0}, {"two",0}};

void main()
{
     pFunc compare;

     compare=one;
     testMenu[0].ptrFunc=compare;
}



This gave the error:
Quote:
*** Error 44 "***.c" Line 283(0,1): Internal Error - Contact CCS LABEL SCR=3322


I also tried doing a direct setting:
Code:
testMenu[0].ptrFunc=one;


Which gave me errors about invaild data types..

Troll Trampas Very Happy
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Tue Sep 07, 2004 6:41 pm     Reply with quote

Okay here you go Trampas
Code:

/*****************************************************************************
    This is an example of how to create a const array of function
    pointers and call them.  It is a bit of a trick to fool the compiler
    but seems to work for the cases that I tested. 
******************************************************************************/
#if defined(__PCM__)
#include <16f877.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
 
#elif defined(__PCH__)
#include <18f452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#endif
 

// Here is where we define the start of menu wrappers.
// !!!! Note that it (all wrapper addresses) must be within
// 1024 bytes of the caller
#define MENU_START_ADDRESS 0x200

// This is how many bytes the wrappers take up.  You might have to
// set this value high and then determine how many the wrappers actually
// take up.  Note that a BRA is usually used for small programs.  However
// on a larger program a CALL instruction might be used which will
// require 2 additional bytes
#define FUNC_SIZE          8

// This is where we define our menu wrapper locations
#define MENU1_ADDRESS      (MENU_START_ADDRESS)
#define MENU2_ADDRESS      (MENU1_ADDRESS+FUNC_SIZE)
#define MENU3_ADDRESS      (MENU2_ADDRESS+FUNC_SIZE)
#define MENU4_ADDRESS      (MENU3_ADDRESS+FUNC_SIZE)

// Typedef for our function pointer
typedef int (*pFunc)(void);

// Typedef for our menu items
typedef struct {
   char name[16];   
   int16 ptrFunc;   // Just tricking the compiler here
} MenuItem;

// Here's that constant array for the menus
const MenuItem Menu[3]={{"String1", MENU1_ADDRESS},
                        {"String2", MENU2_ADDRESS},
                        {"String3", MENU3_ADDRESS}};
 


// These are the real Menus that we want to call
#org 0x2000,0x2100  // org'ed this just to show a CALL insteal of BRA
int RealMenuFunc1(void)
{
  int i = 1;
  return(i);
}

int RealMenuFunc2(void)
{
  int i = 2;
  return(i);
}

int RealMenuFunc3(void)
{
  int i = 3;
  return(i);
}


// These are wrapper menus to make setting the addresses easier.
// It also appears CCS is using a RCALL instruction which limits
// how far the menus can be away from the caller.
#org MENU1_ADDRESS, MENU1_ADDRESS + FUNC_SIZE - 1
int WrapperMenu1(void)
{
  return(RealMenuFunc1());
}

#org MENU2_ADDRESS, MENU2_ADDRESS + FUNC_SIZE - 1
int WrapperMenu2(void)
{
  return(RealMenuFunc2());
}

#org MENU3_ADDRESS, MENU3_ADDRESS + FUNC_SIZE - 1
int WrapperMenu3(void)
{
  return(RealMenuFunc3());
}


void main()
{
  int i;
  pFunc ptrtofunc;

  // Dummy initialization to keep the compiler from complaining
  ptrtofunc = WrapperMenu1;

  // Assign the address to the function pointer
  ptrtofunc = (pFunc)Menu[0].ptrFunc;
  // Call the function
  i=(*ptrtofunc)();

  ptrtofunc = (pFunc)Menu[1].ptrFunc;
  i=(*ptrtofunc)();
  ptrtofunc = (pFunc)Menu[2].ptrFunc;
  i=(*ptrtofunc)();


  while( TRUE )
  {
  }
}


Regards,
Mark
Trampas



Joined: 04 Sep 2004
Posts: 89
Location: NC

View user's profile Send private message MSN Messenger

PostPosted: Wed Sep 08, 2004 9:43 am     Reply with quote

Mark,

That is great! Rather tricky way of doing it but it is pretty neat.

I think I will do more with switch case statements as it is more portable and easier when others have to understand my code.

I really hope CCS is considering adding/fixing the pointers to functions.

Trampas
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Wed Sep 08, 2004 10:26 am     Reply with quote

After all that work and you are not even going to use it Crying or Very sad

This might be useful to someone trying to port over some code. It is also great for creating "CommandHandlers" for serial communications. The string part is the command and the function pointer is that command's handler.

Mark
Trampas



Joined: 04 Sep 2004
Posts: 89
Location: NC

View user's profile Send private message MSN Messenger

PostPosted: Wed Sep 08, 2004 10:52 am     Reply with quote

Mark,

I do appericate your efforts and know that you were kidding... Smile I do that it is a neat way to get around the problem, just not clean.

For other readers who do not fully grasp what I mean, I am sure Mark does, here is an example of a simple command parser using pointers to functions.

Code:


UINT version()
{
     //print version number
}
UINT build()
{
    //prints build date and number
}

typedef struct {
    CHAR cmd[20];
    UINT (*ptrFunc)(void);
} Command;

Command cmds[]={
{"ver", version},
{"build", build},
{"\0",0}
};

UINT cmd_handler(CHAR *inStr)
{
    UINT i;

    i=0;
    while (cmds[i].ptrFunc!=0)
    {
         if (strcmp(inStr),cmds[i].cmd)==0)
         {
              return (*cmds[i].ptrFunc)(); //call function
          }
     }
   
     printf("Invaild Command\n");
     return 0;



So here in this code the use of the pointer to functions makes it really easy to add additional commands. That is you just add the commands to the structure/array.

I actually implement my commands parser with a little more advance features. For example I pass argc and argv parameters to the command handler functions which gives more controll....

Trampas
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Wed Sep 08, 2004 11:01 am     Reply with quote

I even include a little piece of "help" text

Code:

/* local typedefs */
typedef rom struct  _cmd_t
{
  const rom char  *pStr;  /* command */
  void (*pFunction) (enum porttypes port, char *pCmd, char *pArgs); /* command function */
  const rom char  *pDescription;  /* command description (for help) */
} COMMAND_DATA;

static COMMAND_DATA CommandData[] =
{
  /* hidden commands - description is NULL */
  { { "help"      },   VHelpCommand,     { NULL                     } },
  { { "OK"        },   NULL,             { NULL                     } },
  { { "RING"      },   AnswerCommand,    { NULL                     } },
  { { "CONNECT"   },   ConnectCommand,   { NULL                     } },
  { { "TEST"      },   SelfTestCommand,  { NULL                     } },
  { { "VIRGINIZE" },   VirginCommand,    { NULL                     } },
  { { "checksum"  },   ChecksumCommand,  { NULL                     } },
  { { "bootmode"  },   BootCommand,      { NULL                     } },

#ifdef DEBUG_DST
  { { "timeset"   },   TimeSetCommand,   { NULL                     } },
  { { "dateset"   },   DateSetCommand,   { NULL                     } },
  { { "dst"       },   DSTCommand,       { NULL                     } },
#endif

  /* application commands */
  { { "date"      },   DateCommand,      { "date mm/dd/yyyy"        } },
  { { "time"      },   TimeCommand,      { "time hh:mm:ss"          } },
  { { "ver"       },   VersionCommand,   { "sofware version"        } },
  { { "name"      },   NameCommand,      { "device name"            } },
  { { "modem"     },   ModemCommand,     { "enables modem"          } },
  { { "phase"     },   PhaseCommand,     { "device phase"           } },
  { { "event"     },   EventCommand,     { "event data"             } },
  { { "eventlist" },   EventListCommand, { "view today's events"    } },
  { { "holiday"   },   HolidayCommand,   { "holiday dates"          } },
  { { "relay"     },   RelayCommand,     { "relay settings"         } },
  { { "input"     },   InputCommand,     { "input settings"         } },
  { { "photocell" },   PhotocellCommand, { "photocell settings"     } },
  { { "remote"    },   RemoteCommand,    { "remote input settings"  } },
  { { "mask"      },   MaskCommand,      { "input mask settings"    } },
  { { "location"  },   LocationCommand,  { "location for dusk/dawn" } },
  { { "password"  },   PasswordCommand,  { "menu key password"      } },
  { { "data"      },   DataCommand,      { "dump program data"      } },
  { { "restore"   },   ReInitCommand,    { "restore defaults"       } },
  { { "restart"   },   RestartCommand,   { "restart device"         } },
  { { "??"        },   VHelpCommand,     { "verbose help"           } },
  { { "?"         },   THelpCommand,     { "terse help"             } },
  { { NULL        },   NULL,             { NULL                     } },
};

static void VerboseHelpHandler(
  enum porttypes  port,           /* FIX ME: add comment */
  COMMAND_DATA    *pCommandData)  /* FIX ME: add comment */
{
  char  buf[MAX_LINE_LEN];  /* string to print */

  /* spew the commands and their help */
  while (pCommandData->pStr != NULL)
  {
    if (pCommandData->pDescription != NULL)
    {
      sprintf(buf, "%-10S - %S\r\n", pCommandData->pStr, pCommandData->pDescription);
      HostWrite(port, buf);
    }

    pCommandData++;
  }

  HostWriteRomString(port, "\r\n");

  return;
}

static BOOLEAN CommandHandler(
  enum porttypes  port,           /* FIX ME: add comment */
  char            *pArgs,         /* FIX ME: add comment */
  COMMAND_DATA    *pCommandData)  /* FIX ME: add comment */
{
  static char cmd[MAX_LINE_LEN];  /* local buffer */
  BOOLEAN     found = 0;          /* valid command? */

  if (pArgs)
  {
    /* parse and remove the only the first command */
    pArgs = stptok(pArgs, cmd, sizeof(cmd), " ");

    /* find and execute command function */
    while (pCommandData->pStr != NULL)
    {
      if (strcmpipgm2ram(cmd, pCommandData->pStr) == 0)
      {
        if (pCommandData->pFunction != NULL)
        {
          (void)strupr(cmd);
          (void)pCommandData->pFunction(port, cmd, pArgs);
        }

        found = 1;
        break;
      }

      pCommandData++;
    }
  }

  return (found);
}


It sure will be nice when CCS finally fully implements function pointers and pointers to const strings!
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group