Last active
September 8, 2019 07:37
-
-
Save theol0403/9dff35120c9cb39da47ec2dd5e1193b1 to your computer and use it in GitHub Desktop.
V5 Controller printing utility
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// arrays to store the three lines of text | |
std::array<std::string, 3> lines = {}; | |
// used to compare with previous text to see if an update to the controller is needed | |
std::array<std::string, 3> lastLines = {}; | |
// to print text, all you need to do is write to the `lines` array | |
// lines[lineNum] = "text"; | |
void controller_print(void*){ | |
// 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; | |
// loop through each line, i = lineNum | |
for(int i = 0; i < lines.size(); i++) { | |
// check to see if the line needs to be reprinted (if it changed). | |
// to find out if this optimization makes any difference, replace with `if(true)`, | |
// then it will update each line every 52ms | |
if(lines.at(i) != lastLines.at(i)) { | |
// make a copy of the text | |
std::string str = lines.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()); | |
} | |
// send the padded/sized text to the controller | |
// `str.c_str()` is needed to convert the `std::string` into a `const char*` | |
pros::c::controller_set_text(pros::E_CONTROLLER_MASTER, i, 0, str.c_str()); | |
// record the text for future comparison | |
lastLines.at(i) = lines.at(i); | |
// the important delay | |
pros::delay(52); | |
// lets the code know a delay occured, so another one is not neccecary | |
delayed = true; | |
} | |
} | |
// if the printing was skipped, make sure the loop has a delay | |
if(!delayed) { | |
pros::delay(52); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment