2020-10-12 20:32:47 +02:00
|
|
|
/*
|
2022-01-07 02:28:35 +01:00
|
|
|
* © 2022 Harald Barth
|
|
|
|
* © 2020-2021 Chris Harlow
|
|
|
|
* © 2020 Gregor Baues
|
|
|
|
* All rights reserved.
|
2022-01-06 23:03:57 +01:00
|
|
|
*
|
2020-10-12 20:32:47 +02:00
|
|
|
* This file is part of CommandStation-EX
|
|
|
|
*
|
|
|
|
* This is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* It is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with CommandStation. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2021-12-05 13:08:59 +01:00
|
|
|
|
2020-10-05 19:42:31 +02:00
|
|
|
#include <Arduino.h>
|
|
|
|
#include "CommandDistributor.h"
|
2021-12-05 13:08:59 +01:00
|
|
|
#include "SerialManager.h"
|
2020-10-05 19:42:31 +02:00
|
|
|
#include "WiThrottle.h"
|
2021-12-05 13:08:59 +01:00
|
|
|
#include "DIAG.h"
|
|
|
|
#include "defines.h"
|
|
|
|
#include "DCCWaveform.h"
|
2021-12-05 19:06:28 +01:00
|
|
|
#include "DCC.h"
|
2022-02-23 16:44:34 +01:00
|
|
|
#include "TrackManager.h"
|
2021-12-05 13:08:59 +01:00
|
|
|
|
|
|
|
|
2022-07-06 18:05:34 +02:00
|
|
|
#if WIFI_ON || ETHERNET_ON || defined(SERIAL1_COMMANDS) || defined(SERIAL2_COMMANDS) || defined(SERIAL3_COMMANDS)
|
2022-06-10 13:22:28 +02:00
|
|
|
// use a buffer to allow broadcast
|
|
|
|
#define BUFFER broadcastBufferWriter
|
|
|
|
#define FLUSH broadcastBufferWriter->flush();
|
|
|
|
#define SHOVE(type) broadcastToClients(type);
|
|
|
|
StringBuffer * CommandDistributor::broadcastBufferWriter=new StringBuffer();
|
|
|
|
#else
|
2022-07-06 18:05:34 +02:00
|
|
|
// on a single USB connection config, write direct to Serial and ignore flush/shove
|
2022-06-10 13:22:28 +02:00
|
|
|
#define BUFFER &Serial
|
|
|
|
#define FLUSH
|
|
|
|
#define SHOVE(type)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CD_HANDLE_RING
|
|
|
|
// wifi or ethernet ring streams with multiple client types
|
|
|
|
RingStream * CommandDistributor::ring=0;
|
|
|
|
byte CommandDistributor::ringClient=NO_CLIENT;
|
|
|
|
CommandDistributor::clientType CommandDistributor::clients[8]={
|
|
|
|
NONE_TYPE,NONE_TYPE,NONE_TYPE,NONE_TYPE,NONE_TYPE,NONE_TYPE,NONE_TYPE,NONE_TYPE};
|
|
|
|
|
|
|
|
// Parse is called by Withrottle or Ethernet interface to determine which
|
|
|
|
// protocol the client is using and call the appropriate part of dcc++Ex
|
2021-12-05 13:08:59 +01:00
|
|
|
void CommandDistributor::parse(byte clientId,byte * buffer, RingStream * stream) {
|
|
|
|
ring=stream;
|
|
|
|
ringClient=stream->peekTargetMark();
|
|
|
|
if (buffer[0] == '<') {
|
|
|
|
clients[clientId]=COMMAND_TYPE;
|
2022-01-06 23:03:57 +01:00
|
|
|
DCCEXParser::parse(stream, buffer, ring);
|
|
|
|
} else {
|
2021-12-05 13:08:59 +01:00
|
|
|
clients[clientId]=WITHROTTLE_TYPE;
|
|
|
|
WiThrottle::getThrottle(clientId)->parse(ring, buffer);
|
2022-01-06 23:03:57 +01:00
|
|
|
}
|
|
|
|
ringClient=NO_CLIENT;
|
2021-12-05 13:08:59 +01:00
|
|
|
}
|
2020-10-05 19:42:31 +02:00
|
|
|
|
2021-12-05 13:08:59 +01:00
|
|
|
void CommandDistributor::forget(byte clientId) {
|
2022-01-06 23:03:57 +01:00
|
|
|
clients[clientId]=NONE_TYPE;
|
2020-10-05 19:42:31 +02:00
|
|
|
}
|
2022-06-10 13:22:28 +02:00
|
|
|
#endif
|
2021-12-05 13:08:59 +01:00
|
|
|
|
2022-06-10 13:22:28 +02:00
|
|
|
// This will not be called on a uno
|
|
|
|
void CommandDistributor::broadcastToClients(clientType type) {
|
2021-12-05 13:08:59 +01:00
|
|
|
|
2022-01-06 23:03:57 +01:00
|
|
|
/* Boadcast to Serials */
|
2022-06-10 13:22:28 +02:00
|
|
|
if (type==COMMAND_TYPE) SerialManager::broadcast(broadcastBufferWriter->getString());
|
2021-12-05 13:08:59 +01:00
|
|
|
|
2022-06-10 13:22:28 +02:00
|
|
|
#ifdef CD_HANDLE_RING
|
2021-12-05 13:08:59 +01:00
|
|
|
// If we are broadcasting from a wifi/eth process we need to complete its output
|
|
|
|
// before merging broadcasts in the ring, then reinstate it in case
|
2022-01-06 23:03:57 +01:00
|
|
|
// the process continues to output to its client.
|
2021-12-05 13:08:59 +01:00
|
|
|
if (ringClient!=NO_CLIENT) ring->commit();
|
|
|
|
|
|
|
|
/* loop through ring clients */
|
2022-01-06 23:03:57 +01:00
|
|
|
for (byte clientId=0; clientId<sizeof(clients); clientId++) {
|
2022-06-10 13:22:28 +02:00
|
|
|
if (clients[clientId]==type) {
|
|
|
|
ring->mark(clientId);
|
|
|
|
ring->print(broadcastBufferWriter->getString());
|
|
|
|
ring->commit();
|
|
|
|
}
|
2022-01-06 23:03:57 +01:00
|
|
|
}
|
|
|
|
if (ringClient!=NO_CLIENT) ring->mark(ringClient);
|
|
|
|
|
2021-12-05 13:08:59 +01:00
|
|
|
#endif
|
2022-01-06 23:03:57 +01:00
|
|
|
}
|
2021-12-05 13:08:59 +01:00
|
|
|
|
2022-06-10 13:22:28 +02:00
|
|
|
// Public broadcast functions below
|
2021-12-05 13:08:59 +01:00
|
|
|
void CommandDistributor::broadcastSensor(int16_t id, bool on ) {
|
2022-06-10 13:22:28 +02:00
|
|
|
FLUSH
|
|
|
|
StringFormatter::send(BUFFER,F("<%c %d>\n"), on?'Q':'q', id);
|
|
|
|
SHOVE(COMMAND_TYPE)
|
2022-01-06 23:03:57 +01:00
|
|
|
}
|
2021-12-05 13:08:59 +01:00
|
|
|
|
|
|
|
void CommandDistributor::broadcastTurnout(int16_t id, bool isClosed ) {
|
2022-01-06 23:03:57 +01:00
|
|
|
// For DCC++ classic compatibility, state reported to JMRI is 1 for thrown and 0 for closed;
|
2021-12-07 01:24:48 +01:00
|
|
|
// The string below contains serial and Withrottle protocols which should
|
2022-01-06 23:03:57 +01:00
|
|
|
// be safe for both types.
|
2022-06-10 13:22:28 +02:00
|
|
|
FLUSH
|
|
|
|
StringFormatter::send(BUFFER,F("<H %d %d>\n"),id, !isClosed);
|
|
|
|
SHOVE(COMMAND_TYPE)
|
|
|
|
|
|
|
|
#ifdef CD_HANDLE_RING
|
|
|
|
FLUSH
|
|
|
|
StringFormatter::send(BUFFER,F("PTA%c%d\n"), isClosed?'2':'4', id);
|
|
|
|
SHOVE(WITHROTTLE_TYPE)
|
2022-01-06 23:03:57 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void CommandDistributor::broadcastLoco(byte slot) {
|
|
|
|
DCC::LOCO * sp=&DCC::speedTable[slot];
|
2022-06-10 13:22:28 +02:00
|
|
|
FLUSH
|
|
|
|
StringFormatter::send(BUFFER,F("<l %d %d %d %l>\n"),
|
2022-01-06 23:03:57 +01:00
|
|
|
sp->loco,slot,sp->speedCode,sp->functions);
|
2022-06-10 13:22:28 +02:00
|
|
|
SHOVE(COMMAND_TYPE)
|
|
|
|
#ifdef CD_HANDLE_RING
|
2021-12-15 20:51:01 +01:00
|
|
|
WiThrottle::markForBroadcast(sp->loco);
|
2022-01-06 23:03:57 +01:00
|
|
|
#endif
|
2021-12-05 13:08:59 +01:00
|
|
|
}
|
2022-01-06 23:03:57 +01:00
|
|
|
|
2021-12-05 13:08:59 +01:00
|
|
|
void CommandDistributor::broadcastPower() {
|
2022-02-23 16:44:34 +01:00
|
|
|
bool main=TrackManager::getMainPower()==POWERMODE::ON;
|
|
|
|
bool prog=TrackManager::getProgPower()==POWERMODE::ON;
|
2022-03-19 12:22:31 +01:00
|
|
|
bool join=TrackManager::isJoined();
|
2021-12-16 13:11:38 +01:00
|
|
|
const FSH * reason=F("");
|
|
|
|
char state='1';
|
|
|
|
if (main && prog && join) reason=F(" JOIN");
|
|
|
|
else if (main && prog);
|
|
|
|
else if (main) reason=F(" MAIN");
|
|
|
|
else if (prog) reason=F(" PROG");
|
|
|
|
else state='0';
|
2022-06-10 13:22:28 +02:00
|
|
|
FLUSH
|
|
|
|
StringFormatter::send(BUFFER,F("<p %c%S>\n"),state,reason);
|
|
|
|
SHOVE(COMMAND_TYPE)
|
|
|
|
#ifdef CD_HANDLE_RING
|
|
|
|
FLUSH
|
|
|
|
StringFormatter::send(BUFFER,F("PPA%c\n"), main?'1':'0');
|
|
|
|
SHOVE(WITHROTTLE_TYPE)
|
|
|
|
#endif
|
|
|
|
LCD(2,F("Power %S%S"),state=='1'?F("On"):F("Off"),reason);
|
2021-12-05 13:08:59 +01:00
|
|
|
}
|
2022-03-07 17:30:47 +01:00
|
|
|
|
|
|
|
void CommandDistributor::broadcastText(const FSH * msg) {
|
2022-06-10 13:22:28 +02:00
|
|
|
FLUSH
|
|
|
|
StringFormatter::send(BUFFER,F("%S"),msg);
|
|
|
|
SHOVE(COMMAND_TYPE)
|
|
|
|
#ifdef CD_HANDLE_RING
|
|
|
|
FLUSH
|
|
|
|
StringFormatter::send(BUFFER,F("Hm%S\n"), msg);
|
|
|
|
SHOVE(WITHROTTLE_TYPE)
|
|
|
|
#endif
|
2022-03-07 17:30:47 +01:00
|
|
|
}
|