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

A couple of simple coding questions

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
madcat



Joined: 30 Jul 2008
Posts: 17

View user's profile Send private message

A couple of simple coding questions
PostPosted: Sat Jan 03, 2009 5:57 pm     Reply with quote

Hi to all,

I'm writing a code that will display various information screens on LCD.
The following code supposed to get an up/down key and cycle through the available screens.


Code:

#define key_up     'A';
#define key_down    'B';


typedef struct t_gui {
   enum screens {SCR1, SCR2, SCR3} screen;
   char kb,kb_last;
} s_gui;

static s_gui gui = {0};


gui.kb = kbd_getc();
// Needed because this part of code runs many times per second and if nothing is pressed it will return '\0'
if(gui.kb != 0)    
   gui.kb_last = gui.kb;


switch (gui.kb_last) {
   case key_up   : gui.screen = gui.screen + (screens)1;      break;
   case key_down   : gui.screen = gui.screen - (screens)1;    break;
}      
//There is another part of code that reads the "gui.screen" and calls the correct display function when needed.   



Problem #1:

The line "case key_up :" gives compilation error "Error 60 Expecting : " if i replace the key_up with 'A' it compiles and works fine.


Problem #2:

gui.screen = gui.screen + (screens)1; or (screens)gui.screen++; does not work.
I can't even get the gui.screen to display in the "Watch" window. its not there ! (why ?)
The following code does work properly "case key_up : gui.screen = SCR2" But will make cycling through the screens harder...

How can i cycle through the enum members ?

Do i waste any ram by using structure/ enum instead of usual vars ?

Will appreciate any help.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Jan 03, 2009 11:09 pm     Reply with quote

Quote:

#define key_up 'A';
#define key_down 'B';

Problem #1:
The line "case key_up :" gives compilation error "Error 60 Expecting : " if i replace the key_up with 'A' it compiles and works fine.

Normally, you don't put a semi-colon at the end of a #define statement.
Remove the two semi-colons and the error should go away.

Here is a good summary of C:
http://cslibrary.stanford.edu/101/EssentialC.pdf
madcat



Joined: 30 Jul 2008
Posts: 17

View user's profile Send private message

PostPosted: Sat Jan 03, 2009 11:30 pm     Reply with quote

PCM programmer wrote:

Normally, you don't put a semi-colon at the end of a #define statement.


Ouch ! Shocked Embarassed

Thats what happens when you work 20h on a program without a break Confused
I have lots of defines in a few thousand lines prog. Suddenly i decided to put a semi-colon .......
Sometimes a fresh look from outside can save.
Thanks !


Problem #2: solved in this way for now :

Code:

// Define enum outside of the typedef struct
enum screens {SCR1, SCR2, SCR3}

typedef struct t_gui {
   screens screen;
   char kb;
} s_gui;

static s_gui gui = {0};

switch (gui.kb) {      
   case key_up   : gui.screen++; if (gui.screen >= 3  ) gui.screen = 0; break; // Cycle up
   case key_down   : gui.screen--; if (gui.screen >= 255) gui.screen = 2; break; // Cycle down
}      


It works and does show the "screen" in the watch list now. Although the values are very strange there. Instead as acting as int it opens a child tree with all the enum items (screen-> SCR1, SCR2, SCR3) and each one of them has some value (not equal to 1-3) so it is impossible to see the value of gui.screens.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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