Miscellaneous C API module

Files

file misc.h

Namespaces

namespace pros
LLEMU Conditional Include
namespace pros::c

Classes

struct pros::c::date_s_t
struct pros::c::time_s_t

Functions

uint8_t competition_get_status(void)
uint8_t competition_is_disabled(void)
uint8_t competition_is_connected(void)
uint8_t competition_is_autonomous(void)
uint8_t competition_is_field(void)
int32_t controller_is_connected(controller_id_e_t id)
int32_t controller_get_analog(controller_id_e_t id, controller_analog_e_t channel)
Gets the value of an analog channel (joystick) on a controller.
int32_t controller_get_battery_capacity(controller_id_e_t id)
Gets the battery capacity of the given controller.
int32_t controller_get_battery_level(controller_id_e_t id)
Gets the battery level of the given controller.
int32_t controller_get_digital(controller_id_e_t id, controller_digital_e_t button)
Checks if a digital channel (button) on the controller is currently pressed.
int32_t controller_get_digital_new_press(controller_id_e_t id, controller_digital_e_t button)
Returns a rising-edge case for a controller button press.
int32_t controller_print(controller_id_e_t id, uint8_t line, uint8_t col, const char* fmt, ...)
Sets text to the controller LCD screen.
int32_t controller_set_text(controller_id_e_t id, uint8_t line, uint8_t col, const char* str)
Sets text to the controller LCD screen.
int32_t controller_clear_line(controller_id_e_t id, uint8_t line)
Clears an individual line of the controller screen.
int32_t controller_clear(controller_id_e_t id)
Clears all of the lines on the controller screen.
int32_t controller_rumble(controller_id_e_t id, const char* rumble_pattern)
Rumble the controller.
int32_t battery_get_voltage(void)
Gets the current voltage of the battery, as reported by VEXos.
int32_t battery_get_current(void)
Gets the current current of the battery, as reported by VEXos.
double battery_get_temperature(void)
Gets the current temperature of the battery, as reported by VEXos.
double battery_get_capacity(void)
Gets the current capacity of the battery, as reported by VEXos.
int32_t usd_is_installed(void)
Checks if the SD card is installed.
int32_t usd_list_files(const char* path, char* buffer, int32_t len)
Lists the files in a directory specified by the path Puts the list of file names (NOT DIRECTORIES) into the buffer seperated by newlines.

V5 Competition

enum competition_status { COMPETITION_DISABLED = 1 << 0, COMPETITION_CONNECTED = 1 << 2, COMPETITION_AUTONOMOUS = 1 << 1, COMPETITION_SYSTEM = 1 << 3 }

Variables

const char* baked_date
Date and Time
const char* baked_time

Function documentation

uint8_t competition_get_status(void)
#include <pros/misc.h>

Returns The competition control status as a mask of bits with COMPETITION_{ENABLED,AUTONOMOUS,CONNECTED}.

Get the current status of the competition control.

Example

void initialize() {
  if (competition_get_status() & COMPETITION_CONNECTED == true) {
    // Field Control is Connected
    // Run LCD Selector code or similar
  }
}

uint8_t competition_is_disabled(void)
#include <pros/misc.h>

Returns True if the V5 Brain is disabled, false otherwise.

Example

void my_task_fn(void* ignore) {
  while (!competition_is_disabled()) {
  // Run competition tasks (like Lift Control or similar)
  }
}

void initialize() {
  task_t my_task = task_create(my_task_fn, NULL, TASK_PRIO_DEFAULT, TASK_STACK_DEPTH_DEFAULT, "My Task");
}

uint8_t competition_is_connected(void)
#include <pros/misc.h>

Returns True if the V5 Brain is connected to competition control, false otherwise.

Example

void initialize() {
  if (competition_is_connected()) {
    // Field Control is Connected
    // Run LCD Selector code or similar
  }
}

uint8_t competition_is_autonomous(void)
#include <pros/misc.h>

Returns True if the V5 Brain is in autonomous mode, false otherwise.

Example

void my_task_fn(void* ignore) {
  while (!competition_is_autonomous()) {
    // Wait to do anything until autonomous starts
    delay(2);
  }
  while (competition_is_autonomous()) {
    // Run whatever code is desired to just execute in autonomous
    }
}

