Skip to content

Utility Functions

Jeffrey Shen edited this page Aug 18, 2017 · 1 revision

This library ships with a set of utility functions to do common tasks: MIN, MAX, SIGN, BOUND, and isTimedOut.

Usage

  • MIN(X, Y) returns the lower of the two parameters. The parameters can be any data type that is a number. For example:
int x = MIN(-10, 15); //x will be set to -10
  • MAX(X, Y) returns the higher of the two parameters. The parameters can be any data type that is a number. For example:
int x = MAX(-5, -1); //x will be set to -1
  • int SIGN(int x) returns the sign of the integer parameter (so -1 or 1). For example:
int x = SIGN(34); //x will be set to 1
  • int BOUND(int a, int amin, int amax) will make sure that a is between amin and amax. So, if a is less than amin, amin will be returned. For example:
BOUND(-100, -10, 10); //Will return -10
BOUND(5, -10, 10); //Will return 5
BOUND(100, -10, 10); //Will return 10
  • int isTimedOut(int tstop) is used to implement time out logic. If the current time has passed tstop, the method will return 1, but otherwise it will return 0. It's generally a good practice to implement time outs in any loops you create. For example:
int tnow = time1[T1]; //The initial time (in milliseconds)

while(true){
  if(isTimedOut(tnow + 3000)) break; //Timeout after 3 seconds

  wait1Msec(10);
}