1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-12-24 21:21:24 +01:00

Redirected string parsing

This commit is contained in:
Asbelos 2020-05-25 23:03:11 +01:00
parent ed279a26d8
commit a43db19cc0
3 changed files with 37 additions and 33 deletions

View File

@ -31,39 +31,13 @@ void setup() {
DIAG(F("\nCV %d = %d 0x%x %s"),cvnums[x],value,value, value>=0?" VERIFIED OK":"FAILED VERIFICATION"); DIAG(F("\nCV %d = %d 0x%x %s"),cvnums[x],value,value, value>=0?" VERIFIED OK":"FAILED VERIFICATION");
} }
DIAG(F("\n===== CVReader done ==============================\n")); DIAG(F("\n===== CVReader done ==============================\n"));
DIAG(F("\nReady for JMRI commands\n"));
DIAG(F("\nReady for JMRI\n"));
} }
const byte MAX_BUFFER=100;
char buffer[MAX_BUFFER];
byte bufferLength=0;
bool inCommandPayload=false;
void loop() { void loop() {
DCC::loop(); DCC::loop(); // required to keep locos running and check powwer
while(Serial.available()) {
if (bufferLength==MAX_BUFFER) { // This line passes input on Serial to the JMRIparser
DIAG(F("\n**Buffer cleared**\n")); StringParser::loop(Serial, JMRIParser::parse);
bufferLength=0;
inCommandPayload=false;
}
char ch = Serial.read();
if (ch == '<') {
inCommandPayload = true;
bufferLength=0;
buffer[0]='\0';
}
else if (ch == '>') {
buffer[bufferLength]='\0';
JMRIParser::parse(Serial, buffer);
inCommandPayload = false;
} else if(inCommandPayload) {
buffer[bufferLength++]= ch;
}
}
} }

View File

@ -1,6 +1,31 @@
#include "StringParser.h" #include "StringParser.h"
#include <stdarg.h> #include <stdarg.h>
byte StringParser::bufferLength=0;
bool StringParser::inCommandPayload=false;
char StringParser::buffer[MAX_BUFFER];
void StringParser::loop(Stream & stream, void (*callback)(Stream & stream, const char * data) ) {
while(stream.available()) {
if (bufferLength==MAX_BUFFER) {
bufferLength=0;
inCommandPayload=false;
}
char ch = stream.read();
if (ch == '<') {
inCommandPayload = true;
bufferLength=0;
buffer[0]='\0';
}
else if (ch == '>') {
buffer[bufferLength]='\0';
(*callback)(Serial, buffer);
inCommandPayload = false;
} else if(inCommandPayload) {
buffer[bufferLength++]= ch;
}
}
}
int StringParser::parse(const char * com, int result[], byte maxResults) { int StringParser::parse(const char * com, int result[], byte maxResults) {
byte state=1; byte state=1;
byte parameterCount=0; byte parameterCount=0;

View File

@ -2,10 +2,15 @@
class StringParser class StringParser
{ {
public: public:
static void loop(Stream & stream, void (*callback)(Stream & stream, const char * data) );
static int parse(const char * com, int result[], byte maxResults); static int parse(const char * com, int result[], byte maxResults);
static void print( const __FlashStringHelper* input...); static void print( const __FlashStringHelper* input...);
static void send(Stream & serial, const __FlashStringHelper* input...); static void send(Stream & serial, const __FlashStringHelper* input...);
private: private:
static void send(Stream & serial, const __FlashStringHelper* input,va_list args); static void send(Stream & serial, const __FlashStringHelper* input,va_list args);
static byte bufferLength;
static bool inCommandPayload;
static const byte MAX_BUFFER=100;
static char buffer[MAX_BUFFER];
}; };