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

Command Filter and some extra APIs.

This commit is contained in:
Asbelos 2020-06-18 19:36:37 +01:00
parent ffea04a499
commit 5872659ff2
5 changed files with 42 additions and 6 deletions

View File

@ -18,6 +18,14 @@ void myCallback(int result) {
DIAG(F("\n getting Loco Id callback result=%d"),result);
}
void myFilter(Stream & stream, byte & opcode, byte & paramCount, int p[]) {
if (opcode=='T') {
DIAG(F("\nStop messing with Turnouts!"));
opcode=0; // tell parssr to ignore it
}
}
DCCEXParser serialParser;
void setup() {
@ -28,6 +36,7 @@ void setup() {
DCC::getLocoId(myCallback); // myCallback will be called with the result
DIAG(F("\n===== DCC::getLocoId has returned, but wont be executed until we are in loop() ======\n"));
DIAG(F("\nReady for JMRI commands\n"));
DCCEXParser::setFilter(myFilter);
}
void loop() {

View File

@ -234,6 +234,14 @@ void DCC::getLocoId(ACK_CALLBACK callback) {
ackManagerSetup(0,0, LOCO_ID_PROG, callback);
}
void DCC::forgetLoco(int cab) { // removes any speed reminders for this loco
for (int i=0;i<MAX_LOCOS;i++) if (speedTable[i].loco=cab) speedTable[i].loco=0;
}
void DCC::forgetAllLocos() { // removes all speed reminders
for (int i=0;i<MAX_LOCOS;i++) speedTable[i].loco=0;
}
void DCC::loop() {
DCCWaveform::loop(); // power overload checks
ackManagerLoop();

7
DCC.h
View File

@ -51,6 +51,10 @@ class DCC {
static void getLocoId(ACK_CALLBACK callback);
// Enhanced API functions
static void forgetLoco(int cab); // removes any speed reminders for this loco
static void forgetAllLocos(); // removes all speed reminders
private:
struct LOCO {
int loco;
@ -62,7 +66,8 @@ private:
static LOCO speedTable[MAX_LOCOS];
static byte cv1(byte opcode, int cv);
static byte cv2(int cv);
// ACK MANAGER
static ackOp const * ackManagerProg;

View File

@ -94,18 +94,26 @@ void DCCEXParser::loop(Stream & stream) {
return parameterCount;
}
FILTER_CALLBACK DCCEXParser::filterCallback=0;
void DCCEXParser::setFilter(FILTER_CALLBACK filter) {
filterCallback=filter;
}
// See documentation on DCC class for info on this section
void DCCEXParser::parse(Print & stream, const char *com) {
DIAG(F("\nPARSING:%s\n"),com);
(void) EEPROM; // tell compiler not to warn thi is unused
int p[MAX_PARAMS];
int params=splitValues(p, com);
byte params=splitValues(p, com);
if (com[0]=='<') com++;
byte opcode=com[0];
if (filterCallback) filterCallback(stream,opcode,params,p);
// Functions return from this switch if complete, break from switch implies error <X> to send
switch(com[0]) {
switch(opcode) {
case '\0': return; // filterCallback asked us to ignore
case 't': // THROTTLE <t REGISTER CAB SPEED DIRECTION>
DCC::setThrottle(p[1],p[2],p[3]);
StringFormatter::send(stream,F("<T %d %d %d>"), p[0], p[2],p[3]);

View File

@ -1,15 +1,20 @@
#ifndef DCCEXParser_h
#define DCCEXParser_h
#include <Arduino.h>
typedef void (*FILTER_CALLBACK)(Print & stream, byte & opcode, byte & paramCount, int p[]);
struct DCCEXParser
{
DCCEXParser();
void loop(Stream & pstream);
void parse(Print & stream, const char * command);
void flush();
static void setFilter(FILTER_CALLBACK filter);
static const int MAX_PARAMS=10; // Must not exceed this
private:
static const int MAX_PARAMS=10; // longest command sent in
static const int MAX_BUFFER=50; // longest command sent in
byte bufferLength=0;
bool inCommandPayload=false;
@ -28,6 +33,7 @@ struct DCCEXParser
static void callback_W(int result);
static void callback_B(int result);
static void callback_R(int result);
static FILTER_CALLBACK filterCallback;
};