Skip to content

Commit

Permalink
refactor: 🥅 Change error handling in screen code to use errno
Browse files Browse the repository at this point in the history
  • Loading branch information
ion098 committed Jan 2, 2025
1 parent f9f822f commit 458d0de
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
12 changes: 8 additions & 4 deletions src/gamepad/screens/alertScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,15 @@ void AlertScreen::update(uint32_t delta_time) {
}

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);
if (line > 2) {
TODO("add error logging")
errno = EINVAL;
return;
}

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 Down
16 changes: 11 additions & 5 deletions src/gamepad/screens/defaultScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,17 @@ ScreenBuffer DefaultScreen::getScreen(std::set<uint8_t> visible_lines) {

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

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 @@ -47,8 +51,10 @@ void DefaultScreen::printLine(uint8_t line, std::string str) {
}

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);
}

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

0 comments on commit 458d0de

Please sign in to comment.