Skip to content

Commit

Permalink
Merge pull request #24 from LemLib/refactor/fix-naming
Browse files Browse the repository at this point in the history
refactor: ♻️ Use consistent naming scheme
  • Loading branch information
ion098 authored Jan 2, 2025
2 parents 4afd080 + 41d3ee3 commit f9f822f
Show file tree
Hide file tree
Showing 13 changed files with 199 additions and 194 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"
]
}
40 changes: 20 additions & 20 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,10 +89,10 @@ 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.
* and onLongPress listeners may fire together.
*
* @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
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 @@ -241,20 +241,20 @@ class Button {
*/
void update(bool is_held);
/// How long the threshold should be for the longPress and shortRelease events
mutable uint32_t long_press_threshold = 500;
mutable uint32_t m_long_press_threshold = 500;
/// How often repeatPress is called
mutable uint32_t repeat_cooldown = 50;
mutable uint32_t m_repeat_cooldown = 50;
/// The last time the update function was called
uint32_t last_update_time = pros::millis();
uint32_t m_last_update_time = pros::millis();
/// The last time the long press event was fired
uint32_t last_long_press_time = 0;
uint32_t m_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 {};
uint32_t m_last_repeat_time = 0;
mutable _impl::EventHandler<std::string> m_on_press_event {};
mutable _impl::EventHandler<std::string> m_on_long_press_event {};
mutable _impl::EventHandler<std::string> m_on_release_event {};
mutable _impl::EventHandler<std::string> m_on_short_release_event {};
mutable _impl::EventHandler<std::string> m_on_long_release_event {};
mutable _impl::EventHandler<std::string> m_on_repeat_press_event {};
};
} // namespace gamepad
38 changes: 19 additions & 19 deletions include/gamepad/event_handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ 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) {
std::lock_guard lock(mutex);
if (std::find(keys.begin(), keys.end(), key) != keys.end()) return false;
keys.push_back(key);
listeners.push_back(func);
bool 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;
m_keys.push_back(key);
m_listeners.push_back(func);
return true;
}

