From ef7b4721251ba519443909aa6bb9a3cc18f0063c Mon Sep 17 00:00:00 2001 From: Liam Teale Date: Sun, 8 Jun 2025 13:59:45 -0700 Subject: [PATCH 1/3] add more device types to DeviceType enum --- include/pros/devices/devices.hpp | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/include/pros/devices/devices.hpp b/include/pros/devices/devices.hpp index b039ad0e..2ed0758a 100644 --- a/include/pros/devices/devices.hpp +++ b/include/pros/devices/devices.hpp @@ -4,8 +4,26 @@ namespace zest { +/** + * @brief device type enum. Contains all the device types compatible with ZestCode + * + */ enum class DeviceType { - Battery, + AdiExpander, + AiVision, + Bumper, + Controller, + Distance, + Gps, + Imu, + Motor, + Optical, + Radio, + Rotation, + Serial, + Vision, + None, + Unknown, }; /** From e3a5eaabdea8ac83f11af35c6defeab9d08c3cab Mon Sep 17 00:00:00 2001 From: Liam Teale Date: Sun, 8 Jun 2025 14:41:57 -0700 Subject: [PATCH 2/3] add get_device_type function --- include/common/result.hpp | 14 -------- include/pros/devices/devices.hpp | 18 ++++++++--- src/devices/devices.cpp | 55 ++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 19 deletions(-) create mode 100644 src/devices/devices.cpp diff --git a/include/common/result.hpp b/include/common/result.hpp index bdc9f3a3..799c0c5b 100644 --- a/include/common/result.hpp +++ b/include/common/result.hpp @@ -41,20 +41,6 @@ class ResultError { std::optional runtime_data; }; -/** - * @brief Unknown Error - * - */ -class UnknownError : public ResultError { - public: - template - requires std::convertible_to - UnknownError(T&& message) - : message(std::forward(message)) {} - - std::string message; -}; - /** * @brief Trait to define a "sentinel" value for types indicating an error state. * @tparam T Type to provide a sentinel value for. diff --git a/include/pros/devices/devices.hpp b/include/pros/devices/devices.hpp index 2ed0758a..070aecf3 100644 --- a/include/pros/devices/devices.hpp +++ b/include/pros/devices/devices.hpp @@ -1,6 +1,7 @@ #pragma once #include "common/result.hpp" +#include "pros/devices/port.hpp" namespace zest { @@ -22,15 +23,24 @@ enum class DeviceType { Rotation, Serial, Vision, + Invalid, None, Unknown, }; +/** + * @brief Get the type of the device connected to the given smart port + * + * @param port + * @return DeviceType + */ +DeviceType get_device_type(SmartPort port); + /** * @brief V5 Port Mismatch Error * */ -class V5PortMismatchError : public ResultError { +class SmartPortError : public ResultError { public: /** * @brief Construct a new V5 Port Mismatch Error object @@ -38,11 +48,9 @@ class V5PortMismatchError : public ResultError { * @param expected the device expected to be on the port * @param actual the device that is actually on the port */ - V5PortMismatchError(DeviceType expected, DeviceType actual) - : expected(expected), - actual(actual) {} + SmartPortError(DeviceType expected, std::optional actual = std::nullopt); DeviceType expected; - DeviceType actual; + std::optional actual; }; } // namespace zest \ No newline at end of file diff --git a/src/devices/devices.cpp b/src/devices/devices.cpp new file mode 100644 index 00000000..0f31dfd3 --- /dev/null +++ b/src/devices/devices.cpp @@ -0,0 +1,55 @@ +#include "pros/devices/devices.hpp" + +#include "v5_api_patched.h" +#include "v5_apitypes_patched.h" + +namespace zest { +DeviceType get_device_type(SmartPort port) { + // if the port number is greater than 21, return invalid + if (port.as_number() > 21) + return DeviceType::Invalid; + + // vexDeviceGetStatus writes a buffer of V5_DeviceType that is V5_MAX_DEVICE_PORTS long. + // This is the only way to get the device type on a port + std::array types; + vexDeviceGetStatus(types.data()); + + // get the device type on the given port + switch (types.at(port.as_index())) { + case kDeviceTypeNoSensor: + return DeviceType::None; + case kDeviceTypeMotorSensor: + return DeviceType::Motor; + case kDeviceTypeAbsEncSensor: + return DeviceType::Rotation; + case kDeviceTypeImuSensor: + return DeviceType::Imu; + case kDeviceTypeDistanceSensor: + return DeviceType::Distance; + case kDeviceTypeRadioSensor: + return DeviceType::Radio; + case kDeviceTypeTetherSensor: + return DeviceType::Controller; + case kDeviceTypeVisionSensor: + return DeviceType::Vision; + case kDeviceTypeAdiSensor: + return DeviceType::AdiExpander; + case kDeviceTypeOpticalSensor: + return DeviceType::Optical; + case kDeviceTypeGpsSensor: + return DeviceType::Gps; + case kDeviceTypeAiVisionSensor: + return DeviceType::AiVision; + case kDeviceTypeBumperSensor: + return DeviceType::Bumper; + case kDeviceTypeGenericSerial: + return DeviceType::Serial; + default: + return DeviceType::Unknown; + } +} + +SmartPortError::SmartPortError(DeviceType expected, std::optional actual) + : expected(expected), + actual(actual) {} +} // namespace zest \ No newline at end of file From e3d289978a3fa6cc0bff5a25accdecbd4fb03c8d Mon Sep 17 00:00:00 2001 From: Liam Teale Date: Tue, 26 Aug 2025 19:26:37 -0700 Subject: [PATCH 3/3] fix build errors --- include/pros/devices/devices.hpp | 18 ------------------ src/devices/devices.cpp | 6 ++---- 2 files changed, 2 insertions(+), 22 deletions(-) diff --git a/include/pros/devices/devices.hpp b/include/pros/devices/devices.hpp index 070aecf3..1cead666 100644 --- a/include/pros/devices/devices.hpp +++ b/include/pros/devices/devices.hpp @@ -1,6 +1,5 @@ #pragma once -#include "common/result.hpp" #include "pros/devices/port.hpp" namespace zest { @@ -36,21 +35,4 @@ enum class DeviceType { */ DeviceType get_device_type(SmartPort port); -/** - * @brief V5 Port Mismatch Error - * - */ -class SmartPortError : public ResultError { - public: - /** - * @brief Construct a new V5 Port Mismatch Error object - * - * @param expected the device expected to be on the port - * @param actual the device that is actually on the port - */ - SmartPortError(DeviceType expected, std::optional actual = std::nullopt); - - DeviceType expected; - std::optional actual; -}; } // namespace zest \ No newline at end of file diff --git a/src/devices/devices.cpp b/src/devices/devices.cpp index 0f31dfd3..fe7efbf8 100644 --- a/src/devices/devices.cpp +++ b/src/devices/devices.cpp @@ -3,6 +3,8 @@ #include "v5_api_patched.h" #include "v5_apitypes_patched.h" +#include + namespace zest { DeviceType get_device_type(SmartPort port) { // if the port number is greater than 21, return invalid @@ -48,8 +50,4 @@ DeviceType get_device_type(SmartPort port) { return DeviceType::Unknown; } } - -SmartPortError::SmartPortError(DeviceType expected, std::optional actual) - : expected(expected), - actual(actual) {} } // namespace zest \ No newline at end of file