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

✨ Check ports at runtime #8

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
5 changes: 3 additions & 2 deletions include/hardware/Encoder/ADIEncoder.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "hardware/Encoder/Encoder.hpp"
#include "hardware/Port.hpp"
#include "pros/adi.hpp"

namespace lemlib {
Expand Down Expand Up @@ -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
*
Expand All @@ -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
*
Expand Down
5 changes: 3 additions & 2 deletions include/hardware/Encoder/V5RotationSensor.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "hardware/Encoder/Encoder.hpp"
#include "hardware/Port.hpp"
#include "pros/rotation.hpp"

namespace lemlib {
Expand Down Expand Up @@ -36,7 +37,7 @@ class V5RotationSensor : public Encoder {
* }
* @endcode
*/
V5RotationSensor(std::int8_t port);
V5RotationSensor(ReversibleSmartPort port);
/**
* @brief Construct a new V5 Rotation Sensor
*
Expand All @@ -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
*
Expand Down
3 changes: 2 additions & 1 deletion include/hardware/Imu/V5InertialSensor.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "hardware/Imu/Imu.hpp"
#include "hardware/Port.hpp"
#include "pros/imu.hpp"

namespace lemlib {
Expand Down Expand Up @@ -33,7 +34,7 @@ class V5InertialSensor : public Imu {
* }
* @endcode
*/
V5InertialSensor(std::uint8_t port);
V5InertialSensor(SmartPort port);
/**
* @brief calibrate the V5 Inertial Sensor
*
Expand Down
5 changes: 3 additions & 2 deletions include/hardware/Motor/Motor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "pros/motors.hpp"
#include "hardware/encoder/Encoder.hpp"
#include "hardware/Port.hpp"
#include "units/Temperature.hpp"

namespace lemlib {
Expand All @@ -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
*
Expand All @@ -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
*
Expand Down
3 changes: 2 additions & 1 deletion include/hardware/Motor/MotorGroup.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "pros/motor_group.hpp"
#include "hardware/Motor/Motor.hpp"
#include "hardware/Port.hpp"
#include <vector>

namespace lemlib {
Expand Down Expand Up @@ -33,7 +34,7 @@ class MotorGroup : Encoder {
* }
* @endcode
*/
MotorGroup(std::initializer_list<int> ports, AngularVelocity outputVelocity);
MotorGroup(std::initializer_list<ReversibleSmartPort> ports, AngularVelocity outputVelocity);
/**
* @brief Construct a new Motor Group
*
Expand Down
86 changes: 86 additions & 0 deletions include/hardware/Port.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#pragma once

#include <cstdint>

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
5 changes: 3 additions & 2 deletions src/hardware/Encoder/ADIEncoder.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
#include "hardware/Encoder/ADIEncoder.hpp"
#include "hardware/Port.hpp"
#include <cmath>
#include <limits.h>

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() {
Expand Down
5 changes: 3 additions & 2 deletions src/hardware/Encoder/V5RotationSensor.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "hardware/Encoder/V5RotationSensor.hpp"
#include "hardware/Port.hpp"
#include "pros/rotation.h"
#include <limits.h>

Expand All @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion src/hardware/Imu/V5InertialSensor.cpp
Original file line number Diff line number Diff line change
@@ -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(); }
Expand Down
5 changes: 3 additions & 2 deletions src/hardware/Motor/Motor.cpp
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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) {}

Expand Down
3 changes: 2 additions & 1 deletion src/hardware/Motor/MotorGroup.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "hardware/Motor/MotorGroup.hpp"
#include "hardware/Port.hpp"
#include "Motor.hpp"
#include "units/Angle.hpp"
#include "units/Temperature.hpp"
Expand All @@ -7,7 +8,7 @@
#include <errno.h>

namespace lemlib {
MotorGroup::MotorGroup(std::initializer_list<int> ports, AngularVelocity outputVelocity)
MotorGroup::MotorGroup(std::initializer_list<ReversibleSmartPort> ports, AngularVelocity outputVelocity)
: m_outputVelocity(outputVelocity) {
for (const int port : ports) { m_motors.push_back({.port = port, .connectedLastCycle = true, .offset = 0_stDeg}); }
}
Expand Down