Expand All @@ -42,12 +42,12 @@ 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) {
std::lock_guard lock(mutex);
auto i = std::find(keys.begin(), keys.end(), key);
if (i != keys.end()) {
keys.erase(i);
listeners.erase(listeners.begin() + (i - keys.begin()));
bool 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 false;
Expand All @@ -59,9 +59,9 @@ template <typename Key, typename... Args> class EventHandler {
* @return true There are listeners registered
* @return false There are no listeners registered
*/
bool is_empty() {
std::lock_guard lock(mutex);
return listeners.empty();
bool isEmpty() {
std::lock_guard lock(m_mutex);
return m_listeners.empty();
}

/**
Expand All @@ -70,12 +70,12 @@ template <typename Key, typename... Args> class EventHandler {
* @param args The parameters to pass to each listener
*/
void fire(Args... args) {
std::lock_guard lock(mutex);
for (auto listener : listeners) { listener(args...); }
std::lock_guard lock(m_mutex);
for (auto listener : m_listeners) { listener(args...); }
}
private:
std::vector<Key> keys {};
std::vector<Listener> listeners {};
gamepad::_impl::RecursiveMutex mutex {};
std::vector<Key> m_keys {};
std::vector<Listener> m_listeners {};
gamepad::_impl::RecursiveMutex m_mutex {};
};
} // namespace gamepad::_impl
38 changes: 19 additions & 19 deletions include/gamepad/gamepad.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Gamepad {
*/
void update();
/**
* @brief Add a screen to the sceen update loop that can update the controller's screen
* @brief Add a screen to the screen update loop that can update the controller's screen
*
* @param screen the `AbstractScreen` to add to the screen queue
*
Expand All @@ -42,7 +42,7 @@ class Gamepad {
*
* gamepad::master.add_screen(alerts);
*/
void add_screen(std::shared_ptr<AbstractScreen> screen);
void addScreen(std::shared_ptr<AbstractScreen> screen);
/**
* @brief print a line to the console like pros (low priority)
*
Expand All @@ -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 @@ -83,7 +83,7 @@ class Gamepad {
*
* @b Example:
* @code {.cpp}
* // rumbles in the folllowing pattern: short, pause, long, short short
* // rumbles in the following pattern: short, pause, long, short short
* gamepad::master.rumble(". -..");
*/
void rumble(std::string rumble_pattern);
Expand Down Expand Up @@ -145,27 +145,27 @@ 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
*/
static std::string unique_name();
static Button Gamepad::*button_to_ptr(pros::controller_digital_e_t button);
static std::string uniqueName();
static Button Gamepad::*buttonToPtr(pros::controller_digital_e_t button);
void updateButton(pros::controller_digital_e_t button_id);

void updateScreens();

std::shared_ptr<DefaultScreen> defaultScreen = std::make_shared<DefaultScreen>();
std::vector<std::shared_ptr<AbstractScreen>> screens = {};
ScreenBuffer currentScreen = {};
ScreenBuffer nextBuffer = {};
pros::Controller controller;
std::shared_ptr<DefaultScreen> m_default_screen = std::make_shared<DefaultScreen>();
std::vector<std::shared_ptr<AbstractScreen>> m_screens = {};
ScreenBuffer m_current_screen = {};
ScreenBuffer m_next_buffer = {};
pros::Controller m_controller;

uint8_t last_printed_line = 0;
uint32_t last_print_time = 0;
uint32_t last_update_time = 0;
bool screenCleared = false;
pros::Mutex mut {};
uint8_t m_last_printed_line = 0;
uint32_t m_last_print_time = 0;
uint32_t m_last_update_time = 0;
bool m_screen_cleared = false;
pros::Mutex m_mutex {};
};

inline Gamepad Gamepad::master {pros::E_CONTROLLER_MASTER};
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 @@ -24,7 +24,7 @@ class RecursiveMutex {
bool take(std::uint32_t timeout = TIMEOUT_MAX) { return pros::c::mutex_recursive_take(mutex, timeout); }

/**
* @brief Locks the mutex, waiting indefinetely until the mutex is acquired
* @brief Locks the mutex, waiting indefinitely until the mutex is acquired
*
*/
void lock() {
Expand Down
14 changes: 7 additions & 7 deletions include/gamepad/screens/abstractScreen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ typedef std::array<std::optional<std::string>, 4> ScreenBuffer;
class AbstractScreen {
public:
AbstractScreen(uint32_t priority)
: priority(priority) {}
: m_priority(priority) {}

/**
* @brief runs every time the controller's update function is called
* use this if you need to update somthing regardless of if there is an
* use this if you need to update something regardless of if there is an
* available slot in the screen
*
* @param delta_time the time since the last update in milliseconds
Expand All @@ -41,23 +41,23 @@ 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
* @warning it is not recommended to override this function
*/
uint32_t get_priority() { return this->priority; }
uint32_t getPriority() { return m_priority; }
protected:
const uint32_t priority;
const uint32_t m_priority;
};

} // namespace gamepad
12 changes: 6 additions & 6 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,17 +47,17 @@ 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;
uint32_t duration;
};

std::deque<AlertBuffer> screen_buffer {};
std::optional<AlertBuffer> screen_contents {};
uint32_t line_set_time = 0;
pros::Mutex mut {};
std::deque<AlertBuffer> m_screen_buffer {};
std::optional<AlertBuffer> m_screen_contents {};
uint32_t m_line_set_time = 0;
pros::Mutex m_mutex {};
};

} // namespace gamepad
8 changes: 4 additions & 4 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,8 +42,8 @@ class DefaultScreen : public AbstractScreen {
*/
void rumble(std::string rumble_pattern);
private:
ScreenBuffer currentBuffer {};
pros::Mutex mut {};
ScreenBuffer m_current_buffer {};
pros::Mutex m_mutex {};
};

} // namespace gamepad
Loading

0 comments on commit f9f822f

Please sign in to comment.