Refactor SerialCommand to support Stream interface and improve command handling
- Added readSerial(Stream &stream) method to allow reading from any Stream-compatible transport. - Introduced readChar(char inChar) method for processing individual characters. - Updated command matching logic to enhance debugging output. - Improved buffer handling and command execution flow. Enhance platformio.ini for better compatibility and added libraries - Added NimBLE-Arduino and WebSockets libraries for BLE and WiFi support. - Updated upload and monitor ports for better compatibility with macOS. Integrate WiFi and BLE protocol interfaces - Implemented startWebInterface() to initialize WiFi protocol alongside existing web server. - Added BLE support with a new EggBot BLE Serial Protocol for command handling over BLE. - Created WebSocket server for WiFi communication, maintaining compatibility with existing command protocols. Refactor command handling in Functions.cpp - Replaced direct Serial.print calls with protocolWrite for consistent output handling. - Updated command registration to use a lambda function for better readability and maintainability. Add documentation for EggBot protocols - Created separate markdown files for BLE, WiFi, and Serial protocols detailing command structures and usage. - Provided examples of command transactions for better developer guidance. Implement BLE and WiFi protocol handling in respective source files - Developed BLE_Interface.cpp for managing BLE connections and data transmission. - Created WiFi_Protocol.cpp for handling WebSocket communication and data reception.
This commit is contained in:
@@ -71,58 +71,65 @@ void SerialCommand::setDefaultHandler(void (*function)(const char *)) {
|
||||
* buffer for a prefix command, and calls handlers setup by addCommand() member
|
||||
*/
|
||||
void SerialCommand::readSerial() {
|
||||
while (Serial.available() > 0) {
|
||||
char inChar = Serial.read(); // Read single available character, there may be more waiting
|
||||
readSerial(Serial);
|
||||
}
|
||||
|
||||
void SerialCommand::readSerial(Stream &stream) {
|
||||
while (stream.available() > 0) {
|
||||
char inChar = stream.read(); // Read single available character, there may be more waiting
|
||||
readChar(inChar);
|
||||
}
|
||||
}
|
||||
|
||||
void SerialCommand::readChar(char inChar) {
|
||||
#ifdef SERIALCOMMAND_DEBUG
|
||||
Serial.print(inChar); // Echo back to serial stream
|
||||
#endif
|
||||
|
||||
if (inChar == term) { // Check for the terminator (default '\r') meaning end of command
|
||||
#ifdef SERIALCOMMAND_DEBUG
|
||||
Serial.print(inChar); // Echo back to serial stream
|
||||
Serial.print("Received: ");
|
||||
Serial.println(buffer);
|
||||
#endif
|
||||
|
||||
if (inChar == term) { // Check for the terminator (default '\r') meaning end of command
|
||||
#ifdef SERIALCOMMAND_DEBUG
|
||||
Serial.print("Received: ");
|
||||
Serial.println(buffer);
|
||||
#endif
|
||||
char *command = strtok_r(buffer, delim, &last); // Search for command at start of buffer
|
||||
if (command != NULL) {
|
||||
boolean matched = false;
|
||||
for (int i = 0; i < commandCount; i++) {
|
||||
#ifdef SERIALCOMMAND_DEBUG
|
||||
Serial.print("Comparing [");
|
||||
Serial.print(command);
|
||||
Serial.print("] to [");
|
||||
Serial.print(commandList[i].command);
|
||||
Serial.println("]");
|
||||
#endif
|
||||
|
||||
char *command = strtok_r(buffer, delim, &last); // Search for command at start of buffer
|
||||
if (command != NULL) {
|
||||
boolean matched = false;
|
||||
for (int i = 0; i < commandCount; i++) {
|
||||
// Compare the found command against the list of known commands for a match
|
||||
if (strncmp(command, commandList[i].command, SERIALCOMMAND_MAXCOMMANDLENGTH) == 0) {
|
||||
#ifdef SERIALCOMMAND_DEBUG
|
||||
Serial.print("Comparing [");
|
||||
Serial.print(command);
|
||||
Serial.print("] to [");
|
||||
Serial.print(commandList[i].command);
|
||||
Serial.println("]");
|
||||
Serial.print("Matched Command: ");
|
||||
Serial.println(command);
|
||||
#endif
|
||||
|
||||
// Compare the found command against the list of known commands for a match
|
||||
if (strncmp(command, commandList[i].command, SERIALCOMMAND_MAXCOMMANDLENGTH) == 0) {
|
||||
#ifdef SERIALCOMMAND_DEBUG
|
||||
Serial.print("Matched Command: ");
|
||||
Serial.println(command);
|
||||
#endif
|
||||
|
||||
// Execute the stored handler function for the command
|
||||
(*commandList[i].function)();
|
||||
matched = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!matched && (defaultHandler != NULL)) {
|
||||
(*defaultHandler)(command);
|
||||
// Execute the stored handler function for the command
|
||||
(*commandList[i].function)();
|
||||
matched = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
clearBuffer();
|
||||
if (!matched && (defaultHandler != NULL)) {
|
||||
(*defaultHandler)(command);
|
||||
}
|
||||
}
|
||||
else if (isprint(inChar)) { // Only printable characters into the buffer
|
||||
if (bufPos < SERIALCOMMAND_BUFFER) {
|
||||
buffer[bufPos++] = inChar; // Put character into buffer
|
||||
buffer[bufPos] = '\0'; // Null terminate
|
||||
} else {
|
||||
#ifdef SERIALCOMMAND_DEBUG
|
||||
Serial.println("Line buffer is full - increase SERIALCOMMAND_BUFFER");
|
||||
#endif
|
||||
}
|
||||
clearBuffer();
|
||||
} else if (isprint(inChar)) { // Only printable characters into the buffer
|
||||
if (bufPos < SERIALCOMMAND_BUFFER) {
|
||||
buffer[bufPos++] = inChar; // Put character into buffer
|
||||
buffer[bufPos] = '\0'; // Null terminate
|
||||
} else {
|
||||
#ifdef SERIALCOMMAND_DEBUG
|
||||
Serial.println("Line buffer is full - increase SERIALCOMMAND_BUFFER");
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,6 +49,8 @@ class SerialCommand {
|
||||
void setDefaultHandler(void (*function)(const char *)); // A handler to call when no valid command received.
|
||||
|
||||
void readSerial(); // Main entry point.
|
||||
void readSerial(Stream &stream); // Reads commands from any Stream-compatible transport.
|
||||
void readChar(char inChar); // Feeds one incoming character into the parser.
|
||||
void clearBuffer(); // Clears the input buffer.
|
||||
char *next(); // Returns pointer to next token found in command buffer (for getting arguments to commands).
|
||||
|
||||
|
||||
Reference in New Issue
Block a user