gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Coding > help get my etch and sketch working.

#175791 - deathmetalscott - Mon Feb 07, 2011 9:12 pm

I just started in a GBA programming class to fill a second programming requirement in college studies. I have not programmed in C in a very long time and am having some difficulty. My instructor helped me out and I was able to get this far on my own but I can not get the program to compile.
Code:
#include <mygba.h>
#define BUTTONS (volatile unsigned int*)0x04000130;
    // Register: Status of button presses
unsigned short* videoBuffer=(unsigned short*)0x6000000;
    // pointer to video buffer
#define SetMode(mode) REG_DISPCNT=(mode)
#define buttons
#define BUTTON_A 1
#define BUTTON_B 2
#define BUTTON_SELECT 4
#define BUTTON_START 8
#define BUTTON_RIGHT 16
#define BUTTON_LEFT 32
#define BUTTON_UP 64
#define BUTTON_DOWN 128
#define BUTTON_R 256
#define BUTTON_L 512


void drawpixel3(int x, int y, unsigned short c)
{
   videoBuffer[y*240+x]=c;
}

void CheckButtons()
{
    buttons[0] = !((*BUTTONS) & BUTTON_A);
    buttons[1] = !((*BUTTONS) & BUTTON_B);
    buttons[2] = !((*BUTTONS) & BUTTON_LEFT);
    buttons[3] = !((*BUTTONS) & BUTTON_RIGHT);
    buttons[4] = !((*BUTTONS) & BUTTON_UP);
    buttons[5] = !((*BUTTONS) & BUTTON_DOWN);
    buttons[6] = !((*BUTTONS) & BUTTON_START);
    buttons[7] = !((*BUTTONS) & BUTTON_SELECT);
    buttons[8] = !((*BUTTONS) & BUTTON_L);
    buttons[9] = !((*BUTTONS) & BUTTON_R);
}

bool Pressed(int button)
{
    switch(button)
    {
    case BUTTON_A: return buttons[0];
    case BUTTON_B: return buttons[1];
    case BUTTON_LEFT: return buttons[2];
    case BUTTON_RIGHT: return buttons[3];
    case BUTTON_UP: return buttons[4];
    case BUTTON_DOWN: return buttons[5];
    case BUTTON_START: return buttons[6];
    case BUTTON_SELECT: return buttons[7];
    case BUTTON_L: return buttons[8];
    case BUTTON_R: return buttons[9];
    }
}


int main(void)
{
  /*  SetMode(MODE_3 | BG2_ENABLE);

    drawpixel3(120, 80, 0xFFFF);

    //continuous loop
    while(1)
    {
      CheckButtons();
         if (Pressed(BUTTON_LEFT))
         {
         for(n=0;n<10000;n--);
      }
      else if(Pressed(BUTTON_RIGHT))
      {
          for(n=0;n<10000;n++);
        }
        else if(Pressed (BUTTON_UP))
        {
            for(y=0;y<10000;y++);
        }
        else if(Pressed(BUTTON_DOWN))
        {
            for(y=0;y<10000;y--);
        }
   }

}
*/
    return 0;
}


The program is a simple etch and sketch and I keep getting these errors:
Code:

main.c: In function `CheckButtons':
main.c:27: error: parse error before '[' token
main.c: In function `Pressed':
main.c:43: error: parse error before '[' token
main.c:44: error: parse error before '[' token
main.c:45: error: parse error before '[' token
main.c:46: error: parse error before '[' token
main.c:47: error: parse error before '[' token
main.c:48: error: parse error before '[' token
main.c:49: error: parse error before '[' token
main.c:50: error: parse error before '[' token
main.c:51: error: parse error before '[' token
main.c:52: error: parse error before '[' token
make: *** [main.o] Error 1
[/list]

#175792 - elhobbs - Mon Feb 07, 2011 10:31 pm

I think you are supposed to do the assignments yourself...yet, I am amazed your instructor helped you to this result.

There are many logic flaws here but I will point out one...
what do you think this is doing exactly?
Code:
#define buttons

#175793 - deathmetalscott - Mon Feb 07, 2011 11:55 pm

I just need help, I did most of the work myself. As far as that goes, yeah, I saw that and fixed it. Below is what I have now. Everything is compiling except for something in CheckButtons(). I keep getting errors that say: error: parse error before ';' token.
Code:


#include <mygba.h>

///////////////Register: Status of button presses////////
#define BUTTONS (volatile unsigned int*)0x04000130;
////////////////////////////////////////////////////////

// ///////////pointer to video buffer///////////////////
unsigned short* videoBuffer=(unsigned short*)0x6000000;
///////////////////////////////////////////////////////

