Skip to content

Commit

Permalink
refactor: 🥅 Add return value to functions that set errno
Browse files Browse the repository at this point in the history
  • Loading branch information
ion098 committed Jan 4, 2025
1 parent 366415f commit 1473733
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 18 deletions.
5 changes: 4 additions & 1 deletion include/gamepad/gamepad.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,11 @@ class Gamepad {
* @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 true if the alert was added successfully, false if there was an error.
*/
void printLine(uint8_t line, std::string str);
bool printLine(uint8_t line, std::string str);
/**
* @brief clears all lines on the controller, similar to the pros function (low priority)
*
Expand Down
4 changes: 3 additions & 1 deletion include/gamepad/screens/alertScreen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ class AlertScreen : public AbstractScreen {
* @param duration how long the alert should persist on the screen
* @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 true if the alert was added successfully, false if there was an error.
*/
void addAlerts(uint8_t line, std::string strs, uint32_t duration, std::string rumble = "");
bool addAlerts(uint8_t line, std::string strs, uint32_t duration, std::string rumble = "");
private:
struct AlertBuffer {
ScreenBuffer screen;
Expand Down
4 changes: 3 additions & 1 deletion include/gamepad/screens/defaultScreen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ 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 true if the alert was added successfully, false if there was an error.
*/
void printLine(uint8_t line, std::string str);
bool printLine(uint8_t line, std::string str);

/**
* makes the controller rumble like pros
Expand Down
12 changes: 3 additions & 9 deletions 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);
}

void Gamepad::printLine(uint8_t line, std::string str) { m_default_screen->printLine(line, str); }
bool 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 All @@ -146,10 +146,7 @@ float Gamepad::operator[](pros::controller_analog_e_t axis) {
case pros::E_CONTROLLER_ANALOG_LEFT_Y: return this->LeftY;
case pros::E_CONTROLLER_ANALOG_RIGHT_X: return this->RightX;
case pros::E_CONTROLLER_ANALOG_RIGHT_Y: return this->RightY;
default:
TODO("add error logging")
errno = EINVAL;
return 0;
default: TODO("add error logging") return 0;
}
}

Expand All @@ -172,10 +169,7 @@ Button Gamepad::*Gamepad::buttonToPtr(pros::controller_digital_e_t button) {
case pros::E_CONTROLLER_DIGITAL_B: return &Gamepad::m_B;
case pros::E_CONTROLLER_DIGITAL_Y: return &Gamepad::m_Y;
case pros::E_CONTROLLER_DIGITAL_A: return &Gamepad::m_A;
default:
TODO("add error logging")
errno = EINVAL;
return &Gamepad::Fake;
default: TODO("add error logging") return &Gamepad::Fake;
}
}
} // namespace gamepad
5 changes: 3 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;
}

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

if (std::ranges::count(str, '\n') > 2) { TODO("add warn logging") }
Expand All @@ -58,6 +58,7 @@ void AlertScreen::addAlerts(uint8_t line, std::string str, uint32_t duration, st

std::lock_guard<pros::Mutex> guard(m_mutex);
m_screen_buffer.push_back({buffer, duration});
return true;
}

} // namespace gamepad
9 changes: 5 additions & 4 deletions src/gamepad/screens/defaultScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ ScreenBuffer DefaultScreen::getScreen(std::set<uint8_t> visible_lines) {
return output;
}

void DefaultScreen::printLine(uint8_t line, std::string str) {
TODO("change handling for off screen lines")
bool DefaultScreen::printLine(uint8_t line, std::string str) {
if (line > 2) {
TODO("add error logging")
errno = EINVAL;
return;
return false;
}

const std::lock_guard<pros::Mutex> guard(m_mutex);
Expand All @@ -42,10 +42,11 @@ void DefaultScreen::printLine(uint8_t line, std::string str) {
for (uint8_t l = 0; l < 3; l++) {
if (!strs[l].empty()) m_current_buffer[l] = (strs[l]);
}
return;
return true;
}

m_current_buffer[line] = std::move(str);
return true;
}

void DefaultScreen::rumble(std::string rumble_pattern) {
Expand Down

0 comments on commit 1473733

Please sign in to comment.