1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-11-27 01:56:14 +01:00
CommandStation-EX/CommandDistributor.cpp

144 lines
4.8 KiB
C++
Raw Normal View History

/*
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
*
* 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
#include <Arduino.h>
#include "CommandDistributor.h"
2021-12-05 13:08:59 +01:00
#include "SerialManager.h"
#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"
#include "TrackManager.h"
2021-12-05 13:08:59 +01:00
2021-12-17 21:09:38 +01:00
#if defined(BIG_MEMORY) | defined(WIFI_ON) | defined(ETHERNET_ON)
// This section of CommandDistributor is simply not relevant on a uno or similar
2022-01-06 23:03:57 +01:00
const byte NO_CLIENT=255;
2021-12-05 13:08:59 +01:00
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};
2022-01-06 23:03:57 +01:00
RingStream * CommandDistributor::broadcastBufferWriter=new RingStream(100);
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
}
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;
}
2021-12-05 13:08:59 +01:00
2022-01-06 23:03:57 +01:00
void CommandDistributor::broadcast(bool includeWithrottleClients) {
2022-01-06 23:03:57 +01:00
broadcastBufferWriter->write((byte)'\0');
2021-12-05 13:08:59 +01:00
2022-01-06 23:03:57 +01:00
/* Boadcast to Serials */
SerialManager::broadcast(broadcastBufferWriter);
2021-12-05 13:08:59 +01:00
#if defined(WIFI_ON) | defined(ETHERNET_ON)
// 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++) {
if (clients[clientId]==NONE_TYPE) continue;
if ( clients[clientId]==WITHROTTLE_TYPE && !includeWithrottleClients) continue;
ring->mark(clientId);
broadcastBufferWriter->printBuffer(ring);
ring->commit();
}
if (ringClient!=NO_CLIENT) ring->mark(ringClient);
2021-12-05 13:08:59 +01:00
#endif
broadcastBufferWriter->flush();
}
2021-12-17 21:09:38 +01:00
#else
2022-01-06 23:03:57 +01:00
// For a UNO/NANO we can broadcast direct to just one Serial instead of the ring
// Redirect ring output ditrect to Serial
#define broadcastBufferWriter &Serial
// and ignore the internal broadcast call.
void CommandDistributor::broadcast(bool includeWithrottleClients) {
(void)includeWithrottleClients;
}
2021-12-17 21:09:38 +01:00
#endif
2021-12-05 13:08:59 +01:00
void CommandDistributor::broadcastSensor(int16_t id, bool on ) {
StringFormatter::send(broadcastBufferWriter,F("<%c %d>\n"), on?'Q':'q', id);
broadcast(false);
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.
StringFormatter::send(broadcastBufferWriter,F("<H %d %d>\n"),id, !isClosed);
#if defined(WIFI_ON) | defined(ETHERNET_ON)
StringFormatter::send(broadcastBufferWriter,F("PTA%c%d\n"), isClosed?'2':'4', id);
2022-01-06 23:03:57 +01:00
#endif
broadcast(true);
2022-01-06 23:03:57 +01:00
}
void CommandDistributor::broadcastLoco(byte slot) {
DCC::LOCO * sp=&DCC::speedTable[slot];
2021-12-05 19:06:28 +01:00
StringFormatter::send(broadcastBufferWriter,F("<l %d %d %d %l>\n"),
2022-01-06 23:03:57 +01:00
sp->loco,slot,sp->speedCode,sp->functions);
broadcast(false);
#if defined(WIFI_ON) | defined(ETHERNET_ON)
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() {
bool main=TrackManager::getMainPower()==POWERMODE::ON;
bool prog=TrackManager::getProgPower()==POWERMODE::ON;
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-01-06 23:03:57 +01:00
2021-12-13 22:16:58 +01:00
StringFormatter::send(broadcastBufferWriter,
2021-12-16 13:11:38 +01:00
F("<p%c%S>\nPPA%c\n"),state,reason, main?'1':'0');
2022-01-06 23:03:57 +01:00
LCD(2,F("Power %S%S"),state=='1'?F("On"):F("Off"),reason);
broadcast(true);
2021-12-05 13:08:59 +01:00
}
void CommandDistributor::broadcastText(const FSH * msg) {
StringFormatter::send(broadcastBufferWriter,F("%S"),msg);
broadcast(false);
}