diff --git a/include/hardware/Encoder/ADIEncoder.hpp b/include/hardware/Encoder/ADIEncoder.hpp index 78059bd..63f3ecd 100644 --- a/include/hardware/Encoder/ADIEncoder.hpp +++ b/include/hardware/Encoder/ADIEncoder.hpp @@ -1,6 +1,7 @@ #pragma once #include "hardware/Encoder/Encoder.hpp" +#include "hardware/Port.hpp" #include "pros/adi.hpp" namespace lemlib { @@ -38,7 +39,7 @@ class ADIEncoder : public Encoder { * } * @endcode */ - ADIEncoder(std::uint8_t topPort, std::uint8_t bottomPort, bool reversed); + ADIEncoder(ADIPort topPort, ADIPort bottomPort, bool reversed); /** * @brief Construct a new Optical Shaft Encoder * @@ -57,7 +58,7 @@ class ADIEncoder : public Encoder { * } * @endcode */ - ADIEncoder(std::uint8_t expanderPort, std::uint8_t topPort, std::uint8_t bottomPort, bool reversed); + ADIEncoder(SmartPort expanderPort, ADIPort topPort, ADIPort bottomPort, bool reversed); /** * @brief whether the encoder is connected * diff --git a/include/hardware/Encoder/V5RotationSensor.hpp b/include/hardware/Encoder/V5RotationSensor.hpp index 3afc54e..d955dc3 100644 --- a/include/hardware/Encoder/V5RotationSensor.hpp +++ b/include/hardware/Encoder/V5RotationSensor.hpp @@ -1,6 +1,7 @@ #pragma once #include "hardware/Encoder/Encoder.hpp" +#include "hardware/Port.hpp" #include "pros/rotation.hpp" namespace lemlib { @@ -36,7 +37,7 @@ class V5RotationSensor : public Encoder { * } * @endcode */ - V5RotationSensor(std::int8_t port); + V5RotationSensor(ReversibleSmartPort port); /** * @brief Construct a new V5 Rotation Sensor * @@ -51,7 +52,7 @@ class V5RotationSensor : public Encoder { * } * @endcode */ - V5RotationSensor(std::uint8_t port, bool reversed); + V5RotationSensor(SmartPort port, bool reversed); /** * @brief whether the V5 Rotation Sensor is connected * diff --git a/include/hardware/Imu/V5InertialSensor.hpp b/include/hardware/Imu/V5InertialSensor.hpp index 5e68190..11f96c4 100644 --- a/include/hardware/Imu/V5InertialSensor.hpp +++ b/include/hardware/Imu/V5InertialSensor.hpp @@ -1,6 +1,7 @@ #pragma once #include "hardware/Imu/Imu.hpp" +#include "hardware/Port.hpp" #include "pros/imu.hpp" namespace lemlib { @@ -33,7 +34,7 @@ class V5InertialSensor : public Imu { * } * @endcode */ - V5InertialSensor(std::uint8_t port); + V5InertialSensor(SmartPort port); /** * @brief calibrate the V5 Inertial Sensor * diff --git a/include/hardware/Motor/Motor.hpp b/include/hardware/Motor/Motor.hpp index 49e577c..e47a43b 100644 --- a/include/hardware/Motor/Motor.hpp +++ b/include/hardware/Motor/Motor.hpp @@ -2,6 +2,7 @@ #include "pros/motors.hpp" #include "hardware/encoder/Encoder.hpp" +#include "hardware/Port.hpp" #include "units/Temperature.hpp" namespace lemlib { @@ -25,7 +26,7 @@ class Motor : public Encoder { * } * @endcode */ - Motor(int port, AngularVelocity outputVelocity); + Motor(ReversibleSmartPort port, AngularVelocity outputVelocity); /** * @brief Construct a new Motor object * @@ -42,7 +43,7 @@ class Motor : public Encoder { * } * @endcode */ - Motor(uint8_t port, bool reversed, AngularVelocity outputVelocity); + Motor(SmartPort port, bool reversed, AngularVelocity outputVelocity); /** * @brief Construct a new Motor object * diff --git a/include/hardware/Motor/MotorGroup.hpp b/include/hardware/Motor/MotorGroup.hpp index fced502..4115202 100644 --- a/include/hardware/Motor/MotorGroup.hpp +++ b/include/hardware/Motor/MotorGroup.hpp @@ -1,5 +1,6 @@ #include "pros/motor_group.hpp" #include "hardware/Motor/Motor.hpp" +#include "hardware/Port.hpp" #include namespace lemlib { @@ -33,7 +34,7 @@ class MotorGroup : Encoder { * } * @endcode */ - MotorGroup(std::initializer_list ports, AngularVelocity outputVelocity); + MotorGroup(std::initializer_list ports, AngularVelocity outputVelocity); /** * @brief Construct a new Motor Group * diff --git a/include/hardware/Port.hpp b/include/hardware/Port.hpp new file mode 100644 index 0000000..4d1e5b7 --- /dev/null +++ b/include/hardware/Port.hpp @@ -0,0 +1,86 @@ +#pragma once + +#include + +namespace lemlib { + +struct DynamicPort {}; + +constexpr DynamicPort runtime_check_port {}; + +class SmartPort { + public: + consteval SmartPort(std::int64_t port) + : m_port(port) { + // XXX: this doesnt actaully throw an exception since the ctor + // is consteval, it halts compilation instead + if (port < 1 || port > 21) throw "Port out of range!"; + } + + SmartPort(std::int64_t port, DynamicPort) + : m_port(port) { + if (port < 1 || port > 21) m_port = 0; + } + + std::uint8_t get_port() const { return m_port; } + + operator std::uint8_t() const { return m_port; } + private: + std::uint8_t m_port; +}; + +class ReversibleSmartPort { + public: + consteval ReversibleSmartPort(std::int64_t port) + : m_port(port < 0 ? -port : port), + m_reversed(port < 0) { + if (port < 0) port = -port; + // XXX: this doesnt actaully throw an exception since the ctor + // is consteval, it halts compilation instead + if (port < 1 || port > 21) throw "Port out of range!"; + } + + ReversibleSmartPort(std::int64_t port, DynamicPort) + : m_port(port < 0 ? -port : port), + m_reversed(port < 0) { + if (port < 0) port = -port; + if (port < 1 || port > 21) m_port = 0; + } + + std::uint8_t get_port() const { return m_port; } + + bool get_reversed() const { return m_reversed; } + + operator int() const { return m_port; } + private: + std::uint8_t m_port; + bool m_reversed; +}; + +class ADIPort { + public: + consteval ADIPort(std::int64_t port) + : m_port(0) { + if (port >= 'a' && port <= 'h') port -= ('a' - 1); + else if (port >= 'A' && port <= 'H') port -= ('A' - 1); + if (port < 1 || port > 8) throw "Port out of range!"; + m_port = port; + } + + ADIPort(std::int64_t port, DynamicPort) + : m_port(0) { + if (port < 1 || port > 21) m_port = 0; + if (port >= 'a' && port <= 'h') port -= ('a' - 1); + else if (port >= 'A' && port <= 'H') port -= ('A' - 1); + if (port < 1 || port > 8) return; + m_port = port; + } + + std::uint8_t get_port() const { return m_port; } + + operator std::uint8_t() const { return m_port; } + private: + std::uint8_t m_port; +}; + +} // namespace lemlib diff --git a/src/hardware/Encoder/ADIEncoder.cpp b/src/hardware/Encoder/ADIEncoder.cpp index 7f971e7..c1a2068 100644 --- a/src/hardware/Encoder/ADIEncoder.cpp +++ b/src/hardware/Encoder/ADIEncoder.cpp @@ -1,4 +1,5 @@ #include "hardware/Encoder/ADIEncoder.hpp" +#include "hardware/Port.hpp" #include #include @@ -6,10 +7,10 @@ namespace lemlib { ADIEncoder::ADIEncoder(pros::adi::Encoder encoder) : m_encoder(encoder) {} -ADIEncoder::ADIEncoder(std::uint8_t topPort, std::uint8_t bottomPort, bool reversed) +ADIEncoder::ADIEncoder(ADIPort topPort, ADIPort bottomPort, bool reversed) : m_encoder(pros::adi::Encoder(topPort, bottomPort, reversed)) {} -ADIEncoder::ADIEncoder(std::uint8_t expanderPort, std::uint8_t topPort, std::uint8_t bottomPort, bool reversed) +ADIEncoder::ADIEncoder(SmartPort expanderPort, ADIPort topPort, ADIPort bottomPort, bool reversed) : m_encoder({expanderPort, topPort, bottomPort}, reversed) {} int ADIEncoder::isConnected() { diff --git a/src/hardware/Encoder/V5RotationSensor.cpp b/src/hardware/Encoder/V5RotationSensor.cpp index 0765b62..3fde772 100644 --- a/src/hardware/Encoder/V5RotationSensor.cpp +++ b/src/hardware/Encoder/V5RotationSensor.cpp @@ -1,4 +1,5 @@ #include "hardware/Encoder/V5RotationSensor.hpp" +#include "hardware/Port.hpp" #include "pros/rotation.h" #include @@ -9,13 +10,13 @@ V5RotationSensor::V5RotationSensor(pros::Rotation encoder) pros::c::rotation_set_reversed(m_port, m_reversed); } -V5RotationSensor::V5RotationSensor(std::int8_t port) +V5RotationSensor::V5RotationSensor(ReversibleSmartPort port) : m_port(abs(port)), m_reversed(port < 0) { pros::c::rotation_set_reversed(m_port, m_reversed); } -V5RotationSensor::V5RotationSensor(std::uint8_t port, bool reversed) +V5RotationSensor::V5RotationSensor(SmartPort port, bool reversed) : m_port(port), m_reversed(reversed) { pros::c::rotation_set_reversed(m_port, m_reversed); diff --git a/src/hardware/Imu/V5InertialSensor.cpp b/src/hardware/Imu/V5InertialSensor.cpp index d9d7cbd..35dbff0 100644 --- a/src/hardware/Imu/V5InertialSensor.cpp +++ b/src/hardware/Imu/V5InertialSensor.cpp @@ -1,10 +1,11 @@ #include "hardware/Imu/V5InertialSensor.hpp" +#include "hardware/Port.hpp" namespace lemlib { V5InertialSensor::V5InertialSensor(pros::Imu imu) : m_imu(imu) {} -V5InertialSensor::V5InertialSensor(std::uint8_t port) +V5InertialSensor::V5InertialSensor(SmartPort port) : m_imu(port) {} int V5InertialSensor::calibrate() { return m_imu.reset(); } diff --git a/src/hardware/Motor/Motor.cpp b/src/hardware/Motor/Motor.cpp index 1bb5997..761a10e 100644 --- a/src/hardware/Motor/Motor.cpp +++ b/src/hardware/Motor/Motor.cpp @@ -1,4 +1,5 @@ #include "hardware/Motor/Motor.hpp" +#include "hardware/Port.hpp" #include "hardware/util.hpp" #include "units/Angle.hpp" #include "pros/device.h" @@ -11,11 +12,11 @@ using namespace pros; using namespace pros::c; namespace lemlib { -Motor::Motor(int port, AngularVelocity outputVelocity) +Motor::Motor(ReversibleSmartPort port, AngularVelocity outputVelocity) : m_port(port), m_outputVelocity(outputVelocity) {} -Motor::Motor(uint8_t port, bool reversed, AngularVelocity outputVelocity) +Motor::Motor(SmartPort port, bool reversed, AngularVelocity outputVelocity) : m_port(reversed ? -port : port), m_outputVelocity(outputVelocity) {} diff --git a/src/hardware/Motor/MotorGroup.cpp b/src/hardware/Motor/MotorGroup.cpp index a5d2bcb..adffae4 100644 --- a/src/hardware/Motor/MotorGroup.cpp +++ b/src/hardware/Motor/MotorGroup.cpp @@ -1,4 +1,5 @@ #include "hardware/Motor/MotorGroup.hpp" +#include "hardware/Port.hpp" #include "Motor.hpp" #include "units/Angle.hpp" #include "units/Temperature.hpp" @@ -7,7 +8,7 @@ #include namespace lemlib { -MotorGroup::MotorGroup(std::initializer_list ports, AngularVelocity outputVelocity) +MotorGroup::MotorGroup(std::initializer_list ports, AngularVelocity outputVelocity) : m_outputVelocity(outputVelocity) { for (const int port : ports) { m_motors.push_back({.port = port, .connectedLastCycle = true, .offset = 0_stDeg}); } }