Μετρών την ώραν#

Εισαγωγή#

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 V5 program.

Πρόσβαση#

Brain.Timer

Σημειώσεις#

  • Brain.Timer is an instance of the timer class 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 V5 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 V5 Brain
brain Brain = brain();

clear#

Μηδενίζει την τρέχουσα τιμή του χρονοδιακόπτη. Στη συνέχεια, ο χρονοδιακόπτης αρχίζει αμέσως να μετράει ξανά από το μηδέν.

Available Functions
void clear();

Parameters

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

Return Values

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

Examples
while (true) {
  // Reset the timer every time the screen is pressed
  if (Brain.Screen.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 Functions

1 Επιστρέφει τον χρόνο που έχει παρέλθει σε χιλιοστά του δευτερολέπτου.

uint32_t time() const;

2 Επιστρέφει τον χρόνο που έχει παρέλθει στις καθορισμένες χρονικές μονάδες.

double time(
    timeUnits units ) const;

Parameters

Παράμετρος

Τύπος

Περιγραφή

units

timeUnits

The units used to return the elapsed time:

  • seconds / sec — seconds
  • msec — milliseconds

Return Values
  • The parameterless function (1) returns a uint32_t representing the elapsed time in milliseconds.

  • The function with a timeUnits parameter (2) returns a double representing the elapsed time in the specified units.

Notes
  • Το χρονόμετρο αρχίζει να μετράει όταν ξεκινά το έργο.

  • Calling clear resets the timer value to zero.

Examples
// 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 Functions

1 Καταχωρεί μια επανάκληση για εκτέλεση μετά από μια καθορισμένη καθυστέρηση και μεταβιβάζει μια τιμή που ορίζεται από τον χρήστη στην επανάκληση.

static void event(
    void (*callback)(void*),
    uint32_t value );

2 Καταχωρεί μια επανάκληση για εκτέλεση μετά από μια καθορισμένη καθυστέρηση.

static void event(
    void (*callback)(void),
    uint32_t value );

Parameters

Παράμετρος

Τύπος

Περιγραφή

callback

void ()(void)

Μια συνάρτηση που θα κληθεί μετά την καθορισμένη καθυστέρηση και θα λάβει ένα όρισμα που ορίζεται από τον χρήστη.

callback

void (*)(void)

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

value

uint32_t

Η καθυστέρηση, σε χιλιοστά του δευτερολέπτου, πριν από την εκτέλεση της συνάρτησης επανάκλησης.

Return Values

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

Notes
  • Αυτή η συνάρτηση προγραμματίζει μια εφάπαξ επανάκληση. Δεν επαναλαμβάνεται αυτόματα.

  • The callback function must return void.

  • Μπορούν να προγραμματιστούν πολλαπλές εκδηλώσεις ταυτόχρονα.

Examples

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);
}