Skip to content

Commit

Permalink
improve console serial
Browse files Browse the repository at this point in the history
here's a much better version of the serial console

let me know if you need any changes
  • Loading branch information
jstockdale committed Dec 7, 2024
1 parent a43ebfb commit 25e9100
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 7 deletions.
63 changes: 57 additions & 6 deletions esp32_marauder/CommandLine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,69 @@ void CommandLine::RunSetup() {
Serial.println(" " + version_number + "\n");
Serial.println(F(" By: justcallmekoko\n"));
Serial.println(F("--------------------------------\n\n"));

Serial.print("> ");
}

String CommandLine::getSerialInput() {
String input = "";
int bytes_received = 0;
int bytes_available = 0;
String command_line = "";
char command_buffer[255];

memset(command_buffer, '\0', 255);

if (Serial.available() > 0 && serial_buffer_idx < 255) {
bytes_available = Serial.available();
int bytes_to_read = bytes_available < (254 - serial_buffer_idx) ? bytes_available : (254 - serial_buffer_idx);
bytes_received = Serial.readBytes(&serial_buffer[serial_buffer_idx], bytes_to_read);
serial_buffer_idx += bytes_received;
if (serial_buffer_idx < 254) {
serial_buffer[serial_buffer_idx+1] = '\0';
} else if (serial_buffer_idx >= 254) {
Serial.println("Serial buffer overrun?");
}
}

int index_of_newline = -1;

for (int i = 0; i < serial_buffer_idx; i++) {
// this is mind boggling but hey. why not. :D
if (serial_buffer[i] == '\n' || serial_buffer[i] == '\r') {
//Serial.println("Found \"newline\" at index: " + String(i));
index_of_newline = i;
command_buffer[i] = '\0';
} else {
command_buffer[i] = serial_buffer[i];
}
}

if (Serial.available() > 0)
input = Serial.readStringUntil('\n');
command_line = command_buffer;

input.trim();
return input;
if (index_of_newline > -1) {
serial_buffer_idx = 0;
if (serial_buffer[index_of_newline] == '\r' && serial_buffer[index_of_newline+1] == '\n') {
index_of_newline++;
}

for (int i = index_of_newline + 1; i < 255; i++) {
if(serial_buffer[i] == '\0') {
break;
} else if (i > index_of_newline + 1 || i == 254) {
// Always zero extra bytes and null terminate no matter what
serial_buffer[i]='\0';
} else {
// Copy any characters we have after the newline
// but before the null terminator. If they exist.
serial_buffer[i - (index_of_newline + 1)] = serial_buffer[i];
}
}

command_line.trim();
return command_line;
} else {
return "";
}
}

void CommandLine::main(uint32_t currentTime) {
Expand Down
6 changes: 5 additions & 1 deletion esp32_marauder/CommandLine.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,10 @@ const char PROGMEM HELP_BT_WARDRIVE_CMD[] = "btwardrive [-c]";
const char PROGMEM HELP_BT_SKIM_CMD[] = "sniffskim";
const char PROGMEM HELP_FOOT[] = "==================================";


class CommandLine {
private:
static char serial_buffer[255];
static int serial_buffer_idx;
String getSerialInput();
LinkedList<String> parseCommand(String input, char* delim);
String toLowerCase(String str);
Expand Down Expand Up @@ -197,4 +198,7 @@ class CommandLine {
void main(uint32_t currentTime);
};

int CommandLine::serial_buffer_idx = 0;
char CommandLine::serial_buffer[255];

#endif

0 comments on commit 25e9100

Please sign in to comment.