Μετρών την ώραν#
Εισαγωγή#
The timer class is derived from the brain base class and provides functionality for measuring elapsed time, allowing you to track durations and schedule time-based events within a VEX EXP program.
Πρόσβαση#
Brain.Timer
Σημειώσεις#
Brain.Timeris an instance of thetimerclass and provides the same functionality.
Παράδειγμα#
/* This constructor is required when using VS Code.
A Brain is generated automatically at the start of
VEXcode projects. */
// Create the EXP Brain
brain Brain = brain();
// Display the timer as it ticks up
while (true) {
Brain.Screen.clearScreen();
Brain.Screen.setCursor(1, 1);
Brain.Screen.print("Time: %.2f", Brain.Timer.time());
wait(50, msec);
}
Συναρτήσεις Μελών#
The timer class includes the following member functions:
clear— Resets the timer to zero.time— Returns the elapsed time since the timer started.event— Calls a function after a specified number of milliseconds, with optional arguments.
Before calling any timer member functions, a brain instance must be created, as shown below:
// Create the EXP Brain
brain Brain = brain();
clear#
Μηδενίζει την τρέχουσα τιμή του χρονοδιακόπτη. Στη συνέχεια, ο χρονοδιακόπτης αρχίζει αμέσως να μετράει ξανά από το μηδέν.
Available Functionsvoid clear();
Αυτή η συνάρτηση δεν δέχεται καμία παράμετρο.
Return ValuesΑυτή η συνάρτηση δεν επιστρέφει τιμή.
Exampleswhile (true) {
// Reset the timer every time the check button is pressed
if (Brain.buttonCheck.pressing()) {
Brain.Timer.clear();
}
Brain.Screen.clearScreen();
Brain.Screen.setCursor(1, 1);
Brain.Screen.print("Time: %.2f", Brain.Timer.value());
wait(50, msec);
}
time#
Επιστρέφει την τρέχουσα τιμή του χρονοδιακόπτη.
Available Functions1 — Επιστρέφει τον χρόνο που έχει παρέλθει σε χιλιοστά του δευτερολέπτου.
uint32_t time() const;
Parameters2 — Επιστρέφει τον χρόνο που έχει παρέλθει στις καθορισμένες χρονικές μονάδες.
double time( timeUnits units ) const;
Παράμετρος |
Τύπος |
Περιγραφή |
|---|---|---|
|
|
The units used to return the elapsed time:
|
The parameterless function (1) returns a
uint32_trepresenting the elapsed time in milliseconds.The function with a
timeUnitsparameter (2) returns adoublerepresenting the elapsed time in the specified units.
Το χρονόμετρο αρχίζει να μετράει όταν ξεκινά το έργο.
Calling
clearresets the timer value to zero.
// Display the timer as it ticks up
while (true) {
Brain.Screen.clearScreen();
Brain.Screen.setCursor(1, 1);
Brain.Screen.print("Time: %.2f", Brain.Timer.time());
wait(50, msec);
}
event#
Καταγράφει μια συνάρτηση επανάκλησης που θα εκτελεστεί μετά από μια καθορισμένη καθυστέρηση.
Available Functions1 — Καταχωρεί μια επανάκληση για εκτέλεση μετά από μια καθορισμένη καθυστέρηση και μεταβιβάζει μια τιμή που ορίζεται από τον χρήστη στην επανάκληση.
static void event( void (*callback)(void*), uint32_t value );
Parameters2 — Καταχωρεί μια επανάκληση για εκτέλεση μετά από μια καθορισμένη καθυστέρηση.
static void event( void (*callback)(void), uint32_t value );
Παράμετρος |
Τύπος |
Περιγραφή |
|---|---|---|
|
|
Μια συνάρτηση που θα κληθεί μετά την καθορισμένη καθυστέρηση και θα λάβει ένα όρισμα που ορίζεται από τον χρήστη. |
|
|
Μια συνάρτηση που θα κληθεί μετά την καθορισμένη καθυστέρηση. |
|
|
Η καθυστέρηση, σε χιλιοστά του δευτερολέπτου, πριν από την εκτέλεση της συνάρτησης επανάκλησης. |
Αυτή η συνάρτηση δεν επιστρέφει τιμή.
NotesΑυτή η συνάρτηση προγραμματίζει μια εφάπαξ επανάκληση. Δεν επαναλαμβάνεται αυτόματα.
The callback function must return
void.Μπορούν να προγραμματιστούν πολλαπλές εκδηλώσεις ταυτόχρονα.
Define the function (outside of
int main())// Create a function to turn the screen orange void turnOrange() { Brain.Screen.clearScreen(orange); }Register the function inside
int main()int main() { /* vexcodeInit() is only required when using VEXcode. Remove vexcodeInit() if compiling in VS Code. */ vexcodeInit(); // Call turnOrange after 2 seconds Brain.Timer.event(turnOrange, 2000); }