Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat ✨: Add controller text support #1

Merged
merged 77 commits into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from 76 commits
Commits
Show all changes
77 commits
Select commit Hold shift + click to select a range
dee3d53
add buffer and handling
PA055 May 22, 2024
246c781
added line cycleing
PA055 May 22, 2024
e129183
fix critical bug :)
PA055 May 22, 2024
1042eba
Merge branch 'main' into text
ion098 May 22, 2024
2b8d9c9
add rumble and make clarity changes
PA055 May 22, 2024
4be8668
Merge branch 'text' of https://github.com/PA055/Gamepad into text
PA055 May 22, 2024
3fc76b4
merge changes
PA055 May 22, 2024
0d64b62
oops, forgot the rumble function
PA055 May 22, 2024
f04764b
check for rumble pattern length
PA055 May 23, 2024
c5a8982
changed std::uint... to uint...
PA055 May 23, 2024
acda18e
Minor change to formatting to trigger pros-build action
ion098 May 23, 2024
dd053c2
Merge branch 'LemLib:main' into text
PA055 May 23, 2024
26053a5
Fixed off by one error in Gamepad::updateScreen
ion098 May 24, 2024
735acae
add rumble to cycle
PA055 May 26, 2024
3817758
Merge branch 'text' of https://github.com/PA055/Gamepad into text
PA055 May 26, 2024
d457248
Merge branch 'LemLib:main' into text
PA055 Jun 3, 2024
d1394b3
seperate alerts and prints
PA055 Jun 4, 2024
5cdd7a1
change rumble from alert like to print like
PA055 Jun 8, 2024
2bc3862
add multi line alerts
PA055 Jun 26, 2024
1e8182e
add ability to have newlines in alerts
PA055 Jun 26, 2024
ec5c814
make add_alerts private
PA055 Jun 27, 2024
599d3c1
remove unnessasary lines parameter
PA055 Jun 28, 2024
20138bb
add multiline prints
PA055 Jul 6, 2024
e73d095
add rumbles to alerts
PA055 Jul 8, 2024
57371a6
refactored screen update loop
PA055 Jul 8, 2024
022b4df
add thread safety... hopefully?
PA055 Jul 8, 2024
a84efd3
Merge branch 'main' into text
ion098 Jul 20, 2024
b8eb242
some comments
PA055 Jul 27, 2024
f0fa75f
Merge branch 'text' of https://github.com/PA055/Gamepad into text
PA055 Jul 28, 2024
12f4d6f
finished abstract screen interface
PA055 Aug 12, 2024
7781c59
moved the stuff that needs to be moved... kinda
PA055 Aug 13, 2024
18a4ed6
why am i getting these errors💔
PA055 Aug 13, 2024
66945cf
idk what happened but no errors now, also screen update works hopefully
PA055 Aug 13, 2024
4b17c36
shared pointers :sparkles:
PA055 Aug 13, 2024
6c90b92
it builds now
PA055 Aug 13, 2024
8dbb732
alerts should work
PA055 Aug 18, 2024
3929050
Merge branch 'main' into text
ion098 Aug 23, 2024
23c4bbf
what should i do for this?
PA055 Sep 4, 2024
bf82fef
Merge branch 'text' of https://github.com/PA055/Gamepad into text
PA055 Sep 4, 2024
790929c
bug fixing :sob:
PA055 Sep 10, 2024
d82ef22
help :skull:
PA055 Sep 10, 2024
385fc82
why do i need to commit debug.log :interrobang:
PA055 Sep 10, 2024
8395b9b
Merge branch 'main' into text
PA055 Sep 10, 2024
d5b226a
idk but this breaks it
PA055 Sep 10, 2024
466b8e1
bug: :bug: explicit controller constructor isnt getting called
PA055 Sep 21, 2024
9bfcbb3
fix: :construction: it might work
PA055 Sep 25, 2024
4958fa1
feat: :construction_worker: add vexide sim devcontainer
PA055 Sep 25, 2024
2797d2c
feat: 👷 add launch configs and tasks
PA055 Sep 26, 2024
76f1af3
feat: :construction: Hot/Cold linking breaks it
PA055 Sep 26, 2024
4a70a4a
fix: :construction: reenable hot-cold
ion098 Sep 27, 2024
c50258f
refactor: :twisted_rightwards_arrows: Merge branch 'main' into text
PA055 Sep 27, 2024
b2e027b
fix: :construction: more testing
ion098 Sep 27, 2024
075c379
fix: :bug: Fix usage of unintialized fields
ion098 Oct 10, 2024
fe7fa7a
fix: :bug: initalize attributes in all classes
PA055 Oct 10, 2024
db239d4
chore: :twisted_rightwards_arrows: Merge branches
PA055 Oct 10, 2024
72ecda3
fix: :bug: Fix bug where AbsrtactScreen was not abstract
PA055 Oct 12, 2024
d06277a
fix: :bug: fix nullpointers getting turned into empty strings
PA055 Oct 12, 2024
c357038
fix: :bug: multiples line changes not registering
PA055 Oct 12, 2024
a1c3f15
fix: :bug: screen not being cleared first print
PA055 Oct 12, 2024
dd06cda
chore: :poop: get_priority is the problem, idk why
PA055 Oct 12, 2024
82c0937
test: :sparkles: the alerts are kinda working
PA055 Oct 13, 2024
b1e4093
fix: :bug: repeated alert rumbles
PA055 Oct 13, 2024
aa4904d
chore: :fire: remove debug prints
PA055 Oct 13, 2024
993960f
chore: :fire: remove debug prints
PA055 Oct 13, 2024
5606ac5
Merge branch 'text' of https://github.com/PA055/Gamepad into text
PA055 Oct 13, 2024
a5f29cb
refactor: :art: run clang-format
PA055 Oct 13, 2024
85be74a
feat: :sparkles: disconnect/reconnect logic
PA055 Oct 16, 2024
6812d03
test line clearing
PA055 Oct 31, 2024
9c16e78
Merge branch 'main' into text
ion098 Nov 4, 2024
e27010e
docs: :memo: Added all of the documentation
PA055 Nov 13, 2024
0237fe8
feat: :sparkles: actually impliment the handle_events interface
PA055 Nov 13, 2024
f70c9d6
docs: :memo: Add examples
PA055 Nov 13, 2024
2a7ff7a
fix: :bug: actually make it compile
PA055 Nov 13, 2024
bdcd568
chore: :fire: removed debug.log
PA055 Nov 15, 2024
e0db1e4
refactor: :white_check_mark: address code review comments
PA055 Nov 15, 2024
057f8d7
ci: :fire: remove .devcontainer files because of licence stuff
PA055 Nov 19, 2024
1febcc0
Merge branch 'main' into text
PA055 Dec 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .devcontainer/devcontainer.json

This file was deleted.

8 changes: 0 additions & 8 deletions .devcontainer/postCreateCommand.bash

This file was deleted.

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
Loading