Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Return proper error code for VDML functions that return floating point numbers #160

Merged
merged 3 commits into from
Sep 12, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 31 additions & 10 deletions include/vdml/vdml.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,32 +32,53 @@
* If a port isn't yet registered, it registered as a motor automatically.
* If a mutex cannot be taken, errno is set to EACCES (access denied) and
* returns.
*
* This and other similar macros should only be used in functions that return
* int32_t as PROS_ERR could be returned.
*
* \param port
* The V5 port number from 0-20
* \param device_type
* The v5_device_e_t that the port is configured as
* \param error_code
* The error code that return if error checking failed
*/
#define claim_port(port, device_type) \
#define claim_port(port, device_type, error_code) \
if (!VALIDATE_PORT_NO(port)) { \
errno = ENXIO; \
return PROS_ERR; \
return error_code; \
} \
if (registry_validate_binding(port, device_type) != 0) { \
return PROS_ERR; \
return error_code; \
} \
v5_smart_device_s_t* device = registry_get_device(port); \
if (!port_mutex_take(port)) { \
errno = EACCES; \
return PROS_ERR; \
return error_code; \
}

/**
* A function that executes claim_port for functions that do not return an
* int32_t
* Function like claim_port. This macro should only be used in functions
* that return int32_t or enums as PROS_ERR could be returned.
*
* \param port
* The V5 port number from 0-20
* \param device_type
* The v5_device_e_t that the port is configured as
*/
#define claim_port_i(port, device_type) claim_port(port, device_type, PROS_ERR)

/**
* Function like claim_port. This macro should only be used in functions
* that return double or float as PROS_ERR_F could be returned.
*
* \param port
* The V5 port number from 0-20
* \param device_type
* The v5_device_e_t that the port is configured as
*/
#define claim_port_f(port, device_type) claim_port(port, device_type, PROS_ERR_F)

/**
* A function that executes claim_port and allows you to execute a block of
* code if an error occurs.
*
* This function uses the following values of errno when an error state is
* reached:
Expand All @@ -69,7 +90,7 @@
* \param device_type
* The v5_device_e_t that the port is configured as
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* \return 1 if the operation was successful or 0 if the operation
* failed, setting errno.
*/
int32_t claim_port_try(uint8_t port, v5_device_e_t type);
Expand Down
12 changes: 1 addition & 11 deletions src/devices/vdml.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,7 @@ extern void registry_init();
extern void port_mutex_init();

int32_t claim_port_try(uint8_t port, v5_device_e_t type) {
Jerrylum marked this conversation as resolved.
Show resolved Hide resolved
if (!VALIDATE_PORT_NO(port)) {
errno = ENXIO;
return PROS_ERR;
}
if (registry_validate_binding(port, type) != 0) {
return PROS_ERR;
}
if (!port_mutex_take(port)) {
errno = EACCES;
return PROS_ERR;
}
claim_port(port, type, 0);
Jerrylum marked this conversation as resolved.
Show resolved Hide resolved
return 1;
}