void initialize() {
  task_t my_task = task_create(my_task_fn, NULL, TASK_PRIO_DEFAULT, TASK_STACK_DEPTH_DEFAULT, "My Task");
}

uint8_t competition_is_field(void)
#include <pros/misc.h>

Returns True if the V5 Brain is connected to VEXnet Field Controller, false otherwise.

Example

void initialize() {
  if (competition_is_field()) {
    // connected to VEXnet Field Controller
  }
}

int32_t controller_is_connected(controller_id_e_t id)
#include <pros/misc.h>

Returns True if the V5 Brain is connected to VEXnet Competition Switch, false otherwise.

Example

   void initialize() {
     if (competition_is_switch()) {
       // connected to VEXnet Competition Switch
     }
   }
  /
uint8_t competition_is_switch(void);

#ifdef __cplusplus
}
}
}
#endif
///@}

/// \name V5 Controller
///@{
#ifdef __cplusplus
extern "C" {
namespace pros {
#endif

/**
 * \enum
 */
typedef enum {
    /// The master controller.
    E_CONTROLLER_MASTER = 0,
    /// The partner controller.
    E_CONTROLLER_PARTNER
} controller_id_e_t;

/**
 * \enum
 */
typedef enum {
    /// The horizontal axis of the controller’s left analog stick.
    E_CONTROLLER_ANALOG_LEFT_X = 0,
    /// The vertical axis of the controller’s left analog stick.
    E_CONTROLLER_ANALOG_LEFT_Y,
    /// The horizontal axis of the controller’s right analog stick.
    E_CONTROLLER_ANALOG_RIGHT_X,
    /// The vertical axis of the controller’s right analog stick.
    E_CONTROLLER_ANALOG_RIGHT_Y
} controller_analog_e_t;

/**
 * \enum
 */
typedef enum {
    /// The first trigger on the left side of the controller.
    E_CONTROLLER_DIGITAL_L1 = 6,
    /// The second trigger on the left side of the controller.
    E_CONTROLLER_DIGITAL_L2,
    /// The first trigger on the right side of the controller.
    E_CONTROLLER_DIGITAL_R1,
    /// The second trigger on the right side of the controller.
    E_CONTROLLER_DIGITAL_R2,
    /// The up arrow on the left arrow pad of the controller.
    E_CONTROLLER_DIGITAL_UP,
    /// The down arrow on the left arrow pad of the controller.
    E_CONTROLLER_DIGITAL_DOWN,
    /// The left arrow on the left arrow pad of the controller.
    E_CONTROLLER_DIGITAL_LEFT,
    /// The right arrow on the left arrow pad of the controller.
    E_CONTROLLER_DIGITAL_RIGHT,
    /// The ‘X’ button on the right button pad of the controller.
    E_CONTROLLER_DIGITAL_X,
    /// The ‘B’ button on the right button pad of the controller.
    E_CONTROLLER_DIGITAL_B,
    /// The ‘Y’ button on the right button pad of the controller.
    E_CONTROLLER_DIGITAL_Y,
    /// The ‘A’ button on the right button pad of the controller.
    E_CONTROLLER_DIGITAL_A
} controller_digital_e_t;

#ifdef PROS_USE_SIMPLE_NAMES
#ifdef __cplusplus
#define CONTROLLER_MASTER pros::E_CONTROLLER_MASTER
#define CONTROLLER_PARTNER pros::E_CONTROLLER_PARTNER
#define ANALOG_LEFT_X pros::E_CONTROLLER_ANALOG_LEFT_X
#define ANALOG_LEFT_Y pros::E_CONTROLLER_ANALOG_LEFT_Y
#define ANALOG_RIGHT_X pros::E_CONTROLLER_ANALOG_RIGHT_X
#define ANALOG_RIGHT_Y pros::E_CONTROLLER_ANALOG_RIGHT_Y
#define DIGITAL_L1 pros::E_CONTROLLER_DIGITAL_L1
#define DIGITAL_L2 pros::E_CONTROLLER_DIGITAL_L2
#define DIGITAL_R1 pros::E_CONTROLLER_DIGITAL_R1
#define DIGITAL_R2 pros::E_CONTROLLER_DIGITAL_R2
#define DIGITAL_UP pros::E_CONTROLLER_DIGITAL_UP
#define DIGITAL_DOWN pros::E_CONTROLLER_DIGITAL_DOWN
#define DIGITAL_LEFT pros::E_CONTROLLER_DIGITAL_LEFT
#define DIGITAL_RIGHT pros::E_CONTROLLER_DIGITAL_RIGHT
#define DIGITAL_X pros::E_CONTROLLER_DIGITAL_X
#define DIGITAL_B pros::E_CONTROLLER_DIGITAL_B
#define DIGITAL_Y pros::E_CONTROLLER_DIGITAL_Y
#define DIGITAL_A pros::E_CONTROLLER_DIGITAL_A
#else
#define CONTROLLER_MASTER E_CONTROLLER_MASTER
#define CONTROLLER_PARTNER E_CONTROLLER_PARTNER
#define ANALOG_LEFT_X E_CONTROLLER_ANALOG_LEFT_X
#define ANALOG_LEFT_Y E_CONTROLLER_ANALOG_LEFT_Y
#define ANALOG_RIGHT_X E_CONTROLLER_ANALOG_RIGHT_X
#define ANALOG_RIGHT_Y E_CONTROLLER_ANALOG_RIGHT_Y
#define DIGITAL_L1 E_CONTROLLER_DIGITAL_L1
#define DIGITAL_L2 E_CONTROLLER_DIGITAL_L2
#define DIGITAL_R1 E_CONTROLLER_DIGITAL_R1
#define DIGITAL_R2 E_CONTROLLER_DIGITAL_R2
#define DIGITAL_UP E_CONTROLLER_DIGITAL_UP
#define DIGITAL_DOWN E_CONTROLLER_DIGITAL_DOWN
#define DIGITAL_LEFT E_CONTROLLER_DIGITAL_LEFT
#define DIGITAL_RIGHT E_CONTROLLER_DIGITAL_RIGHT
#define DIGITAL_X E_CONTROLLER_DIGITAL_X
#define DIGITAL_B E_CONTROLLER_DIGITAL_B
#define DIGITAL_Y E_CONTROLLER_DIGITAL_Y
#define DIGITAL_A E_CONTROLLER_DIGITAL_A
#endif
#endif

/**
 * \def Given an id and a port, this macro sets the port variable based on the id and allows the mutex to take that
 * port.
 *
 * \returns error (in the function/scope it's in) if the controller failed to connect or an invalid id is given.
 */
#define CONTROLLER_PORT_MUTEX_TAKE(id, port) \
    switch (id) {                              \
        case E_CONTROLLER_MASTER:                \
            port = V5_PORT_CONTROLLER_1;           \
            break;                                 \
        case E_CONTROLLER_PARTNER:               \
            port = V5_PORT_CONTROLLER_2;           \
            break;                                 \
        default:                                 \
            errno = EINVAL;                        \
            return PROS_ERR;                       \
    }                                          \
    if (!internal_port_mutex_take(port)) {     \
        errno = EACCES;                          \
        return PROS_ERR;                         \
    }

#ifdef __cplusplus
namespace c {
#endif

/**
 * Checks if the controller is connected.
 *
 * This function uses the following values of errno when an error state is
 * reached:
 * EINVAL - A value other than E_CONTROLLER_MASTER or E_CONTROLLER_PARTNER is
 * given.
 * EACCES - Another resource is currently trying to access the controller port.
 *
 * \param id
 *        The ID of the controller (e.g. the master or partner controller).
 *        Must be one of CONTROLLER_MASTER or CONTROLLER_PARTNER
 *
 * \return 1 if the controller is connected, 0 otherwise
 *
 * \b Example
 * \code
 * void initialize() {
 *   if (competition_is_connected()) {
 *     // Field Control is Connected
 *     // Run LCD Selector code or similar
 *   }
 * }
 * 

int32_t controller_get_analog(controller_id_e_t id, controller_analog_e_t channel)
#include <pros/misc.h>

Gets the value of an analog channel (joystick) on a controller.

Parameters
id The ID of the controller (e.g. the master or partner controller). Must be one of CONTROLLER_MASTER or CONTROLLER_PARTNER
channel The analog channel to get. Must be one of ANALOG_LEFT_X, ANALOG_LEFT_Y, ANALOG_RIGHT_X, ANALOG_RIGHT_Y
Returns The current reading of the analog channel: [-127, 127]. If the controller was not connected, then 0 is returned

This function uses the following values of errno when an error state is reached: EINVAL - A value other than E_CONTROLLER_MASTER or E_CONTROLLER_PARTNER is given. EACCES - Another resource is currently trying to access the controller port.

Example

void opcontrol() {
  while (true) {
    motor_move(1, controller_get_analog(E_CONTROLLER_MASTER, E_CONTROLLER_ANALOG_LEFT_Y));
    delay(2);
  }
}

int32_t controller_get_battery_capacity(controller_id_e_t id)
#include <pros/misc.h>

Gets the battery capacity of the given controller.

Parameters
id The ID of the controller (e.g. the master or partner controller). Must be one of E_CONTROLLER_MASTER or E_CONTROLLER_PARTNER
Returns The controller's battery capacity

This function uses the following values of errno when an error state is reached: EINVAL - A value other than E_CONTROLLER_MASTER or E_CONTROLLER_PARTNER is given. EACCES - Another resource is currently trying to access the controller port.

Example

void initialize() {
  printf("Battery Capacity: %d\n", controller_get_battery_capacity(E_CONTROLLER_MASTER));
}

int32_t controller_get_battery_level(controller_id_e_t id)
#include <pros/misc.h>

Gets the battery level of the given controller.

Parameters
id The ID of the controller (e.g. the master or partner controller). Must be one of E_CONTROLLER_MASTER or E_CONTROLLER_PARTNER
Returns The controller's battery level

This function uses the following values of errno when an error state is reached: EINVAL - A value other than E_CONTROLLER_MASTER or E_CONTROLLER_PARTNER is given. EACCES - Another resource is currently trying to access the controller port.

Example

void initialize() {
  printf("Battery Level: %d\n", controller_get_battery_level(E_CONTROLLER_MASTER));
}

int32_t controller_get_digital(controller_id_e_t id, controller_digital_e_t button)
#include <pros/misc.h>

Checks if a digital channel (button) on the controller is currently pressed.

Parameters
id The ID of the controller (e.g. the master or partner controller). Must be one of CONTROLLER_MASTER or CONTROLLER_PARTNER
button The button to read. Must be one of DIGITAL_{RIGHT,DOWN,LEFT,UP,A,B,Y,X,R1,R2,L1,L2}
Returns 1 if the button on the controller is pressed. If the controller was not connected, then 0 is returned

This function uses the following values of errno when an error state is reached: EINVAL - A value other than E_CONTROLLER_MASTER or E_CONTROLLER_PARTNER is given. EACCES - Another resource is currently trying to access the controller port.

Example

void opcontrol() {
  while (true) {
  if (controller_get_digital(E_CONTROLLER_MASTER, E_CONTROLLER_DIGITAL_A)) {
    motor_set(1, 100);
  }
  else {
    motor_set(1, 0);
  }
    delay(2);
  }
}

int32_t controller_get_digital_new_press(controller_id_e_t id, controller_digital_e_t button)
#include <pros/misc.h>

Returns a rising-edge case for a controller button press.

Parameters
id The ID of the controller (e.g. the master or partner controller). Must be one of CONTROLLER_MASTER or CONTROLLER_PARTNER
button The button to read. Must be one of DIGITAL_{RIGHT,DOWN,LEFT,UP,A,B,Y,X,R1,R2,L1,L2}
Returns 1 if the button on the controller is pressed and had not been pressed the last time this function was called, 0 otherwise.

This function is not thread-safe. Multiple tasks polling a single button may return different results under the same circumstances, so only one task should call this function for any given button. E.g., Task A calls this function for buttons 1 and 2. Task B may call this function for button 3, but should not for buttons 1 or 2. A typical use-case for this function is to call inside opcontrol to detect new button presses, and not in any other tasks.

This function uses the following values of errno when an error state is reached: EINVAL - A value other than E_CONTROLLER_MASTER or E_CONTROLLER_PARTNER is given. EACCES - Another resource is currently trying to access the controller port.

Example

void opcontrol() {
while (true) {
  if (controller_get_digital_new_press(E_CONTROLLER_MASTER, E_CONTROLLER_DIGITAL_A)) {
    // Toggle pneumatics or other similar actions
  }

  delay(2);
  }
}

int32_t controller_print(controller_id_e_t id, uint8_t line, uint8_t col, const char* fmt, ...)
#include <pros/misc.h>

Sets text to the controller LCD screen.

Parameters
id The ID of the controller (e.g. the master or partner controller). Must be one of CONTROLLER_MASTER or CONTROLLER_PARTNER
line The line number at which the text will be displayed [0-2]
col The column number at which the text will be displayed [0-14]
fmt The format string to print to the controller
Returns 1 if the operation was successful or PROS_ERR if the operation failed, setting errno.

This function uses the following values of errno when an error state is reached: EINVAL - A value other than E_CONTROLLER_MASTER or E_CONTROLLER_PARTNER is given. EACCES - Another resource is currently trying to access the controller port. EAGAIN - Could not send the text to the controller.

Example

void opcontrol() {
  int count = 0;
  while (true) {
  if (!(count % 25)) {
    // Only print every 50ms, the controller text update rate is slow
    controller_print(E_CONTROLLER_MASTER, 0, 0, "Counter: %d", count);
  }
    count++;
    delay(2);
  }
}

int32_t controller_set_text(controller_id_e_t id, uint8_t line, uint8_t col, const char* str)
#include <pros/misc.h>

Sets text to the controller LCD screen.

Parameters
id The ID of the controller (e.g. the master or partner controller). Must be one of CONTROLLER_MASTER or CONTROLLER_PARTNER
line The line number at which the text will be displayed [0-2]
col The column number at which the text will be displayed [0-14]
str The pre-formatted string to print to the controller
Returns 1 if the operation was successful or PROS_ERR if the operation failed, setting errno.

This function uses the following values of errno when an error state is reached: EINVAL - A value other than E_CONTROLLER_MASTER or E_CONTROLLER_PARTNER is given. EACCES - Another resource is currently trying to access the controller port. EAGAIN - Could not send the text to the controller.

Example

void opcontrol() {
  int count = 0;
  while (true) {
    if (!(count % 25)) {
      // Only print every 50ms, the controller text update rate is slow
      controller_set_text(E_CONTROLLER_MASTER, 0, 0, "Example text");
    }
  count++;
  delay(2);
  }
}

int32_t controller_clear_line(controller_id_e_t id, uint8_t line)
#include <pros/misc.h>

Clears an individual line of the controller screen.

Parameters
id The ID of the controller (e.g. the master or partner controller). Must be one of CONTROLLER_MASTER or CONTROLLER_PARTNER
line The line number to clear [0-2]
Returns 1 if the operation was successful or PROS_ERR if the operation failed, setting errno.

This function uses the following values of errno when an error state is reached: EINVAL - A value other than E_CONTROLLER_MASTER or E_CONTROLLER_PARTNER is given. EACCES - Another resource is currently trying to access the controller port.

Example

void opcontrol() {
  controller_set_text(E_CONTROLLER_MASTER, 0, 0, "Example");
  delay(100);
  controller_clear_line(E_CONTROLLER_MASTER, 0);
}

int32_t controller_clear(controller_id_e_t id)
#include <pros/misc.h>

Clears all of the lines on the controller screen.

Parameters
id The ID of the controller (e.g. the master or partner controller). Must be one of CONTROLLER_MASTER or CONTROLLER_PARTNER
Returns 1 if the operation was successful or PROS_ERR if the operation failed, setting errno.

This function uses the following values of errno when an error state is reached: EINVAL - A value other than E_CONTROLLER_MASTER or E_CONTROLLER_PARTNER is given. EACCES - Another resource is currently trying to access the controller port. EAGAIN - Could not send the text to the controller.

Example

void opcontrol() {
  controller_set_text(E_CONTROLLER_MASTER, 0, 0, "Example");
  delay(100);
  controller_clear(E_CONTROLLER_MASTER);
}

int32_t controller_rumble(controller_id_e_t id, const char* rumble_pattern)
#include <pros/misc.h>

Rumble the controller.

Parameters
id The ID of the controller (e.g. the master or partner controller). Must be one of CONTROLLER_MASTER or CONTROLLER_PARTNER
rumble_pattern A string consisting of the characters '.', '-', and ' ', where dots are short rumbles, dashes are long rumbles, and spaces are pauses. Maximum supported length is 8 characters.
Returns 1 if the operation was successful or PROS_ERR if the operation failed, setting errno.

This function uses the following values of errno when an error state is reached: EINVAL - A value other than E_CONTROLLER_MASTER or E_CONTROLLER_PARTNER is given. EACCES - Another resource is currently trying to access the controller port.

Example

void opcontrol() {
  int count = 0;
  while (true) {
  if (!(count % 25)) {
    // Only send every 50ms, the controller update rate is slow
    controller_rumble(E_CONTROLLER_MASTER, ". - . -");
  }
  count++;
  delay(2);
  }
}

int32_t battery_get_voltage(void)
#include <pros/misc.h>

Gets the current voltage of the battery, as reported by VEXos.

Returns The current voltage of the battery

This function uses the following values of errno when an error state is reached: EACCES - Another resource is currently trying to access the battery port.

Example

void initialize() {
  printf("Battery's Voltage: %d\n", battery_get_voltage());
}

int32_t battery_get_current(void)
#include <pros/misc.h>

Gets the current current of the battery, as reported by VEXos.

Returns The current current of the battery

This function uses the following values of errno when an error state is reached: EACCES - Another resource is currently trying to access the battery port.

Example

void initialize() {
  printf("Battery Current: %d\n", battery_get_current());
}

double battery_get_temperature(void)
#include <pros/misc.h>

Gets the current temperature of the battery, as reported by VEXos.

Returns The current temperature of the battery

This function uses the following values of errno when an error state is reached: EACCES - Another resource is currently trying to access the battery port.

Example

void initialize() {
  printf("Battery's Temperature: %d\n", battery_get_temperature());
}

double battery_get_capacity(void)
#include <pros/misc.h>

Gets the current capacity of the battery, as reported by VEXos.

Returns The current capacity of the battery

This function uses the following values of errno when an error state is reached: EACCES - Another resource is currently trying to access the battery port.

Example

void initialize() {
  printf("Battery Level: %d\n", battery_get_capacity());
}

int32_t usd_is_installed(void)
#include <pros/misc.h>

Checks if the SD card is installed.

Returns 1 if the SD card is installed, 0 otherwise

Example

void opcontrol() {
  printf("%i", usd_is_installed());
}

int32_t usd_list_files(const char* path, char* buffer, int32_t len)
#include <pros/misc.h>

Lists the files in a directory specified by the path Puts the list of file names (NOT DIRECTORIES) into the buffer seperated by newlines.

Returns 1 on success or PROS_ERR on failure setting errno

This function uses the following values of errno when an error state is reached:

EIO - Hard error occured in the low level disk I/O layer EINVAL - file or directory is invalid, or length is invalid EBUSY - THe physical drinve cannot work ENOENT - cannot find the path or file EINVAL - the path name format is invalid EACCES - Access denied or directory full EEXIST - Access denied EROFS - SD card is write protected ENXIO - drive number is invalid or not a FAT32 drive ENOBUFS - drive has no work area ENFILE - too many open files

Example

void opcontrol() {
    char* test = (char*) malloc(128);
 pros::c::usd_list_files("/", test, 128);
 pros::delay(200);
 printf("%s\n", test); //Prints the file names in the root directory seperated by newlines
 pros::delay(100);
 pros::c::usd_list_files("/test", test, 128);
 pros::delay(200);
 printf("%s\n", test); //Prints the names of files in the folder named test seperated by newlines
 pros::delay(100);
}

Enum documentation

Variable documentation

const char* baked_date
#include <pros/misc.h>

Date and Time

const char* baked_time
#include <pros/misc.h>