Skip to content

Commit e3a5eaa

Browse files
committed
add get_device_type function
1 parent ef7b472 commit e3a5eaa

3 files changed

Lines changed: 68 additions & 19 deletions

File tree

include/common/result.hpp

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,6 @@ class ResultError {
4141
std::optional<RuntimeData> runtime_data;
4242
};
4343

44-
/**
45-
* @brief Unknown Error
46-
*
47-
*/
48-
class UnknownError : public ResultError {
49-
public:
50-
template<typename T>
51-
requires std::convertible_to<T, std::string>
52-
UnknownError(T&& message)
53-
: message(std::forward<T>(message)) {}
54-
55-
std::string message;
56-
};
57-
5844
/**
5945
* @brief Trait to define a "sentinel" value for types indicating an error state.
6046
* @tparam T Type to provide a sentinel value for.

include/pros/devices/devices.hpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include "common/result.hpp"
4+
#include "pros/devices/port.hpp"
45

56
namespace zest {
67

@@ -22,27 +23,34 @@ enum class DeviceType {
2223
Rotation,
2324
Serial,
2425
Vision,
26+
Invalid,
2527
None,
2628
Unknown,
2729
};
2830

31+
/**
32+
* @brief Get the type of the device connected to the given smart port
33+
*
34+
* @param port
35+
* @return DeviceType
36+
*/
37+
DeviceType get_device_type(SmartPort port);
38+
2939
/**
3040
* @brief V5 Port Mismatch Error
3141
*
3242
*/
33-
class V5PortMismatchError : public ResultError {
43+
class SmartPortError : public ResultError {
3444
public:
3545
/**
3646
* @brief Construct a new V5 Port Mismatch Error object
3747
*
3848
* @param expected the device expected to be on the port
3949
* @param actual the device that is actually on the port
4050
*/
41-
V5PortMismatchError(DeviceType expected, DeviceType actual)
42-
: expected(expected),
43-
actual(actual) {}
51+
SmartPortError(DeviceType expected, std::optional<DeviceType> actual = std::nullopt);
4452

4553
DeviceType expected;
46-
DeviceType actual;
54+
std::optional<DeviceType> actual;
4755
};
4856
} // namespace zest

src/devices/devices.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include "pros/devices/devices.hpp"
2+
3+
#include "v5_api_patched.h"
4+
#include "v5_apitypes_patched.h"
5+
6+
namespace zest {
7+
DeviceType get_device_type(SmartPort port) {
8+
// if the port number is greater than 21, return invalid
9+
if (port.as_number() > 21)
10+
return DeviceType::Invalid;
11+
12+
// vexDeviceGetStatus writes a buffer of V5_DeviceType that is V5_MAX_DEVICE_PORTS long.
13+
// This is the only way to get the device type on a port
14+
std::array<V5_DeviceType, V5_MAX_DEVICE_PORTS> types;
15+
vexDeviceGetStatus(types.data());
16+
17+
// get the device type on the given port
18+
switch (types.at(port.as_index())) {
19+
case kDeviceTypeNoSensor:
20+
return DeviceType::None;
21+
case kDeviceTypeMotorSensor:
22+
return DeviceType::Motor;
23+
case kDeviceTypeAbsEncSensor:
24+
return DeviceType::Rotation;
25+
case kDeviceTypeImuSensor:
26+
return DeviceType::Imu;
27+
case kDeviceTypeDistanceSensor:
28+
return DeviceType::Distance;
29+
case kDeviceTypeRadioSensor:
30+
return DeviceType::Radio;
31+
case kDeviceTypeTetherSensor:
32+
return DeviceType::Controller;
33+
case kDeviceTypeVisionSensor:
34+
return DeviceType::Vision;
35+
case kDeviceTypeAdiSensor:
36+
return DeviceType::AdiExpander;
37+
case kDeviceTypeOpticalSensor:
38+
return DeviceType::Optical;
39+
case kDeviceTypeGpsSensor:
40+
return DeviceType::Gps;
41+
case kDeviceTypeAiVisionSensor:
42+
return DeviceType::AiVision;
43+
case kDeviceTypeBumperSensor:
44+
return DeviceType::Bumper;
45+
case kDeviceTypeGenericSerial:
46+
return DeviceType::Serial;
47+
default:
48+
return DeviceType::Unknown;
49+
}
50+
}
51+
52+
SmartPortError::SmartPortError(DeviceType expected, std::optional<DeviceType> actual)
53+
: expected(expected),
54+
actual(actual) {}
55+
} // namespace zest

0 commit comments

Comments
 (0)