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

✨Better Code Organization #225

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
69897fa
Added inline namespaces as detailed in issue #223 (Branch is WIP)
WillXuCodes Jul 14, 2020
fa2a21f
Added ADI namespace, and backwards compatibility for the reorganized ADI
WillXuCodes Jul 14, 2020
5714c8c
Removed ADI from a constructor header.
WillXuCodes Jul 14, 2020
d890e7d
Added v5 namespace to controller.
WillXuCodes Jul 14, 2020
8c92796
Changed the v5 namespace properly to v5
WillXuCodes Jul 15, 2020
db3fc20
Moved backwards compatibility to area after the adi classes were
WillXuCodes Jul 15, 2020
4dd8612
Removed code related to pull request #222 and issue #217 related to
WillXuCodes Jul 15, 2020
929605e
Added a better comments
WillXuCodes Jul 16, 2020
00d4e9c
Merge branch 'develop' into reorganize
WillXuCodes Jun 7, 2021
a6fd67d
Added accidentally deleted class declaration
WillXuCodes Jun 7, 2021
71f3e24
Named port correctly
WillXuCodes Jun 7, 2021
784bef4
Deleted extraneous port class declaration
WillXuCodes Jun 7, 2021
e42f63f
Removed ADI in ADIDigitalOut
WillXuCodes Jun 7, 2021
9937ba2
Updated comments
WillXuCodes Jun 7, 2021
6d3f22c
Fixed compilation error
WillXuCodes Jun 7, 2021
dd3547a
Fixed version
WillXuCodes Jun 7, 2021
347ed3a
Merge branch 'develop' into reorganize
ayushuk Jan 18, 2022
1b1e097
Fix version numbers
ayushuk Jan 18, 2022
fc96973
Minor formatting
ayushuk Jan 18, 2022
40addc6
Added new sensors to v5 namespace
WillXuCodes Mar 14, 2022
3c9dc30
Merge branch 'develop' into reorganize
WillXuCodes Mar 14, 2022
21df7a8
Revert Version File
WillXuCodes Mar 14, 2022
2ef3311
Merge branch 'develop' into reorganize
WillXuCodes Mar 14, 2022
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
99 changes: 60 additions & 39 deletions include/pros/adi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@

#include <cstdint>

