Skip to content

Instantly share code, notes, and snippets.

@tbnbooij
Last active May 25, 2017 19:47
Show Gist options
  • Save tbnbooij/c0ec9901c926cf9aa073e5114caf8ad3 to your computer and use it in GitHub Desktop.
Save tbnbooij/c0ec9901c926cf9aa073e5114caf8ad3 to your computer and use it in GitHub Desktop.
Demo of the reading capabilities of the Wireless library.
// _ _ _ _
// | | | (_) | |
// | | | |_ _ __ ___| | ___ ___ ___
// | |/\| | | '__/ _ \ |/ _ \/ __/ __|
// \ /\ / | | | __/ | __/\__ \__ \
// \/ \/|_|_| \___|_|\___||___/___/
//
// Demo of the reading functions
// =============================
// Setup: Plug an Arduino into your computer and upload this sketch.
// Then write commands of the following syntax into the serial monitor: #[your message here]!
// Please note that the capabilities of these functions stretch beyond just reading COMPLETE messages.
// It also stores broken off sentences for potential ERROR correction.
// Try it out with the following input:
//
// #thisis <enter>
// broken
// input!
//
// If everything works as intended, the string "thisisbrokeninput" will appear as one of the entries in the array
// after entering the last command.
//
// ~ tbnbooij
#define QUEUE_SIZE 20
String readQueue[QUEUE_SIZE] = {""};
String readBuffer = "";
void addToReadQueue(String input) {
// Iterator and blocking boolean
uint8_t i = 0;
bool block = true;
// Just test every single spot in the array UNTIL we hit an empty spot
// If we don't hit an empty spot, then we should hard-code a larger array
if(input != "") {
while(block) {
// If spot i is empty
if(readQueue[i] == "") {
readQueue[i] = input;
block = false;
}
else {
// Just move up one space in the array and run this test again
i++;
}
}
}
}
void analyseSerialBuffer(String buffer) {
// Search for the first hashtag
int initHash = buffer.indexOf('#');
// This condition determines whether the readBuffer is empty or not and will be used multiple times
bool prevCond = readBuffer != "";
// If the readBuffer is non-empty, well, then we start at the beginning of the next message
// In addition, the buffer needs to have a '#' or '!', because otherwise the wrong decision will be taken
// By the program
if(prevCond && (initHash != -1 || buffer.indexOf('!') != -1)) {
initHash = 0;
}
// If a hashtag is found in the buffer...
if(initHash >= 0) {
uint8_t i = (uint8_t) initHash;
// If there is a closing character (!) for the current message
if(buffer.indexOf('!',initHash) != -1) {
// Then we know the opening index of the current message AND the fact that it will be closed in this buffer
// Now iterate through the chars until we find a '!'
while(buffer[i] != '!' && i <= buffer.length()) {
i++;
}
// We now know where the message BEGINS and ENDS
// Just one check remains: whether there still is some data left over from the previous call
// (i.e. the readBuffer isn't empty)
// In that case, this 'residue' is concatenated to the newly found end of the message
String a;
if(prevCond) {
a = readBuffer + buffer.substring(initHash,i);
readBuffer = "";
}
else {
a = buffer.substring((initHash+1),i);
}
addToReadQueue(a);
// Finally, we can call this function AGAIN to do this all over
// This allows for very compact code AND a lot less work for me :D
String potentialString = buffer.substring(initHash+1);
if(potentialString.indexOf('!') != potentialString.length()-1) {
analyseSerialBuffer(potentialString);
}
}
else {
// If a closing character (!) isn't found, then just add the 'tail' of the buffer to the readBuffer
// for the following analysis
readBuffer += buffer.substring(initHash+1,buffer.length());
}
}
else {
// If there isn't a hashtag in the buffer ..
if(buffer.indexOf('!') != -1) {
// And there is a end character in the buffer, then just store everything UNTIL that character
addToReadQueue(readBuffer + buffer.substring(0, (buffer.indexOf('!'))));
readBuffer = "";
}
else {
// And there is NO end character in the buffer, then just store the entire buffer
readBuffer += buffer;
}
}
}
void read() {
// Only read if something is in the buffer!
if(Serial.available() > 0) {
// Read out the serial buffer as a string
String serialBuffer = Serial.readString();
analyseSerialBuffer(serialBuffer);
Serial.println("");
Serial.println("Current Read Queue");
Serial.println("====================================");
for(int i = 0; i < QUEUE_SIZE; i++) {
if(readQueue[i] != NULL) {
Serial.print(String(i+1) + ": ");
Serial.println(readQueue[i]);
}
else {
Serial.print(String(i+1) + ": ");
Serial.println("(NULL)");
}
}
}
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
read();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment