1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-12-27 06:11:25 +01:00

Merged DIAG to be genaral stream

Separate steaming and parsers
This commit is contained in:
Asbelos 2020-05-25 19:59:47 +01:00
parent 09b1277000
commit ed279a26d8
7 changed files with 63 additions and 45 deletions

View File

@ -58,7 +58,7 @@ void loop() {
}
else if (ch == '>') {
buffer[bufferLength]='\0';
JMRIParser::parse(buffer);
JMRIParser::parse(Serial, buffer);
inCommandPayload = false;
} else if(inCommandPayload) {
buffer[bufferLength++]= ch;

View File

@ -1,27 +0,0 @@
#include <arduino.h>
#include <stdarg.h>
void Serialprint(const __FlashStringHelper* input...) {
// thanks to Jan Turoň https://arduino.stackexchange.com/questions/56517/formatting-strings-in-arduino-for-output
va_list args;
va_start(args, input);
char* flash=(char*)input;
for(int i=0; ; ++i) {
char c=pgm_read_byte_near(flash+i);
if (c=='\0') return;
if(c!='%') { Serial.print(c); continue; }
i++;
c=pgm_read_byte_near(flash+i);
switch(c) {
case '%': Serial.print('%'); break;
case 's': Serial.print(va_arg(args, char*)); break;
case 'd': Serial.print(va_arg(args, int), DEC); break;
case 'b': Serial.print(va_arg(args, int), BIN); break;
case 'o': Serial.print(va_arg(args, int), OCT); break;
case 'x': Serial.print(va_arg(args, int), HEX); break;
case 'f': Serial.print(va_arg(args, double), 2); break;
}
}
va_end(args);
}

4
DIAG.h
View File

@ -1,5 +1,5 @@
void Serialprint(const __FlashStringHelper* input...);
#include "StringParser.h"
#ifndef DIAG_ENABLED
#define DIAG_ENABLED true
#endif
#define DIAG if (DIAG_ENABLED) Serialprint
#define DIAG if (DIAG_ENABLED) StringParser::print

View File

@ -2,7 +2,7 @@
#include "JMRIParser.h"
#include "DCC.h"
#include "DCCWaveform.h"
#include "DIAG.h"
// This is a JMRI command parser
// It doesnt know how the string got here, nor how it gets back.
// It knows nothing about hardware or tracks... it just parses strings and
@ -10,10 +10,10 @@
//
int JMRIParser::p[MAX_PARAMS];
// See documentation on DCC class for info on this section
void JMRIParser::parse(const char *com) {
DIAG(F("\nParsing %s\n"),com);
void JMRIParser::parse(Stream & stream,const char *com) {
StringParser::send(stream,F("\nParsing %s\n"),com);
bool result;
int params=StringParser::parse(com+1,p,MAX_PARAMS);
@ -24,7 +24,7 @@ void JMRIParser::parse(const char *com) {
case 't': // <t REGISTER CAB SPEED DIRECTION>
DCC::setThrottle(p[1],p[2],p[3]);
DIAG(F("<T %d %d %d>"), p[0], p[2],p[3]);
StringParser::send(stream,F("<T %d %d %d>"), p[0], p[2],p[3]);
break;
/***** OPERATE ENGINE DECODER FUNCTIONS F0-F28 ****/
@ -233,7 +233,7 @@ void JMRIParser::parse(const char *com) {
*/
result=DCC::writeCVByte(p[0],p[1]);
DIAG(F("<r%d|%d|%d %d>"), p[2], p[3],p[0],result?p[1]:-1);
StringParser::send(stream,F("<r%d|%d|%d %d>"), p[2], p[3],p[0],result?p[1]:-1);
break;
/***** WRITE CONFIGURATION VARIABLE BIT TO ENGINE DECODER ON PROGRAMMING TRACK ****/
@ -253,7 +253,7 @@ void JMRIParser::parse(const char *com) {
*/
result=DCC::writeCVBit(p[0],p[1],p[2]);
DIAG(F("<r%d|%d|%d %d %d>"), p[3],p[4], p[0],p[1],result?p[2]:-1);
StringParser::send(stream,F("<r%d|%d|%d %d %d>"), p[3],p[4], p[0],p[1],result?p[2]:-1);
break;
/***** READ CONFIGURATION VARIABLE BYTE FROM ENGINE DECODER ON PROGRAMMING TRACK ****/
@ -270,7 +270,7 @@ void JMRIParser::parse(const char *com) {
* where VALUE is a number from 0-255 as read from the requested CV, or -1 if read could not be verified
*/
DIAG(F("<r%d|%d|%d %d>"),p[1],p[2],p[0],DCC::readCV(p[0]));
StringParser::send(stream,F("<r%d|%d|%d %d>"),p[1],p[2],p[0],DCC::readCV(p[0]));
break;
/***** TURN ON POWER FROM MOTOR SHIELD TO TRACKS ****/
@ -283,7 +283,7 @@ void JMRIParser::parse(const char *com) {
*/
DCCWaveform::mainTrack.setPowerMode(POWERMODE::ON);
DCCWaveform::progTrack.setPowerMode(POWERMODE::ON);
DIAG(F("<p1>"));
StringParser::send(stream,F("<p1>"));
break;
/***** TURN OFF POWER FROM MOTOR SHIELD TO TRACKS ****/
@ -296,7 +296,7 @@ void JMRIParser::parse(const char *com) {
*/
DCCWaveform::mainTrack.setPowerMode(POWERMODE::OFF);
DCCWaveform::progTrack.setPowerMode(POWERMODE::OFF);
DIAG(F("<p0>"));
StringParser::send(stream,F("<p0>"));
break;
#ifdef THIS_IS_NOT_YET_COMPLETE
@ -309,7 +309,7 @@ void JMRIParser::parse(const char *com) {
* returns: <a CURRENT>
* where CURRENT = 0-1024, based on exponentially-smoothed weighting scheme
*/
DIAG(F("<a %d>"), DCCWaveform:mainTrack->getLastRead());
StringParser::send(stream,F("<a %d>"), DCCWaveform:mainTrack->getLastRead());
break;
/***** READ STATUS OF DCC++ BASE STATION ****/
@ -360,15 +360,15 @@ void JMRIParser::parse(const char *com) {
CommManager::printf("<O>");
break;
#endif
/***** PRINT CARRIAGE RETURN IN SERIAL MONITOR WINDOW ****/
/***** PRINT CARRIAGE RETURN IN stream MONITOR WINDOW ****/
case ' ': // < >
/*
* simply prints a carriage return - useful when interacting with Ardiuno through serial monitor window
* simply prints a carriage return - useful when interacting with Ardiuno through stream monitor window
*
* returns: a carriage return
*/
DIAG(F("\n"));
StringParser::send(stream,F("\n"));
break;
}
}

View File

@ -1,7 +1,7 @@
struct JMRIParser
{
static void parse(const char * command);
static void parse(Stream & stream,const char * command);
private:
static const int MAX_PARAMS=10;

View File

@ -1,4 +1,5 @@
#include "StringParser.h"
#include <stdarg.h>
int StringParser::parse(const char * com, int result[], byte maxResults) {
byte state=1;
@ -42,3 +43,42 @@ int StringParser::parse(const char * com, int result[], byte maxResults) {
}
return parameterCount;
}
void StringParser::print( const __FlashStringHelper* input...) {
va_list args;
va_start(args, input);
send(Serial,input,args);
}
void StringParser::send(Stream & stream, const __FlashStringHelper* input...) {
va_list args;
va_start(args, input);
send(stream,input,args);
}
void StringParser::send(Stream & stream,const __FlashStringHelper* format, va_list args) {
// thanks to Jan Turoň https://arduino.stackexchange.com/questions/56517/formatting-strings-in-arduino-for-output
char* flash=(char*)format;
for(int i=0; ; ++i) {
char c=pgm_read_byte_near(flash+i);
if (c=='\0') return;
if(c!='%') { stream.print(c); continue; }
i++;
c=pgm_read_byte_near(flash+i);
switch(c) {
case '%': stream.print('%'); break;
case 's': stream.print(va_arg(args, char*)); break;
case 'd': stream.print(va_arg(args, int), DEC); break;
case 'b': stream.print(va_arg(args, int), BIN); break;
case 'o': stream.print(va_arg(args, int), OCT); break;
case 'x': stream.print(va_arg(args, int), HEX); break;
case 'f': stream.print(va_arg(args, double), 2); break;
}
}
va_end(args);
}

View File

@ -3,4 +3,9 @@ class StringParser
{
public:
static int parse(const char * com, int result[], byte maxResults);
static void print( const __FlashStringHelper* input...);
static void send(Stream & serial, const __FlashStringHelper* input...);
private:
static void send(Stream & serial, const __FlashStringHelper* input,va_list args);
};