Expand Down
50 changes: 25 additions & 25 deletions src/devices/vdml_adi.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,35 +93,35 @@ typedef union adi_data {

adi_port_config_e_t adi_port_get_config(uint8_t port) {
transform_adi_port(port);
claim_port(INTERNAL_ADI_PORT, E_DEVICE_ADI);
claim_port_i(INTERNAL_ADI_PORT, E_DEVICE_ADI);
adi_port_config_e_t rtn = (adi_port_config_e_t)vexDeviceAdiPortConfigGet(device->device_info, port);
return_port(INTERNAL_ADI_PORT, rtn);
}

int32_t adi_port_get_value(uint8_t port) {
transform_adi_port(port);
claim_port(INTERNAL_ADI_PORT, E_DEVICE_ADI);
claim_port_i(INTERNAL_ADI_PORT, E_DEVICE_ADI);
int32_t rtn = vexDeviceAdiValueGet(device->device_info, port);
return_port(INTERNAL_ADI_PORT, rtn);
}

int32_t adi_port_set_config(uint8_t port, adi_port_config_e_t type) {
transform_adi_port(port);
claim_port(INTERNAL_ADI_PORT, E_DEVICE_ADI);
claim_port_i(INTERNAL_ADI_PORT, E_DEVICE_ADI);
vexDeviceAdiPortConfigSet(device->device_info, port, (V5_AdiPortConfiguration)type);
return_port(INTERNAL_ADI_PORT, 1);
}

int32_t adi_port_set_value(uint8_t port, int32_t value) {
transform_adi_port(port);
claim_port(INTERNAL_ADI_PORT, E_DEVICE_ADI);
claim_port_i(INTERNAL_ADI_PORT, E_DEVICE_ADI);
vexDeviceAdiValueSet(device->device_info, port, value);
return_port(INTERNAL_ADI_PORT, 1);
}

int32_t adi_analog_calibrate(uint8_t port) {
transform_adi_port(port);
claim_port(INTERNAL_ADI_PORT, E_DEVICE_ADI);
claim_port_i(INTERNAL_ADI_PORT, E_DEVICE_ADI);
validate_type(device, port, E_ADI_ANALOG_IN);
uint32_t total = 0;
for (uint32_t i = 0; i < 512; i++) {
Expand All @@ -135,15 +135,15 @@ int32_t adi_analog_calibrate(uint8_t port) {

int32_t adi_analog_read(uint8_t port) {
transform_adi_port(port);
claim_port(INTERNAL_ADI_PORT, E_DEVICE_ADI);
claim_port_i(INTERNAL_ADI_PORT, E_DEVICE_ADI);
validate_type(device, port, E_ADI_ANALOG_IN);
int32_t rtn = vexDeviceAdiValueGet(device->device_info, port);
return_port(INTERNAL_ADI_PORT, rtn);
}

int32_t adi_analog_read_calibrated(uint8_t port) {
transform_adi_port(port);
claim_port(INTERNAL_ADI_PORT, E_DEVICE_ADI);
claim_port_i(INTERNAL_ADI_PORT, E_DEVICE_ADI);
validate_type(device, port, E_ADI_ANALOG_IN);
adi_data_s_t* const adi_data = &((adi_data_s_t*)(device->pad))[port];
int32_t rtn = (vexDeviceAdiValueGet(device->device_info, port) - (adi_data->analog_data.calib >> 4));
Expand All @@ -152,7 +152,7 @@ int32_t adi_analog_read_calibrated(uint8_t port) {

int32_t adi_analog_read_calibrated_HR(uint8_t port) {
transform_adi_port(port);
claim_port(INTERNAL_ADI_PORT, E_DEVICE_ADI);
claim_port_i(INTERNAL_ADI_PORT, E_DEVICE_ADI);
validate_type(device, port, E_ADI_ANALOG_IN);
adi_data_s_t* const adi_data = &((adi_data_s_t*)(device->pad))[port];
int32_t rtn = ((vexDeviceAdiValueGet(device->device_info, port) << 4) - adi_data->analog_data.calib);
Expand All @@ -161,15 +161,15 @@ int32_t adi_analog_read_calibrated_HR(uint8_t port) {

int32_t adi_digital_read(uint8_t port) {
transform_adi_port(port);
claim_port(INTERNAL_ADI_PORT, E_DEVICE_ADI);
claim_port_i(INTERNAL_ADI_PORT, E_DEVICE_ADI);
validate_type(device, port, E_ADI_DIGITAL_IN);
int32_t rtn = vexDeviceAdiValueGet(device->device_info, port);
return_port(INTERNAL_ADI_PORT, rtn);
}

int32_t adi_digital_get_new_press(uint8_t port) {
transform_adi_port(port);
claim_port(INTERNAL_ADI_PORT, E_DEVICE_ADI);
claim_port_i(INTERNAL_ADI_PORT, E_DEVICE_ADI);
validate_type(device, port, E_ADI_DIGITAL_IN);

adi_data_s_t* const adi_data = &((adi_data_s_t*)(device->pad))[port];
Expand All @@ -189,7 +189,7 @@ int32_t adi_digital_get_new_press(uint8_t port) {

int32_t adi_digital_write(uint8_t port, const bool value) {
transform_adi_port(port);
claim_port(INTERNAL_ADI_PORT, E_DEVICE_ADI);
claim_port_i(INTERNAL_ADI_PORT, E_DEVICE_ADI);
validate_type(device, port, E_ADI_DIGITAL_OUT);
vexDeviceAdiValueSet(device->device_info, port, (int32_t)value);
return_port(INTERNAL_ADI_PORT, 1);
Expand Down Expand Up @@ -218,7 +218,7 @@ int32_t adi_pin_mode(uint8_t port, uint8_t mode) {

int32_t adi_motor_set(uint8_t port, int8_t speed) {
transform_adi_port(port);
claim_port(INTERNAL_ADI_PORT, E_DEVICE_ADI);
claim_port_i(INTERNAL_ADI_PORT, E_DEVICE_ADI);
validate_motor(device, port);
if (speed > ADI_MOTOR_MAX_SPEED)
speed = ADI_MOTOR_MAX_SPEED;
Expand All @@ -230,7 +230,7 @@ int32_t adi_motor_set(uint8_t port, int8_t speed) {

int32_t adi_motor_get(uint8_t port) {
transform_adi_port(port);
claim_port(INTERNAL_ADI_PORT, E_DEVICE_ADI);
claim_port_i(INTERNAL_ADI_PORT, E_DEVICE_ADI);
validate_motor(device, port);
int32_t rtn = vexDeviceAdiValueGet(device->device_info, port) - ADI_MOTOR_MAX_SPEED;
return_port(INTERNAL_ADI_PORT, rtn);
Expand All @@ -243,7 +243,7 @@ int32_t adi_motor_stop(uint8_t port) {
adi_encoder_t adi_encoder_init(uint8_t port_top, uint8_t port_bottom, const bool reverse) {
transform_adi_port(port_top);
transform_adi_port(port_bottom);
claim_port(INTERNAL_ADI_PORT, E_DEVICE_ADI);
claim_port_i(INTERNAL_ADI_PORT, E_DEVICE_ADI);
validate_twowire(port_top, port_bottom);

adi_data_s_t* const adi_data = &((adi_data_s_t*)(device->pad))[port];
Expand All @@ -254,7 +254,7 @@ adi_encoder_t adi_encoder_init(uint8_t port_top, uint8_t port_bottom, const bool

int32_t adi_encoder_get(adi_encoder_t enc) {
transform_adi_port(enc);
claim_port(INTERNAL_ADI_PORT, E_DEVICE_ADI);
claim_port_i(INTERNAL_ADI_PORT, E_DEVICE_ADI);
validate_type(device, enc, E_ADI_LEGACY_ENCODER);

int32_t rtn;
Expand All @@ -266,7 +266,7 @@ int32_t adi_encoder_get(adi_encoder_t enc) {

int32_t adi_encoder_reset(adi_encoder_t enc) {
transform_adi_port(enc);
claim_port(INTERNAL_ADI_PORT, E_DEVICE_ADI);
claim_port_i(INTERNAL_ADI_PORT, E_DEVICE_ADI);
validate_type(device, enc, E_ADI_LEGACY_ENCODER);

vexDeviceAdiValueSet(device->device_info, enc, 0);
Expand All @@ -275,7 +275,7 @@ int32_t adi_encoder_reset(adi_encoder_t enc) {

int32_t adi_encoder_shutdown(adi_encoder_t enc) {
transform_adi_port(enc);
claim_port(INTERNAL_ADI_PORT, E_DEVICE_ADI);
claim_port_i(INTERNAL_ADI_PORT, E_DEVICE_ADI);
validate_type(device, enc, E_ADI_LEGACY_ENCODER);

vexDeviceAdiPortConfigSet(device->device_info, enc, E_ADI_TYPE_UNDEFINED);
Expand All @@ -291,14 +291,14 @@ adi_ultrasonic_t adi_ultrasonic_init(uint8_t port_ping, uint8_t port_echo) {
return PROS_ERR;
}

claim_port(INTERNAL_ADI_PORT, E_DEVICE_ADI);
claim_port_i(INTERNAL_ADI_PORT, E_DEVICE_ADI);
vexDeviceAdiPortConfigSet(device->device_info, port, E_ADI_LEGACY_ULTRASONIC);
return_port(INTERNAL_ADI_PORT, port + 1);
}

int32_t adi_ultrasonic_get(adi_ultrasonic_t ult) {
transform_adi_port(ult);
claim_port(INTERNAL_ADI_PORT, E_DEVICE_ADI);
claim_port_i(INTERNAL_ADI_PORT, E_DEVICE_ADI);
validate_type(device, ult, E_ADI_LEGACY_ULTRASONIC);

int32_t rtn = vexDeviceAdiValueGet(device->device_info, ult);
Expand All @@ -307,7 +307,7 @@ int32_t adi_ultrasonic_get(adi_ultrasonic_t ult) {

int32_t adi_ultrasonic_shutdown(adi_ultrasonic_t ult) {
transform_adi_port(ult);
claim_port(INTERNAL_ADI_PORT, E_DEVICE_ADI);
claim_port_i(INTERNAL_ADI_PORT, E_DEVICE_ADI);
validate_type(device, ult, E_ADI_LEGACY_ULTRASONIC);

vexDeviceAdiPortConfigSet(device->device_info, ult, E_ADI_TYPE_UNDEFINED);
Expand All @@ -316,7 +316,7 @@ int32_t adi_ultrasonic_shutdown(adi_ultrasonic_t ult) {

adi_gyro_t adi_gyro_init(uint8_t port, double multiplier) {
transform_adi_port(port);
claim_port(INTERNAL_ADI_PORT, E_DEVICE_ADI);
claim_port_i(INTERNAL_ADI_PORT, E_DEVICE_ADI);

if (multiplier == 0) multiplier = 1;
adi_data_s_t* const adi_data = &((adi_data_s_t*)(device->pad))[port];
Expand All @@ -340,10 +340,10 @@ adi_gyro_t adi_gyro_init(uint8_t port, double multiplier) {
return_port(INTERNAL_ADI_PORT, port + 1);
}

// Internal wrapper for adi_gyro_get to get around transform_adi_port, claim_port, validate_type and return_port possibly returning PROS_ERR, not PROS_ERR_F
// Internal wrapper for adi_gyro_get to get around transform_adi_port, claim_port_i, validate_type and return_port possibly returning PROS_ERR, not PROS_ERR_F
int32_t _adi_gyro_get(adi_gyro_t gyro, double* out) {
transform_adi_port(gyro);
claim_port(INTERNAL_ADI_PORT, E_DEVICE_ADI);
claim_port_i(INTERNAL_ADI_PORT, E_DEVICE_ADI);
validate_type(device, gyro, E_ADI_LEGACY_GYRO);

double rtn = (double)vexDeviceAdiValueGet(device->device_info, gyro);
Expand All @@ -362,7 +362,7 @@ double adi_gyro_get(adi_gyro_t gyro) {

int32_t adi_gyro_reset(adi_gyro_t gyro) {
transform_adi_port(gyro);
claim_port(INTERNAL_ADI_PORT, E_DEVICE_ADI);
claim_port_i(INTERNAL_ADI_PORT, E_DEVICE_ADI);
validate_type(device, gyro, E_ADI_LEGACY_GYRO);

adi_data_s_t* const adi_data = &((adi_data_s_t*)(device->pad))[gyro];
Expand All @@ -372,7 +372,7 @@ int32_t adi_gyro_reset(adi_gyro_t gyro) {

int32_t adi_gyro_shutdown(adi_gyro_t gyro) {
transform_adi_port(gyro);
claim_port(INTERNAL_ADI_PORT, E_DEVICE_ADI);
claim_port_i(INTERNAL_ADI_PORT, E_DEVICE_ADI);
validate_type(device, gyro, E_ADI_LEGACY_GYRO);

vexDeviceAdiPortConfigSet(device->device_info, gyro, E_ADI_TYPE_UNDEFINED);
Expand Down
Loading