#define LEGACY_TYPEDEF(old_name, new_name) \
using old_name [[deprecated("use " #new_name " instead")]] = new_name

namespace pros {
class ADIPort {
namespace adi {
class Port {
public:
/**
* Configures an ADI port to act as a given sensor type.
Expand All @@ -37,9 +41,9 @@ class ADIPort {
* \param type
* The configuration type for the port
*/
ADIPort(std::uint8_t port, adi_port_config_e_t type = E_ADI_TYPE_UNDEFINED);
Port(std::uint8_t port, adi_port_config_e_t type = E_ADI_TYPE_UNDEFINED);

virtual ~ADIPort(void) = default;
virtual ~Port(void) = default;

/**
* Gets the configuration for the given ADI port.
Expand Down Expand Up @@ -81,11 +85,11 @@ class ADIPort {
std::int32_t set_value(std::int32_t value) const;

protected:
ADIPort(void);
Port(void);
std::uint8_t _port;
};

class ADIAnalogIn : private ADIPort {
class AnalogIn : private Port {
public:
/**
* Configures an ADI port to act as an Analog Input.
Expand All @@ -102,7 +106,7 @@ class ADIAnalogIn : private ADIPort {
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
ADIAnalogIn(std::uint8_t port);
AnalogIn(std::uint8_t port);

/**
* Calibrates the analog sensor on the specified port and returns the new
Expand All @@ -112,8 +116,8 @@ class ADIAnalogIn : private ADIPort {
* this time and computes an average from approximately 500 samples, 1 ms
* apart, for a 0.5 s period of calibration. The average value thus calculated
* is returned and stored for later calls to the
* pros::ADIAnalogIn::get_value_calibrated() and
* pros::ADIAnalogIn::get_value_calibrated_HR() functions. These functions
* pros::AnalogIn::get_value_calibrated() and
* pros::AnalogIn::get_value_calibrated_HR() functions. These functions
* will return the difference between this value and the current sensor value
* when called.
*
Expand All @@ -131,10 +135,10 @@ class ADIAnalogIn : private ADIPort {
/**
* Gets the 12 bit calibrated value of an analog input port.
*
* The pros::ADIAnalogIn::calibrate() function must be run first. This
* The pros::AnalogIn::calibrate() function must be run first. This
* function is inappropriate for sensor values intended for integration, as
* round-off error can accumulate causing drift over time. Use
* pros::ADIAnalogIn::get_value_calibrated_HR() instead.
* pros::AnalogIn::get_value_calibrated_HR() instead.
*
* This function uses the following values of errno when an error state is
* reached:
Expand All @@ -148,7 +152,7 @@ class ADIAnalogIn : private ADIPort {
/**
* Gets the 16 bit calibrated value of an analog input port.
*
* The pros::ADIAnalogIn::calibrate() function must be run first. This is
* The pros::AnalogIn::calibrate() function must be run first. This is
* intended for integrated sensor values such as gyros and accelerometers to
* reduce drift due to round-off, and should not be used on a sensor such as a
* line tracker or potentiometer.
Expand Down Expand Up @@ -181,15 +185,15 @@ class ADIAnalogIn : private ADIPort {
* voltage of nearly 0 V and a value of 4095 reflects an input voltage of
* nearly 5 V
*/
using ADIPort::get_value;
using Port::get_value;
};

using ADIPotentiometer = ADIAnalogIn;
using ADILineSensor = ADIAnalogIn;
using ADILightSensor = ADIAnalogIn;
using ADIAccelerometer = ADIAnalogIn;
using Potentiometer = AnalogIn;
using LineSensor = AnalogIn;
using LightSensor = AnalogIn;
using Accelerometer = AnalogIn;

class ADIAnalogOut : private ADIPort {
class AnalogOut : private Port {
public:
/**
* Configures an ADI port to act as an Analog Output.
Expand All @@ -203,7 +207,7 @@ class ADIAnalogOut : private ADIPort {
* \param type
* The configuration type for the port
*/
ADIAnalogOut(std::uint8_t port);
AnalogOut(std::uint8_t port);

/**
* Sets the value for the given ADI port.
Expand All @@ -221,10 +225,10 @@ class ADIAnalogOut : private ADIPort {
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
using ADIPort::set_value;
using Port::set_value;
};

class ADIDigitalOut : private ADIPort {
class DigitalOut : private Port {
public:
/**
* Configures an ADI port to act as a Digital Output.
Expand All @@ -238,7 +242,7 @@ class ADIDigitalOut : private ADIPort {
* \param type
* The configuration type for the port
*/
ADIDigitalOut(std::uint8_t port, bool init_state = LOW);
DigitalOut(std::uint8_t port, bool init_state = LOW);

/**
* Sets the value for the given ADI port.
Expand All @@ -256,10 +260,10 @@ class ADIDigitalOut : private ADIPort {
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
using ADIPort::set_value;
using Port::set_value;
};

class ADIDigitalIn : private ADIPort {
class DigitalIn : private Port {
public:
/**
* Configures an ADI port to act as a Digital Input.
Expand All @@ -273,7 +277,7 @@ class ADIDigitalIn : private ADIPort {
* \param type
* The configuration type for the port
*/
ADIDigitalIn(std::uint8_t port);
DigitalIn(std::uint8_t port);

/**
* Gets a rising-edge case for a digital button press.
Expand Down Expand Up @@ -304,12 +308,12 @@ class ADIDigitalIn : private ADIPort {
*
* \return The value stored for the given port
*/
using ADIPort::get_value;
using Port::get_value;
};

using ADIButton = ADIDigitalIn;
using Button = DigitalIn;

class ADIMotor : private ADIPort {
class Motor : private Port {
public:
/**
* Configures an ADI port to act as a Motor.
Expand All @@ -323,7 +327,7 @@ class ADIMotor : private ADIPort {
* \param type
* The configuration type for the port
*/
ADIMotor(std::uint8_t port);
Motor(std::uint8_t port);

/**
* Stops the motor on the given port.
Expand Down Expand Up @@ -351,7 +355,7 @@ class ADIMotor : private ADIPort {
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
using ADIPort::set_value;
using Port::set_value;

/**
* Gets the last set speed of the motor on the given port.
Expand All @@ -362,10 +366,10 @@ class ADIMotor : private ADIPort {
*
* \return The last set speed of the motor on the given port
*/
using ADIPort::get_value;
using Port::get_value;
};

class ADIEncoder : private ADIPort {
class Encoder : private Port {
public:
/**
* Configures a set of ADI ports to act as an Encoder.
Expand All @@ -382,7 +386,7 @@ class ADIEncoder : private ADIPort {
* \param reverse
* If "true", the sensor will count in the opposite direction
*/
ADIEncoder(std::uint8_t port_top, std::uint8_t port_bottom, bool reversed = false);
Encoder(std::uint8_t port_top, std::uint8_t port_bottom, bool reversed = false);

/**
* Sets the encoder value to zero.
Expand Down Expand Up @@ -414,7 +418,7 @@ class ADIEncoder : private ADIPort {
std::int32_t get_value(void) const;
};

class ADIUltrasonic : private ADIPort {
class Ultrasonic : private Port {
public:
/**
* Configures a set of ADI ports to act as an Ultrasonic.
Expand All @@ -430,7 +434,7 @@ class ADIUltrasonic : private ADIPort {
* The port connected to the yellow INPUT cable. This should be in the
* next highest port following port_ping.
*/
ADIUltrasonic(std::uint8_t port_ping, std::uint8_t port_echo);
Ultrasonic(std::uint8_t port_ping, std::uint8_t port_echo);

/**
* Gets the current ultrasonic sensor value in centimeters.
Expand All @@ -446,18 +450,18 @@ class ADIUltrasonic : private ADIPort {
* \return The distance to the nearest object in m^-4 (10000 indicates 1
* meter), measured from the sensor's mounting points.
*/
using ADIPort::get_value;
using Port::get_value;
};

class ADIGyro : private ADIPort {
class Gyro : private Port {
public:
/**
* Initializes a gyroscope on the given port. If the given port has not
* previously been configured as a gyro, then this function starts a 1300ms
* calibration period.
*
* It is highly recommended that an ADIGyro object be created in initialize()
* when the robot is stationary to ensure proper calibration. If an ADIGyro
* It is highly recommended that an Gyro object be created in initialize()
* when the robot is stationary to ensure proper calibration. If an Gyro
* object is declared at the global scope, a hardcoded 1300ms delay at the
* beginning of initialize will be necessary to ensure that the gyro's
* returned values are correct at the beginning of autonomous/opcontrol.
Expand All @@ -472,7 +476,7 @@ class ADIGyro : private ADIPort {
* A scalar value that will be multiplied by the gyro heading value
* supplied by the ADI
*/
ADIGyro(std::uint8_t port, double multiplier = 1);
Gyro(std::uint8_t port, double multiplier = 1);

/**
* Gets the current gyro angle in tenths of a degree. Unless a multiplier is
Expand Down Expand Up @@ -502,6 +506,23 @@ class ADIGyro : private ADIPort {
*/
std::int32_t reset(void) const;
};
} // namespace adi
//pros4 upgrade backwards compatibility for ADI api.
LEGACY_TYPEDEF(ADIPort, pros::adi::Port);
LEGACY_TYPEDEF(ADIAnalogIn, pros::adi::AnalogIn);
LEGACY_TYPEDEF(ADIAnalogOut, pros::adi::AnalogOut);
LEGACY_TYPEDEF(ADIDigitalIn, pros::adi::DigitalIn);
LEGACY_TYPEDEF(ADIDigitalOut, pros::adi::DigitalOut);
LEGACY_TYPEDEF(ADIMotor, pros::adi::Motor);
LEGACY_TYPEDEF(ADIGyro, pros::adi::Gyro);
LEGACY_TYPEDEF(ADIEncoder, pros::adi::Encoder);
LEGACY_TYPEDEF(ADIUltrasonic, pros::adi::Ultrasonic);
//Derived Classes
LEGACY_TYPEDEF(ADIPotentiometer,pros::adi::Potentiometer);
LEGACY_TYPEDEF(ADILineSensor,pros::adi::LineSensor);
LEGACY_TYPEDEF(ADILightSensor,pros::adi::LightSensor);
LEGACY_TYPEDEF(ADIAccelerometer,pros::adi::Accelerometer);
LEGACY_TYPEDEF(ADIButton,pros::adi::Button);
} // namespace pros

#endif // _PROS_ADI_HPP_
2 changes: 2 additions & 0 deletions include/pros/imu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "pros/imu.h"

namespace pros {
inline namespace v5{
class Imu {
const std::uint8_t _port;

Expand Down Expand Up @@ -212,6 +213,7 @@ class Imu {
*/
virtual bool is_calibrating() const;
};
} //namespace v5
} // namespace pros

#endif
2 changes: 1 addition & 1 deletion include/pros/misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ int32_t usd_is_installed(void);

#ifdef __cplusplus
}
}
} // namespace pros
}
#endif

Expand Down
2 changes: 2 additions & 0 deletions include/pros/misc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <string>

namespace pros {
inline namespace v5 {
class Controller {
public:
/**
Expand Down Expand Up @@ -258,6 +259,7 @@ class Controller {
private:
controller_id_e_t _id;
};
} // namespace v5

namespace battery {
/**
Expand Down
2 changes: 2 additions & 0 deletions include/pros/motors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "pros/motors.h"

namespace pros {
inline namespace v5{
class Motor {
public:
/**
Expand Down Expand Up @@ -829,5 +830,6 @@ namespace literals {
const pros::Motor operator"" _mtr(const unsigned long long int m);
const pros::Motor operator"" _rmtr(const unsigned long long int m);
} // namespace literals
} // namespace v5
} // namespace pros
#endif // _PROS_MOTORS_HPP_
3 changes: 2 additions & 1 deletion include/pros/rtos.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <type_traits>

namespace pros {
inline namespace rtos {
class Task {
public:
/**
Expand Down Expand Up @@ -359,7 +360,7 @@ class Mutex {
private:
mutex_t mutex;
};

}
/**
* Gets the number of milliseconds since PROS initialized.
*
Expand Down
2 changes: 2 additions & 0 deletions include/pros/vision.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <cstdint>

namespace pros {
inline namespace v5{
class Vision {
public:
/**
Expand Down Expand Up @@ -441,5 +442,6 @@ class Vision {
private:
std::uint8_t _port;
};
} // namespace v5
} // namespace pros
#endif // _PROS_VISION_HPP_
5 changes: 2 additions & 3 deletions src/devices/controller.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include "v5_api.h"
#include "vdml/vdml.h"

#define CONTROLLER_MAX_COLS 19
#define CONTROLLER_MAX_COLS 15

// From enum in misc.h
#define NUM_BUTTONS 12
Expand Down Expand Up @@ -260,8 +260,7 @@ int32_t controller_print(controller_id_e_t id, uint8_t line, uint8_t col, const
}

int32_t controller_clear_line(controller_id_e_t id, uint8_t line) {
static const char* clear = " ";
kassert(strlen(clear) == CONTROLLER_MAX_COLS);
static const char* clear = " ";
return controller_print(id, line, 0, clear);
}

Expand Down
Loading