From 605d5826dc37198f2f65330abf4650af01439dca Mon Sep 17 00:00:00 2001 From: Harald Barth Date: Mon, 1 Apr 2024 11:57:57 +0200 Subject: [PATCH] make Z21 support dependent on ESP32 to compile on all platforms --- CommandDistributor.cpp | 7 +++++++ WifiESP32.cpp | 10 +++++++++- Z21Throttle.cpp | 2 ++ Z21Throttle.h | 4 ++++ config.example.h | 6 ++++++ defines.h | 8 ++++++++ 6 files changed, 36 insertions(+), 1 deletion(-) diff --git a/CommandDistributor.cpp b/CommandDistributor.cpp index 829b581..8348341 100644 --- a/CommandDistributor.cpp +++ b/CommandDistributor.cpp @@ -31,7 +31,10 @@ #include "DCC.h" #include "TrackManager.h" #include "StringFormatter.h" + +#ifdef Z21_PROTOCOL #include "Z21Throttle.h" +#endif // variables to hold clock time int16_t lastclocktime; @@ -150,7 +153,9 @@ void CommandDistributor::broadcastToClients(clientType type) { // Public broadcast functions below void CommandDistributor::broadcastSensor(int16_t id, bool on ) { +#ifdef Z21_PROTOCOL Z21Throttle::broadcastNotifySensor(id, on); +#endif broadcastReply(COMMAND_TYPE, F("<%c %d>\n"), on?'Q':'q', id); } @@ -158,7 +163,9 @@ void CommandDistributor::broadcastTurnout(int16_t id, bool isClosed ) { // 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 // be safe for both types. +#ifdef Z21_PROTOCOL Z21Throttle::broadcastNotifyTurnout(id, isClosed); +#endif broadcastReply(COMMAND_TYPE, F("\n"),id, !isClosed); #ifdef CD_HANDLE_RING broadcastReply(WITHROTTLE_TYPE, F("PTA%c%d\n"), isClosed?'2':'4', id); diff --git a/WifiESP32.cpp b/WifiESP32.cpp index 7e953b9..f2e70dc 100644 --- a/WifiESP32.cpp +++ b/WifiESP32.cpp @@ -30,7 +30,10 @@ #include "RingStream.h" #include "CommandDistributor.h" #include "WiThrottle.h" + +#ifdef Z21_PROTOCOL #include "Z21Throttle.h" +#endif /* #include "soc/rtc_wdt.h" #include "esp_task_wdt.h" @@ -258,10 +261,13 @@ bool WifiESP::setup(const char *SSid, DIAG(F("Wifi setup failed to add withrottle service to mDNS")); } + // server for WiThrottle and DCCEX protocol started here server = new WiFiServer(port); // start listening on tcp port server->begin(); - // server started here +#ifdef Z21_PROTOCOL + // server for Z21 Protocol started here Z21Throttle::setup(localIP, Z21_UDPPORT); +#endif #ifdef WIFI_TASK_ON_CORE0 //start loop task @@ -336,7 +342,9 @@ void WifiESP::loop() { } // all clients WiThrottle::loop(outboundRing); +#ifdef Z21_PROTOCOL Z21Throttle::loop(); +#endif // something to write out? clientId=outboundRing->read(); diff --git a/Z21Throttle.cpp b/Z21Throttle.cpp index 0f4afb6..395b799 100644 --- a/Z21Throttle.cpp +++ b/Z21Throttle.cpp @@ -20,6 +20,7 @@ */ #include #include "defines.h" +#ifdef Z21_PROTOCOL #include #include "Z21Throttle.h" #include "DCC.h" @@ -981,3 +982,4 @@ bool Z21Throttle::parse(byte *networkPacket, int len) { // if we get here, we did parse one or several xbus packets inside USB packets return true; } +#endif // Z21_PROTOCOL diff --git a/Z21Throttle.h b/Z21Throttle.h index 6ff450f..903e75e 100644 --- a/Z21Throttle.h +++ b/Z21Throttle.h @@ -19,6 +19,9 @@ #ifndef Z21Throttle_h #define Z21Throttle_h +#include "defines.h" +#ifdef Z21_PROTOCOL + //#include "CircularBuffer.hpp" #include "NetworkClientUDP.h" @@ -217,4 +220,5 @@ class Z21Throttle { #define LAN_X_DB0_CV_NACK 0x13 +#endif // Z21PROTOCOL #endif diff --git a/config.example.h b/config.example.h index a2e08b2..0de21d0 100644 --- a/config.example.h +++ b/config.example.h @@ -300,6 +300,12 @@ The configuration file for DCC-EX Command Station // //#define BOOSTER_INPUT 26 +// Z21 protocol support +// On ESP32 you have the possibility to use the Z21 app +// and other devices that use the Z21 protocol over IP/UDP. +// +//#define Z21_PROTOCOL + // SABERTOOTH // // This is a very special option and only useful if you happen to have a diff --git a/defines.h b/defines.h index 14dd1c5..502f1c9 100644 --- a/defines.h +++ b/defines.h @@ -157,6 +157,14 @@ #define CPU_TYPE_ERROR #endif +// Only ESP32 does support the Z21 protocol currently +#ifndef ARDUINO_ARCH_ESP32 +#ifdef Z21_PROTOCOL +#warning "Z21 protocol can not be used on this platform, disabling" +#undef Z21_PROTOCOL +#endif +#endif + // replace board type if provided by compiler #ifdef BOARD_NAME #undef ARDUINO_TYPE