Skip to content

Instantly share code, notes, and snippets.

@theol0403
Created December 24, 2019 18:00
Show Gist options
  • Save theol0403/3f9571d6cd72ce22ab8895891cd74ed6 to your computer and use it in GitHub Desktop.
Save theol0403/3f9571d6cd72ce22ab8895891cd74ed6 to your computer and use it in GitHub Desktop.
ControllerPrinter
#include "controllerPrinter.hpp"
using namespace pros::c;
namespace lib7842 {
ControllerPrinter::ControllerPrinter() : task(taskFnc, this) {}
void ControllerPrinter::print(int line, std::string str, pros::controller_id_e_t id) {
lines[id].at(line) = str;
}
void ControllerPrinter::rumble(std::string str, pros::controller_id_e_t id) {
rumbleTexts[id] = str;
}
std::string ControllerPrinter::get(int line, pros::controller_id_e_t id) {
return lines[id].at(line);
}
void ControllerPrinter::copy(
int line, pros::controller_id_e_t source, pros::controller_id_e_t dest) {
lines[dest].at(line) = lines[source].at(line);
}
void ControllerPrinter::run() {
// the code will fill the text with spaces or trim the end of the text,
// so that the length of the text is exactly 15 characters
const int maxWidth = 15;
while (true) {
// make sure there is no starving, if main loop delay does not get called.
// throughout all the code, if no delays get called (happens if text does not change),
// then a delay will be called so the task can yield
bool delayed = false;
for (int id = 0; id < lines.size(); id++) {
if (pros::c::controller_is_connected((pros::controller_id_e_t)id)) {
for (int i = 0; i < lines[id].size(); i++) {
// check to see if the line needs to be reprinted (if it changed).
if (lines[id].at(i) != lastLines[id].at(i)) {
std::string str = lines[id].at(i);
// if the text is smaller than 15 characters
if (str.size() < maxWidth) {
// insert spaces at the end so the text is 15 characters long
// hopefully this overwrites everything on the screen
str.insert(str.end(), maxWidth - str.size(), ' ');
} else {
// if the text is larger than 15 characters,
// trim the end of the text so it is 15 characters long.
// that text won't be shown on the display anyways.
str.erase(str.begin() + maxWidth, str.end());
}
controller_set_text((pros::controller_id_e_t)id, i, 0, str.c_str());
lastLines[id].at(i) = lines[id].at(i);
pros::delay(52);
delayed = true;
}
if (rumbleTexts[id] != "") {
controller_rumble((pros::controller_id_e_t)id, rumbleTexts[id].c_str());
pros::delay(52);
delayed = true;
rumbleTexts[id] = "";
}
}
}
}
if (!delayed) { pros::delay(52); }
}
}
void ControllerPrinter::taskFnc(void* input) {
static_cast<ControllerPrinter*>(input)->run();
}
} // namespace lib7842
#pragma once
#include "main.h"
#include <string>
namespace lib7842 {
class ControllerPrinter {
public:
ControllerPrinter();
void print(int line, std::string str, pros::controller_id_e_t id = pros::E_CONTROLLER_MASTER);
void rumble(std::string str, pros::controller_id_e_t id = pros::E_CONTROLLER_MASTER);
std::string get(int line, pros::controller_id_e_t id);
void copy(int line, pros::controller_id_e_t source, pros::controller_id_e_t dest);
protected:
void run();
static void taskFnc(void* input);
pros::Task task;
// arrays to store the three lines of text
std::array<std::array<std::string, 3>, 2> lines = {};
// used to compare with previous text to see if an update to the controller is needed
std::array<std::array<std::string, 3>, 2> lastLines = {};
// buffer to store the rumble text
std::array<std::string, 2> rumbleTexts = {};
};
} // namespace lib7842
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment