Κουμπί#

Εισαγωγή#

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.

Η μπροστινή και η πάνω πλευρά του χειριστηρίου EXP με τα joystick, τα κουμπιά βέλους και τα κουμπιά δράσης επισημασμένα με κίτρινο χρώμα. Στην επιφάνεια του χειριστηρίου υπάρχουν δύο joystick αριστερά και δεξιά, με κουμπιά βέλους πάνω και κάτω αριστερά και κουμπιά A και B δεξιά. Στο πάνω μέρος του χειριστηρίου βρίσκονται τα L1 και L2 στην αριστερή πλευρά και τα R1 και R2 στη δεξιά πλευρά.

Πρόσβαση#

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

Αντικείμενο

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

Περιγραφή

ButtonA

Controller.ButtonA.pressing()

Το κουμπί A.

ButtonB

Controller.ButtonB.pressing()

Το κουμπί B.

ButtonDown

Controller.ButtonDown.pressing()

Το κουμπί Κάτω.

ButtonUp

Controller.ButtonUp.pressing()

Το κουμπί Πάνω.

ButtonL1

Controller.ButtonL1.pressing()

Το κουμπί L1.

ButtonL2

Controller.ButtonL2.pressing()

Το κουμπί L2.

ButtonL3

Controller.ButtonL3.pressing()

Το κουμπί L3.

ButtonR1

Controller.ButtonR1.pressing()

Το κουμπί R1.

ButtonR2

Controller.ButtonR2.pressing()

Το κουμπί R2.

ButtonR3

Controller.ButtonR3.pressing()

Το κουμπί R3.

Σημειώσεις#

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

// 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 L1 is pressed
while (true) {
  if (Controller.ButtonL1.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 R1 is pressed
void driveForward() {
  Drivetrain.driveFor(forward, 200, mm);
}

Register the callback inside main.

int main() {
  vexcodeInit();

  // Run driveForward when R1 is pressed
  Controller.ButtonR1.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 R1 is released
void stopDriving() {
  Drivetrain.stop();
}

Register the callback inside main.

int main() {
  vexcodeInit();

  // Run stopDriving when R1 is released
  Controller.ButtonR1.released(stopDriving);
}