How do you make buttons on the brain screen??

@eggplant @nenik So we got the buttons working so then we tried to implement a back button and the only thing i could think of doing was make it all into a switch case and the back button doesn’t work. If y’all could see the reason and explain it to me that’d be great!
Here’s the code

void SelectScreenSwitch()
{
    
    int StepNumber = 1;

    //Initilaize all variables
    
    int RectangleWidth = 99;
    int RectangleHeight = 99;
    
    int xTLOrigin = 1;
    int xTLEnd = 100;
    int yTLOrigin = 1;
    int yTLEnd = 100;
    
    int xTROrigin = 381;
    int xTREnd = 480;
    int yTROrigin = 1;
    int yTREnd = 100;
    
    int xBROrigin = 381;
    int xBREnd = 480;
    int yBROrigin = 141;
    int yBREnd = 240;
    
    int xBLOrigin = 1;
    int xBLEnd = 100;
    int yBLOrigin = 141;
    int yBLEnd = 240;
    
    
            //Begin the switch process 
      
            switch(StepNumber)
            {
                case (1):
                    {
              
                    
                //Drawing the screen buttons in all four corners
                Brain.Screen.drawRectangle(xTLOrigin,yTLOrigin,RectangleWidth,RectangleHeight,color::red);
                Brain.Screen.drawRectangle(xTROrigin,yTROrigin,RectangleWidth,RectangleHeight,color::yellow);
                Brain.Screen.drawRectangle(xBROrigin,yBROrigin,RectangleWidth,RectangleHeight,color::blue);
                Brain.Screen.drawRectangle(xBLOrigin,yBLOrigin,RectangleWidth,RectangleHeight,color::green);
                
                while (1)
                {
           
                    
                    
                    //Brain Screen button programming
                    if (Brain.Screen.pressing())
                    {
                    int Xpos = Brain.Screen.xPosition();
                    int Ypos = Brain.Screen.yPosition();
                        
                        
                        
                        if ((Xpos >= xTLOrigin && Xpos <= xTLEnd) && (Ypos >= yTLOrigin && Ypos <= yTLEnd))
                        {
                            Brain.Screen.print("test");
                            StepNumber = 2;
                        
                            //This break seems to break out of the case 1, and switch to whatever step is now there, (2). Without, I cannot get it to work.
                            //break;
                        }
                        
                        else if ((Xpos >= xBLOrigin && Xpos <= xBLEnd) && (Ypos >= yBLOrigin && Ypos <= yBLEnd))//declaring if the screen is being touched within the bottom left box box
                        
                        {
                    
                            StepNumber = 3;
                            //See other comment above for why this break needs to be here. It is also found in the Easy C programs.
                            //break;
                        
                        }
                        
                        else if ((Xpos >= xBROrigin && Xpos <= xBREnd) && (Ypos >= yBROrigin && Ypos <= yBREnd))//declaring if the screen is being touched within the bottom right box box
                            
                        {
                            
                            StepNumber = 4;
                            //break;
              
                        }
                        
                        else if ((Xpos >= xTROrigin && Xpos <= xTREnd) && (Ypos >= yTROrigin && Ypos <= yTREnd))//declaring if the screen is being touched within the top left box box
         
                        {
                    
                            StepNumber = 5;
                            //break;
                            
                        }
                    }
                }
                        break;
                    }
                    
                case (2):
                    {
                    //Red side programs go here
                    Brain.Screen.clearScreen();
                    Brain.Screen.print("Top Left button was touched");
                    BackButton();
                   break;
                    }
                    
                    
                case (3):
                    {
                    //Green side programs go here
                Brain.Screen.clearScreen();
                Brain.Screen.setCursor(1,1);
                Brain.Screen.print("Bottom Left button was touched");
                break;
                    }
                
                    
                case (4):
                    {
                    //Blue side programs go here
                Brain.Screen.clearScreen();
                Brain.Screen.setCursor(1,1);
                Brain.Screen.print("Bottom Right was touched");   
                break;   
                    }
                    
                case (5): 
                    {
                    //Yellow side programs go here
                Brain.Screen.clearScreen();
                Brain.Screen.setCursor(1,1); 
                Brain.Screen.setFont(fontType::mono20);
                Brain.Screen.print("The Battery level is ");
                Brain.Screen.print(Brain.Battery.capacity(percentUnits::pct));
                Brain.Screen.newLine();
                Brain.Screen.newLine();
                Brain.Screen.print("The Battery temperature is ");
                Brain.Screen.print(Brain.Battery.temperature(percentUnits::pct));
                Brain.Screen.newLine();
                Brain.Screen.newLine();
                Brain.Screen.print("Top Right was touched");
                break; 
                    }
            }
        
    }

int main() {

    
    
    SelectScreenSwitch();
}
  

And here’s the back button

