Skip to content

Commit

Permalink
refactor: ♻️ Change functions to return 0 on success
Browse files Browse the repository at this point in the history
  • Loading branch information
ion098 committed Jan 8, 2025
1 parent 1473733 commit 277b084
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 41 deletions.
16 changes: 8 additions & 8 deletions include/gamepad/button.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<void(void)> func) const;
uint32_t onPress(std::string listenerName, std::function<void(void)> func) const;
/**
* @brief Register a function to run when the button is long pressed.
*
Expand All @@ -108,7 +108,7 @@ class Button {
* std::endl; });
* @endcode
*/
bool onLongPress(std::string listenerName, std::function<void(void)> func) const;
uint32_t onLongPress(std::string listenerName, std::function<void(void)> func) const;
/**
* @brief Register a function to run when the button is released.
*
Expand All @@ -125,7 +125,7 @@ class Button {
* gamepad::master.Y.onRelease("stopIntake", []() { intake.move(0); });
* @endcode
*/
bool onRelease(std::string listenerName, std::function<void(void)> func) const;
uint32_t onRelease(std::string listenerName, std::function<void(void)> func) const;
/**
* @brief Register a function to run when the button is short released.
*
Expand All @@ -147,7 +147,7 @@ class Button {
* gamepad::master.B.onShortRelease("intakeOnePiece", []() { intake.move_relative(600, 100); });
* @endcode
*/
bool onShortRelease(std::string listenerName, std::function<void(void)> func) const;
uint32_t onShortRelease(std::string listenerName, std::function<void(void)> func) const;
/**
* @brief Register a function to run when the button is long released.
*
Expand All @@ -168,7 +168,7 @@ class Button {
* @endcode
*
*/
bool onLongRelease(std::string listenerName, std::function<void(void)> func) const;
uint32_t onLongRelease(std::string listenerName, std::function<void(void)> func) const;
/**
* @brief Register a function to run periodically after its been held
*
Expand All @@ -189,7 +189,7 @@ class Button {
* @endcode
*
*/
bool onRepeatPress(std::string listenerName, std::function<void(void)> func) const;
uint32_t onRepeatPress(std::string listenerName, std::function<void(void)> func) const;
/**
* @brief Register a function to run for a given event.
*
Expand All @@ -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<void(void)> func) const;
uint32_t addListener(EventType event, std::string listenerName, std::function<void(void)> func) const;
/**
* @brief Removes a listener from the button
* @warning Usage of this function is discouraged.
Expand All @@ -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.
Expand Down
20 changes: 10 additions & 10 deletions include/gamepad/event_handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,33 @@ template <typename Key, typename... Args> 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;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions include/gamepad/gamepad.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
*
Expand Down
4 changes: 2 additions & 2 deletions include/gamepad/screens/alertScreen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions include/gamepad/screens/defaultScreen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 9 additions & 9 deletions src/gamepad/button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<void(void)> func) const {
uint32_t Button::onPress(std::string listenerName, std::function<void(void)> func) const {
return m_on_press_event.addListener(std::move(listenerName) + "_user", std::move(func));
}

bool Button::onLongPress(std::string listenerName, std::function<void(void)> func) const {
uint32_t Button::onLongPress(std::string listenerName, std::function<void(void)> func) const {
return m_on_long_press_event.addListener(std::move(listenerName) + "_user", std::move(func));
}

bool Button::onRelease(std::string listenerName, std::function<void(void)> func) const {
uint32_t Button::onRelease(std::string listenerName, std::function<void(void)> func) const {
return m_on_release_event.addListener(std::move(listenerName) + "_user", std::move(func));
}

bool Button::onShortRelease(std::string listenerName, std::function<void(void)> func) const {
uint32_t Button::onShortRelease(std::string listenerName, std::function<void(void)> func) const {
return m_on_short_release_event.addListener(std::move(listenerName) + "_user", std::move(func));
}

bool Button::onLongRelease(std::string listenerName, std::function<void(void)> func) const {
uint32_t Button::onLongRelease(std::string listenerName, std::function<void(void)> func) const {
return m_on_long_release_event.addListener(std::move(listenerName) + "_user", std::move(func));
}

bool Button::onRepeatPress(std::string listenerName, std::function<void(void)> func) const {
uint32_t Button::onRepeatPress(std::string listenerName, std::function<void(void)> 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<void(void)> func) const {
uint32_t Button::addListener(EventType event, std::string listenerName, std::function<void(void)> 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));
Expand All @@ -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") ||
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);
}

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

Expand Down
6 changes: 3 additions & 3 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;
}

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") }
Expand All @@ -58,7 +58,7 @@ bool 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;
return 0;
}

} // namespace gamepad
10 changes: 6 additions & 4 deletions src/gamepad/screens/defaultScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "gamepad/screens/abstractScreen.hpp"
#include "gamepad/todo.hpp"
#include <algorithm>
#include <cstdint>
#include <cstdio>
#include <mutex>
#include <optional>
Expand All @@ -20,11 +21,11 @@ ScreenBuffer DefaultScreen::getScreen(std::set<uint8_t> 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<pros::Mutex> guard(m_mutex);
Expand All @@ -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);
}
Expand Down

0 comments on commit 277b084

Please sign in to comment.