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

refactor: 🥅 Change all error handling to use errno, add int32_t return codes #25

Merged
merged 15 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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: 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
13 changes: 8 additions & 5 deletions src/gamepad/screens/alertScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ 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) {
TODO("change handling for off screen lines")
if (line > 2) std::exit(1);
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 false;
}

TODO("warn instead of throw error if there are too many lines")
if (std::ranges::count(str, '\n') > 2) std::exit(1);
if (std::ranges::count(str, '\n') > 2) { TODO("add warn logging") }

std::vector<std::string> strs(3, "");
std::stringstream ss(str);
Expand All @@ -56,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
21 changes: 13 additions & 8 deletions src/gamepad/screens/defaultScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@ 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")
if (line > 2) std::exit(1);
bool DefaultScreen::printLine(uint8_t line, std::string str) {
if (line > 2) {
TODO("add error logging")
errno = EINVAL;
return false;
}

const std::lock_guard<pros::Mutex> guard(m_mutex);

if (str.find('\n') != std::string::npos) {
TODO("warn instead of throw error if there are too many lines")
if (std::ranges::count(str, '\n') > 2) std::exit(1);
if (std::ranges::count(str, '\n') > 2) { TODO("add warn logging for too many lines") }

std::vector<std::string> strs(3);
std::stringstream ss(str);
Expand All @@ -40,15 +42,18 @@ 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) {
TODO("change handling for too long rumble patterns")
if (rumble_pattern.size() > 8) std::exit(1);
if (rumble_pattern.size() > 8) {
TODO("add warn logging")
rumble_pattern.resize(8);
ion098 marked this conversation as resolved.
Show resolved Hide resolved
}

std::lock_guard<pros::Mutex> guard(m_mutex);
m_current_buffer[3] = std::move(rumble_pattern);
Expand Down
Loading