From 40aab4dc7a1715378f26e16b87085e64911ab962 Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Tue, 13 May 2025 09:38:30 +0200 Subject: [PATCH] fix NO_STD issues --- Arduino/ArduinoParticipant.cpp | 12 ++++++++++++ Messages/NetworkIdMsg.cpp | 2 ++ Messages/ParticipantMsg.cpp | 6 +++++- Messages/TextMsg.cpp | 6 +++++- Participant.cpp | 34 +++++++++++++++++++++------------ Participant.h | 14 ++++++++++---- Participants/ParticipantUDP.cpp | 8 +++++++- Participants/SiteServer.cpp | 10 ++++++++++ 8 files changed, 73 insertions(+), 19 deletions(-) diff --git a/Arduino/ArduinoParticipant.cpp b/Arduino/ArduinoParticipant.cpp index eff8f1b..dad27fb 100644 --- a/Arduino/ArduinoParticipant.cpp +++ b/Arduino/ArduinoParticipant.cpp @@ -1,5 +1,9 @@ #include "ArduinoParticipant.h" +#if !defined(NO_STD) +#include +#endif + #if defined(ARDUINO) #if defined(ARDUINO_ARCH_ESP8266) #include @@ -29,7 +33,9 @@ void ParticipantUDP::Setup() { #if defined(UNO_R4) if (WiFi.status() == WL_NO_MODULE) { +#if !defined(NO_STD) std::cout << "No network available!\n"; +#endif return; } #else @@ -42,11 +48,13 @@ void ParticipantUDP::Setup() { udp = new WiFiUDP(); udp->begin(this->port); +#if !defined(NO_STD) std::cout << "Wifi sync started local " << this->port; if (this->remoteSite != nullptr) std::cout << ", remote " << this->remoteSite->ipAddress << ":" << this->remoteSite->port << "\n"; #endif +#endif } void ParticipantUDP::GetBroadcastAddress() { @@ -57,8 +65,10 @@ void ParticipantUDP::GetBroadcastAddress() { this->broadcastIpAddress = new char[broadcastIpString.length() + 1]; broadcastIpString.toCharArray(this->broadcastIpAddress, broadcastIpString.length() + 1); +#if !defined(NO_STD) std::cout << "Broadcast address: " << broadcastIpAddress << "\n"; #endif +#endif } void ParticipantUDP::Receive() { @@ -99,7 +109,9 @@ bool ParticipantUDP::Send(Participant* remoteParticipant, int bufferSize) { int n = 0; do { if (n > 0) { +#if !defined(NO_STD) std::cout << "Retry sending\n"; +#endif delay(10); } n++; diff --git a/Messages/NetworkIdMsg.cpp b/Messages/NetworkIdMsg.cpp index 40592fc..df9c9cf 100644 --- a/Messages/NetworkIdMsg.cpp +++ b/Messages/NetworkIdMsg.cpp @@ -1,5 +1,7 @@ #include "NetworkIdMsg.h" +#include + namespace RoboidControl { NetworkIdMsg::NetworkIdMsg(const char* buffer) { diff --git a/Messages/ParticipantMsg.cpp b/Messages/ParticipantMsg.cpp index c91fce9..7fb1439 100644 --- a/Messages/ParticipantMsg.cpp +++ b/Messages/ParticipantMsg.cpp @@ -1,5 +1,9 @@ #include "ParticipantMsg.h" +#if !defined(NO_STD) +#include +#endif + namespace RoboidControl { ParticipantMsg::ParticipantMsg(char networkId) { @@ -13,7 +17,7 @@ ParticipantMsg::ParticipantMsg(const char* buffer) { ParticipantMsg::~ParticipantMsg() {} unsigned char ParticipantMsg::Serialize(char* buffer) { -#if defined(DEBUG) +#if defined(DEBUG) && !defined(NO_STD) std::cout << "Send ParticipantMsg [" << (int)this->networkId << "] " << std::endl; #endif diff --git a/Messages/TextMsg.cpp b/Messages/TextMsg.cpp index 119a907..942e112 100644 --- a/Messages/TextMsg.cpp +++ b/Messages/TextMsg.cpp @@ -1,5 +1,9 @@ #include "TextMsg.h" +#if !defined(NO_STD) +#include +#endif + namespace RoboidControl { TextMsg::TextMsg(const char* text, unsigned char textLength) { @@ -24,7 +28,7 @@ unsigned char TextMsg::Serialize(char* buffer) { if (this->textLength == 0 || this->text == nullptr) return 0; -#if defined(DEBUG) +#if defined(DEBUG) && !defined(NO_STD) std::cout << "Send TextMsg " << (int)this->textLength << " " << this->text << std::endl; #endif unsigned char ix = 0; diff --git a/Participant.cpp b/Participant.cpp index 0cbe739..82a3555 100644 --- a/Participant.cpp +++ b/Participant.cpp @@ -112,7 +112,8 @@ void Participant::Remove(Thing* thing) { #pragma region ParticipantRegistry Participant* ParticipantRegistry::Get(const char* ipAddress, - unsigned int port) { + unsigned int port) { +#if !defined(NO_STD) for (Participant* participant : ParticipantRegistry::participants) { if (participant == nullptr) continue; @@ -125,10 +126,12 @@ Participant* ParticipantRegistry::Get(const char* ipAddress, } std::cout << "Could not find participant " << ipAddress << ":" << (int)port << std::endl; +#endif return nullptr; } Participant* ParticipantRegistry::Get(unsigned char participantId) { +#if !defined(NO_STD) for (Participant* participant : ParticipantRegistry::participants) { if (participant == nullptr) continue; @@ -136,11 +139,12 @@ Participant* ParticipantRegistry::Get(unsigned char participantId) { return participant; } std::cout << "Could not find participant " << (int)participantId << std::endl; +#endif return nullptr; } Participant* ParticipantRegistry::Add(const char* ipAddress, - unsigned int port) { + unsigned int port) { Participant* participant = new Participant(ipAddress, port); Add(participant); return participant; @@ -152,19 +156,19 @@ void ParticipantRegistry::Add(Participant* participant) { if (foundParticipant == nullptr) { #if defined(NO_STD) - this->things[this->thingCount++] = thing; + //this->things[this->thingCount++] = thing; #else ParticipantRegistry::participants.push_back(participant); #endif - std::cout << "Add participant " << participant->ipAddress << ":" - << participant->port << "[" << (int)participant->networkId - << "]\n"; - std::cout << "participants " << ParticipantRegistry::participants.size() - << "\n"; - } else { - std::cout << "Did not add, existing participant " << participant->ipAddress - << ":" << participant->port << "[" << (int)participant->networkId - << "]\n"; + // std::cout << "Add participant " << participant->ipAddress << ":" + // << participant->port << "[" << (int)participant->networkId + // << "]\n"; + // std::cout << "participants " << ParticipantRegistry::participants.size() + // << "\n"; + // } else { + // std::cout << "Did not add, existing participant " << participant->ipAddress + // << ":" << participant->port << "[" << (int)participant->networkId + // << "]\n"; } } @@ -172,9 +176,15 @@ void ParticipantRegistry::Remove(Participant* participant) { // participants.remove(participant); } +#if defined(NO_STD) +Participant** ParticipantRegistry::GetAll() const { + return ParticipantRegistry::participants; +} +#else const std::list& ParticipantRegistry::GetAll() const { return ParticipantRegistry::participants; } +#endif #pragma endregion ParticipantRegistry diff --git a/Participant.h b/Participant.h index 051589e..358746b 100644 --- a/Participant.h +++ b/Participant.h @@ -31,13 +31,19 @@ class ParticipantRegistry { /// @param participant The participant to remove void Remove(Participant* participant); + private: +#if defined(NO_STD) +public: + Participant** GetAll() const; + int count = 0; +private: + Participant** participants; +#else +public: /// @brief Get all participants /// @return All participants const std::list& GetAll() const; - - private: -#if defined(NO_STD) -#else +private: /// @brief The list of known participants std::list participants; #endif diff --git a/Participants/ParticipantUDP.cpp b/Participants/ParticipantUDP.cpp index 01c2b86..c6c4bb1 100644 --- a/Participants/ParticipantUDP.cpp +++ b/Participants/ParticipantUDP.cpp @@ -143,7 +143,13 @@ void ParticipantUDP::UpdateMyThings(unsigned long currentTimeMs = 0) { } void ParticipantUDP::UpdateOtherThings(unsigned long currentTimeMs = 0) { +#if defined(NO_STD) + Participant** participants = Participant::registry.GetAll(); + for (int ix = 0; ix < Participant::registry.count; ix++) { + Participant* participant = participants[ix]; +#else for (Participant* participant : Participant::registry.GetAll()) { +#endif if (participant == nullptr || participant == this) continue; @@ -441,7 +447,7 @@ void ParticipantUDP::Process(Participant* sender, ModelUrlMsg* msg) { } void ParticipantUDP::Process(Participant* sender, PoseMsg* msg) { -#if !defined(DEBUG) +#if !defined(DEBUG) && !defined(NO_STD) std::cout << this->name << ": process PoseMsg [" << (int)this->networkId << "/" << (int)msg->networkId << "] " << (int)msg->poseType << "\n"; #endif diff --git a/Participants/SiteServer.cpp b/Participants/SiteServer.cpp index fd0fed3..bfe1c2d 100644 --- a/Participants/SiteServer.cpp +++ b/Participants/SiteServer.cpp @@ -31,7 +31,13 @@ void SiteServer::UpdateMyThings(unsigned long currentTimeMs) { if (this->isIsolated == false) { // Send to all other participants +#if defined(NO_STD) + Participant** participants = Participant::registry.GetAll(); + for (int ix = 0; ix < Participant::registry.count; ix++) { + Participant* participant = participants[ix]; +#else for (Participant* participant : Participant::registry.GetAll()) { +#endif if (participant == nullptr || participant == this) continue; @@ -71,8 +77,12 @@ void SiteServer::Process(Participant* sender, ThingMsg* msg) { if (msg->parentId != 0) { thing->SetParent(Get(msg->parentId)); if (thing->GetParent() != nullptr) +#if defined(NO_STD) + ; +#else std::cout << "Could not find parent [" << (int)msg->networkId << "/" << (int)msg->parentId << "]\n"; +#endif } else thing->SetParent(nullptr); }