From 91448e16e4e393c6c40688b140b2d68252418675 Mon Sep 17 00:00:00 2001 From: ion098 <146852218+ion098@users.noreply.github.com> Date: Wed, 20 Nov 2024 13:09:30 -0800 Subject: [PATCH 1/4] feat: :construction: add port class with compile time range checking --- include/hardware/Port.hpp | 46 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 include/hardware/Port.hpp diff --git a/include/hardware/Port.hpp b/include/hardware/Port.hpp new file mode 100644 index 0000000..270b0fd --- /dev/null +++ b/include/hardware/Port.hpp @@ -0,0 +1,46 @@ +namespace lemlib { + +struct DynamicPort {}; + +constexpr DynamicPort runtime_check_port{}; + +class Port { + public: + consteval Port(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!"; + } + Port(int64_t port, DynamicPort) : m_port(port) { + if (port < 1 || port > 21) m_port = 0; + } + uint8_t get_port() const { + return m_port; + } + private: + uint8_t m_port; +}; + +class ReversiblePort { + public: + consteval ReversiblePort(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!"; + } + ReversiblePort(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; + } + uint8_t get_port() const { + return m_port; + } + bool get_reversed() const { + return m_reversed; + } + private: + uint8_t m_port; + bool m_reversed; +}; +} From d690d43e44e403c53a0ff13f973116957bf81af8 Mon Sep 17 00:00:00 2001 From: ion098 <146852218+ion098@users.noreply.github.com> Date: Wed, 20 Nov 2024 13:22:27 -0800 Subject: [PATCH 2/4] feat: :sparkles: use checked port in device classes --- include/hardware/Encoder/ADIEncoder.hpp | 3 ++- include/hardware/Encoder/V5RotationSensor.hpp | 5 +++-- include/hardware/Imu/V5InertialSensor.hpp | 3 ++- include/hardware/Motor/Motor.hpp | 5 +++-- include/hardware/Motor/MotorGroup.hpp | 3 ++- include/hardware/Port.hpp | 6 ++++++ src/hardware/Encoder/ADIEncoder.cpp | 3 ++- src/hardware/Encoder/V5RotationSensor.cpp | 5 +++-- src/hardware/Imu/V5InertialSensor.cpp | 3 ++- src/hardware/Motor/Motor.cpp | 5 +++-- src/hardware/Motor/MotorGroup.cpp | 3 ++- 11 files changed, 30 insertions(+), 14 deletions(-) diff --git a/include/hardware/Encoder/ADIEncoder.hpp b/include/hardware/Encoder/ADIEncoder.hpp index 78059bd..ebf2823 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 { @@ -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(Port expanderPort, std::uint8_t topPort, std::uint8_t 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..ea94ea3 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(ReversiblePort 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(Port 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..a3c3c89 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(Port port); /** * @brief calibrate the V5 Inertial Sensor * diff --git a/include/hardware/Motor/Motor.hpp b/include/hardware/Motor/Motor.hpp index 49e577c..ac4f38b 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(ReversiblePort 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(Port 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..827e635 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 index 270b0fd..4d865d8 100644 --- a/include/hardware/Port.hpp +++ b/include/hardware/Port.hpp @@ -17,6 +17,9 @@ class Port { uint8_t get_port() const { return m_port; } + operator uint8_t() const { + return m_port; + } private: uint8_t m_port; }; @@ -39,6 +42,9 @@ class ReversiblePort { bool get_reversed() const { return m_reversed; } + operator int() const { + return m_port; + } private: uint8_t m_port; bool m_reversed; diff --git a/src/hardware/Encoder/ADIEncoder.cpp b/src/hardware/Encoder/ADIEncoder.cpp index 7f971e7..b9e2811 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 @@ -9,7 +10,7 @@ ADIEncoder::ADIEncoder(pros::adi::Encoder encoder) ADIEncoder::ADIEncoder(std::uint8_t topPort, std::uint8_t 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(Port expanderPort, std::uint8_t topPort, std::uint8_t 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..560b064 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(ReversiblePort 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(Port 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..a0013f7 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(Port 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..2ab830a 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(ReversiblePort port, AngularVelocity outputVelocity) : m_port(port), m_outputVelocity(outputVelocity) {} -Motor::Motor(uint8_t port, bool reversed, AngularVelocity outputVelocity) +Motor::Motor(Port 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..1e255a7 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}); } } From 4a1ec5dadffd895e4dd200e274c8e60e734de9ea Mon Sep 17 00:00:00 2001 From: ion098 <146852218+ion098@users.noreply.github.com> Date: Wed, 20 Nov 2024 13:33:17 -0800 Subject: [PATCH 3/4] feat: :sparkles: check adi ports at compile time, rename stuff --- include/hardware/Encoder/ADIEncoder.hpp | 4 +- include/hardware/Encoder/V5RotationSensor.hpp | 4 +- include/hardware/Imu/V5InertialSensor.hpp | 2 +- include/hardware/Motor/Motor.hpp | 4 +- include/hardware/Motor/MotorGroup.hpp | 2 +- include/hardware/Port.hpp | 50 +++++++++++++++---- src/hardware/Encoder/ADIEncoder.cpp | 4 +- src/hardware/Encoder/V5RotationSensor.cpp | 4 +- src/hardware/Imu/V5InertialSensor.cpp | 2 +- src/hardware/Motor/Motor.cpp | 4 +- src/hardware/Motor/MotorGroup.cpp | 2 +- 11 files changed, 55 insertions(+), 27 deletions(-) diff --git a/include/hardware/Encoder/ADIEncoder.hpp b/include/hardware/Encoder/ADIEncoder.hpp index ebf2823..63f3ecd 100644 --- a/include/hardware/Encoder/ADIEncoder.hpp +++ b/include/hardware/Encoder/ADIEncoder.hpp @@ -39,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 * @@ -58,7 +58,7 @@ class ADIEncoder : public Encoder { * } * @endcode */ - ADIEncoder(Port 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 ea94ea3..d955dc3 100644 --- a/include/hardware/Encoder/V5RotationSensor.hpp +++ b/include/hardware/Encoder/V5RotationSensor.hpp @@ -37,7 +37,7 @@ class V5RotationSensor : public Encoder { * } * @endcode */ - V5RotationSensor(ReversiblePort port); + V5RotationSensor(ReversibleSmartPort port); /** * @brief Construct a new V5 Rotation Sensor * @@ -52,7 +52,7 @@ class V5RotationSensor : public Encoder { * } * @endcode */ - V5RotationSensor(Port 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 a3c3c89..11f96c4 100644 --- a/include/hardware/Imu/V5InertialSensor.hpp +++ b/include/hardware/Imu/V5InertialSensor.hpp @@ -34,7 +34,7 @@ class V5InertialSensor : public Imu { * } * @endcode */ - V5InertialSensor(Port 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 ac4f38b..e47a43b 100644 --- a/include/hardware/Motor/Motor.hpp +++ b/include/hardware/Motor/Motor.hpp @@ -26,7 +26,7 @@ class Motor : public Encoder { * } * @endcode */ - Motor(ReversiblePort port, AngularVelocity outputVelocity); + Motor(ReversibleSmartPort port, AngularVelocity outputVelocity); /** * @brief Construct a new Motor object * @@ -43,7 +43,7 @@ class Motor : public Encoder { * } * @endcode */ - Motor(Port 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 827e635..4115202 100644 --- a/include/hardware/Motor/MotorGroup.hpp +++ b/include/hardware/Motor/MotorGroup.hpp @@ -34,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 index 4d865d8..07bc944 100644 --- a/include/hardware/Port.hpp +++ b/include/hardware/Port.hpp @@ -1,42 +1,44 @@ +#include + namespace lemlib { struct DynamicPort {}; constexpr DynamicPort runtime_check_port{}; -class Port { +class SmartPort { public: - consteval Port(int64_t port): m_port(port) { + 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!"; } - Port(int64_t port, DynamicPort) : m_port(port) { + SmartPort(std::int64_t port, DynamicPort) : m_port(port) { if (port < 1 || port > 21) m_port = 0; } - uint8_t get_port() const { + std::uint8_t get_port() const { return m_port; } - operator uint8_t() const { + operator std::uint8_t() const { return m_port; } private: - uint8_t m_port; + std::uint8_t m_port; }; -class ReversiblePort { +class ReversibleSmartPort { public: - consteval ReversiblePort(int64_t port): m_port(port < 0 ? -port : port), m_reversed(port < 0) { + 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!"; } - ReversiblePort(int64_t port, DynamicPort): m_port(port < 0 ? -port : port), m_reversed(port < 0) { + 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; } - uint8_t get_port() const { + std::uint8_t get_port() const { return m_port; } bool get_reversed() const { @@ -46,7 +48,33 @@ class ReversiblePort { return m_port; } private: - uint8_t m_port; + 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; +}; + } diff --git a/src/hardware/Encoder/ADIEncoder.cpp b/src/hardware/Encoder/ADIEncoder.cpp index b9e2811..c1a2068 100644 --- a/src/hardware/Encoder/ADIEncoder.cpp +++ b/src/hardware/Encoder/ADIEncoder.cpp @@ -7,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(Port 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 560b064..3fde772 100644 --- a/src/hardware/Encoder/V5RotationSensor.cpp +++ b/src/hardware/Encoder/V5RotationSensor.cpp @@ -10,13 +10,13 @@ V5RotationSensor::V5RotationSensor(pros::Rotation encoder) pros::c::rotation_set_reversed(m_port, m_reversed); } -V5RotationSensor::V5RotationSensor(ReversiblePort port) +V5RotationSensor::V5RotationSensor(ReversibleSmartPort port) : m_port(abs(port)), m_reversed(port < 0) { pros::c::rotation_set_reversed(m_port, m_reversed); } -V5RotationSensor::V5RotationSensor(Port 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 a0013f7..35dbff0 100644 --- a/src/hardware/Imu/V5InertialSensor.cpp +++ b/src/hardware/Imu/V5InertialSensor.cpp @@ -5,7 +5,7 @@ namespace lemlib { V5InertialSensor::V5InertialSensor(pros::Imu imu) : m_imu(imu) {} -V5InertialSensor::V5InertialSensor(Port 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 2ab830a..761a10e 100644 --- a/src/hardware/Motor/Motor.cpp +++ b/src/hardware/Motor/Motor.cpp @@ -12,11 +12,11 @@ using namespace pros; using namespace pros::c; namespace lemlib { -Motor::Motor(ReversiblePort port, AngularVelocity outputVelocity) +Motor::Motor(ReversibleSmartPort port, AngularVelocity outputVelocity) : m_port(port), m_outputVelocity(outputVelocity) {} -Motor::Motor(Port 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 1e255a7..adffae4 100644 --- a/src/hardware/Motor/MotorGroup.cpp +++ b/src/hardware/Motor/MotorGroup.cpp @@ -8,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}); } } From bfd81ef01bbe395f6edf471eaa528a783265eb08 Mon Sep 17 00:00:00 2001 From: Liam Teale Date: Wed, 20 Nov 2024 14:17:04 -0800 Subject: [PATCH 4/4] format Port.hpp, use pragma once --- include/hardware/Port.hpp | 66 +++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 30 deletions(-) diff --git a/include/hardware/Port.hpp b/include/hardware/Port.hpp index 07bc944..4d1e5b7 100644 --- a/include/hardware/Port.hpp +++ b/include/hardware/Port.hpp @@ -1,52 +1,57 @@ +#pragma once + #include namespace lemlib { struct DynamicPort {}; -constexpr DynamicPort runtime_check_port{}; +constexpr DynamicPort runtime_check_port {}; class SmartPort { public: - consteval SmartPort(std::int64_t port): m_port(port) { + 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) { + + 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; - } + + 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; + 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) { + + 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; - } + + 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; @@ -54,27 +59,28 @@ class ReversibleSmartPort { class ADIPort { public: - consteval ADIPort(std::int64_t port): m_port(0) { + 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) { + + 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; - } + + 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