View previous topic :: View next topic |
Author |
Message |
madcat
Joined: 30 Jul 2008 Posts: 17
|
A couple of simple coding questions |
Posted: Sat Jan 03, 2009 5:57 pm |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Jan 03, 2009 11:09 pm |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
madcat
Joined: 30 Jul 2008 Posts: 17
|
|
Posted: Sat Jan 03, 2009 11:30 pm |
|
|
PCM programmer wrote: |
Normally, you don't put a semi-colon at the end of a #define statement.
|
Ouch !
Thats what happens when you work 20h on a program without a break
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. |
|
![](templates/subSilver/images/spacer.gif) |
|