Skip to content

Commit

Permalink
alerts should work
Browse files Browse the repository at this point in the history
  • Loading branch information
PA055 committed Aug 18, 2024
1 parent 6c90b92 commit 8dbb732
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 42 deletions.
2 changes: 1 addition & 1 deletion include/gamepad/screens/abstractScreen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ typedef std::array<std::optional<std::string>, 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<uint8_t> visible_lines) = 0;
virtual void handle_events(std::set<pros::controller_digital_e_t> button_events) = 0;
const uint get_priority() { return this->priority; }
Expand Down
16 changes: 8 additions & 8 deletions include/gamepad/screens/alertScreen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

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

Expand All @@ -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<uint8_t> visible_lines);
void handle_events(std::set<pros::controller_digital_e_t> 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<std::deque<Line>, 4> screen_buffer{};
std::array<Line, 4> screen_contents{};
std::array<uint32_t, 4> line_set_time{};
std::deque<AlertBuffer> screen_buffer{};
std::optional<AlertBuffer> screen_contents;
uint line_set_time;
pros::Mutex mut;
};

Expand Down
66 changes: 33 additions & 33 deletions src/gamepad/screens/alertScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,60 +2,60 @@
#include <algorithm>
#include <cstdint>
#include <mutex>
#include <optional>
#include <sstream>
#include "gamepad/todo.hpp"
#include "pros/rtos.hpp"

namespace Gamepad {

ScreenBuffer AlertScreen::get_screen(std::set<uint8_t> visible_lines) {
ScreenBuffer output;
std::lock_guard<pros::Mutex> 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<pros::Mutex> 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<std::string> strs(3);
std::stringstream ss(str);
std::vector<std::string> 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<pros::Mutex> 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<pros::Mutex> guard(this->mut);
this->screen_buffer[line].push_back({ .text = std::move(str), .duration = duration });
this->screen_buffer.push_back({buffer, duration});
}

}
}

0 comments on commit 8dbb732

Please sign in to comment.