//////////////Sets Video Mode/////////////////////////
#define MODE_3 0x3
#define BG2_ENABLE 0x400
#define SetMode(mode) REG_DISPCNT=(mode)
//////////////////////////////////////////////////////

/////////////Button Definitions//////////////////////
#define BUTTON_A 1
#define BUTTON_B 2
#define BUTTON_SELECT 4
#define BUTTON_START 8
#define BUTTON_RIGHT 16
#define BUTTON_LEFT 32
#define BUTTON_UP 64
#define BUTTON_DOWN 128
#define BUTTON_R 256
#define BUTTON_L 512

#define bool int
#define true 1
#define false 0
bool buttons[10];
///////////////////////////////////////////////////

//////////////Draws the pixel in the center of the screen//
void drawpixel3(int x, int y, unsigned short c)
{
   videoBuffer[y*240+x]=c;
}
///////////////////////////////////////////////////////////

/////////Check Buttons///////////////////////////
void CheckButtons()
{
    buttons[0] = !((*BUTTONS) & BUTTON_A);
    buttons[1] = !((*BUTTONS) & BUTTON_B);
    buttons[2] = !((*BUTTONS) & BUTTON_LEFT);
    buttons[3] = !((*BUTTONS) & BUTTON_RIGHT);
    buttons[4] = !((*BUTTONS) & BUTTON_UP);
    buttons[5] = !((*BUTTONS) & BUTTON_DOWN);
    buttons[6] = !((*BUTTONS) & BUTTON_START);
    buttons[7] = !((*BUTTONS) & BUTTON_SELECT);
    buttons[8] = !((*BUTTONS) & BUTTON_L);
    buttons[9] = !((*BUTTONS) & BUTTON_R);
}
///////////////////////////////////////////////

bool Pressed(int button)
{
    switch(button)
    {
    case BUTTON_A: return buttons[0];
    case BUTTON_B: return buttons[1];
    case BUTTON_LEFT: return buttons[2];
    case BUTTON_RIGHT: return buttons[3];
    case BUTTON_UP: return buttons[4];
    case BUTTON_DOWN: return buttons[5];
    case BUTTON_START: return buttons[6];
    case BUTTON_SELECT: return buttons[7];
    case BUTTON_L: return buttons[8];
    case BUTTON_R: return buttons[9];
    }
}


int main(void)
{
    SetMode(MODE_3 | BG2_ENABLE);

    drawpixel3(120, 80, 0xFFFF);

    //continuous loop
    while(1)
    {
      CheckButtons();
         if (Pressed(BUTTON_LEFT))
         {
             int n;
         for(n=0;n<10000;n--);
      }
      else if(Pressed(BUTTON_RIGHT))
      {
          int n;
          for(n=0;n<10000;n++);
        }
        else if(Pressed (BUTTON_UP))
        {
            int y;
            for(y=0;y<10000;y++);
        }
        else if(Pressed(BUTTON_DOWN))
        {
            int y;
            for(y=0;y<10000;y--);
        }
   }

}

    return 0;
}

#175796 - sverx - Tue Feb 08, 2011 10:16 am

remember that #defines are just a kind of 'text replacement' in your source so if you're doing this:

Code:
#define BUTTONS (volatile unsigned int*)0x04000130;


and later you do this

Code:
buttons[3] = !((*BUTTONS) & BUTTON_RIGHT);


it means you're doing this:

Code:
buttons[3] = !((* (volatile unsigned int*)0x04000130; ) & BUTTON_RIGHT);


that has at least one mistake in it...

#175808 - dantheman - Thu Feb 10, 2011 4:36 am

Hehe, I know exactly which class you're in. How's Prof Smith doing lately anyway?

Regardless, like sverx mentioned, #define is like a global find-and-replace that occurs before any compilation actually takes place. So for starters, the semicolon at the end of the #define BUTTONS line shouldn't be there, and there might be an extra asterisk but I'm not entirely certain, been way too long since I took the class so I don't remember much about pointers.

Kind of a side note, but in general you want to make sure to put parenthesis around your input parameters to any #define statement, as you've done with SetMode here. This will be especially important once you add a macro that lets you define colors easier, something like
Code:
#define RGB(r,g,b) ((r)+((g)<<5)+((b)<<10)) //generates hex code for given RGB values 0-31


Without the parenthesis around R, G, and B, if you tried calling RGB(x+15,20,20) it would screw up due to the x+15 part.

Also I don't know what dev environment you're using these days (was Visual Studio for my semester but I know they moved to Eclipse later on) but see if you have any temporary compilation files in your working directory. These would be things like *.i, *.s, possibly *.o files. I had a couple of odd compilation errors that would only go away once I fixed the code (of course) and then deleted the temporary files so it could re-create them rather than attempting to use incorrect cached copies.