From dee3d536cbde6cba96c4b77d292edb5d397d9b60 Mon Sep 17 00:00:00 2001 From: lufimio <67525443+PA055@users.noreply.github.com> Date: Tue, 21 May 2024 20:19:26 -0400 Subject: [PATCH 01/54] add buffer and handling --- include/gamepad/controller.hpp | 12 ++++++++++++ src/gamepad/controller.cpp | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/include/gamepad/controller.hpp b/include/gamepad/controller.hpp index d552aac..dd80ad3 100644 --- a/include/gamepad/controller.hpp +++ b/include/gamepad/controller.hpp @@ -1,7 +1,10 @@ #pragma once #include +#include #include +#include +#include #ifndef PROS_USE_SIMPLE_NAMES #define PROS_USE_SIMPLE_NAMES #endif @@ -47,6 +50,8 @@ class Controller { * @note Create a separate instance for each task. */ void update(); + + void println(std::uint8_t line, std::string str, std::uint32_t duration); /** * Get the state of a button on the controller. * @param button Which button's state you want. @@ -57,6 +62,7 @@ class Controller { * @param joystick Which joystick axis's value to return */ float operator[](pros::controller_analog_e_t joystick); + Button L1{}, L2{}, R1{}, R2{}, Up{}, Down{}, Left{}, Right{}, X{}, B{}, Y{}, A{}; @@ -64,6 +70,12 @@ class Controller { private: static Button Controller::* button_to_ptr(pros::controller_digital_e_t button); void updateButton(pros::controller_digital_e_t button_id); + void updateScreen(); + + std::deque> screen_buffer[3]; + std::pair screen_contents[3]; + uint32_t line_set_time[3]; + uint32_t last_print_time = 0; pros::Controller controller; }; // namespace Gamepad } \ No newline at end of file diff --git a/src/gamepad/controller.cpp b/src/gamepad/controller.cpp index 595bdef..a8e66c5 100644 --- a/src/gamepad/controller.cpp +++ b/src/gamepad/controller.cpp @@ -1,4 +1,9 @@ #include "gamepad/controller.hpp" +#include "pros/rtos.hpp" +#include +#include +#include +#include namespace Gamepad { void Controller::updateButton(pros::controller_digital_e_t button_id) { @@ -15,6 +20,25 @@ void Controller::updateButton(pros::controller_digital_e_t button_id) { } } +void Controller::updateScreen() { + if (pros::millis() - this->last_print_time < 50) { + return; + } + + for (int line = 0; line < 3; line++) { + if (pros::millis() - this->line_set_time[line] < this->screen_contents[line].second) { + continue; + } + + this->controller.clear_line(line); + this->controller.set_text(line, 0, this->screen_buffer[line][0].first.c_str()); + this->screen_contents[line] = this->screen_buffer[line][0]; + this->screen_buffer[line].pop_front(); + this->last_print_time = pros::millis(); + break; + } +} + void Controller::update() { for(int i = DIGITAL_L1; i != DIGITAL_A; ++i) { this->updateButton(static_cast(i)); @@ -24,6 +48,14 @@ void Controller::update() { this->LeftY = this->controller.get_analog(ANALOG_LEFT_Y); this->RightX = this->controller.get_analog(ANALOG_RIGHT_X); this->RightY = this->controller.get_analog(ANALOG_RIGHT_Y); + + this->updateScreen(); +} + +void Controller::println(uint8_t line, std::string str, std::uint32_t duration) { + if (line > 2) std::exit(1); // TODO: change handling + + screen_buffer[line].push_back(std::make_pair(str, duration)); } const Button& Controller::operator[](pros::controller_digital_e_t button) { From 246c7817e8a3a44235130b3c715bbb6d25e6bc0e Mon Sep 17 00:00:00 2001 From: lufimio <67525443+PA055@users.noreply.github.com> Date: Tue, 21 May 2024 20:42:01 -0400 Subject: [PATCH 02/54] added line cycleing --- include/gamepad/controller.hpp | 1 + src/gamepad/controller.cpp | 14 ++++++++------ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/include/gamepad/controller.hpp b/include/gamepad/controller.hpp index dd80ad3..5435a06 100644 --- a/include/gamepad/controller.hpp +++ b/include/gamepad/controller.hpp @@ -75,6 +75,7 @@ class Controller { std::deque> screen_buffer[3]; std::pair screen_contents[3]; uint32_t line_set_time[3]; + uint8_t last_printed_line = 0; uint32_t last_print_time = 0; pros::Controller controller; }; // namespace Gamepad diff --git a/src/gamepad/controller.cpp b/src/gamepad/controller.cpp index a8e66c5..d7e2790 100644 --- a/src/gamepad/controller.cpp +++ b/src/gamepad/controller.cpp @@ -21,19 +21,21 @@ void Controller::updateButton(pros::controller_digital_e_t button_id) { } void Controller::updateScreen() { - if (pros::millis() - this->last_print_time < 50) { + if (pros::millis() - this->last_print_time < 50) return; - } - for (int line = 0; line < 3; line++) { - if (pros::millis() - this->line_set_time[line] < this->screen_contents[line].second) { + for (int i = 1; i < 3; i++) { + int line = (this->last_printed_line + i) % 3; + + if (pros::millis() - this->line_set_time[line] < this->screen_contents[line].second) continue; - } - + this->controller.clear_line(line); this->controller.set_text(line, 0, this->screen_buffer[line][0].first.c_str()); this->screen_contents[line] = this->screen_buffer[line][0]; this->screen_buffer[line].pop_front(); + + this->last_printed_line = line; this->last_print_time = pros::millis(); break; } From e129183e6ff308dbe7f3e893e51911910b69d899 Mon Sep 17 00:00:00 2001 From: lufimio <67525443+PA055@users.noreply.github.com> Date: Wed, 22 May 2024 13:13:54 -0400 Subject: [PATCH 03/54] fix critical bug :) --- src/gamepad/controller.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/gamepad/controller.cpp b/src/gamepad/controller.cpp index d7e2790..897dee5 100644 --- a/src/gamepad/controller.cpp +++ b/src/gamepad/controller.cpp @@ -30,12 +30,14 @@ void Controller::updateScreen() { if (pros::millis() - this->line_set_time[line] < this->screen_contents[line].second) continue; + this->controller.clear_line(line); this->controller.set_text(line, 0, this->screen_buffer[line][0].first.c_str()); this->screen_contents[line] = this->screen_buffer[line][0]; this->screen_buffer[line].pop_front(); this->last_printed_line = line; + this->line_set_time[line] = pros::millis(); this->last_print_time = pros::millis(); break; } From 2b8d9c93c3f060d4e2c81925dfb7d3684b011d9e Mon Sep 17 00:00:00 2001 From: lufimio <67525443+PA055@users.noreply.github.com> Date: Wed, 22 May 2024 18:37:18 -0400 Subject: [PATCH 04/54] add rumble and make clarity changes --- include/gamepad/controller.hpp | 15 +++++++++++---- src/gamepad/controller.cpp | 21 ++++++++++++--------- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/include/gamepad/controller.hpp b/include/gamepad/controller.hpp index 5435a06..c8d0b86 100644 --- a/include/gamepad/controller.hpp +++ b/include/gamepad/controller.hpp @@ -1,9 +1,11 @@ #pragma once +#include #include #include #include #include +#include #include #ifndef PROS_USE_SIMPLE_NAMES #define PROS_USE_SIMPLE_NAMES @@ -51,7 +53,7 @@ class Controller { */ void update(); - void println(std::uint8_t line, std::string str, std::uint32_t duration); + void print_line(std::uint8_t line, std::string str, std::uint32_t duration); /** * Get the state of a button on the controller. * @param button Which button's state you want. @@ -68,13 +70,18 @@ class Controller { X{}, B{}, Y{}, A{}; float LeftX = 0, LeftY = 0, RightX = 0, RightY = 0; private: + struct Line { + std::string text; + uint duration; + }; + static Button Controller::* button_to_ptr(pros::controller_digital_e_t button); void updateButton(pros::controller_digital_e_t button_id); void updateScreen(); - std::deque> screen_buffer[3]; - std::pair screen_contents[3]; - uint32_t line_set_time[3]; + std::array, 4> screen_buffer{}; + std::array screen_contents{}; + std::array line_set_time{}; uint8_t last_printed_line = 0; uint32_t last_print_time = 0; pros::Controller controller; diff --git a/src/gamepad/controller.cpp b/src/gamepad/controller.cpp index 897dee5..f2e27db 100644 --- a/src/gamepad/controller.cpp +++ b/src/gamepad/controller.cpp @@ -24,23 +24,26 @@ void Controller::updateScreen() { if (pros::millis() - this->last_print_time < 50) return; - for (int i = 1; i < 3; i++) { + for (int i = 1; i <= 3; i++) { int line = (this->last_printed_line + i) % 3; - if (pros::millis() - this->line_set_time[line] < this->screen_contents[line].second) + if (pros::millis() - this->line_set_time[line] < this->screen_contents[line].duration) continue; - - - this->controller.clear_line(line); - this->controller.set_text(line, 0, this->screen_buffer[line][0].first.c_str()); + + this->controller.set_text(line, 0, this->screen_buffer[line][0].text + std::string(40, ' ')); this->screen_contents[line] = this->screen_buffer[line][0]; this->screen_buffer[line].pop_front(); this->last_printed_line = line; this->line_set_time[line] = pros::millis(); this->last_print_time = pros::millis(); - break; + return; } + + // nothing to print to screen this update so rumble controller + this->controller.rumble(this->screen_buffer[4][0].text.c_str()); + this->screen_buffer[4].pop_front(); + this->last_print_time = pros::millis(); } void Controller::update() { @@ -56,10 +59,10 @@ void Controller::update() { this->updateScreen(); } -void Controller::println(uint8_t line, std::string str, std::uint32_t duration) { +void Controller::print_line(uint8_t line, std::string str, std::uint32_t duration) { if (line > 2) std::exit(1); // TODO: change handling - screen_buffer[line].push_back(std::make_pair(str, duration)); + screen_buffer[line].push_back({ .text = std::move(str), .duration = duration }); } const Button& Controller::operator[](pros::controller_digital_e_t button) { From 3fc76b49f9e449ec0681a04961188a526c5f8b7c Mon Sep 17 00:00:00 2001 From: lufimio <67525443+PA055@users.noreply.github.com> Date: Wed, 22 May 2024 18:38:51 -0400 Subject: [PATCH 05/54] merge changes --- include/gamepad/controller.hpp | 1 - src/gamepad/controller.cpp | 3 ++- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/gamepad/controller.hpp b/include/gamepad/controller.hpp index c716b8b..f60f3cc 100644 --- a/include/gamepad/controller.hpp +++ b/include/gamepad/controller.hpp @@ -6,7 +6,6 @@ #include #include #include -#include #ifndef PROS_USE_SIMPLE_NAMES #define PROS_USE_SIMPLE_NAMES #endif diff --git a/src/gamepad/controller.cpp b/src/gamepad/controller.cpp index 01bcafc..99dad9c 100644 --- a/src/gamepad/controller.cpp +++ b/src/gamepad/controller.cpp @@ -93,7 +93,8 @@ void Controller::update() { } void Controller::print_line(uint8_t line, std::string str, std::uint32_t duration) { - if (line > 2) std::exit(1); // TODO: change handling + TODO("change handling for off screen lines") + if (line > 2) std::exit(1); screen_buffer[line].push_back({ .text = std::move(str), .duration = duration }); } From 0d64b620c2f394021f3748b15efa3aed5aed524c Mon Sep 17 00:00:00 2001 From: lufimio <67525443+PA055@users.noreply.github.com> Date: Wed, 22 May 2024 18:41:18 -0400 Subject: [PATCH 06/54] oops, forgot the rumble function --- include/gamepad/controller.hpp | 2 ++ src/gamepad/controller.cpp | 6 +++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/include/gamepad/controller.hpp b/include/gamepad/controller.hpp index f60f3cc..5aebd3f 100644 --- a/include/gamepad/controller.hpp +++ b/include/gamepad/controller.hpp @@ -60,6 +60,8 @@ class Controller { void update(); void print_line(std::uint8_t line, std::string str, std::uint32_t duration); + + void rumble(std::string rumble_pattern); /** * Get the state of a button on the controller. * @param button Which button's state you want. diff --git a/src/gamepad/controller.cpp b/src/gamepad/controller.cpp index 99dad9c..083a86b 100644 --- a/src/gamepad/controller.cpp +++ b/src/gamepad/controller.cpp @@ -75,7 +75,7 @@ void Controller::updateScreen() { // nothing to print to screen this update so rumble controller this->controller.rumble(this->screen_buffer[4][0].text.c_str()); - this->screen_buffer[4].pop_front(); + this->screen_buffer[3].pop_front(); this->last_print_time = pros::millis(); } @@ -99,6 +99,10 @@ void Controller::print_line(uint8_t line, std::string str, std::uint32_t duratio screen_buffer[line].push_back({ .text = std::move(str), .duration = duration }); } +void Controller::rumble(std::string rumble_pattern) { + this->screen_buffer[3].push_back({.text = std::move(rumble_pattern), .duration = 0}); +} + const Button& Controller::operator[](pros::controller_digital_e_t button) { return this->*Controller::button_to_ptr(button); } From f04764bc9a10017b104ec1fbef21891ca14ddfc9 Mon Sep 17 00:00:00 2001 From: lufimio <67525443+PA055@users.noreply.github.com> Date: Wed, 22 May 2024 20:36:04 -0400 Subject: [PATCH 07/54] check for rumble pattern length --- src/gamepad/controller.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/gamepad/controller.cpp b/src/gamepad/controller.cpp index 083a86b..727cffd 100644 --- a/src/gamepad/controller.cpp +++ b/src/gamepad/controller.cpp @@ -100,6 +100,9 @@ void Controller::print_line(uint8_t line, std::string str, std::uint32_t duratio } void Controller::rumble(std::string rumble_pattern) { + TODO("change handling for too long rumble patterns") + if (rumble_pattern.size() > 8) std::exit(1); + this->screen_buffer[3].push_back({.text = std::move(rumble_pattern), .duration = 0}); } From c5a89821cd82f8b87e8f9c1eb5378238e9d8e405 Mon Sep 17 00:00:00 2001 From: lufimio <67525443+PA055@users.noreply.github.com> Date: Wed, 22 May 2024 20:42:11 -0400 Subject: [PATCH 08/54] changed std::uint... to uint... --- include/gamepad/controller.hpp | 2 +- src/gamepad/controller.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/gamepad/controller.hpp b/include/gamepad/controller.hpp index 5aebd3f..2bf7ac2 100644 --- a/include/gamepad/controller.hpp +++ b/include/gamepad/controller.hpp @@ -59,7 +59,7 @@ class Controller { */ void update(); - void print_line(std::uint8_t line, std::string str, std::uint32_t duration); + void print_line(uint8_t line, std::string str, uint32_t duration); void rumble(std::string rumble_pattern); /** diff --git a/src/gamepad/controller.cpp b/src/gamepad/controller.cpp index 727cffd..70d3fc1 100644 --- a/src/gamepad/controller.cpp +++ b/src/gamepad/controller.cpp @@ -92,7 +92,7 @@ void Controller::update() { this->updateScreen(); } -void Controller::print_line(uint8_t line, std::string str, std::uint32_t duration) { +void Controller::print_line(uint8_t line, std::string str, uint32_t duration) { TODO("change handling for off screen lines") if (line > 2) std::exit(1); From acda18e1cac593c74c1e4c8c872adcfa8eaca974 Mon Sep 17 00:00:00 2001 From: ion098 <146852218+ion098@users.noreply.github.com> Date: Thu, 23 May 2024 09:48:49 -0700 Subject: [PATCH 09/54] Minor change to formatting to trigger pros-build action --- include/gamepad/controller.hpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/include/gamepad/controller.hpp b/include/gamepad/controller.hpp index 2bf7ac2..a368bf2 100644 --- a/include/gamepad/controller.hpp +++ b/include/gamepad/controller.hpp @@ -74,7 +74,6 @@ class Controller { float operator[](pros::controller_analog_e_t joystick); TODO("hide members and expose getters/const refs") - Button L1{}, L2{}, R1{}, R2{}, Up{}, Down{}, Left{}, Right{}, X{}, B{}, Y{}, A{}; @@ -96,4 +95,4 @@ class Controller { uint32_t last_print_time = 0; pros::Controller controller; }; // namespace Gamepad -} \ No newline at end of file +} From 26053a52e9529964e0f009d771d6fcb0018593a0 Mon Sep 17 00:00:00 2001 From: ion098 <146852218+ion098@users.noreply.github.com> Date: Fri, 24 May 2024 14:49:18 -0700 Subject: [PATCH 10/54] Fixed off by one error in Gamepad::updateScreen --- src/gamepad/controller.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/gamepad/controller.cpp b/src/gamepad/controller.cpp index 70d3fc1..a3d37c9 100644 --- a/src/gamepad/controller.cpp +++ b/src/gamepad/controller.cpp @@ -74,7 +74,7 @@ void Controller::updateScreen() { } // nothing to print to screen this update so rumble controller - this->controller.rumble(this->screen_buffer[4][0].text.c_str()); + this->controller.rumble(this->screen_buffer[3][0].text.c_str()); this->screen_buffer[3].pop_front(); this->last_print_time = pros::millis(); } From 735acae1b7d65fbae2d4eaccb9f1efb45952dec1 Mon Sep 17 00:00:00 2001 From: lufimio <67525443+PA055@users.noreply.github.com> Date: Sun, 26 May 2024 08:58:21 -0400 Subject: [PATCH 11/54] add rumble to cycle --- src/gamepad/controller.cpp | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/gamepad/controller.cpp b/src/gamepad/controller.cpp index 70d3fc1..3009a50 100644 --- a/src/gamepad/controller.cpp +++ b/src/gamepad/controller.cpp @@ -57,9 +57,19 @@ void Controller::updateScreen() { if (pros::millis() - this->last_print_time < 50) return; - for (int i = 1; i <= 3; i++) { - int line = (this->last_printed_line + i) % 3; - + for (int i = 1; i <= 4; i++) { + int line = (this->last_printed_line + i) % 4; + + // not part of the screen so rumble + if (line == 3) { + this->controller.rumble(this->screen_buffer[line][0].text.c_str()); + this->screen_buffer[line].pop_front(); + this->last_printed_line = line; + this->last_print_time = pros::millis(); + return; + } + + // else print to screen if (pros::millis() - this->line_set_time[line] < this->screen_contents[line].duration) continue; @@ -72,11 +82,6 @@ void Controller::updateScreen() { this->last_print_time = pros::millis(); return; } - - // nothing to print to screen this update so rumble controller - this->controller.rumble(this->screen_buffer[4][0].text.c_str()); - this->screen_buffer[3].pop_front(); - this->last_print_time = pros::millis(); } void Controller::update() { From d1394b3abaaab2c3076e3b1d95bccc98f125b43e Mon Sep 17 00:00:00 2001 From: lufimio <67525443+PA055@users.noreply.github.com> Date: Tue, 4 Jun 2024 12:40:25 -0400 Subject: [PATCH 12/54] seperate alerts and prints --- include/gamepad/controller.hpp | 5 ++++- src/gamepad/controller.cpp | 20 +++++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/include/gamepad/controller.hpp b/include/gamepad/controller.hpp index a368bf2..a4be7a1 100644 --- a/include/gamepad/controller.hpp +++ b/include/gamepad/controller.hpp @@ -59,7 +59,9 @@ class Controller { */ void update(); - void print_line(uint8_t line, std::string str, uint32_t duration); + void add_alert(uint8_t line, std::string str, uint32_t duration); + + void print_line(uint8_t line, std::string str); void rumble(std::string rumble_pattern); /** @@ -90,6 +92,7 @@ class Controller { std::array, 4> screen_buffer{}; std::array screen_contents{}; + std::array next_print{}; std::array line_set_time{}; uint8_t last_printed_line = 0; uint32_t last_print_time = 0; diff --git a/src/gamepad/controller.cpp b/src/gamepad/controller.cpp index be4c3c5..a3127a6 100644 --- a/src/gamepad/controller.cpp +++ b/src/gamepad/controller.cpp @@ -3,6 +3,7 @@ #include "pros/rtos.hpp" #include #include +#include #include #include @@ -60,6 +61,16 @@ void Controller::updateScreen() { for (int i = 1; i <= 4; i++) { int line = (this->last_printed_line + i) % 4; + if (this->screen_buffer[line].size() == 0 && next_print[line] != "") { + this->controller.set_text(line, 0, this->next_print[line]); + this->next_print[line] = ""; + this->last_printed_line = line; + this->last_print_time = pros::millis(); + return; + } + + if (screen_buffer[line].size() == 0) return; + // not part of the screen so rumble if (line == 3) { this->controller.rumble(this->screen_buffer[line][0].text.c_str()); @@ -98,13 +109,20 @@ void Controller::update() { this->updateScreen(); } -void Controller::print_line(uint8_t line, std::string str, uint32_t duration) { +void Controller::add_alert(uint8_t line, std::string str, uint32_t duration) { TODO("change handling for off screen lines") if (line > 2) std::exit(1); screen_buffer[line].push_back({ .text = std::move(str), .duration = duration }); } +void Controller::print_line(uint8_t line, std::string str) { + TODO("change handling for off screen lines") + if (line > 2) std::exit(1); + + next_print[line] = str; +} + void Controller::rumble(std::string rumble_pattern) { TODO("change handling for too long rumble patterns") if (rumble_pattern.size() > 8) std::exit(1); From 5cdd7a10f559afef3b59c2874ae1aa085e0e6693 Mon Sep 17 00:00:00 2001 From: lufimio <67525443+PA055@users.noreply.github.com> Date: Sat, 8 Jun 2024 08:07:31 -0400 Subject: [PATCH 13/54] change rumble from alert like to print like --- include/gamepad/controller.hpp | 4 ++-- src/gamepad/controller.cpp | 24 +++++++++++++----------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/include/gamepad/controller.hpp b/include/gamepad/controller.hpp index a4be7a1..daa6f2d 100644 --- a/include/gamepad/controller.hpp +++ b/include/gamepad/controller.hpp @@ -90,10 +90,10 @@ class Controller { void updateButton(pros::controller_digital_e_t button_id); void updateScreen(); - std::array, 4> screen_buffer{}; + std::array, 3> screen_buffer{}; std::array screen_contents{}; - std::array next_print{}; std::array line_set_time{}; + std::array next_print{}; uint8_t last_printed_line = 0; uint32_t last_print_time = 0; pros::Controller controller; diff --git a/src/gamepad/controller.cpp b/src/gamepad/controller.cpp index a3127a6..d9df8f4 100644 --- a/src/gamepad/controller.cpp +++ b/src/gamepad/controller.cpp @@ -1,6 +1,7 @@ #include "gamepad/controller.hpp" #include "gamepad/todo.hpp" #include "pros/rtos.hpp" +#include #include #include #include @@ -61,25 +62,26 @@ void Controller::updateScreen() { for (int i = 1; i <= 4; i++) { int line = (this->last_printed_line + i) % 4; - if (this->screen_buffer[line].size() == 0 && next_print[line] != "") { - this->controller.set_text(line, 0, this->next_print[line]); + // not part of the screen so rumble + if (line == 3) { + this->controller.rumble(this->next_print[line].c_str()); this->next_print[line] = ""; this->last_printed_line = line; this->last_print_time = pros::millis(); return; } - if (screen_buffer[line].size() == 0) return; - - // not part of the screen so rumble - if (line == 3) { - this->controller.rumble(this->screen_buffer[line][0].text.c_str()); - this->screen_buffer[line].pop_front(); + // no alerts so print from string + if (this->screen_buffer[line].size() == 0 && next_print[line] != "") { + this->controller.set_text(line, 0, this->next_print[line]); + this->next_print[line] = ""; this->last_printed_line = line; this->last_print_time = pros::millis(); return; } + if (screen_buffer[line].size() == 0) return; + // else print to screen if (pros::millis() - this->line_set_time[line] < this->screen_contents[line].duration) continue; @@ -113,21 +115,21 @@ void Controller::add_alert(uint8_t line, std::string str, uint32_t duration) { TODO("change handling for off screen lines") if (line > 2) std::exit(1); - screen_buffer[line].push_back({ .text = std::move(str), .duration = duration }); + this->screen_buffer[line].push_back({ .text = std::move(str), .duration = duration }); } void Controller::print_line(uint8_t line, std::string str) { TODO("change handling for off screen lines") if (line > 2) std::exit(1); - next_print[line] = str; + this->next_print[line] = std::move(str); } void Controller::rumble(std::string rumble_pattern) { TODO("change handling for too long rumble patterns") if (rumble_pattern.size() > 8) std::exit(1); - this->screen_buffer[3].push_back({.text = std::move(rumble_pattern), .duration = 0}); + this->next_print[3] = std::move(rumble_pattern); } const Button& Controller::operator[](pros::controller_digital_e_t button) { From 2bc3862e361dfc59c7d19ce4113a05e44d8d5c02 Mon Sep 17 00:00:00 2001 From: lufimio <67525443+PA055@users.noreply.github.com> Date: Wed, 26 Jun 2024 08:08:37 -0700 Subject: [PATCH 14/54] add multi line alerts --- include/gamepad/controller.hpp | 4 +++- src/gamepad/controller.cpp | 34 +++++++++++++++++++++++++++++++--- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/include/gamepad/controller.hpp b/include/gamepad/controller.hpp index daa6f2d..ef25509 100644 --- a/include/gamepad/controller.hpp +++ b/include/gamepad/controller.hpp @@ -59,7 +59,8 @@ class Controller { */ void update(); - void add_alert(uint8_t line, std::string str, uint32_t duration); + void add_alerts(uint8_t line, std::string str, uint32_t duration); + void add_alerts(std::vector lines, std::vector strs, uint32_t duration); void print_line(uint8_t line, std::string str); @@ -89,6 +90,7 @@ class Controller { static Button Controller::* button_to_ptr(pros::controller_digital_e_t button); void updateButton(pros::controller_digital_e_t button_id); void updateScreen(); + uint getTotalDuration(uint8_t line); std::array, 3> screen_buffer{}; std::array screen_contents{}; diff --git a/src/gamepad/controller.cpp b/src/gamepad/controller.cpp index d9df8f4..6aa692f 100644 --- a/src/gamepad/controller.cpp +++ b/src/gamepad/controller.cpp @@ -2,6 +2,7 @@ #include "gamepad/todo.hpp" #include "pros/rtos.hpp" #include +#include #include #include #include @@ -73,7 +74,7 @@ void Controller::updateScreen() { // no alerts so print from string if (this->screen_buffer[line].size() == 0 && next_print[line] != "") { - this->controller.set_text(line, 0, this->next_print[line]); + this->controller.set_text(line, 0, this->next_print[line] + std::string(40, ' ')); this->next_print[line] = ""; this->last_printed_line = line; this->last_print_time = pros::millis(); @@ -111,11 +112,38 @@ void Controller::update() { this->updateScreen(); } -void Controller::add_alert(uint8_t line, std::string str, uint32_t duration) { +uint Controller::getTotalDuration(uint8_t line) { + uint total = 0; + for (Line msg : this->screen_buffer[line]) + total += msg.duration; + return total; +} + +void Controller::add_alerts(uint8_t line, std::string str, uint32_t duration) { TODO("change handling for off screen lines") if (line > 2) std::exit(1); - this->screen_buffer[line].push_back({ .text = std::move(str), .duration = duration }); + this->screen_buffer[line].push_back({ .text = std::move(str), .duration = duration }); +} + +void Controller::add_alerts(std::vector lines, std::vector strs, uint32_t duration) { + assert(lines.size() == strs.size()); + + // get nex available time slot for all lines + uint minSpot = -1; // max uint value + for (uint8_t line : lines) { + TODO("change handling for off screen lines") + if (line > 2) std::exit(1); + + if (getTotalDuration(line) < minSpot) + minSpot = getTotalDuration(line); + } + + for (int i = 0; i < lines.size(); i++) { + if (getTotalDuration(lines[i]) < minSpot) + this->screen_buffer[lines[i]].push_back({ .text = "", .duration = (minSpot - getTotalDuration(lines[i])) }); + this->screen_buffer[lines[i]].push_back({ .text = std::move(strs[i]), .duration = duration }); + } } void Controller::print_line(uint8_t line, std::string str) { From 1e8182ecf33b7df116cdc7b2964a2662de45771b Mon Sep 17 00:00:00 2001 From: lufimio <67525443+PA055@users.noreply.github.com> Date: Wed, 26 Jun 2024 15:08:48 -0700 Subject: [PATCH 15/54] add ability to have newlines in alerts --- src/gamepad/controller.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/gamepad/controller.cpp b/src/gamepad/controller.cpp index 6aa692f..3cf9b05 100644 --- a/src/gamepad/controller.cpp +++ b/src/gamepad/controller.cpp @@ -6,8 +6,10 @@ #include #include #include +#include #include #include +#include namespace Gamepad { @@ -123,6 +125,22 @@ void Controller::add_alerts(uint8_t line, std::string str, uint32_t duration) { TODO("change handling for off screen lines") if (line > 2) std::exit(1); + if (str.find('\n') != std::string::npos) { + std::vector strs; + std::vector lines; + std::stringstream ss(str); + uint8_t l = line; + std::string to; + + while (std::getline(ss, to, '\n')) { + strs.push_back(to); + lines.push_back(l++); + } + + add_alerts(lines, strs, duration); + return; + } + this->screen_buffer[line].push_back({ .text = std::move(str), .duration = duration }); } From ec5c814201e166e7391a6a5f74b5f7e7447f5c38 Mon Sep 17 00:00:00 2001 From: lufimio <67525443+PA055@users.noreply.github.com> Date: Thu, 27 Jun 2024 16:31:12 -0700 Subject: [PATCH 16/54] make add_alerts private --- include/gamepad/controller.hpp | 5 +++-- src/gamepad/controller.cpp | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/include/gamepad/controller.hpp b/include/gamepad/controller.hpp index ef25509..3b4a707 100644 --- a/include/gamepad/controller.hpp +++ b/include/gamepad/controller.hpp @@ -59,8 +59,7 @@ class Controller { */ void update(); - void add_alerts(uint8_t line, std::string str, uint32_t duration); - void add_alerts(std::vector lines, std::vector strs, uint32_t duration); + void add_alert(uint8_t line, std::string str, uint32_t duration); void print_line(uint8_t line, std::string str); @@ -92,6 +91,8 @@ class Controller { void updateScreen(); uint getTotalDuration(uint8_t line); + void add_alerts(std::vector lines, std::vector strs, uint32_t duration); + std::array, 3> screen_buffer{}; std::array screen_contents{}; std::array line_set_time{}; diff --git a/src/gamepad/controller.cpp b/src/gamepad/controller.cpp index 3cf9b05..957f486 100644 --- a/src/gamepad/controller.cpp +++ b/src/gamepad/controller.cpp @@ -121,7 +121,7 @@ uint Controller::getTotalDuration(uint8_t line) { return total; } -void Controller::add_alerts(uint8_t line, std::string str, uint32_t duration) { +void Controller::add_alert(uint8_t line, std::string str, uint32_t duration) { TODO("change handling for off screen lines") if (line > 2) std::exit(1); From 599d3c1a745853e6e039ce36d3128380b8be8451 Mon Sep 17 00:00:00 2001 From: lufimio <67525443+PA055@users.noreply.github.com> Date: Thu, 27 Jun 2024 18:43:21 -0700 Subject: [PATCH 17/54] remove unnessasary lines parameter --- include/gamepad/controller.hpp | 4 ++-- src/gamepad/controller.cpp | 33 +++++++++++++++++---------------- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/include/gamepad/controller.hpp b/include/gamepad/controller.hpp index 3b4a707..05a8238 100644 --- a/include/gamepad/controller.hpp +++ b/include/gamepad/controller.hpp @@ -91,8 +91,8 @@ class Controller { void updateScreen(); uint getTotalDuration(uint8_t line); - void add_alerts(std::vector lines, std::vector strs, uint32_t duration); - + void add_alerts(std::vector strs, uint32_t duration); + std::array, 3> screen_buffer{}; std::array screen_contents{}; std::array line_set_time{}; diff --git a/src/gamepad/controller.cpp b/src/gamepad/controller.cpp index 957f486..e225753 100644 --- a/src/gamepad/controller.cpp +++ b/src/gamepad/controller.cpp @@ -126,41 +126,42 @@ void Controller::add_alert(uint8_t line, std::string str, uint32_t duration) { if (line > 2) std::exit(1); if (str.find('\n') != std::string::npos) { - std::vector strs; - std::vector lines; + TODO("warn instead of throw error if there are too many lines") + if (std::ranges::count(str, '\n') > 2) std::exit(1); + + std::vector strs = {"", "", ""}; std::stringstream ss(str); - uint8_t l = line; std::string to; - while (std::getline(ss, to, '\n')) { - strs.push_back(to); - lines.push_back(l++); + for (int i = line; i < 3; i++) { + if (!std::getline(ss, to, '\n')) break; + strs[i] = std::move(to); } - add_alerts(lines, strs, duration); + add_alerts(strs, duration); return; } this->screen_buffer[line].push_back({ .text = std::move(str), .duration = duration }); } -void Controller::add_alerts(std::vector lines, std::vector strs, uint32_t duration) { - assert(lines.size() == strs.size()); +void Controller::add_alerts(std::vector strs, uint32_t duration) { + TODO("change handling for too many lines") + if (strs.size() > 3) std::exit(1); + // get nex available time slot for all lines uint minSpot = -1; // max uint value - for (uint8_t line : lines) { - TODO("change handling for off screen lines") - if (line > 2) std::exit(1); + for (uint8_t line = 0; line < 3; line++) { if (getTotalDuration(line) < minSpot) minSpot = getTotalDuration(line); } - for (int i = 0; i < lines.size(); i++) { - if (getTotalDuration(lines[i]) < minSpot) - this->screen_buffer[lines[i]].push_back({ .text = "", .duration = (minSpot - getTotalDuration(lines[i])) }); - this->screen_buffer[lines[i]].push_back({ .text = std::move(strs[i]), .duration = duration }); + for (int i = 0; i < 3; i++) { + if (getTotalDuration(i) < minSpot) + this->screen_buffer[i].push_back({ .text = "", .duration = (minSpot - getTotalDuration(i)) }); + this->screen_buffer[i].push_back({ .text = std::move(strs[i]), .duration = duration }); } } From 20138bb1d851fdfca664e908904d4d48a240401f Mon Sep 17 00:00:00 2001 From: lufimio <67525443+PA055@users.noreply.github.com> Date: Fri, 5 Jul 2024 20:36:17 -0700 Subject: [PATCH 18/54] add multiline prints --- src/gamepad/controller.cpp | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/src/gamepad/controller.cpp b/src/gamepad/controller.cpp index e225753..e5ff7e4 100644 --- a/src/gamepad/controller.cpp +++ b/src/gamepad/controller.cpp @@ -129,13 +129,11 @@ void Controller::add_alert(uint8_t line, std::string str, uint32_t duration) { TODO("warn instead of throw error if there are too many lines") if (std::ranges::count(str, '\n') > 2) std::exit(1); - std::vector strs = {"", "", ""}; + std::vector strs(3); std::stringstream ss(str); - std::string to; for (int i = line; i < 3; i++) { - if (!std::getline(ss, to, '\n')) break; - strs[i] = std::move(to); + if (!std::getline(ss, strs[i], '\n')) break; } add_alerts(strs, duration); @@ -149,15 +147,12 @@ void Controller::add_alerts(std::vector strs, uint32_t duration) { TODO("change handling for too many lines") if (strs.size() > 3) std::exit(1); - // get nex available time slot for all lines uint minSpot = -1; // max uint value - for (uint8_t line = 0; line < 3; line++) { - - if (getTotalDuration(line) < minSpot) - minSpot = getTotalDuration(line); - } + for (uint8_t line = 0; line < 3; line++) + minSpot = std::min(minSpot, getTotalDuration(line)); + // Schedule alerts for (int i = 0; i < 3; i++) { if (getTotalDuration(i) < minSpot) this->screen_buffer[i].push_back({ .text = "", .duration = (minSpot - getTotalDuration(i)) }); @@ -169,6 +164,22 @@ void Controller::print_line(uint8_t line, std::string str) { TODO("change handling for off screen lines") if (line > 2) std::exit(1); + if (str.find('\n') != std::string::npos) { + TODO("warn instead of throw error if there are too many lines") + if (std::ranges::count(str, '\n') > 2) std::exit(1); + + std::vector strs(3); + std::stringstream ss(str); + + for (int i = line; i < 3; i++) { + if (!std::getline(ss, strs[i], '\n')) break; + } + + for (uint8_t l = 0; l < 3; l++) + this->next_print[l] = std::move(strs[l]); + return; + } + this->next_print[line] = std::move(str); } From e73d095b3b4d1b459e7628466e89b6a6d07769b3 Mon Sep 17 00:00:00 2001 From: lufimio <67525443+PA055@users.noreply.github.com> Date: Mon, 8 Jul 2024 10:54:12 -0700 Subject: [PATCH 19/54] add rumbles to alerts --- include/gamepad/controller.hpp | 16 ++++++++++------ src/gamepad/controller.cpp | 27 ++++++++++++++++++++------- 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/include/gamepad/controller.hpp b/include/gamepad/controller.hpp index 05a8238..7f2f02f 100644 --- a/include/gamepad/controller.hpp +++ b/include/gamepad/controller.hpp @@ -59,7 +59,7 @@ class Controller { */ void update(); - void add_alert(uint8_t line, std::string str, uint32_t duration); + void add_alert(uint8_t line, std::string str, uint32_t duration, std::string rumble = ""); void print_line(uint8_t line, std::string str); @@ -88,17 +88,21 @@ class Controller { static Button Controller::* button_to_ptr(pros::controller_digital_e_t button); void updateButton(pros::controller_digital_e_t button_id); + void updateScreen(); + void add_alerts(std::vector strs, uint32_t duration, std::string rumble = ""); uint getTotalDuration(uint8_t line); - void add_alerts(std::vector strs, uint32_t duration); + pros::Controller controller; + + // alert queue + std::array, 4> screen_buffer{}; + std::array screen_contents{}; + std::array line_set_time{}; - std::array, 3> screen_buffer{}; - std::array screen_contents{}; - std::array line_set_time{}; std::array next_print{}; uint8_t last_printed_line = 0; uint32_t last_print_time = 0; - pros::Controller controller; + }; // namespace Gamepad } diff --git a/src/gamepad/controller.cpp b/src/gamepad/controller.cpp index e5ff7e4..af61b8a 100644 --- a/src/gamepad/controller.cpp +++ b/src/gamepad/controller.cpp @@ -67,6 +67,18 @@ void Controller::updateScreen() { // not part of the screen so rumble if (line == 3) { + if (this->screen_buffer[line].size() != 0) { + if (pros::millis() - this->line_set_time[line] < this->screen_contents[line].duration) + continue; + + this->controller.set_text(line, 0, this->screen_buffer[line][0].text + std::string(40, ' ')); + this->screen_contents[line] = this->screen_buffer[line][0]; + this->screen_buffer[line].pop_front(); + + this->last_printed_line = line; + this->line_set_time[line] = pros::millis(); + this->last_print_time = pros::millis(); + } this->controller.rumble(this->next_print[line].c_str()); this->next_print[line] = ""; this->last_printed_line = line; @@ -121,7 +133,7 @@ uint Controller::getTotalDuration(uint8_t line) { return total; } -void Controller::add_alert(uint8_t line, std::string str, uint32_t duration) { +void Controller::add_alert(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); @@ -136,27 +148,28 @@ void Controller::add_alert(uint8_t line, std::string str, uint32_t duration) { if (!std::getline(ss, strs[i], '\n')) break; } - add_alerts(strs, duration); + add_alerts(strs, duration, rumble); return; } this->screen_buffer[line].push_back({ .text = std::move(str), .duration = duration }); } -void Controller::add_alerts(std::vector strs, uint32_t duration) { +void Controller::add_alerts(std::vector strs, uint32_t duration, std::string rumble) { TODO("change handling for too many lines") if (strs.size() > 3) std::exit(1); // get nex available time slot for all lines uint minSpot = -1; // max uint value - for (uint8_t line = 0; line < 3; line++) - minSpot = std::min(minSpot, getTotalDuration(line)); + for (uint8_t line = 0; line < 4; line++) minSpot = std::min(minSpot, getTotalDuration(line)); // Schedule alerts - for (int i = 0; i < 3; i++) { + for (int i = 0; i < 4; i++) { if (getTotalDuration(i) < minSpot) this->screen_buffer[i].push_back({ .text = "", .duration = (minSpot - getTotalDuration(i)) }); - this->screen_buffer[i].push_back({ .text = std::move(strs[i]), .duration = duration }); + + if (i == 3) this->screen_buffer[i].push_back({.text = std::move(rumble), .duration = duration}); + else this->screen_buffer[i].push_back({.text = std::move(strs[i]), .duration = 0}); } } From 57371a6e1860f7cb1f2645d81f970cff11eee55e Mon Sep 17 00:00:00 2001 From: lufimio <67525443+PA055@users.noreply.github.com> Date: Mon, 8 Jul 2024 14:41:16 -0700 Subject: [PATCH 20/54] refactored screen update loop --- src/gamepad/controller.cpp | 62 ++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 32 deletions(-) diff --git a/src/gamepad/controller.cpp b/src/gamepad/controller.cpp index af61b8a..1e4d758 100644 --- a/src/gamepad/controller.cpp +++ b/src/gamepad/controller.cpp @@ -65,52 +65,50 @@ void Controller::updateScreen() { for (int i = 1; i <= 4; i++) { int line = (this->last_printed_line + i) % 4; - // not part of the screen so rumble - if (line == 3) { - if (this->screen_buffer[line].size() != 0) { - if (pros::millis() - this->line_set_time[line] < this->screen_contents[line].duration) + // duration expired + if (pros::millis() - this->line_set_time[line] >= this->screen_contents[line].duration) { + if (this->screen_buffer[line].size() == 0) { + // no need to update text + if (this->screen_contents[line].text == this->next_print[line] && line != 3) { + this->next_print[line] = ""; continue; + } + + if (line == 3) this->controller.rumble(this->next_print[line].c_str()); + else this->controller.set_text(line, 0, this->next_print[line] + std::string(40, ' ')); + this->screen_contents[line].text = std::move(this->next_print[line]); + this->next_print[line] = ""; + } else { + // no need to update text + if (this->screen_contents[line].text == this->screen_buffer[line][0].text && line != 3) { + this->screen_contents[line] = this->screen_buffer[line][0]; + this->screen_buffer[line].pop_front(); + this->line_set_time[line] = pros::millis(); + continue; + } - this->controller.set_text(line, 0, this->screen_buffer[line][0].text + std::string(40, ' ')); + if (line == 3) this->controller.rumble(this->screen_buffer[line][0].text.c_str()); + else this->controller.set_text(line, 0, this->screen_buffer[line][0].text + std::string(40, ' ')); this->screen_contents[line] = this->screen_buffer[line][0]; this->screen_buffer[line].pop_front(); - - this->last_printed_line = line; this->line_set_time[line] = pros::millis(); - this->last_print_time = pros::millis(); } - this->controller.rumble(this->next_print[line].c_str()); - this->next_print[line] = ""; this->last_printed_line = line; this->last_print_time = pros::millis(); - return; - } + } else if (this->screen_contents[line].text == "") { + // no need to update text + if (this->screen_contents[line].text == this->next_print[line] && line != 3) { + this->next_print[line] = ""; + continue; + } - // no alerts so print from string - if (this->screen_buffer[line].size() == 0 && next_print[line] != "") { - this->controller.set_text(line, 0, this->next_print[line] + std::string(40, ' ')); + if (line == 3) this->controller.rumble(this->next_print[line].c_str()); + else this->controller.set_text(line, 0, this->next_print[line] + std::string(40, ' ')); this->next_print[line] = ""; this->last_printed_line = line; this->last_print_time = pros::millis(); - return; } - - if (screen_buffer[line].size() == 0) return; - - // else print to screen - if (pros::millis() - this->line_set_time[line] < this->screen_contents[line].duration) - continue; - - this->controller.set_text(line, 0, this->screen_buffer[line][0].text + std::string(40, ' ')); - this->screen_contents[line] = this->screen_buffer[line][0]; - this->screen_buffer[line].pop_front(); - - this->last_printed_line = line; - this->line_set_time[line] = pros::millis(); - this->last_print_time = pros::millis(); - return; } - } void Controller::update() { From 022b4df2e75591be519c72181a4616bf47a3853e Mon Sep 17 00:00:00 2001 From: lufimio <67525443+PA055@users.noreply.github.com> Date: Mon, 8 Jul 2024 16:41:28 -0700 Subject: [PATCH 21/54] add thread safety... hopefully? --- include/gamepad/controller.hpp | 9 +++++++-- src/gamepad/controller.cpp | 11 +++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/include/gamepad/controller.hpp b/include/gamepad/controller.hpp index 7f2f02f..9f92f14 100644 --- a/include/gamepad/controller.hpp +++ b/include/gamepad/controller.hpp @@ -97,12 +97,17 @@ class Controller { // alert queue std::array, 4> screen_buffer{}; - std::array screen_contents{}; - std::array line_set_time{}; + pros::Mutex alert_mut; + std::array next_print{}; + pros::Mutex print_mut; + + std::array screen_contents{}; + std::array line_set_time{}; uint8_t last_printed_line = 0; uint32_t last_print_time = 0; + pros::Mutex scheduling_mut; }; // namespace Gamepad } diff --git a/src/gamepad/controller.cpp b/src/gamepad/controller.cpp index 1e4d758..419b0e3 100644 --- a/src/gamepad/controller.cpp +++ b/src/gamepad/controller.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -59,6 +60,10 @@ void Controller::updateButton(pros::controller_digital_e_t button_id) { } void Controller::updateScreen() { + std::lock_guard guard_scheduling(this->scheduling_mut); + std::lock_guard guard_print(this->print_mut); + std::lock_guard guard_alert(this->alert_mut); + if (pros::millis() - this->last_print_time < 50) return; @@ -150,6 +155,7 @@ void Controller::add_alert(uint8_t line, std::string str, uint32_t duration, std return; } + std::lock_guard guard(this->alert_mut); this->screen_buffer[line].push_back({ .text = std::move(str), .duration = duration }); } @@ -157,6 +163,8 @@ void Controller::add_alerts(std::vector strs, uint32_t duration, st TODO("change handling for too many lines") if (strs.size() > 3) std::exit(1); + std::lock_guard guard(this->alert_mut); + // get nex available time slot for all lines uint minSpot = -1; // max uint value for (uint8_t line = 0; line < 4; line++) minSpot = std::min(minSpot, getTotalDuration(line)); @@ -175,6 +183,8 @@ void Controller::print_line(uint8_t line, std::string str) { TODO("change handling for off screen lines") if (line > 2) std::exit(1); + std::lock_guard guard(this->print_mut); + if (str.find('\n') != std::string::npos) { TODO("warn instead of throw error if there are too many lines") if (std::ranges::count(str, '\n') > 2) std::exit(1); @@ -198,6 +208,7 @@ void Controller::rumble(std::string rumble_pattern) { TODO("change handling for too long rumble patterns") if (rumble_pattern.size() > 8) std::exit(1); + std::lock_guard guard(this->print_mut); this->next_print[3] = std::move(rumble_pattern); } From b8eb2426d52d6a289f415ff01865cc902687a420 Mon Sep 17 00:00:00 2001 From: lufimio <67525443+PA055@users.noreply.github.com> Date: Sat, 27 Jul 2024 08:32:36 -0400 Subject: [PATCH 22/54] some comments --- src/gamepad/controller.cpp | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/gamepad/controller.cpp b/src/gamepad/controller.cpp index 419b0e3..8fa94c5 100644 --- a/src/gamepad/controller.cpp +++ b/src/gamepad/controller.cpp @@ -60,31 +60,35 @@ void Controller::updateButton(pros::controller_digital_e_t button_id) { } void Controller::updateScreen() { + // Lock Mutexes for Thread Safety std::lock_guard guard_scheduling(this->scheduling_mut); std::lock_guard guard_print(this->print_mut); std::lock_guard guard_alert(this->alert_mut); + // Check if enough time has passed for the controller to poll for updates if (pros::millis() - this->last_print_time < 50) return; for (int i = 1; i <= 4; i++) { + // start from the line thats after the line thats been set so we dont get stuck setting the first line int line = (this->last_printed_line + i) % 4; - // duration expired + // if the last alert's duration expired if (pros::millis() - this->line_set_time[line] >= this->screen_contents[line].duration) { + // No alerts to print if (this->screen_buffer[line].size() == 0) { - // no need to update text + // text on screen is the same as last frame's text so no use updating if (this->screen_contents[line].text == this->next_print[line] && line != 3) { this->next_print[line] = ""; continue; } - + // UPDATE TEXT/RUMBLE if (line == 3) this->controller.rumble(this->next_print[line].c_str()); else this->controller.set_text(line, 0, this->next_print[line] + std::string(40, ' ')); this->screen_contents[line].text = std::move(this->next_print[line]); this->next_print[line] = ""; } else { - // no need to update text + // text on screen is the same as the alert's text so just set vars, dont update controller if (this->screen_contents[line].text == this->screen_buffer[line][0].text && line != 3) { this->screen_contents[line] = this->screen_buffer[line][0]; this->screen_buffer[line].pop_front(); @@ -92,6 +96,7 @@ void Controller::updateScreen() { continue; } + // SET ALERT/RUMBLE ALERT if (line == 3) this->controller.rumble(this->screen_buffer[line][0].text.c_str()); else this->controller.set_text(line, 0, this->screen_buffer[line][0].text + std::string(40, ' ')); this->screen_contents[line] = this->screen_buffer[line][0]; @@ -101,14 +106,16 @@ void Controller::updateScreen() { this->last_printed_line = line; this->last_print_time = pros::millis(); } else if (this->screen_contents[line].text == "") { - // no need to update text + // text is the same as last frame's text so no use updating if (this->screen_contents[line].text == this->next_print[line] && line != 3) { this->next_print[line] = ""; continue; } + // UPDATE TEXT/RUMBLE if (line == 3) this->controller.rumble(this->next_print[line].c_str()); else this->controller.set_text(line, 0, this->next_print[line] + std::string(40, ' ')); + this->screen_contents[line].text = std::move(this->next_print[line]); this->next_print[line] = ""; this->last_printed_line = line; this->last_print_time = pros::millis(); @@ -165,12 +172,13 @@ void Controller::add_alerts(std::vector strs, uint32_t duration, st std::lock_guard guard(this->alert_mut); - // get nex available time slot for all lines + // get next available time slot for all lines uint minSpot = -1; // max uint value for (uint8_t line = 0; line < 4; line++) minSpot = std::min(minSpot, getTotalDuration(line)); // Schedule alerts for (int i = 0; i < 4; i++) { + // add delay until theres a spot for all lines together if (getTotalDuration(i) < minSpot) this->screen_buffer[i].push_back({ .text = "", .duration = (minSpot - getTotalDuration(i)) }); From 12f4d6f2c90a2b0c38c0088210834ec0e4ea6dd3 Mon Sep 17 00:00:00 2001 From: lufimio <67525443+PA055@users.noreply.github.com> Date: Mon, 12 Aug 2024 18:15:55 -0400 Subject: [PATCH 23/54] finished abstract screen interface --- include/gamepad/abstractScreen.hpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 include/gamepad/abstractScreen.hpp diff --git a/include/gamepad/abstractScreen.hpp b/include/gamepad/abstractScreen.hpp new file mode 100644 index 0000000..74c1adf --- /dev/null +++ b/include/gamepad/abstractScreen.hpp @@ -0,0 +1,23 @@ +#include +#include +#include +#include + +#include "pros/misc.h" + + +namespace Gamepad { + +typedef std::array, 3> ScreenBuffer; + +class AbstractScreen { + public: + AbstractScreen(uint priority): priority(priority) {} + virtual ScreenBuffer get_screen(std::set visible_lines) = 0; + virtual void handle_events(std::set button_events) = 0; + uint get_priority(); + private: + uint priority; +}; + +} // namespace Gamepad \ No newline at end of file From 7781c59904e2b16b7bfa6ef7ec582d842fb5d165 Mon Sep 17 00:00:00 2001 From: lufimio <67525443+PA055@users.noreply.github.com> Date: Mon, 12 Aug 2024 22:17:25 -0400 Subject: [PATCH 24/54] moved the stuff that needs to be moved... kinda --- include/gamepad/abstractScreen.hpp | 23 ------ include/gamepad/controller.hpp | 38 +++------ include/gamepad/screens/abstractScreen.hpp | 30 +++++++ include/gamepad/screens/alertScreen.hpp | 35 ++++++++ include/gamepad/screens/defaultScreen.hpp | 24 ++++++ src/gamepad/controller.cpp | 93 ++-------------------- src/gamepad/screens/alertScreen.cpp | 56 +++++++++++++ src/gamepad/screens/defaultScreen.cpp | 43 ++++++++++ 8 files changed, 205 insertions(+), 137 deletions(-) delete mode 100644 include/gamepad/abstractScreen.hpp create mode 100644 include/gamepad/screens/abstractScreen.hpp create mode 100644 include/gamepad/screens/alertScreen.hpp create mode 100644 include/gamepad/screens/defaultScreen.hpp create mode 100644 src/gamepad/screens/alertScreen.cpp create mode 100644 src/gamepad/screens/defaultScreen.cpp diff --git a/include/gamepad/abstractScreen.hpp b/include/gamepad/abstractScreen.hpp deleted file mode 100644 index 74c1adf..0000000 --- a/include/gamepad/abstractScreen.hpp +++ /dev/null @@ -1,23 +0,0 @@ -#include -#include -#include -#include - -#include "pros/misc.h" - - -namespace Gamepad { - -typedef std::array, 3> ScreenBuffer; - -class AbstractScreen { - public: - AbstractScreen(uint priority): priority(priority) {} - virtual ScreenBuffer get_screen(std::set visible_lines) = 0; - virtual void handle_events(std::set button_events) = 0; - uint get_priority(); - private: - uint priority; -}; - -} // namespace Gamepad \ No newline at end of file diff --git a/include/gamepad/controller.hpp b/include/gamepad/controller.hpp index 9f92f14..1a8a360 100644 --- a/include/gamepad/controller.hpp +++ b/include/gamepad/controller.hpp @@ -1,15 +1,14 @@ #pragma once -#include #include -#include #include -#include +#include #include #ifndef PROS_USE_SIMPLE_NAMES #define PROS_USE_SIMPLE_NAMES #endif +#include "gamepad/screens/abstractScreen.hpp" #include "event_handler.hpp" #include "pros/misc.hpp" #include "pros/rtos.hpp" @@ -58,12 +57,6 @@ class Controller { * @note Create a separate instance for each task. */ void update(); - - void add_alert(uint8_t line, std::string str, uint32_t duration, std::string rumble = ""); - - void print_line(uint8_t line, std::string str); - - void rumble(std::string rumble_pattern); /** * Get the state of a button on the controller. * @param button Which button's state you want. @@ -81,33 +74,24 @@ class Controller { X{}, B{}, Y{}, A{}; float LeftX = 0, LeftY = 0, RightX = 0, RightY = 0; private: - struct Line { - std::string text; - uint duration; - }; + + struct { + bool operator()(AbstractScreen &l, AbstractScreen &r) { return l.get_priority() < r.get_priority(); } + } AbstractScreenSort; static Button Controller::* button_to_ptr(pros::controller_digital_e_t button); void updateButton(pros::controller_digital_e_t button_id); - void updateScreen(); - void add_alerts(std::vector strs, uint32_t duration, std::string rumble = ""); - uint getTotalDuration(uint8_t line); + void updateScreens(); + std::vector screens; pros::Controller controller; - // alert queue - std::array, 4> screen_buffer{}; - pros::Mutex alert_mut; - - - std::array next_print{}; - pros::Mutex print_mut; - std::array screen_contents{}; - std::array line_set_time{}; uint8_t last_printed_line = 0; - uint32_t last_print_time = 0; - pros::Mutex scheduling_mut; + uint last_print_time = 0; + uint last_update_time = 0; + pros::Mutex mut; }; // namespace Gamepad } diff --git a/include/gamepad/screens/abstractScreen.hpp b/include/gamepad/screens/abstractScreen.hpp new file mode 100644 index 0000000..8e50463 --- /dev/null +++ b/include/gamepad/screens/abstractScreen.hpp @@ -0,0 +1,30 @@ +#pragma once + +#include +#include +#include +#include +#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, 4> ScreenBuffer; + +class AbstractScreen { + public: + AbstractScreen(uint priority): priority(priority) {} + void update(double delta_time) {} + virtual ScreenBuffer get_screen(std::set visible_lines) = 0; + virtual void handle_events(std::set button_events) = 0; + const uint get_priority(); + + bool operator()(AbstractScreen *l, AbstractScreen *r) { return l->get_priority() < r->get_priority(); } + private: + const uint priority; +}; + +} // namespace Gamepad \ No newline at end of file diff --git a/include/gamepad/screens/alertScreen.hpp b/include/gamepad/screens/alertScreen.hpp new file mode 100644 index 0000000..ffd901b --- /dev/null +++ b/include/gamepad/screens/alertScreen.hpp @@ -0,0 +1,35 @@ +#pragma once + +#include +#include +#include +#include +#include "pros/rtos.hpp" +#include "gamepad/screens/abstractScreen.hpp" + +namespace Gamepad { + +class AlertScreen : AbstractScreen { + public: + AlertScreen() : AbstractScreen(UINT32_MAX - 100) {} + void update(double delta_time); + ScreenBuffer get_screen(std::set visible_lines); + void handle_events(std::set button_events); + + void add_alerts(uint8_t line, std::string strs, uint32_t duration, std::string rumble = ""); + + private: + struct Line { + std::string text; + uint duration; + }; + + uint getTotalDuration(uint8_t line); + + std::array, 4> screen_buffer{}; + std::array screen_contents{}; + std::array line_set_time{}; + pros::Mutex mut; +}; + +} \ No newline at end of file diff --git a/include/gamepad/screens/defaultScreen.hpp b/include/gamepad/screens/defaultScreen.hpp new file mode 100644 index 0000000..719082e --- /dev/null +++ b/include/gamepad/screens/defaultScreen.hpp @@ -0,0 +1,24 @@ +#pragma once + +#include "gamepad/screens/abstractScreen.hpp" +#include "pros/rtos.hpp" + +namespace Gamepad { + +class DefaultScreen : AbstractScreen { + public: + DefaultScreen() : AbstractScreen(1) {} + void update(double delta_time); + ScreenBuffer get_screen(std::set visible_lines); + void handle_events(std::set button_events); + + void print_line(uint8_t line, std::string str); + void rumble(std::string rumble_pattern); + + private: + ScreenBuffer currentBuffer; + pros::Mutex mut; + +}; + +} \ No newline at end of file diff --git a/src/gamepad/controller.cpp b/src/gamepad/controller.cpp index 8fa94c5..27f7146 100644 --- a/src/gamepad/controller.cpp +++ b/src/gamepad/controller.cpp @@ -59,11 +59,13 @@ void Controller::updateButton(pros::controller_digital_e_t button_id) { (this->*button).update(is_held); } -void Controller::updateScreen() { +void Controller::updateScreens() { // Lock Mutexes for Thread Safety - std::lock_guard guard_scheduling(this->scheduling_mut); - std::lock_guard guard_print(this->print_mut); - std::lock_guard guard_alert(this->alert_mut); + std::lock_guard guard_scheduling(this->mut); + + for (int i = 0; i < this->screens.size(); i++) { + this->screens[i].update(pros::millis() - this->last_update_time); + } // Check if enough time has passed for the controller to poll for updates if (pros::millis() - this->last_print_time < 50) @@ -136,89 +138,6 @@ void Controller::update() { this->updateScreen(); } -uint Controller::getTotalDuration(uint8_t line) { - uint total = 0; - for (Line msg : this->screen_buffer[line]) - total += msg.duration; - return total; -} - -void Controller::add_alert(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); - - if (str.find('\n') != std::string::npos) { - TODO("warn instead of throw error if there are too many lines") - if (std::ranges::count(str, '\n') > 2) std::exit(1); - - std::vector strs(3); - std::stringstream ss(str); - - for (int i = line; i < 3; i++) { - if (!std::getline(ss, strs[i], '\n')) break; - } - - add_alerts(strs, duration, rumble); - return; - } - - std::lock_guard guard(this->alert_mut); - this->screen_buffer[line].push_back({ .text = std::move(str), .duration = duration }); -} - -void Controller::add_alerts(std::vector strs, uint32_t duration, std::string rumble) { - TODO("change handling for too many lines") - if (strs.size() > 3) std::exit(1); - - std::lock_guard guard(this->alert_mut); - - // get next available time slot for all lines - uint minSpot = -1; // max uint value - for (uint8_t line = 0; line < 4; line++) minSpot = std::min(minSpot, getTotalDuration(line)); - - // Schedule alerts - for (int i = 0; i < 4; i++) { - // add delay until theres a spot for all lines together - if (getTotalDuration(i) < minSpot) - this->screen_buffer[i].push_back({ .text = "", .duration = (minSpot - getTotalDuration(i)) }); - - if (i == 3) this->screen_buffer[i].push_back({.text = std::move(rumble), .duration = duration}); - else this->screen_buffer[i].push_back({.text = std::move(strs[i]), .duration = 0}); - } -} - -void Controller::print_line(uint8_t line, std::string str) { - TODO("change handling for off screen lines") - if (line > 2) std::exit(1); - - std::lock_guard guard(this->print_mut); - - if (str.find('\n') != std::string::npos) { - TODO("warn instead of throw error if there are too many lines") - if (std::ranges::count(str, '\n') > 2) std::exit(1); - - std::vector strs(3); - std::stringstream ss(str); - - for (int i = line; i < 3; i++) { - if (!std::getline(ss, strs[i], '\n')) break; - } - - for (uint8_t l = 0; l < 3; l++) - this->next_print[l] = std::move(strs[l]); - return; - } - - this->next_print[line] = std::move(str); -} - -void Controller::rumble(std::string rumble_pattern) { - TODO("change handling for too long rumble patterns") - if (rumble_pattern.size() > 8) std::exit(1); - - std::lock_guard guard(this->print_mut); - this->next_print[3] = std::move(rumble_pattern); -} const Button& Controller::operator[](pros::controller_digital_e_t button) { return this->*Controller::button_to_ptr(button); diff --git a/src/gamepad/screens/alertScreen.cpp b/src/gamepad/screens/alertScreen.cpp new file mode 100644 index 0000000..2cfe588 --- /dev/null +++ b/src/gamepad/screens/alertScreen.cpp @@ -0,0 +1,56 @@ +#include "gamepad/screens/alertScreen.hpp" +#include +#include +#include +#include +#include "gamepad/todo.hpp" + +namespace Gamepad { + + +uint AlertScreen::getTotalDuration(uint8_t line) { + uint total = 0; + for (Line msg : this->screen_buffer[line]) + total += msg.duration; + return total; +} + +void AlertScreen::add_alerts(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); + + if (str.find('\n') != std::string::npos) { + TODO("warn instead of throw error if there are too many lines") + if (std::ranges::count(str, '\n') > 2) std::exit(1); + + std::vector strs(3); + std::stringstream ss(str); + + // split string by newlines but only take the first 3 lines + for (int i = line; i < 3; i++) { + if (!std::getline(ss, strs[i], '\n')) break; + } + + std::lock_guard guard(this->mut); + + // get next available time slot for all lines + uint minSpot = UINT32_MAX; + for (uint8_t line = 0; line < 4; line++) minSpot = std::min(minSpot, getTotalDuration(line)); + + // Schedule alerts + for (int i = 0; i < 4; i++) { + // add delay until theres a spot for all lines together + if (getTotalDuration(i) < minSpot) + this->screen_buffer[i].push_back({ .text = "", .duration = (minSpot - getTotalDuration(i)) }); + + if (i == 3) this->screen_buffer[i].push_back({.text = std::move(rumble), .duration = duration}); + else this->screen_buffer[i].push_back({.text = std::move(strs[i]), .duration = 0}); + } + return; + } + + std::lock_guard guard(this->mut); + this->screen_buffer[line].push_back({ .text = std::move(str), .duration = duration }); +} + +} \ No newline at end of file diff --git a/src/gamepad/screens/defaultScreen.cpp b/src/gamepad/screens/defaultScreen.cpp new file mode 100644 index 0000000..510f1ed --- /dev/null +++ b/src/gamepad/screens/defaultScreen.cpp @@ -0,0 +1,43 @@ +#include "gamepad/screens/defaultScreen.hpp" +#include +#include +#include +#include "gamepad/todo.hpp" + + +namespace Gamepad { + +void DefaultScreen::print_line(uint8_t line, std::string str) { + TODO("change handling for off screen lines") + if (line > 2) std::exit(1); + + std::lock_guard guard(this->mut); + + if (str.find('\n') != std::string::npos) { + TODO("warn instead of throw error if there are too many lines") + if (std::ranges::count(str, '\n') > 2) std::exit(1); + + std::vector strs(3); + std::stringstream ss(str); + + for (int i = line; i < 3; i++) { + if (!std::getline(ss, strs[i], '\n')) break; + } + + for (uint8_t l = 0; l < 3; l++) + this->currentBuffer[l] = std::move(strs[l]); + return; + } + + this->currentBuffer[line] = std::move(str); +} + +void DefaultScreen::rumble(std::string rumble_pattern) { + TODO("change handling for too long rumble patterns") + if (rumble_pattern.size() > 8) std::exit(1); + + std::lock_guard guard(this->mut); + this->currentBuffer[3] = std::move(rumble_pattern); +} + +} \ No newline at end of file From 18a4ed61f5e65226c4076d0b70d57f82eaab6845 Mon Sep 17 00:00:00 2001 From: lufimio <67525443+PA055@users.noreply.github.com> Date: Tue, 13 Aug 2024 09:49:38 -0400 Subject: [PATCH 25/54] =?UTF-8?q?why=20am=20i=20getting=20these=20errors?= =?UTF-8?q?=F0=9F=92=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/gamepad/controller.hpp | 14 ++--- src/gamepad/controller.cpp | 94 ++++++++++++++++------------------ 2 files changed, 51 insertions(+), 57 deletions(-) diff --git a/include/gamepad/controller.hpp b/include/gamepad/controller.hpp index 1a8a360..0d765b8 100644 --- a/include/gamepad/controller.hpp +++ b/include/gamepad/controller.hpp @@ -1,17 +1,16 @@ #pragma once +#include "screens/abstractScreen.hpp" #include #include #include #include -#ifndef PROS_USE_SIMPLE_NAMES -#define PROS_USE_SIMPLE_NAMES -#endif - -#include "gamepad/screens/abstractScreen.hpp" #include "event_handler.hpp" #include "pros/misc.hpp" #include "pros/rtos.hpp" +#ifndef PROS_USE_SIMPLE_NAMES +#define PROS_USE_SIMPLE_NAMES +#endif namespace Gamepad { @@ -57,6 +56,8 @@ class Controller { * @note Create a separate instance for each task. */ void update(); + + void add_screen(AbstractScreen& screen); /** * Get the state of a button on the controller. * @param button Which button's state you want. @@ -85,9 +86,10 @@ class Controller { void updateScreens(); std::vector screens; + ScreenBuffer currentScreen; + ScreenBuffer nextBuffer; pros::Controller controller; - uint8_t last_printed_line = 0; uint last_print_time = 0; uint last_update_time = 0; diff --git a/src/gamepad/controller.cpp b/src/gamepad/controller.cpp index 27f7146..6aa764b 100644 --- a/src/gamepad/controller.cpp +++ b/src/gamepad/controller.cpp @@ -1,14 +1,15 @@ #include "gamepad/controller.hpp" #include "gamepad/todo.hpp" #include "pros/rtos.hpp" -#include +#include "pros/misc.h" #include #include #include #include #include -#include +#include #include +#include #include #include @@ -63,65 +64,46 @@ void Controller::updateScreens() { // Lock Mutexes for Thread Safety std::lock_guard guard_scheduling(this->mut); - for (int i = 0; i < this->screens.size(); i++) { + // Update all screens and note deltatime + for (int i = 0; i < this->screens.size(); i++) this->screens[i].update(pros::millis() - this->last_update_time); - } + last_update_time = pros::millis(); // Check if enough time has passed for the controller to poll for updates if (pros::millis() - this->last_print_time < 50) return; + for (int i = 0; i < this->screens.size(); i++) { + // get all lines that arent being used by a higher priority screen + std::set visible_lines; + for (uint8_t j = 0; j < 4; j++) + 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 = this->screens[i].get_screen(visible_lines); + for (uint8_t j = 0; j < 4; j++) + if (buffer[j].has_value() && !nextBuffer[j].has_value()) + nextBuffer[j] = std::move(buffer[j]); + } + for (int i = 1; i <= 4; i++) { // start from the line thats after the line thats been set so we dont get stuck setting the first line int line = (this->last_printed_line + i) % 4; - // if the last alert's duration expired - if (pros::millis() - this->line_set_time[line] >= this->screen_contents[line].duration) { - // No alerts to print - if (this->screen_buffer[line].size() == 0) { - // text on screen is the same as last frame's text so no use updating - if (this->screen_contents[line].text == this->next_print[line] && line != 3) { - this->next_print[line] = ""; - continue; - } - // UPDATE TEXT/RUMBLE - if (line == 3) this->controller.rumble(this->next_print[line].c_str()); - else this->controller.set_text(line, 0, this->next_print[line] + std::string(40, ' ')); - this->screen_contents[line].text = std::move(this->next_print[line]); - this->next_print[line] = ""; - } else { - // text on screen is the same as the alert's text so just set vars, dont update controller - if (this->screen_contents[line].text == this->screen_buffer[line][0].text && line != 3) { - this->screen_contents[line] = this->screen_buffer[line][0]; - this->screen_buffer[line].pop_front(); - this->line_set_time[line] = pros::millis(); - continue; - } - - // SET ALERT/RUMBLE ALERT - if (line == 3) this->controller.rumble(this->screen_buffer[line][0].text.c_str()); - else this->controller.set_text(line, 0, this->screen_buffer[line][0].text + std::string(40, ' ')); - this->screen_contents[line] = this->screen_buffer[line][0]; - this->screen_buffer[line].pop_front(); - this->line_set_time[line] = pros::millis(); - } - this->last_printed_line = line; - this->last_print_time = pros::millis(); - } else if (this->screen_contents[line].text == "") { - // text is the same as last frame's text so no use updating - if (this->screen_contents[line].text == this->next_print[line] && line != 3) { - this->next_print[line] = ""; - continue; - } - - // UPDATE TEXT/RUMBLE - if (line == 3) this->controller.rumble(this->next_print[line].c_str()); - else this->controller.set_text(line, 0, this->next_print[line] + std::string(40, ' ')); - this->screen_contents[line].text = std::move(this->next_print[line]); - this->next_print[line] = ""; - this->last_printed_line = line; - this->last_print_time = pros::millis(); + // text on screen is the same as last frame's text so no use updating + if (this->currentScreen[line] == this->nextBuffer[line] && line != 3) { + this->nextBuffer[line] = std::nullopt; + continue; } + + // print to screen or rumble + if (line == 3) this->controller.rumble(this->nextBuffer[line].value_or("").c_str()); + else this->controller.set_text(line, 0, this->nextBuffer[line].value_or("") + std::string(40, ' ')); + this->currentScreen[line] = std::move(this->nextBuffer[line]); + this->nextBuffer[line] = std::nullopt; + this->last_printed_line = line; + this->last_print_time = pros::millis(); } } @@ -135,7 +117,17 @@ void Controller::update() { this->RightX = this->controller.get_analog(ANALOG_RIGHT_X); this->RightY = this->controller.get_analog(ANALOG_RIGHT_Y); - this->updateScreen(); + this->updateScreens(); +} + +void Controller::add_screen(AbstractScreen &screen) { + uint last = UINT32_MAX; uint 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(); + } + this->screens.emplace(this->screens.begin() + pos, screen); } From 66945cf2354feab9a07ddcca0209bf1c4e8c4e61 Mon Sep 17 00:00:00 2001 From: lufimio <67525443+PA055@users.noreply.github.com> Date: Tue, 13 Aug 2024 09:52:19 -0400 Subject: [PATCH 26/54] idk what happened but no errors now, also screen update works hopefully --- include/gamepad/controller.hpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include/gamepad/controller.hpp b/include/gamepad/controller.hpp index 0d765b8..893a799 100644 --- a/include/gamepad/controller.hpp +++ b/include/gamepad/controller.hpp @@ -1,16 +1,16 @@ #pragma once -#include "screens/abstractScreen.hpp" #include #include #include #include -#include "event_handler.hpp" -#include "pros/misc.hpp" -#include "pros/rtos.hpp" #ifndef PROS_USE_SIMPLE_NAMES #define PROS_USE_SIMPLE_NAMES #endif +#include "screens/abstractScreen.hpp" +#include "event_handler.hpp" +#include "pros/misc.hpp" +#include "pros/rtos.hpp" namespace Gamepad { From 4b17c36cf386fafa854fa0c55eb703b698f3c1c6 Mon Sep 17 00:00:00 2001 From: lufimio <67525443+PA055@users.noreply.github.com> Date: Tue, 13 Aug 2024 14:19:22 -0400 Subject: [PATCH 27/54] shared pointers :sparkles: --- include/gamepad/controller.hpp | 10 +++------- include/gamepad/screens/abstractScreen.hpp | 2 -- include/gamepad/screens/defaultScreen.hpp | 3 +-- src/gamepad/controller.cpp | 11 ++++++----- src/gamepad/screens/alertScreen.cpp | 5 +++++ src/gamepad/screens/defaultScreen.cpp | 10 ++++++++++ 6 files changed, 25 insertions(+), 16 deletions(-) diff --git a/include/gamepad/controller.hpp b/include/gamepad/controller.hpp index 893a799..1de2b6b 100644 --- a/include/gamepad/controller.hpp +++ b/include/gamepad/controller.hpp @@ -2,6 +2,7 @@ #include #include +#include #include #include #ifndef PROS_USE_SIMPLE_NAMES @@ -57,7 +58,7 @@ class Controller { */ void update(); - void add_screen(AbstractScreen& screen); + void add_screen(std::shared_ptr screen); /** * Get the state of a button on the controller. * @param button Which button's state you want. @@ -75,17 +76,12 @@ class Controller { X{}, B{}, Y{}, A{}; float LeftX = 0, LeftY = 0, RightX = 0, RightY = 0; private: - - struct { - bool operator()(AbstractScreen &l, AbstractScreen &r) { return l.get_priority() < r.get_priority(); } - } AbstractScreenSort; - static Button Controller::* button_to_ptr(pros::controller_digital_e_t button); void updateButton(pros::controller_digital_e_t button_id); void updateScreens(); - std::vector screens; + std::vector> screens; ScreenBuffer currentScreen; ScreenBuffer nextBuffer; pros::Controller controller; diff --git a/include/gamepad/screens/abstractScreen.hpp b/include/gamepad/screens/abstractScreen.hpp index 8e50463..e1b0fc1 100644 --- a/include/gamepad/screens/abstractScreen.hpp +++ b/include/gamepad/screens/abstractScreen.hpp @@ -21,8 +21,6 @@ class AbstractScreen { virtual ScreenBuffer get_screen(std::set visible_lines) = 0; virtual void handle_events(std::set button_events) = 0; const uint get_priority(); - - bool operator()(AbstractScreen *l, AbstractScreen *r) { return l->get_priority() < r->get_priority(); } private: const uint priority; }; diff --git a/include/gamepad/screens/defaultScreen.hpp b/include/gamepad/screens/defaultScreen.hpp index 719082e..00e69ff 100644 --- a/include/gamepad/screens/defaultScreen.hpp +++ b/include/gamepad/screens/defaultScreen.hpp @@ -8,9 +8,8 @@ namespace Gamepad { class DefaultScreen : AbstractScreen { public: DefaultScreen() : AbstractScreen(1) {} - void update(double delta_time); ScreenBuffer get_screen(std::set visible_lines); - void handle_events(std::set button_events); + void handle_events(std::set button_events) {} void print_line(uint8_t line, std::string str); void rumble(std::string rumble_pattern); diff --git a/src/gamepad/controller.cpp b/src/gamepad/controller.cpp index 6aa764b..f179ba4 100644 --- a/src/gamepad/controller.cpp +++ b/src/gamepad/controller.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -66,7 +67,7 @@ void Controller::updateScreens() { // Update all screens and note deltatime for (int i = 0; i < this->screens.size(); i++) - this->screens[i].update(pros::millis() - this->last_update_time); + this->screens[i]->update(pros::millis() - this->last_update_time); last_update_time = pros::millis(); // Check if enough time has passed for the controller to poll for updates @@ -81,7 +82,7 @@ void Controller::updateScreens() { visible_lines.emplace(j); // get the buffer of the next lower priority screen and set it to be printed - ScreenBuffer buffer = this->screens[i].get_screen(visible_lines); + ScreenBuffer buffer = this->screens[i]->get_screen(visible_lines); for (uint8_t j = 0; j < 4; j++) if (buffer[j].has_value() && !nextBuffer[j].has_value()) nextBuffer[j] = std::move(buffer[j]); @@ -120,12 +121,12 @@ void Controller::update() { this->updateScreens(); } -void Controller::add_screen(AbstractScreen &screen) { +void Controller::add_screen(std::shared_ptr screen) { uint last = UINT32_MAX; uint pos = 0; for (pos = 0; pos < this->screens.size(); pos++) { - if (this->screens[pos].get_priority() < screen.get_priority() && last >= screen.get_priority()) + if (this->screens[pos]->get_priority() < screen->get_priority() && last >= screen->get_priority()) break; - last = this->screens[pos].get_priority(); + last = this->screens[pos]->get_priority(); } this->screens.emplace(this->screens.begin() + pos, screen); } diff --git a/src/gamepad/screens/alertScreen.cpp b/src/gamepad/screens/alertScreen.cpp index 2cfe588..28bba30 100644 --- a/src/gamepad/screens/alertScreen.cpp +++ b/src/gamepad/screens/alertScreen.cpp @@ -7,6 +7,11 @@ namespace Gamepad { +ScreenBuffer AlertScreen::get_screen(std::set visible_lines) { + ScreenBuffer output; + +} + uint AlertScreen::getTotalDuration(uint8_t line) { uint total = 0; diff --git a/src/gamepad/screens/defaultScreen.cpp b/src/gamepad/screens/defaultScreen.cpp index 510f1ed..1010451 100644 --- a/src/gamepad/screens/defaultScreen.cpp +++ b/src/gamepad/screens/defaultScreen.cpp @@ -1,12 +1,22 @@ #include "gamepad/screens/defaultScreen.hpp" #include #include +#include #include #include "gamepad/todo.hpp" namespace Gamepad { +ScreenBuffer DefaultScreen::get_screen(std::set visible_lines) { + ScreenBuffer output; + for (auto i = visible_lines.begin(); i != visible_lines.end(); ++i) { + output[*i] = std::move(this->currentBuffer[*i].value_or("")); + this->currentBuffer[*i] = std::nullopt; + } + return output; +} + void DefaultScreen::print_line(uint8_t line, std::string str) { TODO("change handling for off screen lines") if (line > 2) std::exit(1); From 6c90b923e58acb2efa83ff2a40392b16d2774605 Mon Sep 17 00:00:00 2001 From: lufimio <67525443+PA055@users.noreply.github.com> Date: Tue, 13 Aug 2024 15:02:55 -0400 Subject: [PATCH 28/54] it builds now --- include/gamepad/screens/abstractScreen.hpp | 2 +- include/gamepad/screens/alertScreen.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/gamepad/screens/abstractScreen.hpp b/include/gamepad/screens/abstractScreen.hpp index e1b0fc1..7dde3d7 100644 --- a/include/gamepad/screens/abstractScreen.hpp +++ b/include/gamepad/screens/abstractScreen.hpp @@ -20,7 +20,7 @@ class AbstractScreen { void update(double delta_time) {} virtual ScreenBuffer get_screen(std::set visible_lines) = 0; virtual void handle_events(std::set button_events) = 0; - const uint get_priority(); + const uint get_priority() { return this->priority; } private: const uint priority; }; diff --git a/include/gamepad/screens/alertScreen.hpp b/include/gamepad/screens/alertScreen.hpp index ffd901b..61ead10 100644 --- a/include/gamepad/screens/alertScreen.hpp +++ b/include/gamepad/screens/alertScreen.hpp @@ -14,7 +14,7 @@ class AlertScreen : AbstractScreen { AlertScreen() : AbstractScreen(UINT32_MAX - 100) {} void update(double delta_time); ScreenBuffer get_screen(std::set visible_lines); - void handle_events(std::set button_events); + void handle_events(std::set button_events) {} void add_alerts(uint8_t line, std::string strs, uint32_t duration, std::string rumble = ""); From 8dbb7329338cd9d51e597cc5d7e5eed15d8ee369 Mon Sep 17 00:00:00 2001 From: lufimio <67525443+PA055@users.noreply.github.com> Date: Sat, 17 Aug 2024 20:07:57 -0400 Subject: [PATCH 29/54] alerts should work --- include/gamepad/screens/abstractScreen.hpp | 2 +- include/gamepad/screens/alertScreen.hpp | 16 +++--- src/gamepad/screens/alertScreen.cpp | 66 +++++++++++----------- 3 files changed, 42 insertions(+), 42 deletions(-) diff --git a/include/gamepad/screens/abstractScreen.hpp b/include/gamepad/screens/abstractScreen.hpp index 7dde3d7..665d804 100644 --- a/include/gamepad/screens/abstractScreen.hpp +++ b/include/gamepad/screens/abstractScreen.hpp @@ -17,7 +17,7 @@ typedef std::array, 4> ScreenBuffer; class AbstractScreen { public: AbstractScreen(uint priority): priority(priority) {} - void update(double delta_time) {} + void update(uint delta_time) {} virtual ScreenBuffer get_screen(std::set visible_lines) = 0; virtual void handle_events(std::set button_events) = 0; const uint get_priority() { return this->priority; } diff --git a/include/gamepad/screens/alertScreen.hpp b/include/gamepad/screens/alertScreen.hpp index 61ead10..b725904 100644 --- a/include/gamepad/screens/alertScreen.hpp +++ b/include/gamepad/screens/alertScreen.hpp @@ -2,8 +2,10 @@ #include #include +#include #include #include +#include "abstractScreen.hpp" #include "pros/rtos.hpp" #include "gamepad/screens/abstractScreen.hpp" @@ -12,23 +14,21 @@ namespace Gamepad { class AlertScreen : AbstractScreen { public: AlertScreen() : AbstractScreen(UINT32_MAX - 100) {} - void update(double delta_time); + void update(uint delta_time); ScreenBuffer get_screen(std::set visible_lines); void handle_events(std::set button_events) {} void add_alerts(uint8_t line, std::string strs, uint32_t duration, std::string rumble = ""); private: - struct Line { - std::string text; + struct AlertBuffer { + ScreenBuffer screen; uint duration; }; - uint getTotalDuration(uint8_t line); - - std::array, 4> screen_buffer{}; - std::array screen_contents{}; - std::array line_set_time{}; + std::deque screen_buffer{}; + std::optional screen_contents; + uint line_set_time; pros::Mutex mut; }; diff --git a/src/gamepad/screens/alertScreen.cpp b/src/gamepad/screens/alertScreen.cpp index 28bba30..4e01882 100644 --- a/src/gamepad/screens/alertScreen.cpp +++ b/src/gamepad/screens/alertScreen.cpp @@ -2,60 +2,60 @@ #include #include #include +#include #include #include "gamepad/todo.hpp" +#include "pros/rtos.hpp" namespace Gamepad { ScreenBuffer AlertScreen::get_screen(std::set visible_lines) { - ScreenBuffer output; + std::lock_guard guard(this->mut); + if (this->screen_contents.has_value()) return this->screen_contents->screen; + if (this->screen_buffer.size() < 1) return ScreenBuffer(); -} + for (uint8_t i = 0; i < 4; i++) { + if (!this->screen_buffer[0].screen[i].has_value()) continue; + if (this->screen_buffer[0].screen[i].has_value() && !visible_lines.contains(i)) return ScreenBuffer(); + } + this->screen_contents = std::move(this->screen_buffer[0]); + this->screen_buffer.pop_front(); + this->line_set_time = pros::millis(); + return this->screen_contents->screen; +} -uint AlertScreen::getTotalDuration(uint8_t line) { - uint total = 0; - for (Line msg : this->screen_buffer[line]) - total += msg.duration; - return total; +void AlertScreen::update(uint delta_time) { + std::lock_guard guard(this->mut); + 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) { TODO("change handling for off screen lines") if (line > 2) std::exit(1); - if (str.find('\n') != std::string::npos) { - TODO("warn instead of throw error if there are too many lines") - if (std::ranges::count(str, '\n') > 2) std::exit(1); + TODO("warn instead of throw error if there are too many lines") + if (std::ranges::count(str, '\n') > 2) std::exit(1); - std::vector strs(3); - std::stringstream ss(str); + std::vector strs(3, ""); + std::stringstream ss(str); - // split string by newlines but only take the first 3 lines - for (int i = line; i < 3; i++) { - if (!std::getline(ss, strs[i], '\n')) break; - } - - std::lock_guard guard(this->mut); + // split string by newlines but only take the first 3 lines + for (int i = line; i < 3; i++) { + if (!std::getline(ss, strs[i], '\n')) break; + } - // get next available time slot for all lines - uint minSpot = UINT32_MAX; - for (uint8_t line = 0; line < 4; line++) minSpot = std::min(minSpot, getTotalDuration(line)); - // Schedule alerts - for (int i = 0; i < 4; i++) { - // add delay until theres a spot for all lines together - if (getTotalDuration(i) < minSpot) - this->screen_buffer[i].push_back({ .text = "", .duration = (minSpot - getTotalDuration(i)) }); + ScreenBuffer buffer; - if (i == 3) this->screen_buffer[i].push_back({.text = std::move(rumble), .duration = duration}); - else this->screen_buffer[i].push_back({.text = std::move(strs[i]), .duration = 0}); - } - return; - } + if (strs[0] != "") buffer[0] = std::move(strs[0]); + if (strs[1] != "") buffer[1] = std::move(strs[1]); + if (strs[2] != "") buffer[2] = std::move(strs[2]); + if (rumble != "") buffer[3] = std::move(rumble); std::lock_guard guard(this->mut); - this->screen_buffer[line].push_back({ .text = std::move(str), .duration = duration }); + this->screen_buffer.push_back({buffer, duration}); } -} \ No newline at end of file +} From 23c4bbfca211304bfea8dfae6ae5d8f93bdfe9d4 Mon Sep 17 00:00:00 2001 From: lufimio <67525443+PA055@users.noreply.github.com> Date: Wed, 4 Sep 2024 16:24:33 -0400 Subject: [PATCH 30/54] what should i do for this? --- include/gamepad/controller.hpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/include/gamepad/controller.hpp b/include/gamepad/controller.hpp index 1de2b6b..86209c6 100644 --- a/include/gamepad/controller.hpp +++ b/include/gamepad/controller.hpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #ifndef PROS_USE_SIMPLE_NAMES @@ -57,8 +58,18 @@ class Controller { * @note Create a separate instance for each task. */ void update(); - + /** + * Add a screen to the sceen update loop that can update the controller's screen + * + * @param screen the `AbstractScreen` to + */ void add_screen(std::shared_ptr screen); + void remove_screen(); // TODO: Find a good way to access the screen to remove + + // TODO: Change this to be a better access point + std::vector> get_screens(); + std::vector> get_screens(uint priority); + std::vector> get_screens(uint min_priority, uint max_priority); /** * Get the state of a button on the controller. * @param button Which button's state you want. From 790929ca03414c2796cc09f403869f8d9670a712 Mon Sep 17 00:00:00 2001 From: lufimio <67525443+PA055@users.noreply.github.com> Date: Tue, 10 Sep 2024 18:20:50 -0400 Subject: [PATCH 31/54] bug fixing :sob: --- Makefile | 2 +- debug.log | 2 ++ include/gamepad/api.hpp | 3 +- include/gamepad/controller.hpp | 27 ++++++++++++------ include/gamepad/screens/abstractScreen.hpp | 4 +-- include/gamepad/screens/alertScreen.hpp | 5 ++-- include/gamepad/screens/defaultScreen.hpp | 6 ++-- src/gamepad/controller.cpp | 16 +++++++++++ src/gamepad/screens/defaultScreen.cpp | 15 ++++++++-- src/main.cpp | 32 ++++------------------ 10 files changed, 64 insertions(+), 48 deletions(-) create mode 100644 debug.log diff --git a/Makefile b/Makefile index 10d24a2..37780c2 100644 --- a/Makefile +++ b/Makefile @@ -24,7 +24,7 @@ USE_PACKAGE:=1 EXCLUDE_COLD_LIBRARIES:= # Set this to 1 to add additional rules to compile your project as a PROS library template -IS_LIBRARY:=1 +IS_LIBRARY:=0 # TODO: CHANGE THIS! LIBNAME:=gamepad VERSION:=0.0.1 diff --git a/debug.log b/debug.log new file mode 100644 index 0000000..10fb722 --- /dev/null +++ b/debug.log @@ -0,0 +1,2 @@ +[0909/102831.528:ERROR:registration_protocol_win.cc(108)] CreateFile: The system cannot find the file specified. (0x2) +[0909/102852.263:ERROR:registration_protocol_win.cc(108)] CreateFile: The system cannot find the file specified. (0x2) diff --git a/include/gamepad/api.hpp b/include/gamepad/api.hpp index 835385a..42d042b 100644 --- a/include/gamepad/api.hpp +++ b/include/gamepad/api.hpp @@ -1,4 +1,5 @@ #pragma once #include "gamepad/event_handler.hpp" // IWYU pragma: export -#include "gamepad/controller.hpp" // IWYU pragma: export \ No newline at end of file +#include "gamepad/controller.hpp" // IWYU pragma: export +#include "gamepad/screens/alertScreen.hpp" // IWYU pragma: export diff --git a/include/gamepad/controller.hpp b/include/gamepad/controller.hpp index 21cca46..f627fd1 100644 --- a/include/gamepad/controller.hpp +++ b/include/gamepad/controller.hpp @@ -1,6 +1,7 @@ #pragma once #include "pros/misc.h" +#include "screens/defaultScreen.hpp" #include #include #include @@ -60,15 +61,23 @@ class Controller { /** * Add a screen to the sceen update loop that can update the controller's screen * - * @param screen the `AbstractScreen` to + * @param screen the `AbstractScreen` to add to the screen queue */ void add_screen(std::shared_ptr screen); - void remove_screen(); // TODO: Find a good way to access the screen to remove + /** + * 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) + */ + void print_line(uint8_t line, std::string str); + /** + * 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. + */ + void rumble(std::string rumble_pattern); - // TODO: Change this to be a better access point - std::vector> get_screens(); - std::vector> get_screens(uint priority); - std::vector> get_screens(uint min_priority, uint max_priority); /** * Get the state of a button on the controller. * @param button Which button's state you want. @@ -87,15 +96,15 @@ class Controller { /// The partner controller, same as @ref Gamepad::partner static Controller partner; private: - explicit Controller(pros::controller_id_e_t id) - : controller(id) {} + explicit Controller(pros::controller_id_e_t id) : controller(id) {} static Button Controller::*button_to_ptr(pros::controller_digital_e_t button); void updateButton(pros::controller_digital_e_t button_id); void updateScreens(); - std::vector> screens; + std::shared_ptr defaultScreen; + std::vector> screens{defaultScreen}; ScreenBuffer currentScreen; ScreenBuffer nextBuffer; pros::Controller controller; diff --git a/include/gamepad/screens/abstractScreen.hpp b/include/gamepad/screens/abstractScreen.hpp index 665d804..f3e832f 100644 --- a/include/gamepad/screens/abstractScreen.hpp +++ b/include/gamepad/screens/abstractScreen.hpp @@ -18,8 +18,8 @@ class AbstractScreen { public: AbstractScreen(uint priority): priority(priority) {} void update(uint delta_time) {} - virtual ScreenBuffer get_screen(std::set visible_lines) = 0; - virtual void handle_events(std::set button_events) = 0; + ScreenBuffer get_screen(std::set visible_lines) {return {};} + void handle_events(std::set button_events) {} const uint get_priority() { return this->priority; } private: const uint priority; diff --git a/include/gamepad/screens/alertScreen.hpp b/include/gamepad/screens/alertScreen.hpp index b725904..55e1af8 100644 --- a/include/gamepad/screens/alertScreen.hpp +++ b/include/gamepad/screens/alertScreen.hpp @@ -4,14 +4,13 @@ #include #include #include -#include #include "abstractScreen.hpp" #include "pros/rtos.hpp" #include "gamepad/screens/abstractScreen.hpp" namespace Gamepad { -class AlertScreen : AbstractScreen { +class AlertScreen : public AbstractScreen { public: AlertScreen() : AbstractScreen(UINT32_MAX - 100) {} void update(uint delta_time); @@ -32,4 +31,4 @@ class AlertScreen : AbstractScreen { pros::Mutex mut; }; -} \ No newline at end of file +} diff --git a/include/gamepad/screens/defaultScreen.hpp b/include/gamepad/screens/defaultScreen.hpp index 00e69ff..8333e4e 100644 --- a/include/gamepad/screens/defaultScreen.hpp +++ b/include/gamepad/screens/defaultScreen.hpp @@ -5,9 +5,9 @@ namespace Gamepad { -class DefaultScreen : AbstractScreen { +class DefaultScreen : public AbstractScreen { public: - DefaultScreen() : AbstractScreen(1) {} + DefaultScreen(); ScreenBuffer get_screen(std::set visible_lines); void handle_events(std::set button_events) {} @@ -20,4 +20,4 @@ class DefaultScreen : AbstractScreen { }; -} \ No newline at end of file +} diff --git a/src/gamepad/controller.cpp b/src/gamepad/controller.cpp index aac68ed..abfae71 100644 --- a/src/gamepad/controller.cpp +++ b/src/gamepad/controller.cpp @@ -2,8 +2,11 @@ #include "gamepad/todo.hpp" #include "pros/rtos.hpp" #include "pros/misc.h" +#include "screens/defaultScreen.hpp" +#include #include #include +#include #include #include #include @@ -124,6 +127,19 @@ void Controller::add_screen(std::shared_ptr screen) { this->screens.emplace(this->screens.begin() + pos, screen); } +void Controller::print_line(uint8_t line, std::string str) { + if (std::count(this->screens.begin(), this->screens.end(), this->defaultScreen) == 0) { + this->add_screen(this->defaultScreen); + } + printf("%i elements in screens\n", this->screens.size()); + + this->defaultScreen->print_line(line, str); +} + +void Controller::rumble(std::string rumble_pattern) { + this->defaultScreen->rumble(rumble_pattern); +} + const Button& Controller::operator[](pros::controller_digital_e_t button) { return this->*Controller::button_to_ptr(button); diff --git a/src/gamepad/screens/defaultScreen.cpp b/src/gamepad/screens/defaultScreen.cpp index 1010451..c71ad19 100644 --- a/src/gamepad/screens/defaultScreen.cpp +++ b/src/gamepad/screens/defaultScreen.cpp @@ -3,13 +3,23 @@ #include #include #include +#include "abstractScreen.hpp" #include "gamepad/todo.hpp" namespace Gamepad { +DefaultScreen::DefaultScreen() : AbstractScreen(1) { + printf("ran constructor\n"); +} + ScreenBuffer DefaultScreen::get_screen(std::set visible_lines) { ScreenBuffer output; + printf("{%s, %s, %s, %s}\n", + currentBuffer.at(0).value_or("N/A").c_str(), + currentBuffer.at(1).value_or("N/A").c_str(), + currentBuffer.at(2).value_or("N/A").c_str(), + currentBuffer.at(3).value_or("N/A").c_str()); for (auto i = visible_lines.begin(); i != visible_lines.end(); ++i) { output[*i] = std::move(this->currentBuffer[*i].value_or("")); this->currentBuffer[*i] = std::nullopt; @@ -18,6 +28,7 @@ ScreenBuffer DefaultScreen::get_screen(std::set visible_lines) { } void DefaultScreen::print_line(uint8_t line, std::string str) { + printf("print_line(line: %i, string: %s)\n", line, str.c_str()); TODO("change handling for off screen lines") if (line > 2) std::exit(1); @@ -30,12 +41,12 @@ void DefaultScreen::print_line(uint8_t line, std::string str) { std::vector strs(3); std::stringstream ss(str); - for (int i = line; i < 3; i++) { + for (int i = line; i < 3; i++) if (!std::getline(ss, strs[i], '\n')) break; - } for (uint8_t l = 0; l < 3; l++) this->currentBuffer[l] = std::move(strs[l]); + return; } diff --git a/src/main.cpp b/src/main.cpp index 30d56e9..978f84d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,21 +1,8 @@ #include "main.h" #include "gamepad/api.hpp" +#include "gamepad/controller.hpp" +#include "pros/rtos.hpp" -/** - * A callback function for LLEMU's center button. - * - * When this callback is fired, it will toggle line 2 of the LCD text between - * "I was pressed!" and nothing. - */ -void on_center_button() { - static bool pressed = false; - pressed = !pressed; - if (pressed) { - pros::lcd::set_text(2, "I was pressed!"); - } else { - pros::lcd::clear_line(2); - } -} /** * Runs initialization code. This occurs as soon as the program is started. @@ -25,9 +12,6 @@ void on_center_button() { */ void initialize() { pros::lcd::initialize(); - pros::lcd::set_text(1, "Hello PROS User!"); - - pros::lcd::register_btn1_cb(on_center_button); } /** @@ -75,16 +59,10 @@ void autonomous() {} * task, not resume it from where it left off. */ void opcontrol() { - pros::MotorGroup left_mg({1, -2, 3}); // Creates a motor group with forwards ports 1 & 3 and reversed port 2 - pros::MotorGroup right_mg({-4, 5, -6}); // Creates a motor group with forwards port 4 and reversed ports 4 & 6 - while (true) { Gamepad::master.update(); - // Arcade control scheme - int dir = Gamepad::master.LeftY; // Gets amount forward/backward from left joystick - int turn = Gamepad::master.RightX; // Gets the turn left/right from right joystick - left_mg.move(dir - turn); // Sets left motor voltage - right_mg.move(dir + turn); // Sets right motor voltage - pros::delay(25); // Run for 25 ms then update + + Gamepad::master.print_line(0, "hello"); + pros::delay(25); } } \ No newline at end of file From d82ef228c4411d6b3a236f6c49abb8fdbbbf3869 Mon Sep 17 00:00:00 2001 From: lufimio <67525443+PA055@users.noreply.github.com> Date: Tue, 10 Sep 2024 18:29:45 -0400 Subject: [PATCH 32/54] help :skull: --- debug.log | 1 + src/gamepad/controller.cpp | 5 +---- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/debug.log b/debug.log index 10fb722..a6ae3a0 100644 --- a/debug.log +++ b/debug.log @@ -1,2 +1,3 @@ [0909/102831.528:ERROR:registration_protocol_win.cc(108)] CreateFile: The system cannot find the file specified. (0x2) [0909/102852.263:ERROR:registration_protocol_win.cc(108)] CreateFile: The system cannot find the file specified. (0x2) +[0910/182219.664:ERROR:registration_protocol_win.cc(108)] CreateFile: The system cannot find the file specified. (0x2) diff --git a/src/gamepad/controller.cpp b/src/gamepad/controller.cpp index abfae71..4252b3c 100644 --- a/src/gamepad/controller.cpp +++ b/src/gamepad/controller.cpp @@ -128,10 +128,7 @@ void Controller::add_screen(std::shared_ptr screen) { } void Controller::print_line(uint8_t line, std::string str) { - if (std::count(this->screens.begin(), this->screens.end(), this->defaultScreen) == 0) { - this->add_screen(this->defaultScreen); - } - printf("%i elements in screens\n", this->screens.size()); + printf("wrapping print_line(line:%i, string:%s)\n", line, str.c_str()); this->defaultScreen->print_line(line, str); } From 385fc82a4af4872e6af546217616774a6492481d Mon Sep 17 00:00:00 2001 From: lufimio <67525443+PA055@users.noreply.github.com> Date: Tue, 10 Sep 2024 18:33:26 -0400 Subject: [PATCH 33/54] why do i need to commit debug.log :interrobang: --- debug.log | 3 +++ 1 file changed, 3 insertions(+) diff --git a/debug.log b/debug.log index a6ae3a0..86c1de7 100644 --- a/debug.log +++ b/debug.log @@ -1,3 +1,6 @@ [0909/102831.528:ERROR:registration_protocol_win.cc(108)] CreateFile: The system cannot find the file specified. (0x2) [0909/102852.263:ERROR:registration_protocol_win.cc(108)] CreateFile: The system cannot find the file specified. (0x2) [0910/182219.664:ERROR:registration_protocol_win.cc(108)] CreateFile: The system cannot find the file specified. (0x2) +[0910/183206.884:ERROR:registration_protocol_win.cc(108)] CreateFile: The system cannot find the file specified. (0x2) +[0910/183223.865:ERROR:registration_protocol_win.cc(108)] CreateFile: The system cannot find the file specified. (0x2) +[0910/183232.392:ERROR:registration_protocol_win.cc(108)] CreateFile: The system cannot find the file specified. (0x2) From d5b226a0c5d2c9059f47075b3baf903daa18a09a Mon Sep 17 00:00:00 2001 From: lufimio <67525443+PA055@users.noreply.github.com> Date: Tue, 10 Sep 2024 19:03:50 -0400 Subject: [PATCH 34/54] idk but this breaks it --- src/main.cpp | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index abb25a0..f5ba7bc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -19,17 +19,17 @@ void leftLongPress1() { printf("Left Long Press!\n"); } void leftShortRelease1() { printf("Left Short Release!\n"); } void initialize() { - // We can register functions to run when buttons are pressed - Gamepad::master.Down.onPress("downPress1", downPress1); - // ...or when they're released - Gamepad::master.Up.onRelease("downRelease1", upRelease1); - // There's also the longPress event - Gamepad::master.Left.onLongPress("leftLongPress1", leftLongPress1); - // We can have two functions on one button, - // just remember to give them different names - Gamepad::master.Left.onShortRelease("leftShortRelease", leftShortRelease1); - // And we can use lambda's too - Gamepad::master.X.onShortRelease("xShortRelease1", []() { printf("X Short Release!\n"); }); + // // We can register functions to run when buttons are pressed + // Gamepad::master.Down.onPress("downPress1", downPress1); + // // ...or when they're released + // Gamepad::master.Up.onRelease("downRelease1", upRelease1); + // // There's also the longPress event + // Gamepad::master.Left.onLongPress("leftLongPress1", leftLongPress1); + // // We can have two functions on one button, + // // just remember to give them different names + // Gamepad::master.Left.onShortRelease("leftShortRelease", leftShortRelease1); + // // And we can use lambda's too + // Gamepad::master.X.onShortRelease("xShortRelease1", []() { printf("X Short Release!\n"); }); } /** @@ -77,8 +77,8 @@ void autonomous() {} * task, not resume it from where it left off. */ void opcontrol() { - pros::MotorGroup left_mg({1, -2, 3}); // Creates a motor group with forwards ports 1 & 3 and reversed port 2 - pros::MotorGroup right_mg({-4, 5, -6}); // Creates a motor group with forwards port 4 and reversed ports 4 & 6 + // pros::MotorGroup left_mg({1, -2, 3}); // Creates a motor group with forwards ports 1 & 3 and reversed port 2 + // pros::MotorGroup right_mg({-4, 5, -6}); // Creates a motor group with forwards port 4 and reversed ports 4 & 6 while (true) { // Remember to ALWAYS call update at the start of your while loop! @@ -87,10 +87,10 @@ void opcontrol() { Gamepad::master.print_line(0, "hello\n\nhi"); // We'll use the arcade control scheme - int dir = Gamepad::master.LeftY; // Gets amount forward/backward from left joystick - int turn = Gamepad::master.RightX; // Gets the turn left/right from right joystick - left_mg.move(dir - turn); // Sets left motor voltage - right_mg.move(dir + turn); // Sets right motor voltage + // int dir = Gamepad::master.LeftY; // Gets amount forward/backward from left joystick + // int turn = Gamepad::master.RightX; // Gets the turn left/right from right joystick + // left_mg.move(dir - turn); // Sets left motor voltage + // right_mg.move(dir + turn); // Sets right motor voltage pros::delay(25); // Wait for 25 ms, then update the motor values again } From 466b8e1c15831b5c008315b38bacd69ffe3c542a Mon Sep 17 00:00:00 2001 From: lufimio <67525443+PA055@users.noreply.github.com> Date: Sat, 21 Sep 2024 10:19:31 -0400 Subject: [PATCH 35/54] bug: :bug: explicit controller constructor isnt getting called --- include/gamepad/controller.hpp | 8 ++++-- include/gamepad/screens/defaultScreen.hpp | 3 ++- src/gamepad/screens/defaultScreen.cpp | 22 +++++++++++++---- src/main.cpp | 30 +---------------------- 4 files changed, 26 insertions(+), 37 deletions(-) diff --git a/include/gamepad/controller.hpp b/include/gamepad/controller.hpp index d725338..6613338 100644 --- a/include/gamepad/controller.hpp +++ b/include/gamepad/controller.hpp @@ -267,7 +267,11 @@ class Controller { /// The partner controller, same as @ref Gamepad::partner static Controller partner; private: - explicit Controller(pros::controller_id_e_t id) : controller(id) {} + explicit Controller(pros::controller_id_e_t id) : controller(id) { + printf("screens length: %i\n", screens.size()); + defaultScreen = std::make_shared(); + screens.push_back(defaultScreen); + } 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 {}; @@ -288,7 +292,7 @@ class Controller { void updateScreens(); std::shared_ptr defaultScreen; - std::vector> screens{defaultScreen}; + std::vector> screens; ScreenBuffer currentScreen; ScreenBuffer nextBuffer; pros::Controller controller; diff --git a/include/gamepad/screens/defaultScreen.hpp b/include/gamepad/screens/defaultScreen.hpp index 8333e4e..5304d22 100644 --- a/include/gamepad/screens/defaultScreen.hpp +++ b/include/gamepad/screens/defaultScreen.hpp @@ -2,6 +2,7 @@ #include "gamepad/screens/abstractScreen.hpp" #include "pros/rtos.hpp" +#include namespace Gamepad { @@ -15,7 +16,7 @@ class DefaultScreen : public AbstractScreen { void rumble(std::string rumble_pattern); private: - ScreenBuffer currentBuffer; + ScreenBuffer currentBuffer{std::nullopt, std::nullopt, std::nullopt, std::nullopt}; pros::Mutex mut; }; diff --git a/src/gamepad/screens/defaultScreen.cpp b/src/gamepad/screens/defaultScreen.cpp index c71ad19..30e3212 100644 --- a/src/gamepad/screens/defaultScreen.cpp +++ b/src/gamepad/screens/defaultScreen.cpp @@ -1,5 +1,6 @@ #include "gamepad/screens/defaultScreen.hpp" #include +#include #include #include #include @@ -20,6 +21,7 @@ ScreenBuffer DefaultScreen::get_screen(std::set visible_lines) { currentBuffer.at(1).value_or("N/A").c_str(), currentBuffer.at(2).value_or("N/A").c_str(), currentBuffer.at(3).value_or("N/A").c_str()); + // const std::lock_guard guard(this->mut); for (auto i = visible_lines.begin(); i != visible_lines.end(); ++i) { output[*i] = std::move(this->currentBuffer[*i].value_or("")); this->currentBuffer[*i] = std::nullopt; @@ -32,24 +34,34 @@ void DefaultScreen::print_line(uint8_t line, std::string str) { TODO("change handling for off screen lines") if (line > 2) std::exit(1); - std::lock_guard guard(this->mut); + printf("is this the problem\n"); + // const std::lock_guard guard(this->mut); + printf("or is this the problem\n"); if (str.find('\n') != std::string::npos) { TODO("warn instead of throw error if there are too many lines") + printf("1\n"); if (std::ranges::count(str, '\n') > 2) std::exit(1); + printf("2\n"); std::vector strs(3); std::stringstream ss(str); - for (int i = line; i < 3; i++) + printf("3\n"); + for (int i = line; i < 3; i++) { + printf("iteration %i\n", i); if (!std::getline(ss, strs[i], '\n')) break; + } - for (uint8_t l = 0; l < 3; l++) - this->currentBuffer[l] = std::move(strs[l]); - + printf("4\n"); + for (uint8_t l = 0; l < 3; l++) { + printf("iteration %i, str: %s, buffer: %s\n", l, strs[l].c_str(), "test"); // currentBuffer[l].value_or("nullopt").c_str()); + if (!strs[l].empty()) this->currentBuffer[l] = (strs[l]); + } return; } + printf("5\n"); this->currentBuffer[line] = std::move(str); } diff --git a/src/main.cpp b/src/main.cpp index f5ba7bc..18f53d0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,26 +9,7 @@ * All other competition modes are blocked by initialize; it is recommended * to keep execution time for this mode under a few seconds. */ - -void downPress1() { printf("Down Press!\n"); } - -void upRelease1() { printf("Up Release!\n"); } - -void leftLongPress1() { printf("Left Long Press!\n"); } - -void leftShortRelease1() { printf("Left Short Release!\n"); } - void initialize() { - // // We can register functions to run when buttons are pressed - // Gamepad::master.Down.onPress("downPress1", downPress1); - // // ...or when they're released - // Gamepad::master.Up.onRelease("downRelease1", upRelease1); - // // There's also the longPress event - // Gamepad::master.Left.onLongPress("leftLongPress1", leftLongPress1); - // // We can have two functions on one button, - // // just remember to give them different names - // Gamepad::master.Left.onShortRelease("leftShortRelease", leftShortRelease1); - // // And we can use lambda's too // Gamepad::master.X.onShortRelease("xShortRelease1", []() { printf("X Short Release!\n"); }); } @@ -77,20 +58,11 @@ void autonomous() {} * task, not resume it from where it left off. */ void opcontrol() { - // pros::MotorGroup left_mg({1, -2, 3}); // Creates a motor group with forwards ports 1 & 3 and reversed port 2 - // pros::MotorGroup right_mg({-4, 5, -6}); // Creates a motor group with forwards port 4 and reversed ports 4 & 6 - while (true) { // Remember to ALWAYS call update at the start of your while loop! Gamepad::master.update(); - Gamepad::master.print_line(0, "hello\n\nhi"); - - // We'll use the arcade control scheme - // int dir = Gamepad::master.LeftY; // Gets amount forward/backward from left joystick - // int turn = Gamepad::master.RightX; // Gets the turn left/right from right joystick - // left_mg.move(dir - turn); // Sets left motor voltage - // right_mg.move(dir + turn); // Sets right motor voltage + // Gamepad::master.print_line(0, "hello\n\nhi"); pros::delay(25); // Wait for 25 ms, then update the motor values again } From 9bfcbb33123ba49cfc9e17f77f1c247995c0b021 Mon Sep 17 00:00:00 2001 From: lufimio <67525443+PA055@users.noreply.github.com> Date: Wed, 25 Sep 2024 18:15:13 -0400 Subject: [PATCH 36/54] fix: :construction: it might work testing in the vexide sim did not result in any errors, idk why --- include/gamepad/screens/defaultScreen.hpp | 2 +- src/gamepad/screens/defaultScreen.cpp | 10 +++++----- src/main.cpp | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/include/gamepad/screens/defaultScreen.hpp b/include/gamepad/screens/defaultScreen.hpp index 5304d22..9101281 100644 --- a/include/gamepad/screens/defaultScreen.hpp +++ b/include/gamepad/screens/defaultScreen.hpp @@ -16,7 +16,7 @@ class DefaultScreen : public AbstractScreen { void rumble(std::string rumble_pattern); private: - ScreenBuffer currentBuffer{std::nullopt, std::nullopt, std::nullopt, std::nullopt}; + ScreenBuffer currentBuffer; pros::Mutex mut; }; diff --git a/src/gamepad/screens/defaultScreen.cpp b/src/gamepad/screens/defaultScreen.cpp index 30e3212..74a4c49 100644 --- a/src/gamepad/screens/defaultScreen.cpp +++ b/src/gamepad/screens/defaultScreen.cpp @@ -10,17 +10,17 @@ namespace Gamepad { -DefaultScreen::DefaultScreen() : AbstractScreen(1) { +DefaultScreen::DefaultScreen() : AbstractScreen(1), currentBuffer({}) { printf("ran constructor\n"); } ScreenBuffer DefaultScreen::get_screen(std::set visible_lines) { ScreenBuffer output; printf("{%s, %s, %s, %s}\n", - currentBuffer.at(0).value_or("N/A").c_str(), - currentBuffer.at(1).value_or("N/A").c_str(), - currentBuffer.at(2).value_or("N/A").c_str(), - currentBuffer.at(3).value_or("N/A").c_str()); + currentBuffer.at(0).value_or("nullopt").c_str(), + currentBuffer.at(1).value_or("nullopt").c_str(), + currentBuffer.at(2).value_or("nullopt").c_str(), + currentBuffer.at(3).value_or("nullopt").c_str()); // const std::lock_guard guard(this->mut); for (auto i = visible_lines.begin(); i != visible_lines.end(); ++i) { output[*i] = std::move(this->currentBuffer[*i].value_or("")); diff --git a/src/main.cpp b/src/main.cpp index 18f53d0..5f61401 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -62,7 +62,7 @@ void opcontrol() { // Remember to ALWAYS call update at the start of your while loop! Gamepad::master.update(); - // Gamepad::master.print_line(0, "hello\n\nhi"); + Gamepad::master.print_line(0, "hello\n\nhi"); pros::delay(25); // Wait for 25 ms, then update the motor values again } From 4958fa113ce27d101ec20cb422bc4f24deee25df Mon Sep 17 00:00:00 2001 From: lufimio <67525443+PA055@users.noreply.github.com> Date: Wed, 25 Sep 2024 18:22:08 -0400 Subject: [PATCH 37/54] feat: :construction_worker: add vexide sim devcontainer --- .devcontainer/Dockerfile | 62 +++++++++++++++++++++++++++++++++ .devcontainer/devcontainer.json | 13 ++++++- .devcontainer/packagelist | 6 ++++ 3 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 .devcontainer/Dockerfile create mode 100644 .devcontainer/packagelist diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile new file mode 100644 index 0000000..1c1b408 --- /dev/null +++ b/.devcontainer/Dockerfile @@ -0,0 +1,62 @@ +FROM rust:1.67 as builder + +RUN git clone https://github.com/vexide/vex-v5-qemu.git /vex-v5-qemu + +WORKDIR /vex-v5-qemu +RUN cd packages/kernel; cargo build --target-dir /target/kernel + +RUN cd packages/client-cli; cargo install --path . --root /target/client-cli + +FROM mcr.microsoft.com/devcontainers/cpp:1.0-jammy +# ------------ +# Install Required Packages +# ------------ +RUN sudo apt-get update +COPY ./.devcontainer/packagelist /packagelist +RUN sudo apt-get -y install $(cat /packagelist) +RUN rm /packagelist # Cleanup image +RUN sudo apt-get clean # Cleanup image + +# ------------ +# Install Clangd +# ------------ +RUN curl -sLo clangd.zip $( \ + curl -s https://api.github.com/repos/clangd/clangd/releases/latest \ + | jq -r '[.assets[] | select(.name | test ("^clangd-linux"))][0].browser_download_url' \ + ) \ + && unzip clangd.zip -d /usr/local/share \ + && mv /usr/local/share/clangd_*/ /usr/local/share/clangd \ + && rm clangd.zip + +ENV PATH="$PATH:/usr/local/share/clangd/bin" + +# ------------ +# Install PROS CLI +# ------------ +RUN python3 -m pip install pros-cli + +# ------------ +# Install ARM Toolchain +# ------------ +COPY --from=ghcr.io/lemlib/pros-build:v2.0.2 /gcc-arm-none-eabi-10.3-2021.10 /usr/local/share/arm-none-eabi +ENV PATH="$PATH:/usr/local/share/arm-none-eabi/bin" + +# Copy the simulator binary +COPY --from=builder /target/client-cli/bin/client-cli /usr/local/bin/simulator + +# Clone pros kernel source so we can reference it when debugging +COPY ./project.pros /project.pros +ENV PROS_SOURCE_PATH="$HOME/.pros" +RUN git clone https://github.com/purduesigbots/pros.git $PROS_SOURCE_PATH \ + --depth 1 \ + --branch $( Date: Thu, 26 Sep 2024 00:18:56 +0000 Subject: [PATCH 38/54] =?UTF-8?q?feat:=20=F0=9F=91=B7=20add=20launch=20con?= =?UTF-8?q?figs=20and=20tasks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 2 +- src/gamepad/screens/defaultScreen.cpp | 9 ++------- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index 37780c2..fbd001f 100644 --- a/Makefile +++ b/Makefile @@ -17,7 +17,7 @@ EXTRA_CFLAGS= EXTRA_CXXFLAGS= # Set to 1 to enable hot/cold linking -USE_PACKAGE:=1 +USE_PACKAGE:=0 # Add libraries you do not wish to include in the cold image here # EXCLUDE_COLD_LIBRARIES:= $(FWDIR)/your_library.a diff --git a/src/gamepad/screens/defaultScreen.cpp b/src/gamepad/screens/defaultScreen.cpp index 74a4c49..121a94e 100644 --- a/src/gamepad/screens/defaultScreen.cpp +++ b/src/gamepad/screens/defaultScreen.cpp @@ -35,27 +35,22 @@ void DefaultScreen::print_line(uint8_t line, std::string str) { if (line > 2) std::exit(1); printf("is this the problem\n"); - // const std::lock_guard guard(this->mut); + const std::lock_guard guard(this->mut); printf("or is this the problem\n"); if (str.find('\n') != std::string::npos) { TODO("warn instead of throw error if there are too many lines") - printf("1\n"); if (std::ranges::count(str, '\n') > 2) std::exit(1); - printf("2\n"); std::vector strs(3); std::stringstream ss(str); - printf("3\n"); for (int i = line; i < 3; i++) { - printf("iteration %i\n", i); if (!std::getline(ss, strs[i], '\n')) break; } - printf("4\n"); for (uint8_t l = 0; l < 3; l++) { - printf("iteration %i, str: %s, buffer: %s\n", l, strs[l].c_str(), "test"); // currentBuffer[l].value_or("nullopt").c_str()); + printf("iteration %i, str: %s, buffer: %s\n", l, strs[l].c_str(), currentBuffer[l].value_or("nullopt").c_str()); if (!strs[l].empty()) this->currentBuffer[l] = (strs[l]); } return; From 76f1af3431f354aeda571c2e888a9164667aea86 Mon Sep 17 00:00:00 2001 From: lufimio <67525443+PA055@users.noreply.github.com> Date: Wed, 25 Sep 2024 20:39:46 -0400 Subject: [PATCH 39/54] feat: :construction: Hot/Cold linking breaks it --- Makefile | 2 +- src/gamepad/screens/defaultScreen.cpp | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/Makefile b/Makefile index fbd001f..5fccea8 100644 --- a/Makefile +++ b/Makefile @@ -17,7 +17,7 @@ EXTRA_CFLAGS= EXTRA_CXXFLAGS= # Set to 1 to enable hot/cold linking -USE_PACKAGE:=0 +USE_PACKAGE:=0 # this doesnt work with hot/cold linking for some reason # Add libraries you do not wish to include in the cold image here # EXCLUDE_COLD_LIBRARIES:= $(FWDIR)/your_library.a diff --git a/src/gamepad/screens/defaultScreen.cpp b/src/gamepad/screens/defaultScreen.cpp index 121a94e..c7ddf83 100644 --- a/src/gamepad/screens/defaultScreen.cpp +++ b/src/gamepad/screens/defaultScreen.cpp @@ -34,9 +34,7 @@ void DefaultScreen::print_line(uint8_t line, std::string str) { TODO("change handling for off screen lines") if (line > 2) std::exit(1); - printf("is this the problem\n"); const std::lock_guard guard(this->mut); - printf("or is this the problem\n"); if (str.find('\n') != std::string::npos) { TODO("warn instead of throw error if there are too many lines") @@ -50,7 +48,6 @@ void DefaultScreen::print_line(uint8_t line, std::string str) { } for (uint8_t l = 0; l < 3; l++) { - printf("iteration %i, str: %s, buffer: %s\n", l, strs[l].c_str(), currentBuffer[l].value_or("nullopt").c_str()); if (!strs[l].empty()) this->currentBuffer[l] = (strs[l]); } return; From 4a70a4a071c841f9693acee6e3fac64d07383e57 Mon Sep 17 00:00:00 2001 From: ion098 <146852218+ion098@users.noreply.github.com> Date: Fri, 27 Sep 2024 16:58:00 +0000 Subject: [PATCH 40/54] fix: :construction: reenable hot-cold --- .devcontainer/Dockerfile | 2 +- Makefile | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile index 1c1b408..1214dab 100644 --- a/.devcontainer/Dockerfile +++ b/.devcontainer/Dockerfile @@ -1,6 +1,6 @@ FROM rust:1.67 as builder -RUN git clone https://github.com/vexide/vex-v5-qemu.git /vex-v5-qemu +RUN git clone https://github.com/ion098/vex-v5-qemu.git -b feat/hot-cold --single-branch /vex-v5-qemu WORKDIR /vex-v5-qemu RUN cd packages/kernel; cargo build --target-dir /target/kernel diff --git a/Makefile b/Makefile index 5fccea8..09beb71 100644 --- a/Makefile +++ b/Makefile @@ -17,14 +17,14 @@ EXTRA_CFLAGS= EXTRA_CXXFLAGS= # Set to 1 to enable hot/cold linking -USE_PACKAGE:=0 # this doesnt work with hot/cold linking for some reason +USE_PACKAGE:=1 # this doesnt work with hot/cold linking for some reason # Add libraries you do not wish to include in the cold image here # EXCLUDE_COLD_LIBRARIES:= $(FWDIR)/your_library.a EXCLUDE_COLD_LIBRARIES:= # Set this to 1 to add additional rules to compile your project as a PROS library template -IS_LIBRARY:=0 +IS_LIBRARY:=1 # TODO: CHANGE THIS! LIBNAME:=gamepad VERSION:=0.0.1 From b2e027b5ec909536ff75e657dd123c486a4be52d Mon Sep 17 00:00:00 2001 From: ion098 <146852218+ion098@users.noreply.github.com> Date: Fri, 27 Sep 2024 21:37:48 +0000 Subject: [PATCH 41/54] fix: :construction: more testing --- Makefile | 2 +- src/main.cpp | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 09beb71..10d24a2 100644 --- a/Makefile +++ b/Makefile @@ -17,7 +17,7 @@ EXTRA_CFLAGS= EXTRA_CXXFLAGS= # Set to 1 to enable hot/cold linking -USE_PACKAGE:=1 # this doesnt work with hot/cold linking for some reason +USE_PACKAGE:=1 # Add libraries you do not wish to include in the cold image here # EXCLUDE_COLD_LIBRARIES:= $(FWDIR)/your_library.a diff --git a/src/main.cpp b/src/main.cpp index 3f362c4..5ee53d8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -63,5 +63,6 @@ void opcontrol() { gamepad::master.print_line(0, "hello\n\nhi"); pros::delay(25); // Wait for 25 ms, then update the motor values again + exit(1); } } \ No newline at end of file From 075c379444a2ac2486384995c653398fd5a4e49d Mon Sep 17 00:00:00 2001 From: ion098 <146852218+ion098@users.noreply.github.com> Date: Thu, 10 Oct 2024 19:10:37 +0000 Subject: [PATCH 42/54] fix: :bug: Fix usage of unintialized fields This should fix the data aborts that would occur when running the code. Previously the code would access fields in Gamepad that were uninitialized, leading to the code crashing. --- include/gamepad/gamepad.hpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/include/gamepad/gamepad.hpp b/include/gamepad/gamepad.hpp index 2440f18..21e7a80 100644 --- a/include/gamepad/gamepad.hpp +++ b/include/gamepad/gamepad.hpp @@ -119,16 +119,16 @@ class Gamepad { void updateScreens(); - std::shared_ptr defaultScreen; - std::vector> screens; - ScreenBuffer currentScreen; - ScreenBuffer nextBuffer; + std::shared_ptr defaultScreen = std::make_shared(); + std::vector> screens{}; + ScreenBuffer currentScreen{}; + ScreenBuffer nextBuffer{}; pros::Controller controller; uint8_t last_printed_line = 0; uint last_print_time = 0; uint last_update_time = 0; - pros::Mutex mut; + pros::Mutex mut{}; }; inline Gamepad Gamepad::master {pros::E_CONTROLLER_MASTER}; From fe7fa7a8b540d4eade9189b576954c33d562977f Mon Sep 17 00:00:00 2001 From: lufimio <67525443+PA055@users.noreply.github.com> Date: Thu, 10 Oct 2024 19:19:50 -0400 Subject: [PATCH 43/54] fix: :bug: initalize attributes in all classes --- include/gamepad/gamepad.hpp | 27 +++++++++++++---------- include/gamepad/screens/alertScreen.hpp | 20 +++++++++-------- include/gamepad/screens/defaultScreen.hpp | 9 ++++---- src/gamepad/gamepad.cpp | 2 +- src/gamepad/screens/alertScreen.cpp | 4 ++-- src/gamepad/screens/defaultScreen.cpp | 4 ++-- 6 files changed, 35 insertions(+), 31 deletions(-) diff --git a/include/gamepad/gamepad.hpp b/include/gamepad/gamepad.hpp index 2440f18..f8461bd 100644 --- a/include/gamepad/gamepad.hpp +++ b/include/gamepad/gamepad.hpp @@ -33,21 +33,22 @@ class Gamepad { void update(); /** * 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 */ void add_screen(std::shared_ptr screen); /** * 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) */ void print_line(uint8_t line, std::string str); /** * 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. + * + * @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); /** @@ -99,7 +100,9 @@ class Gamepad { static Gamepad partner; private: Gamepad(pros::controller_id_e_t id) - : controller(id) {} + : controller(id) { + screens.push_back(defaultScreen); + } 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 {}; @@ -114,21 +117,21 @@ class Gamepad { * @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 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; - std::vector> screens; - ScreenBuffer currentScreen; - ScreenBuffer nextBuffer; + std::shared_ptr defaultScreen {}; + std::vector> screens {}; + ScreenBuffer currentScreen {}; + ScreenBuffer nextBuffer {}; pros::Controller controller; uint8_t last_printed_line = 0; uint last_print_time = 0; uint last_update_time = 0; - pros::Mutex mut; + pros::Mutex mut {}; }; inline Gamepad Gamepad::master {pros::E_CONTROLLER_MASTER}; diff --git a/include/gamepad/screens/alertScreen.hpp b/include/gamepad/screens/alertScreen.hpp index 7446c04..2d6926b 100644 --- a/include/gamepad/screens/alertScreen.hpp +++ b/include/gamepad/screens/alertScreen.hpp @@ -12,23 +12,25 @@ namespace gamepad { class AlertScreen : public AbstractScreen { public: - AlertScreen() : AbstractScreen(UINT32_MAX - 100) {} + AlertScreen() + : AbstractScreen(UINT32_MAX - 100) {} + void update(uint delta_time); ScreenBuffer get_screen(std::set visible_lines); + void handle_events(std::set button_events) {} void add_alerts(uint8_t line, std::string strs, uint32_t duration, std::string rumble = ""); - private: struct AlertBuffer { - ScreenBuffer screen; - uint duration; + ScreenBuffer screen; + uint duration; }; - std::deque screen_buffer{}; - std::optional screen_contents; - uint line_set_time; - pros::Mutex mut; + std::deque screen_buffer {}; + std::optional screen_contents {}; + uint line_set_time = 0; + pros::Mutex mut {}; }; -} +} // namespace gamepad diff --git a/include/gamepad/screens/defaultScreen.hpp b/include/gamepad/screens/defaultScreen.hpp index d4e4b98..61ebbe4 100644 --- a/include/gamepad/screens/defaultScreen.hpp +++ b/include/gamepad/screens/defaultScreen.hpp @@ -9,15 +9,14 @@ class DefaultScreen : public AbstractScreen { public: DefaultScreen(); ScreenBuffer get_screen(std::set visible_lines); + void handle_events(std::set button_events) {} void print_line(uint8_t line, std::string str); void rumble(std::string rumble_pattern); - private: - ScreenBuffer currentBuffer; - pros::Mutex mut; - + ScreenBuffer currentBuffer {}; + pros::Mutex mut {}; }; -} +} // namespace gamepad diff --git a/src/gamepad/gamepad.cpp b/src/gamepad/gamepad.cpp index f904194..8199d69 100644 --- a/src/gamepad/gamepad.cpp +++ b/src/gamepad/gamepad.cpp @@ -2,6 +2,7 @@ #include "gamepad/todo.hpp" #include "pros/misc.h" #include "pros/rtos.hpp" +#include #include #include #include @@ -9,7 +10,6 @@ #include #include #include -#include namespace gamepad { diff --git a/src/gamepad/screens/alertScreen.cpp b/src/gamepad/screens/alertScreen.cpp index 10a17c8..7cba265 100644 --- a/src/gamepad/screens/alertScreen.cpp +++ b/src/gamepad/screens/alertScreen.cpp @@ -1,11 +1,11 @@ #include "gamepad/screens/alertScreen.hpp" +#include "gamepad/todo.hpp" +#include "pros/rtos.hpp" #include #include #include #include #include -#include "gamepad/todo.hpp" -#include "pros/rtos.hpp" namespace gamepad { diff --git a/src/gamepad/screens/defaultScreen.cpp b/src/gamepad/screens/defaultScreen.cpp index 777fb25..a7dbb09 100644 --- a/src/gamepad/screens/defaultScreen.cpp +++ b/src/gamepad/screens/defaultScreen.cpp @@ -1,11 +1,11 @@ #include "gamepad/screens/defaultScreen.hpp" +#include "gamepad/screens/abstractScreen.hpp" +#include "gamepad/todo.hpp" #include #include #include #include #include -#include "abstractScreen.hpp" -#include "gamepad/todo.hpp" namespace gamepad { From 72ecda356a79c97206f446b7f57af91254c410e3 Mon Sep 17 00:00:00 2001 From: lufimio <67525443+PA055@users.noreply.github.com> Date: Fri, 11 Oct 2024 20:29:36 -0400 Subject: [PATCH 44/54] fix: :bug: Fix bug where AbsrtactScreen was not abstract marked most functions in abstractScreen as abstract --- include/gamepad/screens/abstractScreen.hpp | 15 +++++--- src/gamepad/gamepad.cpp | 45 +++++++++------------- src/gamepad/screens/defaultScreen.cpp | 23 +++++------ src/main.cpp | 7 +++- 4 files changed, 43 insertions(+), 47 deletions(-) diff --git a/include/gamepad/screens/abstractScreen.hpp b/include/gamepad/screens/abstractScreen.hpp index eb6cbb7..502e546 100644 --- a/include/gamepad/screens/abstractScreen.hpp +++ b/include/gamepad/screens/abstractScreen.hpp @@ -10,16 +10,21 @@ 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 + * of text on the controller screen and the last being a rumble pattern */ typedef std::array, 4> ScreenBuffer; class AbstractScreen { public: - AbstractScreen(uint priority): priority(priority) {} - void update(uint delta_time) {} - ScreenBuffer get_screen(std::set visible_lines) {return {};} - void handle_events(std::set button_events) {} + AbstractScreen(uint priority) + : priority(priority) {} + + virtual void update(uint delta_time) {} + + virtual ScreenBuffer get_screen(std::set visible_lines) = 0; + + virtual void handle_events(std::set button_events) {} + const uint get_priority() { return this->priority; } private: const uint priority; diff --git a/src/gamepad/gamepad.cpp b/src/gamepad/gamepad.cpp index 8199d69..46c7eb0 100644 --- a/src/gamepad/gamepad.cpp +++ b/src/gamepad/gamepad.cpp @@ -11,40 +11,39 @@ #include #include - namespace gamepad { void Gamepad::updateButton(pros::controller_digital_e_t button_id) { - Button Gamepad::*button = Gamepad::button_to_ptr(button_id); + Button Gamepad::* button = Gamepad::button_to_ptr(button_id); bool is_held = this->controller.get_digital(button_id); (this->*button).update(is_held); } - void Gamepad::updateScreens() { // Lock Mutexes for Thread Safety std::lock_guard guard_scheduling(this->mut); // Update all screens and note deltatime - for (int i = 0; i < this->screens.size(); i++) - this->screens[i]->update(pros::millis() - this->last_update_time); + for (int i = 0; i < this->screens.size(); i++) this->screens[i]->update(pros::millis() - this->last_update_time); last_update_time = pros::millis(); // Check if enough time has passed for the Gamepad to poll for updates - if (pros::millis() - this->last_print_time < 50) - return; + if (pros::millis() - this->last_print_time < 50) return; for (int i = 0; i < this->screens.size(); i++) { // get all lines that arent being used by a higher priority screen std::set visible_lines; for (uint8_t j = 0; j < 4; j++) - if (!this->nextBuffer[j].has_value()) - visible_lines.emplace(j); - + 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 = this->screens[i]->get_screen(visible_lines); for (uint8_t j = 0; j < 4; j++) - if (buffer[j].has_value() && !nextBuffer[j].has_value()) - nextBuffer[j] = std::move(buffer[j]); + if (buffer[j].has_value() && !nextBuffer[j].has_value()) nextBuffer[j] = std::move(buffer[j]); + + printf("nextBuffer = {%s, %s, %s, %s}\n", nextBuffer.at(0).value_or("nullopt").c_str(), + nextBuffer.at(1).value_or("nullopt").c_str(), nextBuffer.at(2).value_or("nullopt").c_str(), + nextBuffer.at(3).value_or("nullopt").c_str()); + } for (int i = 1; i <= 4; i++) { @@ -81,28 +80,20 @@ void Gamepad::update() { } void Gamepad::add_screen(std::shared_ptr screen) { - uint last = UINT32_MAX; uint pos = 0; + uint last = UINT32_MAX; + uint pos = 0; for (pos = 0; pos < this->screens.size(); pos++) { - if (this->screens[pos]->get_priority() < screen->get_priority() && last >= screen->get_priority()) - break; + if (this->screens[pos]->get_priority() < screen->get_priority() && last >= screen->get_priority()) break; last = this->screens[pos]->get_priority(); } this->screens.emplace(this->screens.begin() + pos, screen); } -void Gamepad::print_line(uint8_t line, std::string str) { - printf("wrapping print_line(line:%i, string:%s)\n", line, str.c_str()); +void Gamepad::print_line(uint8_t line, std::string str) { this->defaultScreen->print_line(line, str); } - this->defaultScreen->print_line(line, str); -} - -void Gamepad::rumble(std::string rumble_pattern) { - this->defaultScreen->rumble(rumble_pattern); -} +void Gamepad::rumble(std::string rumble_pattern) { this->defaultScreen->rumble(rumble_pattern); } -const Button& Gamepad::operator[](pros::controller_digital_e_t button) { - return this->*Gamepad::button_to_ptr(button); -} +const Button& Gamepad::operator[](pros::controller_digital_e_t button) { return this->*Gamepad::button_to_ptr(button); } float Gamepad::operator[](pros::controller_analog_e_t axis) { switch (axis) { @@ -122,7 +113,7 @@ std::string Gamepad::unique_name() { return std::to_string(i++) + "_internal"; } -Button Gamepad::*Gamepad::button_to_ptr(pros::controller_digital_e_t button) { +Button Gamepad::* Gamepad::button_to_ptr(pros::controller_digital_e_t button) { switch (button) { case pros::E_CONTROLLER_DIGITAL_L1: return &Gamepad::m_L1; case pros::E_CONTROLLER_DIGITAL_L2: return &Gamepad::m_L2; diff --git a/src/gamepad/screens/defaultScreen.cpp b/src/gamepad/screens/defaultScreen.cpp index a7dbb09..2c9a22e 100644 --- a/src/gamepad/screens/defaultScreen.cpp +++ b/src/gamepad/screens/defaultScreen.cpp @@ -7,21 +7,20 @@ #include #include - namespace gamepad { -DefaultScreen::DefaultScreen() : AbstractScreen(1), currentBuffer({}) { - printf("ran constructor\n"); -} +DefaultScreen::DefaultScreen() + : AbstractScreen(1), + currentBuffer({}) {} ScreenBuffer DefaultScreen::get_screen(std::set visible_lines) { ScreenBuffer output; - printf("{%s, %s, %s, %s}\n", - currentBuffer.at(0).value_or("nullopt").c_str(), - currentBuffer.at(1).value_or("nullopt").c_str(), - currentBuffer.at(2).value_or("nullopt").c_str(), - currentBuffer.at(3).value_or("nullopt").c_str()); - // const std::lock_guard guard(this->mut); + + printf("currentBuffer = {%s, %s, %s, %s}\n", currentBuffer.at(0).value_or("nullopt").c_str(), + currentBuffer.at(1).value_or("nullopt").c_str(), currentBuffer.at(2).value_or("nullopt").c_str(), + currentBuffer.at(3).value_or("nullopt").c_str()); + + const std::lock_guard guard(this->mut); for (auto i = visible_lines.begin(); i != visible_lines.end(); ++i) { output[*i] = std::move(this->currentBuffer[*i].value_or("")); this->currentBuffer[*i] = std::nullopt; @@ -30,7 +29,6 @@ ScreenBuffer DefaultScreen::get_screen(std::set visible_lines) { } void DefaultScreen::print_line(uint8_t line, std::string str) { - printf("print_line(line: %i, string: %s)\n", line, str.c_str()); TODO("change handling for off screen lines") if (line > 2) std::exit(1); @@ -53,7 +51,6 @@ void DefaultScreen::print_line(uint8_t line, std::string str) { return; } - printf("5\n"); this->currentBuffer[line] = std::move(str); } @@ -65,4 +62,4 @@ void DefaultScreen::rumble(std::string rumble_pattern) { this->currentBuffer[3] = std::move(rumble_pattern); } -} \ No newline at end of file +} // namespace gamepad \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 5ee53d8..3a7a09d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,8 @@ #include "main.h" #include "gamepad/api.hpp" +#include "gamepad/gamepad.hpp" +#include "pros/misc.h" +#include "pros/misc.hpp" /** * Runs initialization code. This occurs as soon as the program is started. @@ -8,7 +11,7 @@ * to keep execution time for this mode under a few seconds. */ void initialize() { - // Gamepad::master.X.onShortRelease("xShortRelease1", []() { printf("X Short Release!\n"); }); + gamepad::master.X.onPress("rumble", []() { gamepad::master.rumble("..."); }); } /** @@ -56,6 +59,7 @@ void autonomous() {} * task, not resume it from where it left off. */ void opcontrol() { + pros::Controller controller(pros::E_CONTROLLER_MASTER); while (true) { // Remember to ALWAYS call update at the start of your while loop! gamepad::master.update(); @@ -63,6 +67,5 @@ void opcontrol() { gamepad::master.print_line(0, "hello\n\nhi"); pros::delay(25); // Wait for 25 ms, then update the motor values again - exit(1); } } \ No newline at end of file From d06277a87a6be6e6cd00582bc1ce2a8d2d7cf98d Mon Sep 17 00:00:00 2001 From: lufimio <67525443+PA055@users.noreply.github.com> Date: Fri, 11 Oct 2024 20:58:26 -0400 Subject: [PATCH 45/54] fix: :bug: fix nullpointers getting turned into empty strings --- include/gamepad/gamepad.hpp | 11 +++++++++++ src/gamepad/gamepad.cpp | 26 +++++++++++++++++++------- src/gamepad/screens/defaultScreen.cpp | 8 ++------ src/main.cpp | 1 - 4 files changed, 32 insertions(+), 14 deletions(-) diff --git a/include/gamepad/gamepad.hpp b/include/gamepad/gamepad.hpp index ba0ac91..508d494 100644 --- a/include/gamepad/gamepad.hpp +++ b/include/gamepad/gamepad.hpp @@ -44,6 +44,17 @@ class Gamepad { * @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); + /** + * @brief clears all lines on the controller, similar to the pros function (low priority) + * + */ + 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) + */ + void clear(uint8_t line); /** * makes the controller rumble like pros (low priority) * diff --git a/src/gamepad/gamepad.cpp b/src/gamepad/gamepad.cpp index 46c7eb0..35a1117 100644 --- a/src/gamepad/gamepad.cpp +++ b/src/gamepad/gamepad.cpp @@ -38,17 +38,22 @@ void Gamepad::updateScreens() { // get the buffer of the next lower priority screen and set it to be printed ScreenBuffer buffer = this->screens[i]->get_screen(visible_lines); for (uint8_t j = 0; j < 4; j++) - if (buffer[j].has_value() && !nextBuffer[j].has_value()) nextBuffer[j] = std::move(buffer[j]); - - printf("nextBuffer = {%s, %s, %s, %s}\n", nextBuffer.at(0).value_or("nullopt").c_str(), - nextBuffer.at(1).value_or("nullopt").c_str(), nextBuffer.at(2).value_or("nullopt").c_str(), - nextBuffer.at(3).value_or("nullopt").c_str()); - + if (buffer[j].has_value() && !buffer[j]->empty() && !nextBuffer[j].has_value()) + nextBuffer[j] = std::move(buffer[j]); } - for (int i = 1; i <= 4; i++) { + for (int i = 0; i < 4; i++) { // start from the line thats after the line thats been set so we dont get stuck setting the first line int line = (this->last_printed_line + i) % 4; + printf("Line = %i\n", line); + + printf("nextBuffer = {%s, %s, %s, %s}\n", nextBuffer.at(0).value_or("nullopt").c_str(), + nextBuffer.at(1).value_or("nullopt").c_str(), nextBuffer.at(2).value_or("nullopt").c_str(), + nextBuffer.at(3).value_or("nullopt").c_str()); + + printf("currentScreen = {%s, %s, %s, %s}\n", currentScreen.at(0).value_or("nullopt").c_str(), + currentScreen.at(1).value_or("nullopt").c_str(), currentScreen.at(2).value_or("nullopt").c_str(), + currentScreen.at(3).value_or("nullopt").c_str()); // text on screen is the same as last frame's text so no use updating if (this->currentScreen[line] == this->nextBuffer[line] && line != 3) { @@ -64,6 +69,9 @@ void Gamepad::updateScreens() { this->last_printed_line = line; this->last_print_time = pros::millis(); } + + // just to make the cycle more regular + this->last_printed_line = 0; } void Gamepad::update() { @@ -91,6 +99,10 @@ void Gamepad::add_screen(std::shared_ptr screen) { void Gamepad::print_line(uint8_t line, std::string str) { this->defaultScreen->print_line(line, str); } +void Gamepad::clear() { this->defaultScreen->print_line(0, " \n \n "); } + +void Gamepad::clear(uint8_t line) { this->defaultScreen->print_line(line, " "); } + void Gamepad::rumble(std::string rumble_pattern) { this->defaultScreen->rumble(rumble_pattern); } const Button& Gamepad::operator[](pros::controller_digital_e_t button) { return this->*Gamepad::button_to_ptr(button); } diff --git a/src/gamepad/screens/defaultScreen.cpp b/src/gamepad/screens/defaultScreen.cpp index 2c9a22e..57110be 100644 --- a/src/gamepad/screens/defaultScreen.cpp +++ b/src/gamepad/screens/defaultScreen.cpp @@ -15,14 +15,10 @@ DefaultScreen::DefaultScreen() ScreenBuffer DefaultScreen::get_screen(std::set visible_lines) { ScreenBuffer output; - - printf("currentBuffer = {%s, %s, %s, %s}\n", currentBuffer.at(0).value_or("nullopt").c_str(), - currentBuffer.at(1).value_or("nullopt").c_str(), currentBuffer.at(2).value_or("nullopt").c_str(), - currentBuffer.at(3).value_or("nullopt").c_str()); - const std::lock_guard guard(this->mut); + for (auto i = visible_lines.begin(); i != visible_lines.end(); ++i) { - output[*i] = std::move(this->currentBuffer[*i].value_or("")); + output[*i] = std::move(this->currentBuffer[*i]); this->currentBuffer[*i] = std::nullopt; } return output; diff --git a/src/main.cpp b/src/main.cpp index 3a7a09d..d172ff8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -59,7 +59,6 @@ void autonomous() {} * task, not resume it from where it left off. */ void opcontrol() { - pros::Controller controller(pros::E_CONTROLLER_MASTER); while (true) { // Remember to ALWAYS call update at the start of your while loop! gamepad::master.update(); From c3570385c02d87d96400ba47a18355ad0fef6264 Mon Sep 17 00:00:00 2001 From: lufimio <67525443+PA055@users.noreply.github.com> Date: Fri, 11 Oct 2024 22:12:21 -0400 Subject: [PATCH 46/54] fix: :bug: multiples line changes not registering needed to return after writing to a line, and also removed debug statements --- include/gamepad/gamepad.hpp | 1 + src/gamepad/gamepad.cpp | 20 +++++++++----------- src/main.cpp | 16 ++++++++++++---- 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/include/gamepad/gamepad.hpp b/include/gamepad/gamepad.hpp index 508d494..a42216f 100644 --- a/include/gamepad/gamepad.hpp +++ b/include/gamepad/gamepad.hpp @@ -142,6 +142,7 @@ class Gamepad { uint8_t last_printed_line = 0; uint last_print_time = 0; uint last_update_time = 0; + bool screenCleared = true; pros::Mutex mut {}; }; diff --git a/src/gamepad/gamepad.cpp b/src/gamepad/gamepad.cpp index 35a1117..3ab9020 100644 --- a/src/gamepad/gamepad.cpp +++ b/src/gamepad/gamepad.cpp @@ -27,7 +27,7 @@ void Gamepad::updateScreens() { last_update_time = pros::millis(); // Check if enough time has passed for the Gamepad to poll for updates - if (pros::millis() - this->last_print_time < 50) return; + if (pros::millis() - this->last_print_time <= 50) return; for (int i = 0; i < this->screens.size(); i++) { // get all lines that arent being used by a higher priority screen @@ -45,15 +45,15 @@ void Gamepad::updateScreens() { for (int i = 0; i < 4; i++) { // start from the line thats after the line thats been set so we dont get stuck setting the first line int line = (this->last_printed_line + i) % 4; - printf("Line = %i\n", line); - printf("nextBuffer = {%s, %s, %s, %s}\n", nextBuffer.at(0).value_or("nullopt").c_str(), - nextBuffer.at(1).value_or("nullopt").c_str(), nextBuffer.at(2).value_or("nullopt").c_str(), - nextBuffer.at(3).value_or("nullopt").c_str()); + // theres nothing on this line so we can skip it + if (!this->nextBuffer[line].has_value()) continue; - printf("currentScreen = {%s, %s, %s, %s}\n", currentScreen.at(0).value_or("nullopt").c_str(), - currentScreen.at(1).value_or("nullopt").c_str(), currentScreen.at(2).value_or("nullopt").c_str(), - currentScreen.at(3).value_or("nullopt").c_str()); + if (!this->screenCleared) { + this->controller.clear(); + this->last_print_time = pros::millis(); + return; + } // text on screen is the same as last frame's text so no use updating if (this->currentScreen[line] == this->nextBuffer[line] && line != 3) { @@ -68,10 +68,8 @@ void Gamepad::updateScreens() { this->nextBuffer[line] = std::nullopt; this->last_printed_line = line; this->last_print_time = pros::millis(); + return; } - - // just to make the cycle more regular - this->last_printed_line = 0; } void Gamepad::update() { diff --git a/src/main.cpp b/src/main.cpp index d172ff8..4b57db6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,8 +1,12 @@ #include "main.h" #include "gamepad/api.hpp" #include "gamepad/gamepad.hpp" -#include "pros/misc.h" -#include "pros/misc.hpp" +#include "gamepad/screens/alertScreen.hpp" +#include "pros/rtos.hpp" +#include +#include + +std::shared_ptr alerts{}; /** * Runs initialization code. This occurs as soon as the program is started. @@ -11,7 +15,13 @@ * to keep execution time for this mode under a few seconds. */ void initialize() { + gamepad::master.add_screen(alerts); + gamepad::master.A.onPress("alert", []() { alerts->add_alerts(0, "a very\nimportant\nalert", 3000, "-.-"); }); + gamepad::master.B.onPress( + "print02", []() { gamepad::master.print_line(0, "the time is\n\n" + std::to_string(pros::millis()) + " ms"); }); gamepad::master.X.onPress("rumble", []() { gamepad::master.rumble("..."); }); + gamepad::master.Y.onPress("print1", []() { gamepad::master.print_line(1, "this should be cleared"); }); + gamepad::master.Y.onRelease("clear1", []() { gamepad::master.clear(1); }); } /** @@ -63,8 +73,6 @@ void opcontrol() { // Remember to ALWAYS call update at the start of your while loop! gamepad::master.update(); - gamepad::master.print_line(0, "hello\n\nhi"); - pros::delay(25); // Wait for 25 ms, then update the motor values again } } \ No newline at end of file From a1c3f15e862ee86df9a63865e2c47471f5b69c50 Mon Sep 17 00:00:00 2001 From: lufimio <67525443+PA055@users.noreply.github.com> Date: Sat, 12 Oct 2024 10:38:32 -0400 Subject: [PATCH 47/54] fix: :bug: screen not being cleared first print i initialized screen cleared to the wrong value, and i forgot to clear it --- include/gamepad/gamepad.hpp | 4 ++-- include/gamepad/screens/abstractScreen.hpp | 8 ++++---- include/gamepad/screens/alertScreen.hpp | 2 -- include/gamepad/screens/defaultScreen.hpp | 6 +++--- src/gamepad/gamepad.cpp | 3 ++- src/gamepad/screens/defaultScreen.cpp | 4 ---- src/main.cpp | 6 ++++-- 7 files changed, 15 insertions(+), 18 deletions(-) diff --git a/include/gamepad/gamepad.hpp b/include/gamepad/gamepad.hpp index a42216f..5f55367 100644 --- a/include/gamepad/gamepad.hpp +++ b/include/gamepad/gamepad.hpp @@ -112,7 +112,7 @@ class Gamepad { private: Gamepad(pros::controller_id_e_t id) : controller(id) { - screens.push_back(defaultScreen); + this->add_screen(defaultScreen); } Button m_L1 {}, m_L2 {}, m_R1 {}, m_R2 {}, m_Up {}, m_Down {}, m_Left {}, m_Right {}, m_X {}, m_B {}, m_Y {}, @@ -142,7 +142,7 @@ class Gamepad { uint8_t last_printed_line = 0; uint last_print_time = 0; uint last_update_time = 0; - bool screenCleared = true; + bool screenCleared = false; pros::Mutex mut {}; }; diff --git a/include/gamepad/screens/abstractScreen.hpp b/include/gamepad/screens/abstractScreen.hpp index 502e546..10638f0 100644 --- a/include/gamepad/screens/abstractScreen.hpp +++ b/include/gamepad/screens/abstractScreen.hpp @@ -4,6 +4,7 @@ #include #include #include +#include #include "pros/misc.h" namespace gamepad { @@ -16,8 +17,7 @@ typedef std::array, 4> ScreenBuffer; class AbstractScreen { public: - AbstractScreen(uint priority) - : priority(priority) {} + AbstractScreen(uint priority) : priority(priority) {} virtual void update(uint delta_time) {} @@ -25,8 +25,8 @@ class AbstractScreen { virtual void handle_events(std::set button_events) {} - const uint get_priority() { return this->priority; } - private: + uint get_priority() { return this->priority; } + protected: const uint priority; }; diff --git a/include/gamepad/screens/alertScreen.hpp b/include/gamepad/screens/alertScreen.hpp index 2d6926b..7656eaf 100644 --- a/include/gamepad/screens/alertScreen.hpp +++ b/include/gamepad/screens/alertScreen.hpp @@ -18,8 +18,6 @@ class AlertScreen : public AbstractScreen { void update(uint delta_time); ScreenBuffer get_screen(std::set visible_lines); - void handle_events(std::set button_events) {} - void add_alerts(uint8_t line, std::string strs, uint32_t duration, std::string rumble = ""); private: struct AlertBuffer { diff --git a/include/gamepad/screens/defaultScreen.hpp b/include/gamepad/screens/defaultScreen.hpp index 61ebbe4..ad62a1c 100644 --- a/include/gamepad/screens/defaultScreen.hpp +++ b/include/gamepad/screens/defaultScreen.hpp @@ -7,10 +7,10 @@ namespace gamepad { class DefaultScreen : public AbstractScreen { public: - DefaultScreen(); - ScreenBuffer get_screen(std::set visible_lines); + DefaultScreen() + : AbstractScreen(1) {} - void handle_events(std::set button_events) {} + ScreenBuffer get_screen(std::set visible_lines); void print_line(uint8_t line, std::string str); void rumble(std::string rumble_pattern); diff --git a/src/gamepad/gamepad.cpp b/src/gamepad/gamepad.cpp index 3ab9020..1333b25 100644 --- a/src/gamepad/gamepad.cpp +++ b/src/gamepad/gamepad.cpp @@ -49,8 +49,9 @@ void Gamepad::updateScreens() { // theres nothing on this line so we can skip it if (!this->nextBuffer[line].has_value()) continue; - if (!this->screenCleared) { + if (!this->screenCleared && line != 3) { this->controller.clear(); + screenCleared = true; this->last_print_time = pros::millis(); return; } diff --git a/src/gamepad/screens/defaultScreen.cpp b/src/gamepad/screens/defaultScreen.cpp index 57110be..1afd516 100644 --- a/src/gamepad/screens/defaultScreen.cpp +++ b/src/gamepad/screens/defaultScreen.cpp @@ -9,10 +9,6 @@ namespace gamepad { -DefaultScreen::DefaultScreen() - : AbstractScreen(1), - currentBuffer({}) {} - ScreenBuffer DefaultScreen::get_screen(std::set visible_lines) { ScreenBuffer output; const std::lock_guard guard(this->mut); diff --git a/src/main.cpp b/src/main.cpp index 4b57db6..2447f38 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,6 +3,7 @@ #include "gamepad/gamepad.hpp" #include "gamepad/screens/alertScreen.hpp" #include "pros/rtos.hpp" +#include #include #include @@ -15,8 +16,9 @@ std::shared_ptr alerts{}; * to keep execution time for this mode under a few seconds. */ void initialize() { - gamepad::master.add_screen(alerts); - gamepad::master.A.onPress("alert", []() { alerts->add_alerts(0, "a very\nimportant\nalert", 3000, "-.-"); }); + printf("alert priority: %i\n", alerts->get_priority()); + // gamepad::master.add_screen(alerts); + // gamepad::master.A.onPress("alert", []() { alerts->add_alerts(0, "a very\nimportant\nalert", 3000, "-.-"); }); gamepad::master.B.onPress( "print02", []() { gamepad::master.print_line(0, "the time is\n\n" + std::to_string(pros::millis()) + " ms"); }); gamepad::master.X.onPress("rumble", []() { gamepad::master.rumble("..."); }); From dd06cda254dbf4aec4525af09df2b4bd79c04d0f Mon Sep 17 00:00:00 2001 From: lufimio <67525443+PA055@users.noreply.github.com> Date: Sat, 12 Oct 2024 11:23:07 -0400 Subject: [PATCH 48/54] chore: :poop: get_priority is the problem, idk why --- src/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index 2447f38..56488d8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,7 +18,7 @@ std::shared_ptr alerts{}; void initialize() { printf("alert priority: %i\n", alerts->get_priority()); // gamepad::master.add_screen(alerts); - // gamepad::master.A.onPress("alert", []() { alerts->add_alerts(0, "a very\nimportant\nalert", 3000, "-.-"); }); + // gamepad::master.A.onPress("alert", []() { alerts->add_alerts(0, "a very\nimportant alert\nat " + pros::millis() + "ms", 3000, "-.-"); }); gamepad::master.B.onPress( "print02", []() { gamepad::master.print_line(0, "the time is\n\n" + std::to_string(pros::millis()) + " ms"); }); gamepad::master.X.onPress("rumble", []() { gamepad::master.rumble("..."); }); From 82c0937c336594bb6994995a5ac210123911f1ad Mon Sep 17 00:00:00 2001 From: lufimio <67525443+PA055@users.noreply.github.com> Date: Sat, 12 Oct 2024 21:47:15 -0400 Subject: [PATCH 49/54] test: :sparkles: the alerts are kinda working the logic in the abstract screen seems weird based on the behavior --- include/gamepad/gamepad.hpp | 4 ++-- include/gamepad/screens/abstractScreen.hpp | 3 ++- src/gamepad/screens/alertScreen.cpp | 11 ++++------- src/main.cpp | 9 +++++---- 4 files changed, 13 insertions(+), 14 deletions(-) diff --git a/include/gamepad/gamepad.hpp b/include/gamepad/gamepad.hpp index 5f55367..52701f5 100644 --- a/include/gamepad/gamepad.hpp +++ b/include/gamepad/gamepad.hpp @@ -46,12 +46,12 @@ class Gamepad { void print_line(uint8_t line, std::string str); /** * @brief clears all lines on the controller, similar to the pros function (low priority) - * + * */ 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) */ void clear(uint8_t line); diff --git a/include/gamepad/screens/abstractScreen.hpp b/include/gamepad/screens/abstractScreen.hpp index 10638f0..0d4f749 100644 --- a/include/gamepad/screens/abstractScreen.hpp +++ b/include/gamepad/screens/abstractScreen.hpp @@ -17,7 +17,8 @@ typedef std::array, 4> ScreenBuffer; class AbstractScreen { public: - AbstractScreen(uint priority) : priority(priority) {} + AbstractScreen(uint priority) + : priority(priority) {} virtual void update(uint delta_time) {} diff --git a/src/gamepad/screens/alertScreen.cpp b/src/gamepad/screens/alertScreen.cpp index 7cba265..c8dae93 100644 --- a/src/gamepad/screens/alertScreen.cpp +++ b/src/gamepad/screens/alertScreen.cpp @@ -13,7 +13,7 @@ ScreenBuffer AlertScreen::get_screen(std::set visible_lines) { std::lock_guard guard(this->mut); if (this->screen_contents.has_value()) return this->screen_contents->screen; if (this->screen_buffer.size() < 1) return ScreenBuffer(); - + for (uint8_t i = 0; i < 4; i++) { if (!this->screen_buffer[0].screen[i].has_value()) continue; if (this->screen_buffer[0].screen[i].has_value() && !visible_lines.contains(i)) return ScreenBuffer(); @@ -22,13 +22,11 @@ ScreenBuffer AlertScreen::get_screen(std::set visible_lines) { this->screen_buffer.pop_front(); this->line_set_time = pros::millis(); return this->screen_contents->screen; - } void AlertScreen::update(uint delta_time) { std::lock_guard guard(this->mut); - if (pros::millis() - this->line_set_time >= this->screen_contents->duration) - this->screen_contents = std::nullopt; + 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) { @@ -46,8 +44,7 @@ void AlertScreen::add_alerts(uint8_t line, std::string str, uint32_t duration, s if (!std::getline(ss, strs[i], '\n')) break; } - - ScreenBuffer buffer; + ScreenBuffer buffer; if (strs[0] != "") buffer[0] = std::move(strs[0]); if (strs[1] != "") buffer[1] = std::move(strs[1]); @@ -58,4 +55,4 @@ void AlertScreen::add_alerts(uint8_t line, std::string str, uint32_t duration, s this->screen_buffer.push_back({buffer, duration}); } -} +} // namespace gamepad diff --git a/src/main.cpp b/src/main.cpp index 56488d8..8499e08 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,7 +7,7 @@ #include #include -std::shared_ptr alerts{}; +std::shared_ptr alerts = std::make_shared(); /** * Runs initialization code. This occurs as soon as the program is started. @@ -16,9 +16,10 @@ std::shared_ptr alerts{}; * to keep execution time for this mode under a few seconds. */ void initialize() { - printf("alert priority: %i\n", alerts->get_priority()); - // gamepad::master.add_screen(alerts); - // gamepad::master.A.onPress("alert", []() { alerts->add_alerts(0, "a very\nimportant alert\nat " + pros::millis() + "ms", 3000, "-.-"); }); + printf("alert priority: %u\n", alerts->get_priority()); + gamepad::master.add_screen(alerts); + gamepad::master.A.onPress( + "alert", []() { alerts->add_alerts(0, "a very\nimportant alert\nat " + std::to_string(pros::millis()) + " ms", 3000, "-.-"); }); gamepad::master.B.onPress( "print02", []() { gamepad::master.print_line(0, "the time is\n\n" + std::to_string(pros::millis()) + " ms"); }); gamepad::master.X.onPress("rumble", []() { gamepad::master.rumble("..."); }); From b1e4093bf618443a57ff63b0f861e8f3b7701675 Mon Sep 17 00:00:00 2001 From: lufimio <67525443+PA055@users.noreply.github.com> Date: Sun, 13 Oct 2024 08:41:34 -0400 Subject: [PATCH 50/54] fix: :bug: repeated alert rumbles rumbles in alerts were being repeated constantly for the duration of the alert, i had to clear it after 1 iteration --- src/gamepad/gamepad.cpp | 10 ++++++++++ src/gamepad/screens/alertScreen.cpp | 5 ++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/gamepad/gamepad.cpp b/src/gamepad/gamepad.cpp index 1333b25..9a26c50 100644 --- a/src/gamepad/gamepad.cpp +++ b/src/gamepad/gamepad.cpp @@ -62,6 +62,16 @@ void Gamepad::updateScreens() { continue; } + printf("Line = %i\n", line); + + printf("nextBuffer = {%s, %s, %s, %s}\n", nextBuffer.at(0).value_or("nullopt").c_str(), + nextBuffer.at(1).value_or("nullopt").c_str(), nextBuffer.at(2).value_or("nullopt").c_str(), + nextBuffer.at(3).value_or("nullopt").c_str()); + + // printf("currentScreen = {%s, %s, %s, %s}\n", currentScreen.at(0).value_or("nullopt").c_str(), + // currentScreen.at(1).value_or("nullopt").c_str(), currentScreen.at(2).value_or("nullopt").c_str(), + // currentScreen.at(3).value_or("nullopt").c_str()); + // print to screen or rumble if (line == 3) this->controller.rumble(this->nextBuffer[line].value_or("").c_str()); else this->controller.set_text(line, 0, this->nextBuffer[line].value_or("") + std::string(40, ' ')); diff --git a/src/gamepad/screens/alertScreen.cpp b/src/gamepad/screens/alertScreen.cpp index c8dae93..a8ea556 100644 --- a/src/gamepad/screens/alertScreen.cpp +++ b/src/gamepad/screens/alertScreen.cpp @@ -11,7 +11,10 @@ namespace gamepad { ScreenBuffer AlertScreen::get_screen(std::set visible_lines) { std::lock_guard guard(this->mut); - if (this->screen_contents.has_value()) return this->screen_contents->screen; + if (this->screen_contents.has_value()) { + this->screen_contents->screen.at(3) = std::nullopt; + return this->screen_contents->screen; + } if (this->screen_buffer.size() < 1) return ScreenBuffer(); for (uint8_t i = 0; i < 4; i++) { From aa4904d486059fc3092e2f0055fb90dc50878c07 Mon Sep 17 00:00:00 2001 From: lufimio <67525443+PA055@users.noreply.github.com> Date: Sun, 13 Oct 2024 08:42:11 -0400 Subject: [PATCH 51/54] chore: :fire: remove debug prints --- src/gamepad/gamepad.cpp | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/gamepad/gamepad.cpp b/src/gamepad/gamepad.cpp index 9a26c50..1333b25 100644 --- a/src/gamepad/gamepad.cpp +++ b/src/gamepad/gamepad.cpp @@ -62,16 +62,6 @@ void Gamepad::updateScreens() { continue; } - printf("Line = %i\n", line); - - printf("nextBuffer = {%s, %s, %s, %s}\n", nextBuffer.at(0).value_or("nullopt").c_str(), - nextBuffer.at(1).value_or("nullopt").c_str(), nextBuffer.at(2).value_or("nullopt").c_str(), - nextBuffer.at(3).value_or("nullopt").c_str()); - - // printf("currentScreen = {%s, %s, %s, %s}\n", currentScreen.at(0).value_or("nullopt").c_str(), - // currentScreen.at(1).value_or("nullopt").c_str(), currentScreen.at(2).value_or("nullopt").c_str(), - // currentScreen.at(3).value_or("nullopt").c_str()); - // print to screen or rumble if (line == 3) this->controller.rumble(this->nextBuffer[line].value_or("").c_str()); else this->controller.set_text(line, 0, this->nextBuffer[line].value_or("") + std::string(40, ' ')); From 993960fab6a6ce465ef0e217d6c2c1b47503a237 Mon Sep 17 00:00:00 2001 From: lufimio <67525443+PA055@users.noreply.github.com> Date: Sun, 13 Oct 2024 08:42:11 -0400 Subject: [PATCH 52/54] chore: :fire: remove debug prints --- src/gamepad/gamepad.cpp | 10 ---------- src/main.cpp | 1 - 2 files changed, 11 deletions(-) diff --git a/src/gamepad/gamepad.cpp b/src/gamepad/gamepad.cpp index 9a26c50..1333b25 100644 --- a/src/gamepad/gamepad.cpp +++ b/src/gamepad/gamepad.cpp @@ -62,16 +62,6 @@ void Gamepad::updateScreens() { continue; } - printf("Line = %i\n", line); - - printf("nextBuffer = {%s, %s, %s, %s}\n", nextBuffer.at(0).value_or("nullopt").c_str(), - nextBuffer.at(1).value_or("nullopt").c_str(), nextBuffer.at(2).value_or("nullopt").c_str(), - nextBuffer.at(3).value_or("nullopt").c_str()); - - // printf("currentScreen = {%s, %s, %s, %s}\n", currentScreen.at(0).value_or("nullopt").c_str(), - // currentScreen.at(1).value_or("nullopt").c_str(), currentScreen.at(2).value_or("nullopt").c_str(), - // currentScreen.at(3).value_or("nullopt").c_str()); - // print to screen or rumble if (line == 3) this->controller.rumble(this->nextBuffer[line].value_or("").c_str()); else this->controller.set_text(line, 0, this->nextBuffer[line].value_or("") + std::string(40, ' ')); diff --git a/src/main.cpp b/src/main.cpp index 8499e08..c15ccc6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -16,7 +16,6 @@ std::shared_ptr alerts = std::make_sharedget_priority()); gamepad::master.add_screen(alerts); gamepad::master.A.onPress( "alert", []() { alerts->add_alerts(0, "a very\nimportant alert\nat " + std::to_string(pros::millis()) + " ms", 3000, "-.-"); }); From a5f29cb33a99ded3d48735fe14512d7318868063 Mon Sep 17 00:00:00 2001 From: lufimio <67525443+PA055@users.noreply.github.com> Date: Sun, 13 Oct 2024 09:02:35 -0400 Subject: [PATCH 53/54] refactor: :art: run clang-format --- src/main.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index c15ccc6..c168570 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -17,8 +17,9 @@ std::shared_ptr alerts = std::make_sharedadd_alerts(0, "a very\nimportant alert\nat " + std::to_string(pros::millis()) + " ms", 3000, "-.-"); }); + gamepad::master.A.onPress("alert", []() { + alerts->add_alerts(0, "a very\nimportant alert\nat " + std::to_string(pros::millis()) + " ms", 3000, "-.-"); + }); gamepad::master.B.onPress( "print02", []() { gamepad::master.print_line(0, "the time is\n\n" + std::to_string(pros::millis()) + " ms"); }); gamepad::master.X.onPress("rumble", []() { gamepad::master.rumble("..."); }); From 85be74a00939fda5618dffb039b10e5632d6c87a Mon Sep 17 00:00:00 2001 From: lufimio <67525443+PA055@users.noreply.github.com> Date: Wed, 16 Oct 2024 19:22:00 -0400 Subject: [PATCH 54/54] feat: :sparkles: disconnect/reconnect logic still bugged, debug prints are included however commented out --- src/gamepad/gamepad.cpp | 46 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/src/gamepad/gamepad.cpp b/src/gamepad/gamepad.cpp index 1333b25..eb593c7 100644 --- a/src/gamepad/gamepad.cpp +++ b/src/gamepad/gamepad.cpp @@ -22,9 +22,35 @@ void Gamepad::updateScreens() { // Lock Mutexes for Thread Safety std::lock_guard guard_scheduling(this->mut); + // Disable screen updates if the controller is disconnected + if (!this->controller.is_connected()) { + if (this->screenCleared) { + // printf("disconnected\n"); + + this->nextBuffer = std::move(this->currentScreen); + this->currentScreen = {}; + this->screenCleared = false; + } + return; + } + + // Clear current screen and reset last update time on reconnect + if (this->controller.is_connected() && !screenCleared) { + // printf("reconnected\n"); + + this->currentScreen = {}; + this->last_update_time = pros::millis(); + + // printf("nextBuffer = {%s, %s, %s, %s}\n", nextBuffer.at(0).value_or("nullopt").c_str(), + // nextBuffer.at(1).value_or("nullopt").c_str(), nextBuffer.at(2).value_or("nullopt").c_str(), + // nextBuffer.at(3).value_or("nullopt").c_str()); + } + // Update all screens and note deltatime - for (int i = 0; i < this->screens.size(); i++) this->screens[i]->update(pros::millis() - this->last_update_time); - last_update_time = pros::millis(); + for (int i = 0; i < this->screens.size(); i++) { + this->screens[i]->update(pros::millis() - this->last_update_time); + } + this->last_update_time = pros::millis(); // Check if enough time has passed for the Gamepad to poll for updates if (pros::millis() - this->last_print_time <= 50) return; @@ -50,8 +76,10 @@ void Gamepad::updateScreens() { if (!this->nextBuffer[line].has_value()) continue; if (!this->screenCleared && line != 3) { + // printf("clearing screen for init\n"); this->controller.clear(); - screenCleared = true; + this->screenCleared = true; + this->currentScreen = {}; this->last_print_time = pros::millis(); return; } @@ -62,10 +90,20 @@ void Gamepad::updateScreens() { continue; } + // printf("\nLine = %i\n", line); + + // printf("nextBuffer = {%s, %s, %s, %s}\n", nextBuffer.at(0).value_or("nullopt").c_str(), + // nextBuffer.at(1).value_or("nullopt").c_str(), nextBuffer.at(2).value_or("nullopt").c_str(), + // nextBuffer.at(3).value_or("nullopt").c_str()); + + // printf("currentScreen = {%s, %s, %s, %s}\n", currentScreen.at(0).value_or("nullopt").c_str(), + // currentScreen.at(1).value_or("nullopt").c_str(), currentScreen.at(2).value_or("nullopt").c_str(), + // currentScreen.at(3).value_or("nullopt").c_str()); + // print to screen or rumble if (line == 3) this->controller.rumble(this->nextBuffer[line].value_or("").c_str()); else this->controller.set_text(line, 0, this->nextBuffer[line].value_or("") + std::string(40, ' ')); - this->currentScreen[line] = std::move(this->nextBuffer[line]); + if (line != 3) this->currentScreen[line] = std::move(this->nextBuffer[line]); this->nextBuffer[line] = std::nullopt; this->last_printed_line = line; this->last_print_time = pros::millis();