void BackButton(){
    
    int xBROrigin = 381;
    int xBREnd = 480;
    int yBROrigin = 141;
    int yBREnd = 240;
    
    int RectangleWidth = 99;
    int RectangleHeight = 99;
    
    Brain.Screen.drawRectangle(xBROrigin,yBROrigin,RectangleWidth,RectangleHeight,color::white);
    
    while(1){
        
         if (Brain.Screen.pressing())
      {
         int Xpos = Brain.Screen.xPosition();
         int Ypos = Brain.Screen.yPosition();
              
          if ((Xpos >= xBROrigin && Xpos <= xBREnd) && (Ypos >= yBROrigin && Ypos <= yBREnd))//declaring if the screen is being touched within the bottom right box
          {
            int StepNumber = 1;
          }
        
    }
}
}

@abennett5139 You need to make StepNumber a global variable

How do I do that? Do I just put it at the very top of my program?

That would work

Is there anyway else to do it?

to make it global you just need to declare it outside of any functions

Ok thanks!

@abennett5139 Don’t forget to remove the


int

in front of StepNumber in SelectScreenSwitch and BackButton

@jpearman Is there a reason you reference/dereference pointers here rather than using a . member reference for the button struct? I’m walking through this code with some students and want to avoid broaching the topic of pointers if at all humanly possible.

It compiles fine with member references, but often there’s other reasons… Thanks!

/*-----------------------------------------------------------------------------*/
/** @brief      Check if touch is inside button                                */
/*-----------------------------------------------------------------------------*/
int findButton(  int16_t xpos, int16_t ypos ) {
    int nButtons = sizeof(buttons) / sizeof(button);

    for( int index=0;index < nButtons;index++) {
      button *pButton = &buttons index ];
      if( xpos < pButton->xpos || xpos > (pButton->xpos + pButton->width) )
        continue;

      if( ypos < pButton->ypos || ypos > (pButton->ypos + pButton->height) )
        continue;
      
      return(index);
    }
    return (-1);
}

No, not really. Often it’s just a combination of habit and pulling in code from other projects.

I’m not getting a response on the display when I touch it. What am I doing wrong?
I am running this in the pre_auton of my main robot program.

int selection = 1;
int xP;
int yP;

void touchPos(){
    xP = Brain.Screen.xPosition();
    yP = Brain.Screen.yPosition();
}

void pre_auton( void ){
    
    //display autons //size 480x272 //make button sizes 125x125
    Brain.Screen.clearScreen(vex::color::black);
    Brain.Screen.drawRectangle(5 + 120 * 0,5,110,110,vex::color::red);
    Brain.Screen.drawRectangle(5 + 120 * 1,5,110,110,vex::color::red);
    Brain.Screen.drawRectangle(5 + 120 * 2,5,110,110,vex::color::red);
    Brain.Screen.drawRectangle(5 + 120 * 3,5,110,110,vex::color::red);
    Brain.Screen.drawRectangle(5 + 120 * 0,5 + 120,110,110,vex::color::blue);
    Brain.Screen.drawRectangle(5 + 120 * 1,5 + 120,110,110,vex::color::blue);
    Brain.Screen.drawRectangle(5 + 120 * 2,5 + 120,110,110,vex::color::blue);
    Brain.Screen.drawRectangle(5 + 120 * 3,5 + 120,110,110,vex::color::blue);
    Brain.Screen.render();
    
    //program names
    while (true){
        Brain.Screen.pressed(touchPos);
        Brain.Screen.clearScreen(vex::color::black);
        Brain.Screen.print("Selection = %d", xP);
        Brain.Screen.setCursor(2,1);
        Brain.Screen.print("Selection = %d", yP);

        /*if (((5 + 120 * 0) < xP) && (xP < (5 + 135 * 0 + 125))) { selection = 1;}
        else if (((5 + 120 * 1) < xP) && (xP < (5 + 120 * 1 + 125))) { selection = 2;}
        else if (((5 + 120 * 2) < xP) && (xP < (5 + 120 * 2 + 125))) { selection = 3;}
        else if (((5 + 120 * 3) < xP) && (xP < (5 + 120 * 3 + 125))) { selection = 4;}

        if ((125 <= yP) && (yP <= 235)) { selection += 4; }

        Brain.Screen.clearScreen(vex::color::black);
        Brain.Screen.print("Selection = %d", selection);*/
        
        if (selection != 1) {break;}
    }
}

You need to use %d, not d%.

Here you clear the screen, try writing to it, then loop back and clear the screen immediately. Try following the writing with a render command like you do in the section before it.

How would you call it in the autonomous method if you wanted to select various programs for autonomous. @jpearman

see this

Thanks @jpearman

Let me know if there are any fixes or clarification for our sample code… Always looking to make things better for all.

How I do this code in vex c++?

All the code in this thread is vex C++