View previous topic :: View next topic |
Author |
Message |
Trampas Guest
|
|
|
bdavis
Joined: 31 May 2004 Posts: 86 Location: Colorado Springs, CO
|
|
Posted: Sat Sep 04, 2004 8:50 am |
|
|
I read somewhere in the help section that CCS does not support them. Mark replied in the earlier post stating they are supported, so he may have a workaround or maybe they were added since the manual was printed or something... |
|
|
Trampas Guest
|
|
Posted: Sat Sep 04, 2004 9:03 am |
|
|
Yeap, well I know Mark way toying with the C18 compiler which does support pointer to functions, thus this may be the confusion.
What I am trying to do is create a menu system:
typedef struct {
CHAR name[16];
int (*ptrFunc)();
} MenuItem;
So I create an array of these structures and then pass them to a show menu function, if the user selects the menu it would call the function. This is a really clean way to to do menus and very flexiable.
Maybe someone else has a better method? The only other way I can figured out how to do it is by using a switch case statements, which is not as simple or as clean...
Trampas |
|
|
bdavis
Joined: 31 May 2004 Posts: 86 Location: Colorado Springs, CO
|
|
Posted: Sat Sep 04, 2004 9:18 am |
|
|
I'm doing about the same thing - I wanted a struct with a pointer to a function, the string to look for that if found would call the function, and string for a description. It is a clean way to do a command handler or menu since you only have to modify the struct to add new commands. Anyway - I'm using the 18FXXX and couldn't get it to compile, so I went with separate structures and a switch statement for the functions. I replied to Mark's last post on this, so maybe he can show us how it's done. |
|
|
Trampas
Joined: 04 Sep 2004 Posts: 89 Location: NC
|
|
Posted: Sat Sep 04, 2004 2:06 pm |
|
|
That is funny! I ran into the same problem for a command structure. I developmed the command line system on a TI DSP and then tried to port to PIC...
BTW check out TI's newer F28xx DSPs they rock! 150MIPS 32bit processor with internal Flash memory, for $15! They are going to be releasing a smaller version in the spring for around $5. However they are 3.3V IO and for somethings the PIC is just more suited.
Trampas |
|
|
bdavis
Joined: 31 May 2004 Posts: 86 Location: Colorado Springs, CO
|
|
Posted: Sat Sep 04, 2004 2:42 pm |
|
|
It still amazes me how micro's are getting so cheap, and have so much packed into 'em! They must have a good cache too to run at 150Mips with Flash... |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Sun Sep 05, 2004 6:10 pm |
|
|
http://www.ccsinfo.com/forum/viewtopic.php?t=18345&highlight=function+pointer
Also see "EX_QSORT.C" for another example. I think I read somewhere around 3.160 it was added.
Edit:
3.160 Pointers to functions are now supported
3.160 Examples updated to use more modern chips
3.160 PCW IDE improvments incorporated
3.160 Some COFF debug file problems are resolved |
|
|
Trampas
Joined: 04 Sep 2004 Posts: 89 Location: NC
|
|
Posted: Tue Sep 07, 2004 8:30 am |
|
|
I must be doing something wrong or it does not work:
[code]
typedef int (*pFunc)(void);
typedef struct {
CHAR name[16];
pFunc ptrFunc;
} MenuItem;
UINT one()
{
return 0;
}
MenuItem testMenu[2]={{"one", one}, {"two",0}};
*** Error 27 "...***.c" Line 15(35,36): Expression must evaluate to a constant
That is I get this error where I defince the testMenu array.
Trampas |
|
|
Trampas
Joined: 04 Sep 2004 Posts: 89 Location: NC
|
|
Posted: Tue Sep 07, 2004 8:34 am |
|
|
If I try to declare the pointer to a function the method like K&R states:
Code: |
typedef struct {
CHAR name[16];
int (*ptrFunc)();
} MenuItem; |
Then I get the error:
Quote: | Error 36 "menu.h" Line 22(14,15): Expecting a ; or , |
I guess I should try compiling the qsort example next.
Trampas |
|
|
Trampas
Joined: 04 Sep 2004 Posts: 89 Location: NC
|
|
Posted: Tue Sep 07, 2004 8:42 am |
|
|
I went and compiled the Qsort example and it compiled correctly.
I changed the code to the following:
Code: |
void main() {
int i;
char c;
_Cmpfun compare;
while( TRUE ) {
do {
printf("\r\n\r\nSort Assending or Desending (A,D): ");
c=toupper(getc());
} while ((c!='A')&&(c!='D'));
if(c == 'A')
qsort(data, items, maxchars, assending);
//compare=assending;
else
compare=desending;
qsort(data, items, maxchars, compare);
for(i=0; i<items; ++i)
printf("\r\n%s",data[i]);
}
} |
Which created the errors:
Quote: | *** Error 51 "C:\Program Files\PICC\Examples\EX_QSORT.C" Line 94(31,40): A numeric expression must appear here
*** Error 51 "C:\Program Files\PICC\Examples\EX_QSORT.C" Line 96(7,11): A numeric expression must appear here
2 Errors, |
So the answer is that CCS supports pointer to functions sort of... From what I can tell the pointers to functions have to be assigned to a none constant variable, ie variable in data memory. This really sucks as it would be nice to be able to implement menu systems and state machines using pointer to functions in static arrays.
Trampas |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Tue Sep 07, 2004 9:07 am |
|
|
Maybe one of these days they will fully implement it as well as pointers to constant strings! Until then, I'll continue to use the C18 compiler. Here is a snippet of something similar to what you are doing but it does use the C18 compiler.
Code: |
typedef rom struct _menu
{
enum menus menu;
enum menus parentmenu;
rom const char *pStr;
PTR_ROM *pViewHelp;
PTR_ROM *pEditHelp;
void (*Soft_Key) (signed char index); /* command function */
void (*Up_Key) (signed char index); /* command function */
void (*Down_Key) (signed char index); /* command function */
void (*Left_Key) (signed char index); /* command function */
void (*Right_Key) (signed char index); /* command function */
void (*Input_Key) (signed char index); /* command function */
void (*Relay_Key) (signed char index); /* command function */
void (*Day_Key) (signed char index); /* command function */
void (*Menu_Key) (signed char index); /* command function */
void (*Display_Menu) (void); /* command function */
}MENU;
MENU MENU_INPUT =
{
m_Inputs, /* MenuID */
m_Menu, /* Parent Menu */
" INPUTS", /* Menu Text */
INPUT_HELP, /* View Help Text */
E_INPUT_HELP, /* Edit Help Text */
Input_Softkey, /* Softkey */
Input_Upkey, /* Upkey */
Input_Downkey, /* Downkey */
Input_Leftkey, /* Leftkey */
Input_Rightkey, /* Rightkey */
Input_Inputkey, /* Inputkey */
Input_Relaykey, /* Relaykey */
NULL, /* Daykey */
Default_Menukey, /* Menukey */
DisplayInputMenu /* Display function */
};
|
|
|
|
Trampas
Joined: 04 Sep 2004 Posts: 89 Location: NC
|
|
Posted: Tue Sep 07, 2004 9:26 am |
|
|
Yeap, maybe it is time to switch to a real compiler...
I would think constant arrays of pointer to functions would be better as the compiler could statically determine execution path.
Trampas |
|
|
Trampas
Joined: 04 Sep 2004 Posts: 89 Location: NC
|
|
Posted: Tue Sep 07, 2004 1:52 pm |
|
|
Yeap I guess you can call me Troll if it makes you feel better.
Trampas |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Tue Sep 07, 2004 1:56 pm |
|
|
Trampas,
I wouldn't waste my time with "Guest". He is obviously not a professional. Everyone is entitled to their own opinion and you certainly have the right to express yours. |
|
|
Trampas
Joined: 04 Sep 2004 Posts: 89 Location: NC
|
|
Posted: Tue Sep 07, 2004 2:11 pm |
|
|
Yeap, I learned a long time ago to leave my feelings at home...
I was thinking about the pointers to function some more and was thinking about trying assigning the pointers at run time..
I will give it a go and get back to you guys.
Trampas |
|
|
|