How do you pass and store button variables in vexcode

Hello!
I’m currently in the process of rewriting a controller mapper I previously made, but I am struggling with trying to store button variables. Let me write some simple code just for starters:


bool check_if_button_pressing(/*what do I put here*/ choice){

return (&Controller1)->choice->pressing();

}

int main(){

/*what do I put here?*/ selected_button = ButtonR1;

bool pressing;
while(1){

pressing = check_if_button_pressing(selected_button);
std::cout << "pressing" << pressing << std::endl;

pressing = check_if_button_pressing(ButtonL1);
std::cout << "also pressing" << pressing << std::endl;

vex::task::sleep(500);
}

}

Hopefully this makes sense. I’m not sure how to store and pass buttons in vexcode. Any help is appreciated :slight_smile:

Note: I’m still kinda new to enums. They confuse me slightly.

If I understand correctly, you want to pass a button as a function parameter. I did that, but I don’t know if it works, because I haven’t had a chance to download the code to a robot. When I built the code, it didn’t come up with any errors, though. (DISCLAIMER: I did in in IQ.)

I did it like this (I think):

void ButtonManager(controller::button pushed, void function()) {
  if (pushed.pressing()) {
    function();
  }
}

void LDown() {
  catapult.spin(forward);
}

int main() {
  while (true) {
    ButtonManager(Controller.ButtonLDown, LDown());
    vex::task::sleep(20);
  }
}

I’ve never (that I know of) heard of enums. According to this page, it seems in V5 you would do the same thing. Maybe you have to say vex::controller::button instead of controller::button though? I don’t think so, though.

4 Likes

yea, pretty close.

void ButtonManager(controller::button button, void (* callback)(void)) {

  if (button.pressing()) {
    callback();
  }
}

void LDown() {
  catapult.spin(forward);
}

int main() {
  while (true) {
    ButtonManager( Controller.ButtonL1, LDown );
    vex::task::sleep(20);
  }
}
7 Likes

Thank you both so much @jpearman and @242EProgrammer
Much appreciations!