Skip to content

Commit

Permalink
refactor: ♻️ change method names to use lowerCamelCase
Browse files Browse the repository at this point in the history
  • Loading branch information
ion098 authored Dec 16, 2024
1 parent 4afd080 commit d2c4702
Show file tree
Hide file tree
Showing 13 changed files with 74 additions and 69 deletions.
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"githubPullRequests.ignoredPullRequestBranches": [
"main"
]
}
28 changes: 14 additions & 14 deletions include/gamepad/button.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ class Button {
* @b Example:
* @code {.cpp}
* // change the threshold
* gamepad::master.Left.set_long_press_threshold(5000);
* gamepad::master.Left.setLongPressThreshold(5000);
* // then call the function
* gamepad::master.Left.onLongPress("longPress1", []() {
* std::cout << "I was held for 5000ms instead of the 500ms default!" << std::endl;
* });
* @endcode
*/
void set_long_press_threshold(uint32_t threshold) const;
void setLongPressThreshold(uint32_t threshold) const;
/**
* @brief Set the interval for the repeatPress event to repeat
*
Expand All @@ -60,14 +60,14 @@ class Button {
* @b Example:
* @code {.cpp}
* // change the threshold
* gamepad::master.Up.set_repeat_cooldown(100);
* gamepad::master.Up.setRepeatCooldown(100);
* // then call the function
* gamepad::master.Up.onRepeatPress("repeatPress1", []() {
* std::cout << "I'm being repeated every 100ms instead of the 50ms default!" << std::endl;
* });
* @endcode
*/
void set_repeat_cooldown(uint32_t cooldown) const;
void setRepeatCooldown(uint32_t cooldown) const;
/**
* @brief Register a function to run when the button is pressed.
*
Expand All @@ -89,7 +89,7 @@ class Button {
* @brief Register a function to run when the button is long pressed.
*
* By default, onLongPress will fire when the button has been held down for
* 500ms or more, this threshold can be adjusted via the set_long_press_threshold() method.
* 500ms or more, this threshold can be adjusted via the setLongPressThreshold() method.
*
* @warning When using this event along with onPress, both the onPress
* and onlongPress listeners may fire together.
Expand Down Expand Up @@ -130,7 +130,7 @@ class Button {
* @brief Register a function to run when the button is short released.
*
* By default, shortRelease will fire when the button has been released before 500ms, this threshold can be
* adjusted via the set_long_press_threshold() method.
* adjusted via the setLongPressThreshold() method.
*
* @note This event will most likely be used along with the longPress event.
*
Expand All @@ -152,7 +152,7 @@ class Button {
* @brief Register a function to run when the button is long released.
*
* By default, longRelease will fire when the button has been released after 500ms, this threshold can be
* adjusted via the set_long_press_threshold() method.
* adjusted via the setLongPressThreshold() method.
*
* @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
Expand All @@ -173,7 +173,7 @@ class Button {
* @brief Register a function to run periodically after its been held
*
* By default repeatPress will start repeating after 500ms and repeat every 50ms, this can be adjusted via the
* set_long_press_threshold() and set_repeat_cooldown() methods respectively
* setLongPressThreshold() and setRepeatCooldown() methods respectively
*
* @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
Expand Down Expand Up @@ -250,11 +250,11 @@ class Button {
uint32_t last_long_press_time = 0;
/// The last time the repeat event was called
uint32_t last_repeat_time = 0;
mutable _impl::EventHandler<std::string> onPressEvent {};
mutable _impl::EventHandler<std::string> onLongPressEvent {};
mutable _impl::EventHandler<std::string> onReleaseEvent {};
mutable _impl::EventHandler<std::string> onShortReleaseEvent {};
mutable _impl::EventHandler<std::string> onLongReleaseEvent {};
mutable _impl::EventHandler<std::string> onRepeatPressEvent {};
mutable _impl::EventHandler<std::string> on_press_event {};
mutable _impl::EventHandler<std::string> on_long_press_event {};
mutable _impl::EventHandler<std::string> on_release_event {};
mutable _impl::EventHandler<std::string> on_short_release_event {};
mutable _impl::EventHandler<std::string> on_long_release_event {};
mutable _impl::EventHandler<std::string> on_repeat_press_event {};
};
} // namespace gamepad
6 changes: 3 additions & 3 deletions include/gamepad/event_handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ template <typename Key, typename... Args> class EventHandler {
* @return true The listener was successfully added
* @return false The listener was NOT successfully added (there is already a listener with the same key)
*/
bool add_listener(Key key, Listener func) {
bool addListener(Key key, Listener func) {
std::lock_guard lock(mutex);
if (std::find(keys.begin(), keys.end(), key) != keys.end()) return false;
keys.push_back(key);
Expand All @@ -42,7 +42,7 @@ template <typename Key, typename... Args> class EventHandler {
* @return true The listener was successfully removed
* @return false The listener was NOT successfully removed (there is no listener with the same key)
*/
bool remove_listener(Key key) {
bool removeListener(Key key) {
std::lock_guard lock(mutex);
auto i = std::find(keys.begin(), keys.end(), key);
if (i != keys.end()) {
Expand All @@ -59,7 +59,7 @@ template <typename Key, typename... Args> class EventHandler {
* @return true There are listeners registered
* @return false There are no listeners registered
*/
bool is_empty() {
bool isEmpty() {
std::lock_guard lock(mutex);
return listeners.empty();
}
Expand Down
8 changes: 4 additions & 4 deletions include/gamepad/gamepad.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ class Gamepad {
*
* @b Example:
* @code {.cpp}
* gamepad::master.print_line(1, "This will print on the middle line");
* gamepad::master.print_line(0, "this will print\n\naround the middle line");
* gamepad::master.printLine(1, "This will print on the middle line");
* gamepad::master.printLine(0, "this will print\n\naround the middle line");
*/
void print_line(uint8_t line, std::string str);
void printLine(uint8_t line, std::string str);
/**
* @brief clears all lines on the controller, similar to the pros function (low priority)
*
Expand Down Expand Up @@ -145,7 +145,7 @@ class Gamepad {
* @brief Gets a unique name for a listener that will not conflict with user listener names.
*
* @important: when using the function, you must register the listener by
* directly calling add_listener on the EventHandler, do NOT use onPress/addListener,etc.
* directly calling addListener on the EventHandler, do NOT use onPress/addListener,etc.
*
* @return std::string A unique listener name
*/
Expand Down
2 changes: 1 addition & 1 deletion include/gamepad/recursive_mutex.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class RecursiveMutex {
* @return true The mutex was successfully acquired
* @return false The mutex was not successfully acquired
*/
bool try_lock() { return this->take(0); }
bool tryLock() { return this->take(0); }

/**
* @brief Unlocks the mutex
Expand Down
6 changes: 3 additions & 3 deletions include/gamepad/screens/abstractScreen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,21 @@ class AbstractScreen {
*
* @returns a the lines to be printed, any lines that are not available will be ignored
*/
virtual ScreenBuffer get_screen(std::set<uint8_t> visible_lines) = 0;
virtual ScreenBuffer getScreen(std::set<uint8_t> visible_lines) = 0;

/**
* @brief a function where button events are pushed, use this to handle button events.
*
* @param button_events a set of the button events that happened this update
*/
virtual void handle_events(std::set<pros::controller_digital_e_t> button_events) {}
virtual void handleEvents(std::set<pros::controller_digital_e_t> button_events) {}

/**
* @brief returns the priority of the screen
*
* @important it is not reccomended to override this function
*/
uint32_t get_priority() { return this->priority; }
uint32_t getPriority() { return this->priority; }
protected:
const uint32_t 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 @@ -35,7 +35,7 @@ class AlertScreen : public AbstractScreen {
*
* @returns a the lines to be printed, any lines that are not available will be ignored
*/
ScreenBuffer get_screen(std::set<uint8_t> visible_lines);
ScreenBuffer getScreen(std::set<uint8_t> visible_lines);

/**
* @brief add an alert to the alert queue, to be printed as soon as there is an available space
Expand All @@ -47,7 +47,7 @@ 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.
*/
void add_alerts(uint8_t line, std::string strs, uint32_t duration, std::string rumble = "");
void addAlerts(uint8_t line, std::string strs, uint32_t duration, std::string rumble = "");
private:
struct AlertBuffer {
ScreenBuffer screen;
Expand Down
6 changes: 3 additions & 3 deletions include/gamepad/screens/defaultScreen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ class DefaultScreen : public AbstractScreen {
*
* @returns a the lines to be printed, any lines that are not available will be ignored
*/
ScreenBuffer get_screen(std::set<uint8_t> visible_lines);
ScreenBuffer getScreen(std::set<uint8_t> visible_lines);

/**
* @brief print a line to the console like pros
*
* @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)
*/
void print_line(uint8_t line, std::string str);
void printLine(uint8_t line, std::string str);

/**
* makes the controller rumble like pros
Expand All @@ -42,7 +42,7 @@ class DefaultScreen : public AbstractScreen {
*/
void rumble(std::string rumble_pattern);
private:
ScreenBuffer currentBuffer {};
ScreenBuffer current_buffer {};
pros::Mutex mut {};
};

Expand Down
40 changes: 20 additions & 20 deletions src/gamepad/button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,32 @@
#include <cstdint>

namespace gamepad {
void Button::set_long_press_threshold(uint32_t threshold) const { this->long_press_threshold = threshold; }
void Button::setLongPressThreshold(uint32_t threshold) const { this->long_press_threshold = threshold; }

void Button::set_repeat_cooldown(uint32_t cooldown) const { this->repeat_cooldown = cooldown; }
void Button::setRepeatCooldown(uint32_t cooldown) const { this->repeat_cooldown = cooldown; }

bool Button::onPress(std::string listenerName, std::function<void(void)> func) const {
return this->onPressEvent.add_listener(std::move(listenerName) + "_user", std::move(func));
return this->on_press_event.addListener(std::move(listenerName) + "_user", std::move(func));
}

bool Button::onLongPress(std::string listenerName, std::function<void(void)> func) const {
return this->onLongPressEvent.add_listener(std::move(listenerName) + "_user", std::move(func));
return this->on_long_press_event.addListener(std::move(listenerName) + "_user", std::move(func));
}

bool Button::onRelease(std::string listenerName, std::function<void(void)> func) const {
return this->onReleaseEvent.add_listener(std::move(listenerName) + "_user", std::move(func));
return this->on_release_event.addListener(std::move(listenerName) + "_user", std::move(func));
}

bool Button::onShortRelease(std::string listenerName, std::function<void(void)> func) const {
return this->onShortReleaseEvent.add_listener(std::move(listenerName) + "_user", std::move(func));
return this->on_short_release_event.addListener(std::move(listenerName) + "_user", std::move(func));
}

bool Button::onLongRelease(std::string listenerName, std::function<void(void)> func) const {
return this->onLongReleaseEvent.add_listener(std::move(listenerName) + "_user", std::move(func));
return this->on_long_release_event.addListener(std::move(listenerName) + "_user", std::move(func));
}

bool Button::onRepeatPress(std::string listenerName, std::function<void(void)> func) const {
return this->onRepeatPressEvent.add_listener(std::move(listenerName) + "_user", std::move(func));
return this->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 {
Expand All @@ -49,12 +49,12 @@ bool Button::addListener(EventType event, std::string listenerName, std::functio
}

bool Button::removeListener(std::string listenerName) const {
return this->onPressEvent.remove_listener(listenerName + "_user") ||
this->onLongPressEvent.remove_listener(listenerName + "_user") ||
this->onReleaseEvent.remove_listener(listenerName + "_user") ||
this->onShortReleaseEvent.remove_listener(listenerName + "_user") ||
this->onLongReleaseEvent.remove_listener(listenerName + "_user") ||
this->onRepeatPressEvent.remove_listener(listenerName + "_user");
return this->on_press_event.removeListener(listenerName + "_user") ||
this->on_long_press_event.removeListener(listenerName + "_user") ||
this->on_release_event.removeListener(listenerName + "_user") ||
this->on_short_release_event.removeListener(listenerName + "_user") ||
this->on_long_release_event.removeListener(listenerName + "_user") ||
this->on_repeat_press_event.removeListener(listenerName + "_user");
}

void Button::update(const bool is_held) {
Expand All @@ -65,22 +65,22 @@ void Button::update(const bool is_held) {
else this->time_released += pros::millis() - this->last_update_time;

if (this->rising_edge) {
this->onPressEvent.fire();
this->on_press_event.fire();
} else if (this->is_pressed && this->time_held >= this->long_press_threshold &&
this->last_long_press_time <= pros::millis() - this->time_held) {
this->onLongPressEvent.fire();
this->on_long_press_event.fire();
this->last_long_press_time = pros::millis();
this->last_repeat_time = pros::millis() - this->repeat_cooldown;
this->repeat_iterations = 0;
} else if (this->is_pressed && this->time_held >= this->long_press_threshold &&
pros::millis() - this->last_repeat_time >= this->repeat_cooldown) {
this->repeat_iterations++;
this->onRepeatPressEvent.fire();
this->on_repeat_press_event.fire();
this->last_repeat_time = pros::millis();
} else if (this->falling_edge) {
this->onReleaseEvent.fire();
if (this->time_held < this->long_press_threshold) this->onShortReleaseEvent.fire();
else this->onLongReleaseEvent.fire();
this->on_release_event.fire();
if (this->time_held < this->long_press_threshold) this->on_short_release_event.fire();
else this->on_long_release_event.fire();
}

if (this->rising_edge) this->time_held = 0;
Expand Down
14 changes: 7 additions & 7 deletions src/gamepad/gamepad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void Gamepad::updateScreens() {
// Update all screens, and send new button presses, also note deltatime
for (std::shared_ptr<AbstractScreen> screen : this->screens) {
screen->update(pros::millis() - this->last_update_time);
screen->handle_events(buttonUpdates);
screen->handleEvents(buttonUpdates);
}
this->last_update_time = pros::millis();

Expand All @@ -69,7 +69,7 @@ void Gamepad::updateScreens() {
if (!this->nextBuffer[j].has_value()) visible_lines.emplace(j);

// get the buffer of the next lower priority screen and set it to be printed
ScreenBuffer buffer = screen->get_screen(visible_lines);
ScreenBuffer buffer = screen->getScreen(visible_lines);
for (uint8_t j = 0; j < 4; j++)
if (buffer[j].has_value() && !buffer[j]->empty() && !nextBuffer[j].has_value())
nextBuffer[j] = std::move(buffer[j]);
Expand Down Expand Up @@ -124,17 +124,17 @@ void Gamepad::add_screen(std::shared_ptr<AbstractScreen> screen) {
uint32_t last = UINT32_MAX;
uint32_t pos = 0;
for (pos = 0; pos < this->screens.size(); pos++) {
if (this->screens[pos]->get_priority() < screen->get_priority() && last >= screen->get_priority()) break;
last = this->screens[pos]->get_priority();
if (this->screens[pos]->getPriority() < screen->getPriority() && last >= screen->getPriority()) break;
last = this->screens[pos]->getPriority();
}
this->screens.emplace(this->screens.begin() + pos, screen);
}

void Gamepad::print_line(uint8_t line, std::string str) { this->defaultScreen->print_line(line, str); }
void Gamepad::printLine(uint8_t line, std::string str) { this->defaultScreen->printLine(line, str); }

void Gamepad::clear() { this->defaultScreen->print_line(0, " \n \n "); }
void Gamepad::clear() { this->defaultScreen->printLine(0, " \n \n "); }

void Gamepad::clear(uint8_t line) { this->defaultScreen->print_line(line, " "); }
void Gamepad::clear(uint8_t line) { this->defaultScreen->printLine(line, " "); }

void Gamepad::rumble(std::string rumble_pattern) { this->defaultScreen->rumble(rumble_pattern); }

Expand Down
4 changes: 2 additions & 2 deletions src/gamepad/screens/alertScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace gamepad {

ScreenBuffer AlertScreen::get_screen(std::set<uint8_t> visible_lines) {
ScreenBuffer AlertScreen::getScreen(std::set<uint8_t> visible_lines) {
std::lock_guard<pros::Mutex> guard(this->mut);
if (this->screen_contents.has_value()) {
this->screen_contents->screen.at(3) = std::nullopt;
Expand All @@ -32,7 +32,7 @@ void AlertScreen::update(uint32_t delta_time) {
if (pros::millis() - this->line_set_time >= this->screen_contents->duration) this->screen_contents = std::nullopt;
}

void AlertScreen::add_alerts(uint8_t line, std::string str, uint32_t duration, std::string rumble) {
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);

Expand Down
Loading

0 comments on commit d2c4702

Please sign in to comment.