Sensor::printAll(stream);
return;
case 's': //
StringFormatter::send(stream, F(""), DCCWaveform::mainTrack.getPowerMode() == POWERMODE::ON);
StringFormatter::send(stream, F(""), F(VERSION), F(ARDUINO_TYPE), DCC::getMotorShieldName(), F(GITHUB_SHA));
Turnout::printAll(stream); //send all Turnout states
Output::printAll(stream); //send all Output states
Sensor::printAll(stream); //send all Sensor states
// TODO Send stats of speed reminders table
return;
case 'E': // STORE EPROM
EEStore::store();
StringFormatter::send(stream, F(""), EEStore::eeStore->data.nTurnouts, EEStore::eeStore->data.nSensors, EEStore::eeStore->data.nOutputs);
return;
case 'e': // CLEAR EPROM
EEStore::clear();
StringFormatter::send(stream, F(""));
return;
case ' ': // < >
StringFormatter::send(stream, F("\n"));
return;
case 'D': // < >
if (parseD(stream, params, p))
return;
return;
case '#': // NUMBER OF LOCOSLOTS <#>
StringFormatter::send(stream, F("<# %d>"), MAX_LOCOS);
return;
case 'F': // New command to call the new Loco Function API
if (Diag::CMD)
DIAG(F("Setting loco %d F%d %S"), p[0], p[1], p[2] ? F("ON") : F("OFF"));
DCC::setFn(p[0], p[1], p[2] == 1);
return;
case '+': // Complex Wifi interface command (not usual parse)
if (atCommandCallback) {
DCCWaveform::mainTrack.setPowerMode(POWERMODE::OFF);
DCCWaveform::progTrack.setPowerMode(POWERMODE::OFF);
atCommandCallback(com);
return;
}
break;
default: //anything else will diagnose and drop out to
DIAG(F("\nOpcode=%c params=%d\n"), opcode, params);
for (int i = 0; i < params; i++)
DIAG(F("p[%d]=%d (0x%x)\n"), i, p[i], p[i]);
break;
} // end of opcode switch
// Any fallout here sends an
StringFormatter::send(stream, F(""));
}
bool DCCEXParser::parseZ(Print *stream, int params, int p[])
{
switch (params)
{
case 2: //
{
Output *o = Output::get(p[0]);
if (o == NULL)
return false;
o->activate(p[1]);
StringFormatter::send(stream, F(""), p[0], p[1]);
}
return true;
case 3: //
if (!Output::create(p[0], p[1], p[2], 1))
return false;
StringFormatter::send(stream, F(""));
return true;
case 1: //
if (!Output::remove(p[0]))
return false;
StringFormatter::send(stream, F(""));
return true;
case 0: // list Output definitions
{
bool gotone = false;
for (Output *tt = Output::firstOutput; tt != NULL; tt = tt->nextOutput)
{
gotone = true;
StringFormatter::send(stream, F(""), tt->data.id, tt->data.pin, tt->data.iFlag, tt->data.oStatus);
}
return gotone;
}
default:
return false;
}
}
//===================================
bool DCCEXParser::parsef(Print *stream, int params, int p[])
{
// JMRI sends this info in DCC message format but it's not exactly
// convenient for other processing
if (params == 2)
{
byte instructionField = p[1] & 0xE0; // 1110 0000
if (instructionField == 0x80) // 1000 0000 Function group 1
{
// Shuffle bits from order F0 F4 F3 F2 F1 to F4 F3 F2 F1 F0
byte normalized = (p[1] << 1 & 0x1e) | (p[1] >> 4 & 0x01);
funcmap(p[0], normalized, 0, 4);
}
else if (instructionField == 0xA0) // 1010 0000 Function group 2
{
if (p[1] & 0x10) // 0001 0000 Bit selects F5toF8 / F9toF12
funcmap(p[0], p[1], 5, 8);
else
funcmap(p[0], p[1], 9, 12);
}
}
if (params == 3)
{
if (p[1] == 222)
funcmap(p[0], p[2], 13, 20);
else if (p[1] == 223)
funcmap(p[0], p[2], 21, 28);
}
(void)stream; // NO RESPONSE
return true;
}
void DCCEXParser::funcmap(int cab, byte value, byte fstart, byte fstop)
{
for (int i = fstart; i <= fstop; i++)
{
DCC::setFn(cab, i, value & 1);
value >>= 1;
}
}
//===================================
bool DCCEXParser::parseT(Print *stream, int params, int p[])
{
switch (params)
{
case 0: // list turnout definitions
{
bool gotOne = false;
for (Turnout *tt = Turnout::firstTurnout; tt != NULL; tt = tt->nextTurnout)
{
gotOne = true;
StringFormatter::send(stream, F(""), tt->data.id, tt->data.address,
tt->data.subAddress, (tt->data.tStatus & STATUS_ACTIVE)!=0);
}
return gotOne; // will if none found
}
case 1: // delete turnout
if (!Turnout::remove(p[0]))
return false;
StringFormatter::send(stream, F(""));
return true;
case 2: // activate turnout
{
Turnout *tt = Turnout::get(p[0]);
if (!tt)
return false;
tt->activate(p[1]);
StringFormatter::send(stream, F(""), tt->data.id, (tt->data.tStatus & STATUS_ACTIVE)!=0);
}
return true;
case 3: // define turnout
if (!Turnout::create(p[0], p[1], p[2]))
return false;
StringFormatter::send(stream, F(""));
return true;
default:
return false; // will
}
}
bool DCCEXParser::parseS(Print *stream, int params, int p[])
{
switch (params)
{
case 3: // create sensor. pullUp indicator (0=LOW/1=HIGH)
if (!Sensor::create(p[0], p[1], p[2]))
return false;
StringFormatter::send(stream, F(""));
return true;
case 1: // S id> remove sensor
if (!Sensor::remove(p[0]))
return false;
StringFormatter::send(stream, F(""));
return true;
case 0: // list sensor definitions
if (Sensor::firstSensor == NULL)
return false;
for (Sensor *tt = Sensor::firstSensor; tt != NULL; tt = tt->nextSensor)
{
StringFormatter::send(stream, F(""), tt->data.snum, tt->data.pin, tt->data.pullUp);
}
return true;
default: // invalid number of arguments
break;
}
return false;
}
bool DCCEXParser::parseD(Print *stream, int params, int p[])
{
if (params == 0)
return false;
bool onOff = (params > 0) && (p[1] == 1 || p[1] == HASH_KEYWORD_ON); // dont care if other stuff or missing... just means off
switch (p[0])
{
case HASH_KEYWORD_CABS: //
DCC::displayCabList(stream);
return true;
case HASH_KEYWORD_RAM: //
StringFormatter::send(stream, F("\nFree memory=%d\n"), freeMemory());
break;
case HASH_KEYWORD_ACK: //
if (params >= 3) {
if (p[1] == HASH_KEYWORD_LIMIT) {
DCCWaveform::progTrack.setAckLimit(p[2]);
StringFormatter::send(stream, F("\nAck limit=%dmA\n"), p[2]);
} else if (p[1] == HASH_KEYWORD_MIN) {
DCCWaveform::progTrack.setMinAckPulseDuration(p[2]);
StringFormatter::send(stream, F("\nAck min=%dus\n"), p[2]);
} else if (p[1] == HASH_KEYWORD_MAX) {
DCCWaveform::progTrack.setMaxAckPulseDuration(p[2]);
StringFormatter::send(stream, F("\nAck max=%dus\n"), p[2]);
}
} else {
StringFormatter::send(stream, F("\nAck diag %S\n"), onOff ? F("on") : F("off"));
Diag::ACK = onOff;
}
return true;
case HASH_KEYWORD_CMD: //
Diag::CMD = onOff;
return true;
case HASH_KEYWORD_WIFI: //
Diag::WIFI = onOff;
return true;
case HASH_KEYWORD_ETHERNET: //
Diag::ETHERNET = onOff;
return true;
case HASH_KEYWORD_WIT: //
Diag::WITHROTTLE = onOff;
return true;
case HASH_KEYWORD_PROGBOOST:
DCC::setProgTrackBoost(true);
return true;
case HASH_KEYWORD_EEPROM: //
if (params >= 2)
EEStore::dump(p[1]);
return true;
default: // invalid/unknown
break;
}
return false;
}
// CALLBACKS must be static
bool DCCEXParser::stashCallback(Print *stream, int p[MAX_COMMAND_PARAMS], RingStream * ringStream)
{
if (stashBusy )
return false;
stashBusy = true;
stashStream = stream;
stashRingStream=ringStream;
if (ringStream) stashTarget= ringStream->peekTargetMark();
memcpy(stashP, p, MAX_COMMAND_PARAMS * sizeof(p[0]));
return true;
}
Print * DCCEXParser::getAsyncReplyStream() {
if (stashRingStream) {
stashRingStream->mark(stashTarget);
return stashRingStream;
}
return stashStream;
}
void DCCEXParser::commitAsyncReplyStream() {
if (stashRingStream) stashRingStream->commit();
stashBusy = false;
}
void DCCEXParser::callback_W(int result)
{
StringFormatter::send(getAsyncReplyStream(),
F(""), stashP[2], stashP[3], stashP[0], result == 1 ? stashP[1] : -1);
commitAsyncReplyStream();
}
void DCCEXParser::callback_B(int result)
{
StringFormatter::send(getAsyncReplyStream(),
F(""), stashP[3], stashP[4], stashP[0], stashP[1], result == 1 ? stashP[2] : -1);
commitAsyncReplyStream();
}
void DCCEXParser::callback_Vbit(int result)
{
StringFormatter::send(getAsyncReplyStream(), F(""), stashP[0], stashP[1], result);
commitAsyncReplyStream();
}
void DCCEXParser::callback_Vbyte(int result)
{
StringFormatter::send(getAsyncReplyStream(), F(""), stashP[0], result);
commitAsyncReplyStream();
}
void DCCEXParser::callback_R(int result)
{
StringFormatter::send(getAsyncReplyStream(), F(""), stashP[1], stashP[2], stashP[0], result);
commitAsyncReplyStream();
}
void DCCEXParser::callback_Rloco(int result)
{
StringFormatter::send(getAsyncReplyStream(), F(""), result);
commitAsyncReplyStream();
}
void DCCEXParser::callback_Wloco(int result)
{
if (result==1) result=stashP[0]; // pick up original requested id from command
StringFormatter::send(getAsyncReplyStream(), F(""), result);
commitAsyncReplyStream();
}