Skip to content

Commit

Permalink
Merge pull request #1 from PA055/text
Browse files Browse the repository at this point in the history
feat ✨ Add controller text support
  • Loading branch information
ion098 authored Dec 7, 2024
2 parents 6e70274 + 1febcc0 commit 4afd080
Show file tree
Hide file tree
Showing 12 changed files with 584 additions and 119 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ temp.errors

# MacOS
.DS_Store

# Linux
debug.log
3 changes: 2 additions & 1 deletion include/gamepad/api.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once

#include "gamepad/event_handler.hpp" // IWYU pragma: export
#include "gamepad/controller.hpp" // IWYU pragma: export
#include "gamepad/gamepad.hpp" // IWYU pragma: export
#include "gamepad/screens/alertScreen.hpp" // IWYU pragma: export
79 changes: 76 additions & 3 deletions include/gamepad/controller.hpp → include/gamepad/gamepad.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
#pragma once

#include "pros/misc.h"
#include "screens/defaultScreen.hpp"
#include <cstdint>
#include <string>

#include <memory>
#include <vector>
#include "screens/abstractScreen.hpp"
#include "button.hpp"
#include "pros/misc.hpp"

Expand All @@ -26,6 +30,63 @@ class Gamepad {
*
*/
void update();
/**
* @brief Add a screen to the sceen update loop that can update the controller's screen
*
* @param screen the `AbstractScreen` to add to the screen queue
*
* @b Example:
* @code {.cpp}
* // initialize the alerts screen so we can have alerts on the controller
* std::shared_ptr<gamepad::AlertScreen> alerts = std::make_shared<gamepad::AlertScreen>();
*
* gamepad::master.add_screen(alerts);
*/
void add_screen(std::shared_ptr<AbstractScreen> screen);
/**
* @brief print a line to the console like pros (low priority)
*
* @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)
*
* @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");
*/
void print_line(uint8_t line, std::string str);
/**
* @brief clears all lines on the controller, similar to the pros function (low priority)
*
* @b Example:
* @code {.cpp}
* // clears the whole screen on the controller
* gamepad::master.clear()
*/
void clear();
/**
* @brief clears the specific line on the controller, similar to the pros function clear_line (low priority)
*
* @param line the line to clear (0-2)
*
* @b Example:
* @code {.cpp}
* // clears the center line on the controller
* gamepad::master.clear(1);
*/
void clear(uint8_t line);
/**
* makes the controller rumble like pros (low priority)
*
* @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.
*
* @b Example:
* @code {.cpp}
* // rumbles in the folllowing pattern: short, pause, long, short short
* gamepad::master.rumble(". -..");
*/
void rumble(std::string rumble_pattern);
/**
* @brief Get the state of a button on the controller.
*
Expand Down Expand Up @@ -74,8 +135,7 @@ class Gamepad {
/// The partner controller, same as @ref gamepad::partner
static Gamepad partner;
private:
Gamepad(pros::controller_id_e_t id)
: controller(id) {}
Gamepad(pros::controller_id_e_t id);

Button m_L1 {}, m_L2 {}, m_R1 {}, m_R2 {}, m_Up {}, m_Down {}, m_Left {}, m_Right {}, m_X {}, m_B {}, m_Y {},
m_A {};
Expand All @@ -92,7 +152,20 @@ class Gamepad {
static std::string unique_name();
static Button Gamepad::*button_to_ptr(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;

uint8_t last_printed_line = 0;
uint32_t last_print_time = 0;
uint32_t last_update_time = 0;
bool screenCleared = false;
pros::Mutex mut {};
};

inline Gamepad Gamepad::master {pros::E_CONTROLLER_MASTER};
Expand Down
63 changes: 63 additions & 0 deletions include/gamepad/screens/abstractScreen.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#pragma once

#include <array>
#include <cstdint>
#include <optional>
#include <set>
#include <string>
#include "pros/misc.h"

namespace gamepad {

/**
* @brief type for conveying a full screen with the first 3 being the lines
* of text on the controller screen and the last being a rumble pattern
*/
typedef std::array<std::optional<std::string>, 4> ScreenBuffer;

/**
* @brief The abstract class for interacting with the controller screen
*
*/
class AbstractScreen {
public:
AbstractScreen(uint32_t priority)
: 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
* available slot in the screen
*
* @param delta_time the time since the last update in milliseconds
*/
virtual void update(uint32_t delta_time) {}

/**
* @brief runs if there is an empty line that is available to print
*
* @param visible_lines a set that contains the line numbers of all lines that
* are empty and available for printing
*
* @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;

/**
* @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) {}

/**
* @brief returns the priority of the screen
*
* @important it is not reccomended to override this function
*/
uint32_t get_priority() { return this->priority; }
protected:
const uint32_t priority;
};

} // namespace gamepad
63 changes: 63 additions & 0 deletions include/gamepad/screens/alertScreen.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#pragma once

#include <cstdint>
#include <deque>
#include <optional>
#include <string>
#include "abstractScreen.hpp"
#include "pros/rtos.hpp"
#include "gamepad/screens/abstractScreen.hpp"

namespace gamepad {

/**
* @brief a screen that sends alerts to the controller, duration of alerts can be customized
*
* @note priority: UINT32_MAX - 100
*/
class AlertScreen : public AbstractScreen {
public:
AlertScreen()
: AbstractScreen(UINT32_MAX - 100) {}

/**
* @brief updates the alert loop
*
* @param delta_time the time since the last update
*/
void update(uint32_t delta_time);

/**
* @brief return the next alert to print if there is space for it on the screen
*
* @param visible_lines a set that contains the line numbers of all lines that
* are empty and available for printing
*
* @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);

/**
* @brief add an alert to the alert queue, to be printed as soon as there is an available space
*
* @param line the line number to print the alert at (0-2)
* @param strs the string to print on the controller, "\n" to go to the next line
* lines that go over 2 will be cropped out
* @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.
*/
void add_alerts(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 {};
};

} // namespace gamepad
49 changes: 49 additions & 0 deletions include/gamepad/screens/defaultScreen.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#pragma once

#include "gamepad/screens/abstractScreen.hpp"
#include "pros/rtos.hpp"

namespace gamepad {

/**
* @brief A basic screen that allows basic prints, similar to pros controller api
*
* @note The gamepad class has wrappers around this class
* @note priority: 1
*/
class DefaultScreen : public AbstractScreen {
public:
DefaultScreen()
: AbstractScreen(1) {}

/**
* @brief returns any lines that have space to print on the controller
*
* @param visible_lines a set that contains the line numbers of all lines that
* are empty and available for printing
*
* @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);

/**
* @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);

/**
* makes the controller rumble like pros
*
* @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);
private:
ScreenBuffer currentBuffer {};
pros::Mutex mut {};
};

} // namespace gamepad
3 changes: 1 addition & 2 deletions src/gamepad/button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include "gamepad/todo.hpp"
#include "pros/rtos.hpp"
#include <cstdint>
#include <sys/types.h>

namespace gamepad {
void Button::set_long_press_threshold(uint32_t threshold) const { this->long_press_threshold = threshold; }
Expand Down Expand Up @@ -88,4 +87,4 @@ void Button::update(const bool is_held) {
if (this->falling_edge) this->time_released = 0;
this->last_update_time = pros::millis();
}
} // namespace gamepad
} // namespace gamepad
65 changes: 0 additions & 65 deletions src/gamepad/controller.cpp

This file was deleted.

Loading

0 comments on commit 4afd080

Please sign in to comment.