Κουμπί#

Εισαγωγή#

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.

Η μπροστινή και η πίσω πλευρά του χειριστηρίου V5 με τα κουμπιά επισημασμένα με κίτρινο χρώμα. Στην επιφάνεια του χειριστηρίου υπάρχουν κουμπιά βέλους πάνω, κάτω, αριστερά και δεξιά στα αριστερά και κουμπιά X, A, B, Y δεξιόστροφα από τις 12 η ώρα στα δεξιά. Στην πίσω πλευρά του χειριστηρίου υπάρχουν 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.

ButtonX

Controller.ButtonX.pressing()

Το κουμπί X.

ButtonY

Controller.ButtonY.pressing()

Το κουμπί Y.

ButtonDown

Controller.ButtonDown.pressing()

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

ButtonUp

Controller.ButtonUp.pressing()

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

ButtonLeft

Controller.ButtonLeft.pressing()

Το κουμπί Αριστερά.

ButtonRight

Controller.ButtonRight.pressing()

Το κουμπί Δεξιά.

ButtonL1

Controller.ButtonL1.pressing()

Το κουμπί L1.

ButtonL2

Controller.ButtonL2.pressing()

Το κουμπί L2.

ButtonR1

Controller.ButtonR1.pressing()

Το κουμπί R1.

ButtonR2

Controller.ButtonR2.pressing()

Το κουμπί R2.

Σημειώσεις#

  • 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 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 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 A is pressed
void driveForward() {
  Drivetrain.driveFor(forward, 100, mm);
}

Register the callback inside main.

int main() {
  vexcodeInit();

  // Run driveForward when A is pressed
  Controller.ButtonA.pressed(driveForward);
}

κυκλοφόρησε#

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 A is released
void stopDriving() {
  Drivetrain.stop();
}

Register the callback inside main.

int main() {
  vexcodeInit();

  // Run stopDriving when A is released
  Controller.ButtonA.released(stopDriving);
}