mirror of
https://github.com/DCC-EX/CommandStation-EX.git
synced 2024-11-23 08:06:13 +01:00
Command Filter and some extra APIs.
This commit is contained in:
parent
ffea04a499
commit
5872659ff2
|
@ -18,6 +18,14 @@ void myCallback(int result) {
|
||||||
DIAG(F("\n getting Loco Id callback result=%d"),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;
|
DCCEXParser serialParser;
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
|
@ -28,6 +36,7 @@ void setup() {
|
||||||
DCC::getLocoId(myCallback); // myCallback will be called with the result
|
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("\n===== DCC::getLocoId has returned, but wont be executed until we are in loop() ======\n"));
|
||||||
DIAG(F("\nReady for JMRI commands\n"));
|
DIAG(F("\nReady for JMRI commands\n"));
|
||||||
|
DCCEXParser::setFilter(myFilter);
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
|
|
8
DCC.cpp
8
DCC.cpp
|
@ -234,6 +234,14 @@ void DCC::getLocoId(ACK_CALLBACK callback) {
|
||||||
ackManagerSetup(0,0, LOCO_ID_PROG, 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() {
|
void DCC::loop() {
|
||||||
DCCWaveform::loop(); // power overload checks
|
DCCWaveform::loop(); // power overload checks
|
||||||
ackManagerLoop();
|
ackManagerLoop();
|
||||||
|
|
5
DCC.h
5
DCC.h
|
@ -51,6 +51,10 @@ class DCC {
|
||||||
|
|
||||||
static void getLocoId(ACK_CALLBACK callback);
|
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:
|
private:
|
||||||
struct LOCO {
|
struct LOCO {
|
||||||
int loco;
|
int loco;
|
||||||
|
@ -64,6 +68,7 @@ private:
|
||||||
static byte cv2(int cv);
|
static byte cv2(int cv);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// ACK MANAGER
|
// ACK MANAGER
|
||||||
static ackOp const * ackManagerProg;
|
static ackOp const * ackManagerProg;
|
||||||
static byte ackManagerByte;
|
static byte ackManagerByte;
|
||||||
|
|
|
@ -94,18 +94,26 @@ void DCCEXParser::loop(Stream & stream) {
|
||||||
return parameterCount;
|
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
|
// See documentation on DCC class for info on this section
|
||||||
void DCCEXParser::parse(Print & stream, const char *com) {
|
void DCCEXParser::parse(Print & stream, const char *com) {
|
||||||
DIAG(F("\nPARSING:%s\n"),com);
|
DIAG(F("\nPARSING:%s\n"),com);
|
||||||
(void) EEPROM; // tell compiler not to warn thi is unused
|
(void) EEPROM; // tell compiler not to warn thi is unused
|
||||||
int p[MAX_PARAMS];
|
int p[MAX_PARAMS];
|
||||||
int params=splitValues(p, com);
|
byte params=splitValues(p, com);
|
||||||
|
|
||||||
if (com[0]=='<') 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
|
// 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>
|
case 't': // THROTTLE <t REGISTER CAB SPEED DIRECTION>
|
||||||
DCC::setThrottle(p[1],p[2],p[3]);
|
DCC::setThrottle(p[1],p[2],p[3]);
|
||||||
StringFormatter::send(stream,F("<T %d %d %d>"), p[0], p[2],p[3]);
|
StringFormatter::send(stream,F("<T %d %d %d>"), p[0], p[2],p[3]);
|
||||||
|
|
|
@ -1,15 +1,20 @@
|
||||||
#ifndef DCCEXParser_h
|
#ifndef DCCEXParser_h
|
||||||
#define DCCEXParser_h
|
#define DCCEXParser_h
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
|
||||||
|
typedef void (*FILTER_CALLBACK)(Print & stream, byte & opcode, byte & paramCount, int p[]);
|
||||||
|
|
||||||
struct DCCEXParser
|
struct DCCEXParser
|
||||||
{
|
{
|
||||||
DCCEXParser();
|
DCCEXParser();
|
||||||
void loop(Stream & pstream);
|
void loop(Stream & pstream);
|
||||||
void parse(Print & stream, const char * command);
|
void parse(Print & stream, const char * command);
|
||||||
void flush();
|
void flush();
|
||||||
|
static void setFilter(FILTER_CALLBACK filter);
|
||||||
|
static const int MAX_PARAMS=10; // Must not exceed this
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
static const int MAX_PARAMS=10; // longest command sent in
|
|
||||||
static const int MAX_BUFFER=50; // longest command sent in
|
static const int MAX_BUFFER=50; // longest command sent in
|
||||||
byte bufferLength=0;
|
byte bufferLength=0;
|
||||||
bool inCommandPayload=false;
|
bool inCommandPayload=false;
|
||||||
|
@ -28,6 +33,7 @@ struct DCCEXParser
|
||||||
static void callback_W(int result);
|
static void callback_W(int result);
|
||||||
static void callback_B(int result);
|
static void callback_B(int result);
|
||||||
static void callback_R(int result);
|
static void callback_R(int result);
|
||||||
|
static FILTER_CALLBACK filterCallback;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user