VEX Generic Device C++ API (For Advanced Users) module

Contents

Classes

class pros::v5::Device

Functions

Device(const std::uint8_t port) explicit
Creates a Device object.
std::uint8_t get_port(void) const
Gets the port number of the Smart Device.
bool is_installed() virtual
Checks if the device is installed.
pros::DeviceType get_plugged_type() const
Gets the type of device.
static pros::DeviceType get_plugged_type(std::uint8_t port)
Gets the type of device on a given port.
static std::vector<Device> get_all_devices(pros::DeviceType device_type = pros::DeviceType::undefined)
Gets all devices of a given device type.
Device(const std::uint8_t port, const enum DeviceType deviceType) private
Creates a Device object.

Enums

enum class DeviceType { none = 0, motor = 2, rotation = 4, imu = 6, distance = 7, radio = 8, vision = 11, adi = 12, optical = 16, gps = 20, serial = 129, undefined = 255 }
enum class DeviceType { none = 0, motor = 2, rotation = 4, imu = 6, distance = 7, radio = 8, vision = 11, adi = 12, optical = 16, gps = 20, serial = 129, undefined = 255 }

Variables

const std::uint8_t _port
enum DeviceType _deviceType

Function documentation

Device(const std::uint8_t port) explicit

Creates a Device object.

Parameters
port The V5 port number from 1-21

Example

#define DEVICE_PORT 1

void opcontrol() {
  Device device(DEVICE_PORT);
}

std::uint8_t get_port(void) const

Gets the port number of the Smart Device.

Returns The smart device's port number.

Example

void opcontrol() {
#define DEVICE_PORT 1
  while (true) {
   Device device(DEVICE_PORT);
    printf("device plugged type: {port: %d}\n", device.get_port());
    delay(20);
  }
}

bool is_installed() virtual

Checks if the device is installed.

Returns true if the corresponding device is installed, false otherwise. Example
#define DEVICE_PORT 1

void opcontrol() {
 Device device(DEVICE_PORT);
  while (true) {
    printf("device plugged type: {is_installed: %d}\n", device.is_installed());
    delay(20);
  }
}

pros::DeviceType get_plugged_type() const

Gets the type of device.

Returns The device type as an enum.

This function uses the following values of errno when an error state is reached: EACCES - Mutex of port cannot be taken (access denied).

Example

#define DEVICE_PORT 1

void opcontrol() {
 Device device(DEVICE_PORT);
  while (true) { 
    DeviceType dt = device.get_plugged_type();
    printf("device plugged type: {plugged type: %d}\n", dt);
    delay(20);
  }
}

static pros::DeviceType get_plugged_type(std::uint8_t port)

Gets the type of device on a given port.

Parameters
port The V5 port number from 1-21
Returns The device type as an enum.

This function uses the following values of errno when an error state is reached: EACCES - Mutex of port cannot be taken (access denied).

Example

#define DEVICE_PORT 1

void opcontrol() {
  while (true) { 
    DeviceType dt = pros::Device::get_plugged_type(DEVICE_PORT);
    printf("device plugged type: {plugged type: %d}\n", dt);
    delay(20);
  }
}

static std::vector<Device> get_all_devices(pros::DeviceType device_type = pros::DeviceType::undefined)

Gets all devices of a given device type.

Parameters
device_type The pros::DeviceType enum that matches the type of device desired.
Returns A vector of Device objects for the given device type.

Example

void opcontrol() {
  std::vector<Device> motor_devices = pros::Device::get_all_devices(pros::DeviceType::motor);  // All Device objects are motors
}

Device(const std::uint8_t port, const enum DeviceType deviceType) private

Creates a Device object.

Parameters
port The V5 port number from 1-21
deviceType The type of the constructed device

Enum documentation

enum class DeviceType

Enumerators
none

No device is plugged into the port.

motor

A motor is plugged into the port.

rotation

A rotation sensor is plugged into the port.

imu

An inertial sensor is plugged into the port.

distance

A distance sensor is plugged into the port.

radio

A radio is plugged into the port.

vision

A vision sensor is plugged into the port.

adi

This port is an ADI expander.

optical

An optical sensor is plugged into the port.

gps

A GPS sensor is plugged into the port.

serial

A serial device is plugged into the port.

undefined

The device type is not defined, or is not a valid device.

enum class DeviceType

Enumerators
none

No device is plugged into the port.

motor

A motor is plugged into the port.

rotation

A rotation sensor is plugged into the port.

imu

An inertial sensor is plugged into the port.

distance

A distance sensor is plugged into the port.

radio

A radio is plugged into the port.

vision

A vision sensor is plugged into the port.

adi

This port is an ADI expander.

optical

An optical sensor is plugged into the port.

gps

A GPS sensor is plugged into the port.

serial

A serial device is plugged into the port.

undefined

The device type is not defined, or is not a valid device.

Variable documentation

const std::uint8_t _port

enum DeviceType _deviceType