Κουμπί#

Εισαγωγή#

The button class is a derived class of the controller class. It provides access to controller button states and button event callbacks.

A button object is accessed through a controller object. It is not constructed directly.

Μια μπροστινή και μια πίσω όψη του χειριστηρίου IQ 2ης γενιάς με όλα τα κουμπιά επισημασμένα με κίτρινο χρώμα. Η επιφάνεια του χειριστηρίου περιέχει δύο κουμπιά joystick στην επάνω αριστερή και δεξιά γωνία, με το E πάνω και κάτω κάτω από το αριστερό joystick και το F πάνω και κάτω κάτω από τη δεξιά. Στο πίσω μέρος του χειριστηρίου υπάρχουν το L πάνω και κάτω και το R πάνω και κάτω στην αριστερή και δεξιά πλευρά αντίστοιχα.

Πρόσβαση#

The controller class provides the following button objects. Each object is an instance of the button class.

Αντικείμενο

Παράδειγμα Χρήσης

Περιγραφή

ButtonEDown

Controller.ButtonEDown.pressing()

Το κουμπί E ▼.

ButtonEUp

Controller.ButtonEUp.pressing()

Το κουμπί E ▲.

ButtonFDown

Controller.ButtonFDown.pressing()

Το κουμπί F ▼.

ButtonFUp

Controller.ButtonFUp.pressing()

Το κουμπί F ▲.

ButtonL3

Controller.ButtonL3.pressing()

Το κουμπί Αριστερό Joystick. Μόνο για το χειριστήριο IQ (2ης γενιάς).

ButtonLDown

Controller.ButtonLDown.pressing()

Το κουμπί L ▼.

ButtonLUp

Controller.ButtonLUp.pressing()

Το κουμπί L ▲.

ButtonR3

Controller.ButtonR3.pressing()

Το κουμπί του Δεξιού Joystick. Μόνο για το χειριστήριο IQ (2ης γενιάς).

ButtonRDown

Controller.ButtonRDown.pressing()

Το κουμπί R ▼.

ButtonRUp

Controller.ButtonRUp.pressing()

Το κουμπί R ▲.

Σημειώσεις#

  • Before using a button member function, create a controller object.

  • ButtonL3 and ButtonR3 are only available on the IQ (2nd gen) Controller.

// Create a controller object
controller Controller = controller();

Συναρτήσεις Μελών#

The button class includes the following member functions:

Getter — Ανάγνωση κατάστασης κουμπιού.

  • pressing — Returns whether the button is being pressed.

Επιστροφές κλήσεων — Εκτέλεση συναρτήσεων όταν προκύπτουν συμβάντα κουμπιών.

  • pressed — Registers a function to run when the button is pressed.

  • released — Registers a function to run when the button is released.

Λήπτης#

pressing#

pressing returns whether the button is currently being pressed.

Διαθέσιμη λειτουργία

bool pressing() const;

Παράμετροι

Αυτή η συνάρτηση δεν έχει παραμέτρους.

Επιστρεφόμενη τιμή

Επιστρέφει μια λογική τιμή.

  • true — The button is being pressed.

  • false — The button is not being pressed.

Παράδειγμα

// Turn right while R Up is pressed
while (true) {
  if (Controller.ButtonRUp.pressing()) {
    Drivetrain.turn(right);
  }
  else {
    Drivetrain.stop();
  }

  wait(20, msec);
}

Επανακλήσεις#

pressed#

pressed registers a function that runs when the button is pressed.

Διαθέσιμη λειτουργία

void pressed(void (* callback)(void)) const;

Παράμετρος

Τύπος

Περιγραφή

callback

void (*)(void)

Μια λειτουργία που εκτελείται όταν πατηθεί το κουμπί.

Επιστρεφόμενη τιμή

Αυτή η συνάρτηση δεν επιστρέφει τιμή.

Παράδειγμα

Define the callback function outside of main.

// Drive forward when F Up is pressed
void driveForward() {
  Drivetrain.driveFor(forward, 100, mm);
}

Register the callback inside main.

int main() {
  vexcodeInit();

  // Run driveForward when F Up is pressed
  Controller.ButtonFUp.pressed(driveForward);
}

released#

released registers a function that runs when the button is released.

Διαθέσιμη λειτουργία

void released(void (* callback)(void)) const;

Παράμετρος

Τύπος

Περιγραφή

callback

void (*)(void)

Μια συνάρτηση που εκτελείται όταν απελευθερώνεται το κουμπί.

Επιστρεφόμενη τιμή

Αυτή η συνάρτηση δεν επιστρέφει τιμή.

Παράδειγμα

Define the callback function outside of main.

// Stop the drivetrain when F Up is released
void stopDriving() {
  Drivetrain.stop();
}

Register the callback inside main.

int main() {
  vexcodeInit();

  // Run stopDriving when F Up is released
  Controller.ButtonFUp.released(stopDriving);
}