Skip to content

Commit

Permalink
refactor: ♻️ Change functions to return int32_t error codes
Browse files Browse the repository at this point in the history
  • Loading branch information
ion098 committed Jan 22, 2025
1 parent 4da1dc5 commit 5a5fced
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 14 deletions.
13 changes: 11 additions & 2 deletions include/gamepad/gamepad.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,20 @@ class Gamepad {
* @param line the line number to print the string on (0-2)
* @param str the string to print onto the controller (\n to go to the next line)
*
* This function uses the following value(s) of errno when an error state is reached:
*
* EINVAL: The line number is not in the interval [0, 2]
*
* @b Example:
* @code {.cpp}
* gamepad::master.printLine(1, "This will print on the middle line");
* gamepad::master.printLine(0, "this will print\n\naround the middle line");
* @endcode
*
* @return 0 if the alert was added successfully, UINT32_MAX if there was an error.
* @return 0 if the alert was added successfully
* @return INT32_MAX if there was an error, setting errno
*/
uint32_t printLine(uint8_t line, std::string str);
int32_t printLine(uint8_t line, std::string str);
/**
* @brief clears all lines on the controller, similar to the pros function (low priority)
*
Expand All @@ -84,6 +89,10 @@ class Gamepad {
* @param rumble_pattern A string consisting of the characters '.', '-', and ' ', where dots are short rumbles,
* dashes are long rumbles, and spaces are pauses. Maximum supported length is 8 characters.
*
* This function uses the following value(s) of errno when an error state is reached:
*
* EINVAL: The rumble pattern was truncated to 8 characters
*
* @b Example:
* @code {.cpp}
* // rumbles in the following pattern: short, pause, long, short short
Expand Down
10 changes: 8 additions & 2 deletions include/gamepad/screens/alertScreen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,15 @@ class AlertScreen : public AbstractScreen {
* @param rumble A string consisting of the characters '.', '-', and ' ', where dots are short rumbles,
* dashes are long rumbles, and spaces are pauses. Maximum supported length is 8 characters.
*
* @return 0 if the alert was added successfully, UINT32_MAX if there was an error.
* This function uses the following value(s) of errno when an error state is reached:
*
* EINVAL: The line number is not in the interval [0, 2]
*
* @return 0 if the alert was added successfully
* @return INT32_MAX if there was an error, setting errno
*
*/
uint32_t addAlerts(uint8_t line, std::string strs, uint32_t duration, std::string rumble = "");
int32_t addAlerts(uint8_t line, std::string strs, uint32_t duration, std::string rumble = "");
private:
struct AlertBuffer {
ScreenBuffer screen;
Expand Down
11 changes: 8 additions & 3 deletions include/gamepad/screens/defaultScreen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,22 @@ class DefaultScreen : public AbstractScreen {
* @param line the line number to print the string on (0-2)
* @param str the string to print onto the controller (\n to go to the next line)
*
* @return 0 if the alert was added successfully, UINT32_MAX if there was an error.
* This function uses the following value(s) of errno when an error state is reached:
*
* EINVAL: The line number is not in the interval [0, 2]
*
* @return 0 if the alert was added successfully
* @return INT32_MAX if there was an error, setting errno
*/
uint32_t printLine(uint8_t line, std::string str);
int32_t printLine(uint8_t line, std::string str);

/**
* makes the controller rumble like pros
*
* @param rumble_pattern A string consisting of the characters '.', '-', and ' ', where dots are short rumbles,
* dashes are long rumbles, and spaces are pauses. Maximum supported length is 8 characters.
*/
void rumble(std::string rumble_pattern);
int32_t rumble(std::string rumble_pattern);
private:
ScreenBuffer m_current_buffer {};
pros::Mutex m_mutex {};
Expand Down
2 changes: 1 addition & 1 deletion src/gamepad/gamepad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ void Gamepad::addScreen(std::shared_ptr<AbstractScreen> screen) {
m_screens.emplace(m_screens.begin() + pos, screen);
}

uint32_t Gamepad::printLine(uint8_t line, std::string str) { return m_default_screen->printLine(line, str); }
int32_t Gamepad::printLine(uint8_t line, std::string str) { return m_default_screen->printLine(line, str); }

void Gamepad::clear() { m_default_screen->printLine(0, " \n \n "); }

Expand Down
4 changes: 2 additions & 2 deletions src/gamepad/screens/alertScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ void AlertScreen::update(uint32_t delta_time) {
if (pros::millis() - m_line_set_time >= m_screen_contents->duration) m_screen_contents = std::nullopt;
}

uint32_t AlertScreen::addAlerts(uint8_t line, std::string str, uint32_t duration, std::string rumble) {
int32_t AlertScreen::addAlerts(uint8_t line, std::string str, uint32_t duration, std::string rumble) {
if (line > 2) {
TODO("add error logging")
errno = EINVAL;
return UINT32_MAX;
return INT32_MAX;
}

if (std::ranges::count(str, '\n') > 2) { TODO("add warn logging") }
Expand Down
18 changes: 14 additions & 4 deletions src/gamepad/screens/defaultScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ ScreenBuffer DefaultScreen::getScreen(std::set<uint8_t> visible_lines) {
return output;
}

uint32_t DefaultScreen::printLine(uint8_t line, std::string str) {
int32_t DefaultScreen::printLine(uint8_t line, std::string str) {
if (line > 2) {
TODO("add error logging")
errno = EINVAL;
return UINT32_MAX;
return INT32_MAX;
}

const std::lock_guard<pros::Mutex> guard(m_mutex);
Expand All @@ -50,15 +50,25 @@ uint32_t DefaultScreen::printLine(uint8_t line, std::string str) {
return 0;
}

void DefaultScreen::rumble(std::string rumble_pattern) {
int32_t DefaultScreen::rumble(std::string rumble_pattern) {
bool is_err = false;
if (rumble_pattern.size() > 8) {
errno = EINVAL;
TODO("add warn logging")
errno = EINVAL;
is_err = true;
rumble_pattern.resize(8);
}

if (rumble_pattern.find_first_not_of(".- ") != std::string::npos) {
TODO("add error logging")
errno = EINVAL;
return INT32_MAX;
}

std::lock_guard<pros::Mutex> guard(m_mutex);
m_current_buffer[3] = std::move(rumble_pattern);
if (is_err) return INT32_MAX;
else return 0;
}

} // namespace gamepad

0 comments on commit 5a5fced

Please sign in to comment.