From 458d0de3c3b96b1f3e8df9071539b6697737be07 Mon Sep 17 00:00:00 2001 From: ion098 <146852218+ion098@users.noreply.github.com> Date: Thu, 2 Jan 2025 14:40:43 -0800 Subject: [PATCH 01/15] refactor: :goal_net: Change error handling in screen code to use errno --- src/gamepad/screens/alertScreen.cpp | 12 ++++++++---- src/gamepad/screens/defaultScreen.cpp | 16 +++++++++++----- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/gamepad/screens/alertScreen.cpp b/src/gamepad/screens/alertScreen.cpp index 4d23d45..f8f590d 100644 --- a/src/gamepad/screens/alertScreen.cpp +++ b/src/gamepad/screens/alertScreen.cpp @@ -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 strs(3, ""); std::stringstream ss(str); diff --git a/src/gamepad/screens/defaultScreen.cpp b/src/gamepad/screens/defaultScreen.cpp index 6fa23d4..998abfe 100644 --- a/src/gamepad/screens/defaultScreen.cpp +++ b/src/gamepad/screens/defaultScreen.cpp @@ -22,13 +22,17 @@ ScreenBuffer DefaultScreen::getScreen(std::set 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 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 strs(3); std::stringstream ss(str); @@ -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 guard(m_mutex); m_current_buffer[3] = std::move(rumble_pattern); From 366415f4a3fa9b51dfc81404b9cc8fca6ee061e1 Mon Sep 17 00:00:00 2001 From: ion098 <146852218+ion098@users.noreply.github.com> Date: Thu, 2 Jan 2025 14:46:32 -0800 Subject: [PATCH 02/15] style: :art: Fix formatting --- src/gamepad/screens/alertScreen.cpp | 4 +--- src/gamepad/screens/defaultScreen.cpp | 4 +--- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/src/gamepad/screens/alertScreen.cpp b/src/gamepad/screens/alertScreen.cpp index f8f590d..1621d35 100644 --- a/src/gamepad/screens/alertScreen.cpp +++ b/src/gamepad/screens/alertScreen.cpp @@ -39,9 +39,7 @@ void AlertScreen::addAlerts(uint8_t line, std::string str, uint32_t duration, st return; } - if (std::ranges::count(str, '\n') > 2) { - TODO("add warn logging") - } + if (std::ranges::count(str, '\n') > 2) { TODO("add warn logging") } std::vector strs(3, ""); std::stringstream ss(str); diff --git a/src/gamepad/screens/defaultScreen.cpp b/src/gamepad/screens/defaultScreen.cpp index 998abfe..abf4f09 100644 --- a/src/gamepad/screens/defaultScreen.cpp +++ b/src/gamepad/screens/defaultScreen.cpp @@ -30,9 +30,7 @@ void DefaultScreen::printLine(uint8_t line, std::string str) { const std::lock_guard guard(m_mutex); if (str.find('\n') != std::string::npos) { - if (std::ranges::count(str, '\n') > 2) { - TODO("add warn logging for too many lines") - } + if (std::ranges::count(str, '\n') > 2) { TODO("add warn logging for too many lines") } std::vector strs(3); std::stringstream ss(str); From 14737339b936cfdebb24f970221b4b57d4ce7201 Mon Sep 17 00:00:00 2001 From: ion098 <146852218+ion098@users.noreply.github.com> Date: Sat, 4 Jan 2025 14:36:49 -0800 Subject: [PATCH 03/15] refactor: :goal_net: Add return value to functions that set errno --- include/gamepad/gamepad.hpp | 5 ++++- include/gamepad/screens/alertScreen.hpp | 4 +++- include/gamepad/screens/defaultScreen.hpp | 4 +++- src/gamepad/gamepad.cpp | 12 +++--------- src/gamepad/screens/alertScreen.cpp | 5 +++-- src/gamepad/screens/defaultScreen.cpp | 9 +++++---- 6 files changed, 21 insertions(+), 18 deletions(-) diff --git a/include/gamepad/gamepad.hpp b/include/gamepad/gamepad.hpp index 901739f..1e369c3 100644 --- a/include/gamepad/gamepad.hpp +++ b/include/gamepad/gamepad.hpp @@ -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) * diff --git a/include/gamepad/screens/alertScreen.hpp b/include/gamepad/screens/alertScreen.hpp index 50a40cc..0180608 100644 --- a/include/gamepad/screens/alertScreen.hpp +++ b/include/gamepad/screens/alertScreen.hpp @@ -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; diff --git a/include/gamepad/screens/defaultScreen.hpp b/include/gamepad/screens/defaultScreen.hpp index f85363e..00c98c4 100644 --- a/include/gamepad/screens/defaultScreen.hpp +++ b/include/gamepad/screens/defaultScreen.hpp @@ -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 diff --git a/src/gamepad/gamepad.cpp b/src/gamepad/gamepad.cpp index 486b7d7..5ab4ce8 100644 --- a/src/gamepad/gamepad.cpp +++ b/src/gamepad/gamepad.cpp @@ -130,7 +130,7 @@ void Gamepad::addScreen(std::shared_ptr 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 "); } @@ -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; } } @@ -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 diff --git a/src/gamepad/screens/alertScreen.cpp b/src/gamepad/screens/alertScreen.cpp index 1621d35..c1784d2 100644 --- a/src/gamepad/screens/alertScreen.cpp +++ b/src/gamepad/screens/alertScreen.cpp @@ -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") } @@ -58,6 +58,7 @@ void AlertScreen::addAlerts(uint8_t line, std::string str, uint32_t duration, st std::lock_guard guard(m_mutex); m_screen_buffer.push_back({buffer, duration}); + return true; } } // namespace gamepad diff --git a/src/gamepad/screens/defaultScreen.cpp b/src/gamepad/screens/defaultScreen.cpp index abf4f09..771f538 100644 --- a/src/gamepad/screens/defaultScreen.cpp +++ b/src/gamepad/screens/defaultScreen.cpp @@ -20,11 +20,11 @@ ScreenBuffer DefaultScreen::getScreen(std::set 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 guard(m_mutex); @@ -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) { From 277b08458c0f001233824bb5548f189f355d96b5 Mon Sep 17 00:00:00 2001 From: ion098 <146852218+ion098@users.noreply.github.com> Date: Wed, 8 Jan 2025 17:06:54 +0000 Subject: [PATCH 04/15] refactor: :recycle: Change functions to return 0 on success --- include/gamepad/button.hpp | 16 ++++++++-------- include/gamepad/event_handler.hpp | 20 ++++++++++---------- include/gamepad/gamepad.hpp | 4 ++-- include/gamepad/screens/alertScreen.hpp | 4 ++-- include/gamepad/screens/defaultScreen.hpp | 4 ++-- src/gamepad/button.cpp | 18 +++++++++--------- src/gamepad/gamepad.cpp | 2 +- src/gamepad/screens/alertScreen.cpp | 6 +++--- src/gamepad/screens/defaultScreen.cpp | 10 ++++++---- 9 files changed, 43 insertions(+), 41 deletions(-) diff --git a/include/gamepad/button.hpp b/include/gamepad/button.hpp index 1e25cff..80781bb 100644 --- a/include/gamepad/button.hpp +++ b/include/gamepad/button.hpp @@ -84,7 +84,7 @@ class Button { * gamepad::master.Up.onPress("upPress1", []() { std::cout << "I was pressed!" << std::endl; }); * @endcode */ - bool onPress(std::string listenerName, std::function func) const; + uint32_t onPress(std::string listenerName, std::function func) const; /** * @brief Register a function to run when the button is long pressed. * @@ -108,7 +108,7 @@ class Button { * std::endl; }); * @endcode */ - bool onLongPress(std::string listenerName, std::function func) const; + uint32_t onLongPress(std::string listenerName, std::function func) const; /** * @brief Register a function to run when the button is released. * @@ -125,7 +125,7 @@ class Button { * gamepad::master.Y.onRelease("stopIntake", []() { intake.move(0); }); * @endcode */ - bool onRelease(std::string listenerName, std::function func) const; + uint32_t onRelease(std::string listenerName, std::function func) const; /** * @brief Register a function to run when the button is short released. * @@ -147,7 +147,7 @@ class Button { * gamepad::master.B.onShortRelease("intakeOnePiece", []() { intake.move_relative(600, 100); }); * @endcode */ - bool onShortRelease(std::string listenerName, std::function func) const; + uint32_t onShortRelease(std::string listenerName, std::function func) const; /** * @brief Register a function to run when the button is long released. * @@ -168,7 +168,7 @@ class Button { * @endcode * */ - bool onLongRelease(std::string listenerName, std::function func) const; + uint32_t onLongRelease(std::string listenerName, std::function func) const; /** * @brief Register a function to run periodically after its been held * @@ -189,7 +189,7 @@ class Button { * @endcode * */ - bool onRepeatPress(std::string listenerName, std::function func) const; + uint32_t onRepeatPress(std::string listenerName, std::function func) const; /** * @brief Register a function to run for a given event. * @@ -207,7 +207,7 @@ class Button { * gamepad::master.L1.addListener(gamepad::ON_RELEASE, "stop_spin", []() { motor1.brake(); }); * @endcode */ - bool addListener(EventType event, std::string listenerName, std::function func) const; + uint32_t addListener(EventType event, std::string listenerName, std::function func) const; /** * @brief Removes a listener from the button * @warning Usage of this function is discouraged. @@ -224,7 +224,7 @@ class Button { * gamepad::master.L1.removeListener("do_something"); * @endcode */ - bool removeListener(std::string listenerName) const; + uint32_t removeListener(std::string listenerName) const; /** * @brief Returns a value indicating whether the button is currently being held. diff --git a/include/gamepad/event_handler.hpp b/include/gamepad/event_handler.hpp index a5a7a5b..345ccce 100644 --- a/include/gamepad/event_handler.hpp +++ b/include/gamepad/event_handler.hpp @@ -24,33 +24,33 @@ template class EventHandler { * * @param key The listener key (this must be a unique key value) * @param func The function to run when this event is fired - * @return true The listener was successfully added - * @return false The listener was NOT successfully added (there is already a listener with the same key) + * @return 0 The listener was successfully added + * @return UINT32_MAX The listener was NOT successfully added (there is already a listener with the same key) */ - bool addListener(Key key, Listener func) { + uint32_t addListener(Key key, Listener func) { std::lock_guard lock(m_mutex); - if (std::find(m_keys.begin(), m_keys.end(), key) != m_keys.end()) return false; + if (std::find(m_keys.begin(), m_keys.end(), key) != m_keys.end()) return UINT32_MAX; m_keys.push_back(key); m_listeners.push_back(func); - return true; + return 0; } /** * @brief Remove a listener from the list of listeners * * @param key The listener key (this must be a unique key value) - * @return true The listener was successfully removed - * @return false The listener was NOT successfully removed (there is no listener with the same key) + * @return 0 The listener was successfully removed + * @return UINT32_MAX The listener was NOT successfully removed (there is no listener with the same key) */ - bool removeListener(Key key) { + uint32_t removeListener(Key key) { std::lock_guard lock(m_mutex); auto i = std::find(m_keys.begin(), m_keys.end(), key); if (i != m_keys.end()) { m_keys.erase(i); m_listeners.erase(m_listeners.begin() + (i - m_keys.begin())); - return true; + return 0; } - return false; + return UINT32_MAX; } /** diff --git a/include/gamepad/gamepad.hpp b/include/gamepad/gamepad.hpp index 1e369c3..83c4c74 100644 --- a/include/gamepad/gamepad.hpp +++ b/include/gamepad/gamepad.hpp @@ -55,9 +55,9 @@ class Gamepad { * 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. + * @return 0 if the alert was added successfully, UINT32_MAX if there was an error. */ - bool printLine(uint8_t line, std::string str); + uint32_t printLine(uint8_t line, std::string str); /** * @brief clears all lines on the controller, similar to the pros function (low priority) * diff --git a/include/gamepad/screens/alertScreen.hpp b/include/gamepad/screens/alertScreen.hpp index 0180608..64157ae 100644 --- a/include/gamepad/screens/alertScreen.hpp +++ b/include/gamepad/screens/alertScreen.hpp @@ -47,9 +47,9 @@ 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 true if the alert was added successfully, false if there was an error. + * @return 0 if the alert was added successfully, UINT32_MAX if there was an error. */ - bool addAlerts(uint8_t line, std::string strs, uint32_t duration, std::string rumble = ""); + uint32_t addAlerts(uint8_t line, std::string strs, uint32_t duration, std::string rumble = ""); private: struct AlertBuffer { ScreenBuffer screen; diff --git a/include/gamepad/screens/defaultScreen.hpp b/include/gamepad/screens/defaultScreen.hpp index 00c98c4..ff23eb1 100644 --- a/include/gamepad/screens/defaultScreen.hpp +++ b/include/gamepad/screens/defaultScreen.hpp @@ -32,9 +32,9 @@ 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. + * @return 0 if the alert was added successfully, UINT32_MAX if there was an error. */ - bool printLine(uint8_t line, std::string str); + uint32_t printLine(uint8_t line, std::string str); /** * makes the controller rumble like pros diff --git a/src/gamepad/button.cpp b/src/gamepad/button.cpp index d3f3965..94e41b3 100644 --- a/src/gamepad/button.cpp +++ b/src/gamepad/button.cpp @@ -8,31 +8,31 @@ void Button::setLongPressThreshold(uint32_t threshold) const { m_long_press_thre void Button::setRepeatCooldown(uint32_t cooldown) const { m_repeat_cooldown = cooldown; } -bool Button::onPress(std::string listenerName, std::function func) const { +uint32_t Button::onPress(std::string listenerName, std::function func) const { return m_on_press_event.addListener(std::move(listenerName) + "_user", std::move(func)); } -bool Button::onLongPress(std::string listenerName, std::function func) const { +uint32_t Button::onLongPress(std::string listenerName, std::function func) const { return m_on_long_press_event.addListener(std::move(listenerName) + "_user", std::move(func)); } -bool Button::onRelease(std::string listenerName, std::function func) const { +uint32_t Button::onRelease(std::string listenerName, std::function func) const { return m_on_release_event.addListener(std::move(listenerName) + "_user", std::move(func)); } -bool Button::onShortRelease(std::string listenerName, std::function func) const { +uint32_t Button::onShortRelease(std::string listenerName, std::function func) const { return m_on_short_release_event.addListener(std::move(listenerName) + "_user", std::move(func)); } -bool Button::onLongRelease(std::string listenerName, std::function func) const { +uint32_t Button::onLongRelease(std::string listenerName, std::function func) const { return m_on_long_release_event.addListener(std::move(listenerName) + "_user", std::move(func)); } -bool Button::onRepeatPress(std::string listenerName, std::function func) const { +uint32_t Button::onRepeatPress(std::string listenerName, std::function func) const { return m_on_repeat_press_event.addListener(std::move(listenerName) + "_user", std::move(func)); } -bool Button::addListener(EventType event, std::string listenerName, std::function func) const { +uint32_t Button::addListener(EventType event, std::string listenerName, std::function func) const { switch (event) { case gamepad::EventType::ON_PRESS: return this->onPress(std::move(listenerName), std::move(func)); case gamepad::EventType::ON_LONG_PRESS: return this->onLongPress(std::move(listenerName), std::move(func)); @@ -44,11 +44,11 @@ bool Button::addListener(EventType event, std::string listenerName, std::functio default: TODO("add error logging") errno = EINVAL; - return false; + return UINT32_MAX; } } -bool Button::removeListener(std::string listenerName) const { +uint32_t Button::removeListener(std::string listenerName) const { return m_on_press_event.removeListener(listenerName + "_user") || m_on_long_press_event.removeListener(listenerName + "_user") || m_on_release_event.removeListener(listenerName + "_user") || diff --git a/src/gamepad/gamepad.cpp b/src/gamepad/gamepad.cpp index 5ab4ce8..effe524 100644 --- a/src/gamepad/gamepad.cpp +++ b/src/gamepad/gamepad.cpp @@ -130,7 +130,7 @@ void Gamepad::addScreen(std::shared_ptr screen) { m_screens.emplace(m_screens.begin() + pos, screen); } -bool Gamepad::printLine(uint8_t line, std::string str) { return m_default_screen->printLine(line, str); } +uint32_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 "); } diff --git a/src/gamepad/screens/alertScreen.cpp b/src/gamepad/screens/alertScreen.cpp index c1784d2..6e56940 100644 --- a/src/gamepad/screens/alertScreen.cpp +++ b/src/gamepad/screens/alertScreen.cpp @@ -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; } -bool AlertScreen::addAlerts(uint8_t line, std::string str, uint32_t duration, std::string rumble) { +uint32_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 false; + return UINT32_MAX; } if (std::ranges::count(str, '\n') > 2) { TODO("add warn logging") } @@ -58,7 +58,7 @@ bool AlertScreen::addAlerts(uint8_t line, std::string str, uint32_t duration, st std::lock_guard guard(m_mutex); m_screen_buffer.push_back({buffer, duration}); - return true; + return 0; } } // namespace gamepad diff --git a/src/gamepad/screens/defaultScreen.cpp b/src/gamepad/screens/defaultScreen.cpp index 771f538..e7a99d0 100644 --- a/src/gamepad/screens/defaultScreen.cpp +++ b/src/gamepad/screens/defaultScreen.cpp @@ -2,6 +2,7 @@ #include "gamepad/screens/abstractScreen.hpp" #include "gamepad/todo.hpp" #include +#include #include #include #include @@ -20,11 +21,11 @@ ScreenBuffer DefaultScreen::getScreen(std::set visible_lines) { return output; } -bool DefaultScreen::printLine(uint8_t line, std::string str) { +uint32_t DefaultScreen::printLine(uint8_t line, std::string str) { if (line > 2) { TODO("add error logging") errno = EINVAL; - return false; + return UINT32_MAX; } const std::lock_guard guard(m_mutex); @@ -42,15 +43,16 @@ bool 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 true; + return 0; } m_current_buffer[line] = std::move(str); - return true; + return 0; } void DefaultScreen::rumble(std::string rumble_pattern) { if (rumble_pattern.size() > 8) { + errno = EINVAL; TODO("add warn logging") rumble_pattern.resize(8); } From 4da1dc584ee630bd393790bca64150fb49035712 Mon Sep 17 00:00:00 2001 From: ion098 <146852218+ion098@users.noreply.github.com> Date: Wed, 22 Jan 2025 18:44:24 +0000 Subject: [PATCH 05/15] refactor: :technologist: Hide TODO messages from compiler output --- include/gamepad/todo.hpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/include/gamepad/todo.hpp b/include/gamepad/todo.hpp index c1e0b7b..4b0ffd4 100644 --- a/include/gamepad/todo.hpp +++ b/include/gamepad/todo.hpp @@ -1,5 +1,13 @@ #pragma once #define DO_PRAGMA(x) _Pragma(#x) -#define TODO(x) DO_PRAGMA(message("TODO - " #x)) -#define FIXME(x) DO_PRAGMA(warning("FIXME - " #x)) \ No newline at end of file + +// We only define the TODO/FIXME macros if the file is being compiled by Microsoft Intellisense +// or clangd. This way, the TODO/FIXME messages don't clutter the compilation messages. +#if defined(_debug) || defined(__clang__) +#define TODO(x) DO_PRAGMA(message("TODO - " x)) +#define FIXME(x) DO_PRAGMA(warning("FIXME - " x)) +#else +#define TODO(x) +#define FIXME(x) +#endif \ No newline at end of file From 5a5fced8735727990b9cfd00f4caf22db3704ef0 Mon Sep 17 00:00:00 2001 From: ion098 <146852218+ion098@users.noreply.github.com> Date: Wed, 22 Jan 2025 18:45:01 +0000 Subject: [PATCH 06/15] refactor: :recycle: Change functions to return int32_t error codes --- include/gamepad/gamepad.hpp | 13 +++++++++++-- include/gamepad/screens/alertScreen.hpp | 10 ++++++++-- include/gamepad/screens/defaultScreen.hpp | 11 ++++++++--- src/gamepad/gamepad.cpp | 2 +- src/gamepad/screens/alertScreen.cpp | 4 ++-- src/gamepad/screens/defaultScreen.cpp | 18 ++++++++++++++---- 6 files changed, 44 insertions(+), 14 deletions(-) diff --git a/include/gamepad/gamepad.hpp b/include/gamepad/gamepad.hpp index 83c4c74..7adeeca 100644 --- a/include/gamepad/gamepad.hpp +++ b/include/gamepad/gamepad.hpp @@ -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) * @@ -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 diff --git a/include/gamepad/screens/alertScreen.hpp b/include/gamepad/screens/alertScreen.hpp index 64157ae..09d1fca 100644 --- a/include/gamepad/screens/alertScreen.hpp +++ b/include/gamepad/screens/alertScreen.hpp @@ -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; diff --git a/include/gamepad/screens/defaultScreen.hpp b/include/gamepad/screens/defaultScreen.hpp index ff23eb1..1f31381 100644 --- a/include/gamepad/screens/defaultScreen.hpp +++ b/include/gamepad/screens/defaultScreen.hpp @@ -32,9 +32,14 @@ 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 @@ -42,7 +47,7 @@ class DefaultScreen : public AbstractScreen { * @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 {}; diff --git a/src/gamepad/gamepad.cpp b/src/gamepad/gamepad.cpp index effe524..cbd3c27 100644 --- a/src/gamepad/gamepad.cpp +++ b/src/gamepad/gamepad.cpp @@ -130,7 +130,7 @@ void Gamepad::addScreen(std::shared_ptr 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 "); } diff --git a/src/gamepad/screens/alertScreen.cpp b/src/gamepad/screens/alertScreen.cpp index 6e56940..0c4ee31 100644 --- a/src/gamepad/screens/alertScreen.cpp +++ b/src/gamepad/screens/alertScreen.cpp @@ -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") } diff --git a/src/gamepad/screens/defaultScreen.cpp b/src/gamepad/screens/defaultScreen.cpp index e7a99d0..4ac099a 100644 --- a/src/gamepad/screens/defaultScreen.cpp +++ b/src/gamepad/screens/defaultScreen.cpp @@ -21,11 +21,11 @@ ScreenBuffer DefaultScreen::getScreen(std::set 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 guard(m_mutex); @@ -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 guard(m_mutex); m_current_buffer[3] = std::move(rumble_pattern); + if (is_err) return INT32_MAX; + else return 0; } } // namespace gamepad \ No newline at end of file From f127b09f4d8173894f08b6e3f4859aada983e100 Mon Sep 17 00:00:00 2001 From: ion098 <146852218+ion098@users.noreply.github.com> Date: Wed, 22 Jan 2025 19:08:06 +0000 Subject: [PATCH 07/15] style: :art: Format code --- include/gamepad/gamepad.hpp | 2 +- include/gamepad/screens/alertScreen.hpp | 2 +- src/gamepad/gamepad.cpp | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/include/gamepad/gamepad.hpp b/include/gamepad/gamepad.hpp index 7adeeca..c14d34b 100644 --- a/include/gamepad/gamepad.hpp +++ b/include/gamepad/gamepad.hpp @@ -162,7 +162,7 @@ class Gamepad { * @return std::string A unique listener name */ static std::string uniqueName(); - static Button Gamepad::*buttonToPtr(pros::controller_digital_e_t button); + static Button Gamepad::* buttonToPtr(pros::controller_digital_e_t button); void updateButton(pros::controller_digital_e_t button_id); void updateScreens(); diff --git a/include/gamepad/screens/alertScreen.hpp b/include/gamepad/screens/alertScreen.hpp index 09d1fca..ac4fb9e 100644 --- a/include/gamepad/screens/alertScreen.hpp +++ b/include/gamepad/screens/alertScreen.hpp @@ -53,7 +53,7 @@ class AlertScreen : public AbstractScreen { * * @return 0 if the alert was added successfully * @return INT32_MAX if there was an error, setting errno - * + * */ int32_t addAlerts(uint8_t line, std::string strs, uint32_t duration, std::string rumble = ""); private: diff --git a/src/gamepad/gamepad.cpp b/src/gamepad/gamepad.cpp index cbd3c27..69155c3 100644 --- a/src/gamepad/gamepad.cpp +++ b/src/gamepad/gamepad.cpp @@ -19,7 +19,7 @@ Gamepad::Gamepad(pros::controller_id_e_t id) } void Gamepad::updateButton(pros::controller_digital_e_t button_id) { - Button Gamepad::*button = Gamepad::buttonToPtr(button_id); + Button Gamepad::* button = Gamepad::buttonToPtr(button_id); bool is_held = m_controller.get_digital(button_id); (this->*button).update(is_held); } @@ -155,7 +155,7 @@ std::string Gamepad::uniqueName() { return std::to_string(i++) + "_internal"; } -Button Gamepad::*Gamepad::buttonToPtr(pros::controller_digital_e_t button) { +Button Gamepad::* Gamepad::buttonToPtr(pros::controller_digital_e_t button) { switch (button) { case pros::E_CONTROLLER_DIGITAL_L1: return &Gamepad::m_L1; case pros::E_CONTROLLER_DIGITAL_L2: return &Gamepad::m_L2; From 08e67239c49cf4015f6cf410008f1472038983b7 Mon Sep 17 00:00:00 2001 From: ion098 <146852218+ion098@users.noreply.github.com> Date: Wed, 29 Jan 2025 21:50:47 +0000 Subject: [PATCH 08/15] refactor: :label: Change functions returning uint32_t to return int32_t --- include/gamepad/button.hpp | 16 ++++++++-------- include/gamepad/event_handler.hpp | 8 ++++---- src/gamepad/button.cpp | 16 ++++++++-------- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/include/gamepad/button.hpp b/include/gamepad/button.hpp index 80781bb..82423e9 100644 --- a/include/gamepad/button.hpp +++ b/include/gamepad/button.hpp @@ -84,7 +84,7 @@ class Button { * gamepad::master.Up.onPress("upPress1", []() { std::cout << "I was pressed!" << std::endl; }); * @endcode */ - uint32_t onPress(std::string listenerName, std::function func) const; + int32_t onPress(std::string listenerName, std::function func) const; /** * @brief Register a function to run when the button is long pressed. * @@ -108,7 +108,7 @@ class Button { * std::endl; }); * @endcode */ - uint32_t onLongPress(std::string listenerName, std::function func) const; + int32_t onLongPress(std::string listenerName, std::function func) const; /** * @brief Register a function to run when the button is released. * @@ -125,7 +125,7 @@ class Button { * gamepad::master.Y.onRelease("stopIntake", []() { intake.move(0); }); * @endcode */ - uint32_t onRelease(std::string listenerName, std::function func) const; + int32_t onRelease(std::string listenerName, std::function func) const; /** * @brief Register a function to run when the button is short released. * @@ -147,7 +147,7 @@ class Button { * gamepad::master.B.onShortRelease("intakeOnePiece", []() { intake.move_relative(600, 100); }); * @endcode */ - uint32_t onShortRelease(std::string listenerName, std::function func) const; + int32_t onShortRelease(std::string listenerName, std::function func) const; /** * @brief Register a function to run when the button is long released. * @@ -168,7 +168,7 @@ class Button { * @endcode * */ - uint32_t onLongRelease(std::string listenerName, std::function func) const; + int32_t onLongRelease(std::string listenerName, std::function func) const; /** * @brief Register a function to run periodically after its been held * @@ -189,7 +189,7 @@ class Button { * @endcode * */ - uint32_t onRepeatPress(std::string listenerName, std::function func) const; + int32_t onRepeatPress(std::string listenerName, std::function func) const; /** * @brief Register a function to run for a given event. * @@ -207,7 +207,7 @@ class Button { * gamepad::master.L1.addListener(gamepad::ON_RELEASE, "stop_spin", []() { motor1.brake(); }); * @endcode */ - uint32_t addListener(EventType event, std::string listenerName, std::function func) const; + int32_t addListener(EventType event, std::string listenerName, std::function func) const; /** * @brief Removes a listener from the button * @warning Usage of this function is discouraged. @@ -224,7 +224,7 @@ class Button { * gamepad::master.L1.removeListener("do_something"); * @endcode */ - uint32_t removeListener(std::string listenerName) const; + int32_t removeListener(std::string listenerName) const; /** * @brief Returns a value indicating whether the button is currently being held. diff --git a/include/gamepad/event_handler.hpp b/include/gamepad/event_handler.hpp index 345ccce..c0b4d8f 100644 --- a/include/gamepad/event_handler.hpp +++ b/include/gamepad/event_handler.hpp @@ -27,9 +27,9 @@ template class EventHandler { * @return 0 The listener was successfully added * @return UINT32_MAX The listener was NOT successfully added (there is already a listener with the same key) */ - uint32_t addListener(Key key, Listener func) { + int32_t addListener(Key key, Listener func) { std::lock_guard lock(m_mutex); - if (std::find(m_keys.begin(), m_keys.end(), key) != m_keys.end()) return UINT32_MAX; + if (std::find(m_keys.begin(), m_keys.end(), key) != m_keys.end()) return INT32_MAX; m_keys.push_back(key); m_listeners.push_back(func); return 0; @@ -42,7 +42,7 @@ template class EventHandler { * @return 0 The listener was successfully removed * @return UINT32_MAX The listener was NOT successfully removed (there is no listener with the same key) */ - uint32_t removeListener(Key key) { + int32_t removeListener(Key key) { std::lock_guard lock(m_mutex); auto i = std::find(m_keys.begin(), m_keys.end(), key); if (i != m_keys.end()) { @@ -50,7 +50,7 @@ template class EventHandler { m_listeners.erase(m_listeners.begin() + (i - m_keys.begin())); return 0; } - return UINT32_MAX; + return INT32_MAX; } /** diff --git a/src/gamepad/button.cpp b/src/gamepad/button.cpp index 94e41b3..6b24de1 100644 --- a/src/gamepad/button.cpp +++ b/src/gamepad/button.cpp @@ -8,31 +8,31 @@ void Button::setLongPressThreshold(uint32_t threshold) const { m_long_press_thre void Button::setRepeatCooldown(uint32_t cooldown) const { m_repeat_cooldown = cooldown; } -uint32_t Button::onPress(std::string listenerName, std::function func) const { +int32_t Button::onPress(std::string listenerName, std::function func) const { return m_on_press_event.addListener(std::move(listenerName) + "_user", std::move(func)); } -uint32_t Button::onLongPress(std::string listenerName, std::function func) const { +int32_t Button::onLongPress(std::string listenerName, std::function func) const { return m_on_long_press_event.addListener(std::move(listenerName) + "_user", std::move(func)); } -uint32_t Button::onRelease(std::string listenerName, std::function func) const { +int32_t Button::onRelease(std::string listenerName, std::function func) const { return m_on_release_event.addListener(std::move(listenerName) + "_user", std::move(func)); } -uint32_t Button::onShortRelease(std::string listenerName, std::function func) const { +int32_t Button::onShortRelease(std::string listenerName, std::function func) const { return m_on_short_release_event.addListener(std::move(listenerName) + "_user", std::move(func)); } -uint32_t Button::onLongRelease(std::string listenerName, std::function func) const { +int32_t Button::onLongRelease(std::string listenerName, std::function func) const { return m_on_long_release_event.addListener(std::move(listenerName) + "_user", std::move(func)); } -uint32_t Button::onRepeatPress(std::string listenerName, std::function func) const { +int32_t Button::onRepeatPress(std::string listenerName, std::function func) const { return m_on_repeat_press_event.addListener(std::move(listenerName) + "_user", std::move(func)); } -uint32_t Button::addListener(EventType event, std::string listenerName, std::function func) const { +int32_t Button::addListener(EventType event, std::string listenerName, std::function func) const { switch (event) { case gamepad::EventType::ON_PRESS: return this->onPress(std::move(listenerName), std::move(func)); case gamepad::EventType::ON_LONG_PRESS: return this->onLongPress(std::move(listenerName), std::move(func)); @@ -48,7 +48,7 @@ uint32_t Button::addListener(EventType event, std::string listenerName, std::fun } } -uint32_t Button::removeListener(std::string listenerName) const { +int32_t Button::removeListener(std::string listenerName) const { return m_on_press_event.removeListener(listenerName + "_user") || m_on_long_press_event.removeListener(listenerName + "_user") || m_on_release_event.removeListener(listenerName + "_user") || From 0ac0ebdb2761ad376e4a249093859ea50e4ef47d Mon Sep 17 00:00:00 2001 From: ion098 <146852218+ion098@users.noreply.github.com> Date: Sun, 2 Feb 2025 23:50:50 +0000 Subject: [PATCH 09/15] fix: :bug: Fix broken impl of Button::removeListener --- include/gamepad/button.hpp | 13 ++++++++++-- src/gamepad/button.cpp | 42 ++++++++++++++++++++++++++------------ 2 files changed, 40 insertions(+), 15 deletions(-) diff --git a/include/gamepad/button.hpp b/include/gamepad/button.hpp index 82423e9..1ebbd19 100644 --- a/include/gamepad/button.hpp +++ b/include/gamepad/button.hpp @@ -212,6 +212,7 @@ class Button { * @brief Removes a listener from the button * @warning Usage of this function is discouraged. * + * @param event the event type of the listener * @param listenerName The name of the listener to remove * @return true The specified listener was successfully removed * @return false The specified listener could not be removed @@ -221,10 +222,10 @@ class Button { * // Add an event listener... * gamepad::master.L1.addListener(gamepad::ON_PRESS, "do_something", doSomething); * // ...and now get rid of it - * gamepad::master.L1.removeListener("do_something"); + * gamepad::master.L1.removeListener(gamepad::ON_PRESS, "do_something"); * @endcode */ - int32_t removeListener(std::string listenerName) const; + int32_t removeListener(EventType event, std::string listenerName) const; /** * @brief Returns a value indicating whether the button is currently being held. @@ -240,6 +241,14 @@ class Button { * @param is_held Whether or not the button is currently held down */ void update(bool is_held); + /** + * @brief Get the handler object for the given event type + * + * @param event The desired event type + * @return nullptr The event value is invalid + * @return _impl::EventHandler* A pointer to the given event's handler + */ + _impl::EventHandler* get_handler(EventType event) const; /// How long the threshold should be for the longPress and shortRelease events mutable uint32_t m_long_press_threshold = 500; /// How often repeatPress is called diff --git a/src/gamepad/button.cpp b/src/gamepad/button.cpp index 6b24de1..037b4db 100644 --- a/src/gamepad/button.cpp +++ b/src/gamepad/button.cpp @@ -2,8 +2,21 @@ #include "gamepad/todo.hpp" #include "pros/rtos.hpp" #include +#include namespace gamepad { +_impl::EventHandler* Button::get_handler(EventType event) const { + switch (event) { + case gamepad::EventType::ON_PRESS: return &m_on_press_event; + case gamepad::EventType::ON_LONG_PRESS: return &m_on_long_press_event; + case gamepad::EventType::ON_RELEASE: return &m_on_release_event; + case gamepad::EventType::ON_SHORT_RELEASE: return &m_on_short_release_event; + case gamepad::EventType::ON_LONG_RELEASE: return &m_on_long_release_event; + case gamepad::EventType::ON_REPEAT_PRESS: return &m_on_repeat_press_event; + default: return nullptr; + } +} + void Button::setLongPressThreshold(uint32_t threshold) const { m_long_press_threshold = threshold; } void Button::setRepeatCooldown(uint32_t cooldown) const { m_repeat_cooldown = cooldown; } @@ -33,28 +46,31 @@ int32_t Button::onRepeatPress(std::string listenerName, std::function func) const { - switch (event) { - case gamepad::EventType::ON_PRESS: return this->onPress(std::move(listenerName), std::move(func)); - case gamepad::EventType::ON_LONG_PRESS: return this->onLongPress(std::move(listenerName), std::move(func)); - case gamepad::EventType::ON_RELEASE: return this->onRelease(std::move(listenerName), std::move(func)); - case gamepad::EventType::ON_SHORT_RELEASE: - return this->onShortRelease(std::move(listenerName), std::move(func)); - case gamepad::EventType::ON_LONG_RELEASE: return this->onLongRelease(std::move(listenerName), std::move(func)); - case gamepad::EventType::ON_REPEAT_PRESS: return this->onRepeatPress(std::move(listenerName), std::move(func)); - default: - TODO("add error logging") - errno = EINVAL; - return UINT32_MAX; + auto handler = this->get_handler(event); + if (handler != nullptr) { + return handler->addListener(listenerName + "_user", func); + } else { + TODO("add error logging") + errno = EINVAL; + return INT32_MAX; } } -int32_t Button::removeListener(std::string listenerName) const { +int32_t Button::removeListener(EventType event, std::string listenerName) const { return m_on_press_event.removeListener(listenerName + "_user") || m_on_long_press_event.removeListener(listenerName + "_user") || m_on_release_event.removeListener(listenerName + "_user") || m_on_short_release_event.removeListener(listenerName + "_user") || m_on_long_release_event.removeListener(listenerName + "_user") || m_on_repeat_press_event.removeListener(listenerName + "_user"); + auto handler = this->get_handler(event); + if (handler != nullptr) { + return handler->removeListener(listenerName + "_user"); + } else { + TODO("add error logging") + errno = EINVAL; + return INT32_MAX; + } } void Button::update(const bool is_held) { From 607f3fc82233c357442c508b198950631c67a85b Mon Sep 17 00:00:00 2001 From: ion098 <146852218+ion098@users.noreply.github.com> Date: Sun, 2 Feb 2025 16:10:07 -0800 Subject: [PATCH 10/15] fix(display): :bug: Add handling for too big messages --- src/gamepad/screens/alertScreen.cpp | 10 ++++++++-- src/gamepad/screens/defaultScreen.cpp | 23 ++++++++++++++--------- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/gamepad/screens/alertScreen.cpp b/src/gamepad/screens/alertScreen.cpp index 0c4ee31..d960d16 100644 --- a/src/gamepad/screens/alertScreen.cpp +++ b/src/gamepad/screens/alertScreen.cpp @@ -2,6 +2,7 @@ #include "gamepad/todo.hpp" #include "pros/rtos.hpp" #include +#include #include #include #include @@ -33,13 +34,18 @@ void AlertScreen::update(uint32_t delta_time) { } int32_t AlertScreen::addAlerts(uint8_t line, std::string str, uint32_t duration, std::string rumble) { + int32_t ret_val = 0; if (line > 2) { TODO("add error logging") errno = EINVAL; return INT32_MAX; } - if (std::ranges::count(str, '\n') > 2) { TODO("add warn logging") } + if (std::ranges::count(str, '\n') > 2) { + TODO("add warn logging") + errno = EMSGSIZE; + ret_val = INT32_MAX; + } std::vector strs(3, ""); std::stringstream ss(str); @@ -58,7 +64,7 @@ int32_t AlertScreen::addAlerts(uint8_t line, std::string str, uint32_t duration, std::lock_guard guard(m_mutex); m_screen_buffer.push_back({buffer, duration}); - return 0; + return ret_val; } } // namespace gamepad diff --git a/src/gamepad/screens/defaultScreen.cpp b/src/gamepad/screens/defaultScreen.cpp index 4ac099a..eade5b1 100644 --- a/src/gamepad/screens/defaultScreen.cpp +++ b/src/gamepad/screens/defaultScreen.cpp @@ -2,6 +2,7 @@ #include "gamepad/screens/abstractScreen.hpp" #include "gamepad/todo.hpp" #include +#include #include #include #include @@ -22,6 +23,7 @@ ScreenBuffer DefaultScreen::getScreen(std::set visible_lines) { } int32_t DefaultScreen::printLine(uint8_t line, std::string str) { + int32_t ret_val = 0; if (line > 2) { TODO("add error logging") errno = EINVAL; @@ -31,7 +33,11 @@ int32_t DefaultScreen::printLine(uint8_t line, std::string str) { const std::lock_guard guard(m_mutex); if (str.find('\n') != std::string::npos) { - if (std::ranges::count(str, '\n') > 2) { TODO("add warn logging for too many lines") } + if (std::ranges::count(str, '\n') > 2) { + TODO("add warn logging for too many lines") + errno = EMSGSIZE; + ret_val = INT32_MAX; + } std::vector strs(3); std::stringstream ss(str); @@ -43,19 +49,19 @@ int32_t 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 0; + return ret_val; } m_current_buffer[line] = std::move(str); - return 0; + return ret_val; } int32_t DefaultScreen::rumble(std::string rumble_pattern) { - bool is_err = false; + int32_t ret_val = 0; if (rumble_pattern.size() > 8) { - TODO("add warn logging") - errno = EINVAL; - is_err = true; + TODO("add error logging") + errno = EMSGSIZE; + ret_val = INT32_MAX; rumble_pattern.resize(8); } @@ -67,8 +73,7 @@ int32_t DefaultScreen::rumble(std::string rumble_pattern) { std::lock_guard guard(m_mutex); m_current_buffer[3] = std::move(rumble_pattern); - if (is_err) return INT32_MAX; - else return 0; + return ret_val; } } // namespace gamepad \ No newline at end of file From 38aa8d933121d8d4172175a316c8d7037582d733 Mon Sep 17 00:00:00 2001 From: ion098 <146852218+ion098@users.noreply.github.com> Date: Sun, 2 Feb 2025 16:12:26 -0800 Subject: [PATCH 11/15] refactor(buttons): :heavy_minus_sign: Remove unneeded include --- src/gamepad/button.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/gamepad/button.cpp b/src/gamepad/button.cpp index 037b4db..f343eeb 100644 --- a/src/gamepad/button.cpp +++ b/src/gamepad/button.cpp @@ -2,7 +2,6 @@ #include "gamepad/todo.hpp" #include "pros/rtos.hpp" #include -#include namespace gamepad { _impl::EventHandler* Button::get_handler(EventType event) const { From 739c0f5ddb5c389ae1e97dd6443fad9c8d84c5d9 Mon Sep 17 00:00:00 2001 From: ion098 <146852218+ion098@users.noreply.github.com> Date: Sun, 2 Feb 2025 16:13:09 -0800 Subject: [PATCH 12/15] docs(display): :memo: Document all possible errno values --- include/gamepad/screens/alertScreen.hpp | 1 + include/gamepad/screens/defaultScreen.hpp | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/include/gamepad/screens/alertScreen.hpp b/include/gamepad/screens/alertScreen.hpp index ac4fb9e..3c8edc6 100644 --- a/include/gamepad/screens/alertScreen.hpp +++ b/include/gamepad/screens/alertScreen.hpp @@ -50,6 +50,7 @@ class AlertScreen : public AbstractScreen { * 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] + * EMSGSIZE: The alert is more than 3 lines long * * @return 0 if the alert was added successfully * @return INT32_MAX if there was an error, setting errno diff --git a/include/gamepad/screens/defaultScreen.hpp b/include/gamepad/screens/defaultScreen.hpp index 1f31381..6c46c82 100644 --- a/include/gamepad/screens/defaultScreen.hpp +++ b/include/gamepad/screens/defaultScreen.hpp @@ -35,6 +35,7 @@ class DefaultScreen : public AbstractScreen { * 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] + * EMSGSIZE: The string is more than 3 lines long * * @return 0 if the alert was added successfully * @return INT32_MAX if there was an error, setting errno @@ -46,6 +47,14 @@ class DefaultScreen : public AbstractScreen { * * @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 contains a character other than '.', '-', or ' ' + * EMSGSIZE: The pattern is more than 8 characters long + * + * @return 0 if the alert was added successfully + * @return INT32_MAX if there was an error, setting errno */ int32_t rumble(std::string rumble_pattern); private: From 0ffae91828beaf4b785dd0b25b695667dbcfe51c Mon Sep 17 00:00:00 2001 From: ion098 <146852218+ion098@users.noreply.github.com> Date: Sun, 2 Feb 2025 16:16:55 -0800 Subject: [PATCH 13/15] chore: :technologist: Add conventional commits scopes --- .gitignore | 5 ++++- .vscode/settings.json | 5 +++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 5419594..c64778c 100644 --- a/.gitignore +++ b/.gitignore @@ -8,7 +8,7 @@ # PROS bin/ -.vscode/ +.vscode/* .cache/ compile_commands.json temp.log @@ -24,3 +24,6 @@ temp.errors # Linux debug.log + +# Always include development settings +!.vscode/settings.json \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index b242572..0120171 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,10 @@ { "githubPullRequests.ignoredPullRequestBranches": [ "main" + ], + "conventionalCommits.scopes": [ + "buttons", + "display", + "joysticks" ] } \ No newline at end of file From 5ce9764aec45c52eb205299df494fc2a9b2460ce Mon Sep 17 00:00:00 2001 From: ion098 <146852218+ion098@users.noreply.github.com> Date: Sun, 2 Feb 2025 16:39:40 -0800 Subject: [PATCH 14/15] fix(display): :bug: Return error code from clear method --- include/gamepad/gamepad.hpp | 21 ++++++++++++++++++--- src/gamepad/gamepad.cpp | 2 +- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/include/gamepad/gamepad.hpp b/include/gamepad/gamepad.hpp index c14d34b..64bc7ea 100644 --- a/include/gamepad/gamepad.hpp +++ b/include/gamepad/gamepad.hpp @@ -52,6 +52,7 @@ class Gamepad { * 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] + * EMSGSIZE: The string is more than 3 lines long * * @b Example: * @code {.cpp} @@ -59,7 +60,7 @@ class Gamepad { * gamepad::master.printLine(0, "this will print\n\naround the middle line"); * @endcode * - * @return 0 if the alert was added successfully + * @return 0 if the line was printed successfully * @return INT32_MAX if there was an error, setting errno */ int32_t printLine(uint8_t line, std::string str); @@ -70,6 +71,7 @@ class Gamepad { * @code {.cpp} * // clears the whole screen on the controller * gamepad::master.clear() + * @endcode */ void clear(); /** @@ -77,12 +79,20 @@ class Gamepad { * * @param line the line to clear (0-2) * + * 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} * // clears the center line on the controller * gamepad::master.clear(1); + * @endcode + * + * @return 0 if the line was cleared successfully + * @return INT32_MAX if there was an error, setting errno */ - void clear(uint8_t line); + int32_t clear(uint8_t line); /** * makes the controller rumble like pros (low priority) * @@ -91,12 +101,17 @@ class Gamepad { * * This function uses the following value(s) of errno when an error state is reached: * - * EINVAL: The rumble pattern was truncated to 8 characters + * EINVAL: The rumble pattern contains a character other than '.', '-', or ' ' + * EMSGSIZE: The pattern is more than 8 characters long * * @b Example: * @code {.cpp} * // rumbles in the following pattern: short, pause, long, short short * gamepad::master.rumble(". -.."); + * @endcode + * + * @return 0 if the rumble was successful + * @return INT32_MAX if there was an error, setting errno */ void rumble(std::string rumble_pattern); /** diff --git a/src/gamepad/gamepad.cpp b/src/gamepad/gamepad.cpp index 69155c3..cbaa797 100644 --- a/src/gamepad/gamepad.cpp +++ b/src/gamepad/gamepad.cpp @@ -134,7 +134,7 @@ int32_t Gamepad::printLine(uint8_t line, std::string str) { return m_default_scr void Gamepad::clear() { m_default_screen->printLine(0, " \n \n "); } -void Gamepad::clear(uint8_t line) { m_default_screen->printLine(line, " "); } +int32_t Gamepad::clear(uint8_t line) { return m_default_screen->printLine(line, " "); } void Gamepad::rumble(std::string rumble_pattern) { m_default_screen->rumble(rumble_pattern); } From f584161ff9e51d4cf3a21d6428c13065f677b8a4 Mon Sep 17 00:00:00 2001 From: ion098 <146852218+ion098@users.noreply.github.com> Date: Sun, 2 Feb 2025 16:40:17 -0800 Subject: [PATCH 15/15] docs(display): :memo: Fix incorrect docs for error codes --- include/gamepad/button.hpp | 32 +++++++++++++++---------------- include/gamepad/event_handler.hpp | 6 +++--- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/include/gamepad/button.hpp b/include/gamepad/button.hpp index 1ebbd19..a0a63ac 100644 --- a/include/gamepad/button.hpp +++ b/include/gamepad/button.hpp @@ -73,8 +73,8 @@ class Button { * * @param listenerName The name of the listener, this must be a unique name * @param func The function to run when the button is pressed, the function MUST NOT block - * @return true The listener was successfully registered - * @return false The listener was not successfully registered (there is already a listener with this name) + * @return 0 The listener was successfully registered + * @return INT32_MAX The listener was not successfully registered (there is already a listener with this name) * * @b Example: * @code {.cpp} @@ -96,8 +96,8 @@ class Button { * * @param listenerName The name of the listener, this must be a unique name * @param func The function to run when the button is long pressed, the function MUST NOT block - * @return true The listener was successfully registered - * @return false The listener was not successfully registered (there is already a listener with this name) + * @return 0 The listener was successfully registered + * @return INT32_MAX The listener was not successfully registered (there is already a listener with this name) * * @b Example: * @code {.cpp} @@ -114,8 +114,8 @@ class Button { * * @param listenerName The name of the listener, this must be a unique name * @param func The function to run when the button is released, the function MUST NOT block - * @return true The listener was successfully registered - * @return false The listener was not successfully registered (there is already a listener with this name) + * @return 0 The listener was successfully registered + * @return INT32_MAX The listener was not successfully registered (there is already a listener with this name) * * @b Example: * @code {.cpp} @@ -136,8 +136,8 @@ class Button { * * @param listenerName The name of the listener, this must be a unique name * @param func The function to run when the button is short released, the function MUST NOT block - * @return true The listener was successfully registered - * @return false The listener was not successfully registered (there is already a listener with this name) + * @return 0 The listener was successfully registered + * @return INT32_MAX The listener was not successfully registered (there is already a listener with this name) * * @b Example: * @code {.cpp} @@ -156,8 +156,8 @@ class Button { * * @param listenerName The name of the listener, this must be a unique name * @param func The function to run when the button is long released, the function MUST NOT block - * @return true The listener was successfully registered - * @return false The listener was not successfully registered (there is already a listener with this name) + * @return 0 The listener was successfully registered + * @return INT32_MAX The listener was not successfully registered (there is already a listener with this name) * * @b Example: * @code {.cpp} @@ -177,8 +177,8 @@ class Button { * * @param listenerName The name of the listener, this must be a unique name * @param func the function to run periodically when the button is held, the function MUST NOT block - * @return true The listener was successfully registered - * @return false The listener was not successfully registered (there is already a listener with this name) + * @return 0 The listener was successfully registered + * @return INT32_MAX The listener was not successfully registered (there is already a listener with this name) * * @b Example: * @code {.cpp} @@ -196,8 +196,8 @@ class Button { * @param event Which event to register the listener on. * @param listenerName The name of the listener, this must be a unique name * @param func The function to run for the given event, the function MUST NOT block - * @return true The listener was successfully registered - * @return false The listener was not successfully registered (there is already a listener with this name) + * @return 0 The listener was successfully registered + * @return INT32_MAX The listener was not successfully registered (there is already a listener with this name) * * @b Example: * @code {.cpp} @@ -214,8 +214,8 @@ class Button { * * @param event the event type of the listener * @param listenerName The name of the listener to remove - * @return true The specified listener was successfully removed - * @return false The specified listener could not be removed + * @return 0 The specified listener was successfully removed + * @return INT32_MAX The specified listener could not be removed * * @b Example: * @code {.cpp} diff --git a/include/gamepad/event_handler.hpp b/include/gamepad/event_handler.hpp index c0b4d8f..c9f3d47 100644 --- a/include/gamepad/event_handler.hpp +++ b/include/gamepad/event_handler.hpp @@ -25,7 +25,7 @@ template class EventHandler { * @param key The listener key (this must be a unique key value) * @param func The function to run when this event is fired * @return 0 The listener was successfully added - * @return UINT32_MAX The listener was NOT successfully added (there is already a listener with the same key) + * @return INT32_MAX The listener was NOT successfully added (there is already a listener with the same key) */ int32_t addListener(Key key, Listener func) { std::lock_guard lock(m_mutex); @@ -40,7 +40,7 @@ template class EventHandler { * * @param key The listener key (this must be a unique key value) * @return 0 The listener was successfully removed - * @return UINT32_MAX The listener was NOT successfully removed (there is no listener with the same key) + * @return INT32_MAX The listener was NOT successfully removed (there is no listener with the same key) */ int32_t removeListener(Key key) { std::lock_guard lock(m_mutex); @@ -54,7 +54,7 @@ template class EventHandler { } /** - * @brief Whther or not there are any listeners registered + * @brief Whether or not there are any listeners registered * * @return true There are listeners registered * @return false There are no listeners registered