1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-11-30 03:26:13 +01:00

Replace FLUSH-SHOVE with broadcastReply()

This commit is contained in:
Harald Barth 2022-08-19 14:33:04 +02:00
parent 86538a4902
commit 5eb04f77a8
2 changed files with 33 additions and 47 deletions

View File

@ -32,16 +32,19 @@
#if WIFI_ON || ETHERNET_ON || defined(SERIAL1_COMMANDS) || defined(SERIAL2_COMMANDS) || defined(SERIAL3_COMMANDS) #if WIFI_ON || ETHERNET_ON || defined(SERIAL1_COMMANDS) || defined(SERIAL2_COMMANDS) || defined(SERIAL3_COMMANDS)
// use a buffer to allow broadcast // use a buffer to allow broadcast
#define BUFFER broadcastBufferWriter StringBuffer * CommandDistributor::broadcastBufferWriter=new StringBuffer();
#define FLUSH broadcastBufferWriter->flush(); template<typename... Targs> void CommandDistributor::broadcastReply(clientType type, Targs... msg){
#define SHOVE(type) broadcastToClients(type); broadcastBufferWriter->flush();
StringBuffer * CommandDistributor::broadcastBufferWriter=new StringBuffer(); StringFormatter::send(broadcastBufferWriter, msg...);
broadcastToClients(type);
}
#else #else
// on a single USB connection config, write direct to Serial and ignore flush/shove // on a single USB connection config, write direct to Serial and ignore flush/shove
#define BUFFER &Serial template<typename... Targs> void CommandDistributor::broadcastReply(clientType type, Targs... msg){
#define FLUSH (void)type; //shut up compiler warning
#define SHOVE(type) StringFormatter::send(&Serial, msg...);
}
#endif #endif
#ifdef CD_HANDLE_RING #ifdef CD_HANDLE_RING
@ -103,8 +106,9 @@ void CommandDistributor::forget(byte clientId) {
void CommandDistributor::broadcastToClients(clientType type) { void CommandDistributor::broadcastToClients(clientType type) {
byte rememberClient; byte rememberClient;
(void)rememberClient; // shut up compiler warning
/* Boadcast to Serials */ // Broadcast to Serials
if (type==COMMAND_TYPE) SerialManager::broadcast(broadcastBufferWriter->getString()); if (type==COMMAND_TYPE) SerialManager::broadcast(broadcastBufferWriter->getString());
#ifdef CD_HANDLE_RING #ifdef CD_HANDLE_RING
@ -116,7 +120,7 @@ void CommandDistributor::broadcastToClients(clientType type) {
//DIAG(F("CD precommit client %d"), rememberClient); //DIAG(F("CD precommit client %d"), rememberClient);
ring->commit(); ring->commit();
} }
/* loop through ring clients */ // loop through ring clients
for (byte clientId=0; clientId<sizeof(clients); clientId++) { for (byte clientId=0; clientId<sizeof(clients); clientId++) {
if (clients[clientId]==type) { if (clients[clientId]==type) {
//DIAG(F("CD mark client %d"), clientId); //DIAG(F("CD mark client %d"), clientId);
@ -138,32 +142,22 @@ void CommandDistributor::broadcastToClients(clientType type) {
// Public broadcast functions below // Public broadcast functions below
void CommandDistributor::broadcastSensor(int16_t id, bool on ) { void CommandDistributor::broadcastSensor(int16_t id, bool on ) {
FLUSH broadcastReply(COMMAND_TYPE, F("<%c %d>\n"), on?'Q':'q', id);
StringFormatter::send(BUFFER,F("<%c %d>\n"), on?'Q':'q', id);
SHOVE(COMMAND_TYPE)
} }
void CommandDistributor::broadcastTurnout(int16_t id, bool isClosed ) { void CommandDistributor::broadcastTurnout(int16_t id, bool isClosed ) {
// For DCC++ classic compatibility, state reported to JMRI is 1 for thrown and 0 for closed; // For DCC++ classic compatibility, state reported to JMRI is 1 for thrown and 0 for closed;
// The string below contains serial and Withrottle protocols which should // The string below contains serial and Withrottle protocols which should
// be safe for both types. // be safe for both types.
FLUSH broadcastReply(COMMAND_TYPE, F("<H %d %d>\n"),id, !isClosed);
StringFormatter::send(BUFFER,F("<H %d %d>\n"),id, !isClosed);
SHOVE(COMMAND_TYPE)
#ifdef CD_HANDLE_RING #ifdef CD_HANDLE_RING
FLUSH broadcastReply(WITHROTTLE_TYPE, F("PTA%c%d\n"), isClosed?'2':'4', id);
StringFormatter::send(BUFFER,F("PTA%c%d\n"), isClosed?'2':'4', id);
SHOVE(WITHROTTLE_TYPE)
#endif #endif
} }
void CommandDistributor::broadcastLoco(byte slot) { void CommandDistributor::broadcastLoco(byte slot) {
DCC::LOCO * sp=&DCC::speedTable[slot]; DCC::LOCO * sp=&DCC::speedTable[slot];
FLUSH broadcastReply(COMMAND_TYPE, F("<l %d %d %d %l>\n"), sp->loco,slot,sp->speedCode,sp->functions);
StringFormatter::send(BUFFER,F("<l %d %d %d %l>\n"),
sp->loco,slot,sp->speedCode,sp->functions);
SHOVE(COMMAND_TYPE)
#ifdef CD_HANDLE_RING #ifdef CD_HANDLE_RING
WiThrottle::markForBroadcast(sp->loco); WiThrottle::markForBroadcast(sp->loco);
#endif #endif
@ -180,24 +174,16 @@ void CommandDistributor::broadcastPower() {
else if (main) reason=F(" MAIN"); else if (main) reason=F(" MAIN");
else if (prog) reason=F(" PROG"); else if (prog) reason=F(" PROG");
else state='0'; else state='0';
FLUSH broadcastReply(COMMAND_TYPE, F("<p %c%S>\n"),state,reason);
StringFormatter::send(BUFFER,F("<p %c%S>\n"),state,reason);
SHOVE(COMMAND_TYPE)
#ifdef CD_HANDLE_RING #ifdef CD_HANDLE_RING
FLUSH broadcastReply(WITHROTTLE_TYPE, F("PPA%c\n"), main?'1':'0');
StringFormatter::send(BUFFER,F("PPA%c\n"), main?'1':'0');
SHOVE(WITHROTTLE_TYPE)
#endif #endif
LCD(2,F("Power %S%S"),state=='1'?F("On"):F("Off"),reason); LCD(2,F("Power %S%S"),state=='1'?F("On"):F("Off"),reason);
} }
void CommandDistributor::broadcastText(const FSH * msg) { void CommandDistributor::broadcastText(const FSH * msg) {
FLUSH broadcastReply(COMMAND_TYPE, F("<I %S>\n"),msg);
StringFormatter::send(BUFFER,F("%S"),msg);
SHOVE(COMMAND_TYPE)
#ifdef CD_HANDLE_RING #ifdef CD_HANDLE_RING
FLUSH broadcastReply(WITHROTTLE_TYPE, F("Hm%S\n"), msg);
StringFormatter::send(BUFFER,F("Hm%S\n"), msg);
SHOVE(WITHROTTLE_TYPE)
#endif #endif
} }

View File

@ -32,15 +32,6 @@
#endif #endif
class CommandDistributor { class CommandDistributor {
public :
static void parse(byte clientId,byte* buffer, RingStream * ring);
static void broadcastLoco(byte slot);
static void broadcastSensor(int16_t id, bool value);
static void broadcastTurnout(int16_t id, bool isClosed);
static void broadcastPower();
static void broadcastText(const FSH * msg);
static void forget(byte clientId);
private: private:
enum clientType: byte {NONE_TYPE,COMMAND_TYPE,WITHROTTLE_TYPE}; enum clientType: byte {NONE_TYPE,COMMAND_TYPE,WITHROTTLE_TYPE};
static void broadcastToClients(clientType type); static void broadcastToClients(clientType type);
@ -49,6 +40,15 @@ private:
static RingStream * ring; static RingStream * ring;
static clientType clients[8]; static clientType clients[8];
#endif #endif
public :
static void parse(byte clientId,byte* buffer, RingStream * ring);
static void broadcastLoco(byte slot);
static void broadcastSensor(int16_t id, bool value);
static void broadcastTurnout(int16_t id, bool isClosed);
static void broadcastPower();
static void broadcastText(const FSH * msg);
template<typename... Targs> static void broadcastReply(clientType type, Targs... msg);
static void forget(byte clientId);
}; };
#endif #endif