From 54ab547d5da7b41bcc13864addd2fb6a545efd91 Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Sat, 11 Jan 2025 14:48:50 +0100 Subject: [PATCH] We are receiving temperatures --- Participant.cpp | 6 ++--- Sensors/TemperatureSensor.cpp | 5 ++++ Sensors/TemperatureSensor.h | 1 + SiteServer.cpp | 1 + Thing.cpp | 50 +++++++++-------------------------- Thing.h | 6 ++--- 6 files changed, 23 insertions(+), 46 deletions(-) diff --git a/Participant.cpp b/Participant.cpp index e18ccb5..25339e2 100644 --- a/Participant.cpp +++ b/Participant.cpp @@ -1,8 +1,6 @@ #include "Participant.h" -// #include - -#define BUF_SIZE 1024 +#include "Thing.h" #include "UdpArduino.h" #include "UdpPosix.h" @@ -211,7 +209,7 @@ void Participant::Process(Participant *sender, NetworkIdMsg *msg) { // sender->SendThingInfo(thing); // } - for (Thing *thing : allThings) { + for (Thing *thing : Thing::allThings) { sender->SendThingInfo(thing); } } diff --git a/Sensors/TemperatureSensor.cpp b/Sensors/TemperatureSensor.cpp index abba1c2..6cc1644 100644 --- a/Sensors/TemperatureSensor.cpp +++ b/Sensors/TemperatureSensor.cpp @@ -18,5 +18,10 @@ void TemperatureSensor::SendBytes(char *buffer, unsigned char *ix) { LowLevelMessages::SendFloat16(buffer, ix, this->temp); } +void TemperatureSensor::ProcessBytes(char *bytes) { + unsigned char ix = 0; + this->temp = LowLevelMessages::ReceiveFloat16(bytes, &ix); +} + } // namespace Control } // namespace Passer \ No newline at end of file diff --git a/Sensors/TemperatureSensor.h b/Sensors/TemperatureSensor.h index 16365e8..22f8083 100644 --- a/Sensors/TemperatureSensor.h +++ b/Sensors/TemperatureSensor.h @@ -12,6 +12,7 @@ public: virtual void SetTemperature(float temp); void SendBytes(char *buffer, unsigned char *ix) override; + virtual void ProcessBytes(char *bytes) override; protected: float temp = 0; diff --git a/SiteServer.cpp b/SiteServer.cpp index 66131bc..a76bd3d 100644 --- a/SiteServer.cpp +++ b/SiteServer.cpp @@ -34,6 +34,7 @@ void SiteServer::Process(Participant *sender, ClientMsg *msg) { void SiteServer::Process(Participant *sender, NetworkIdMsg *msg) {} void SiteServer::Process(ThingMsg *msg) { + Thing *thing = Thing::Get(msg->networkId, msg->thingId); if (thing == nullptr) { switch ((Thing::Type)msg->thingType) { diff --git a/Thing.cpp b/Thing.cpp index 638c03f..0f59e10 100644 --- a/Thing.cpp +++ b/Thing.cpp @@ -46,6 +46,8 @@ Passer::Control::Thing::Thing(Participant *participant, unsigned char networkId, this->linearVelocity = Spherical16::zero; this->angularVelocity = Spherical16::zero; + std::cout << "Added thing " << (int)this->networkId << "/" << (int)this->id + << "\n"; } void Thing::Terminate() { Thing::Remove(this); } @@ -176,37 +178,19 @@ Spherical16 Thing::GetLinearVelocity() { return this->linearVelocity; } Spherical16 Thing::GetAngularVelocity() { return this->angularVelocity; } // All things -// Thing *Thing::allThings[THING_STORE_SIZE] = {nullptr}; - -// Thing **Passer::Control::Thing::GetAllThings() { return allThings; } +std::list Thing::allThings; Thing *Thing::Get(unsigned char networkId, unsigned char thingId) { - // for (uint16_t ix = 0; ix < THING_STORE_SIZE; ix++) { - // Thing *thing = allThings[ix]; - // if (thing == nullptr) - // continue; - // if (thing->networkId == networkId && thing->id == thingId) - // return thing; - // } - // return nullptr; - std::find_if(allThings.begin(), allThings.end(), - [networkId, thingId](Thing *thing) { - return thing->networkId == networkId && thing->id == thingId; - }); + + for (auto &thing : allThings) { + if (thing->networkId == networkId && thing->id == thingId) { + return thing; + } + } return nullptr; } int Thing::Add(Thing *newThing) { - // Exclude 0 because that value is reserved for 'no thing' - // for (uint16_t ix = 1; ix < THING_STORE_SIZE; ix++) { - // Thing *thing = allThings[ix]; - // if (thing == nullptr) { - // allThings[ix] = newThing; - - // std::cout << " Add new thing " << (int)ix << "\n"; - // return ix; - // } - // } for (Thing *thing : allThings) { if (thing == newThing) return thing->id; @@ -216,23 +200,13 @@ int Thing::Add(Thing *newThing) { } void Thing::Remove(Thing *thing) { - // std::cout << " remove " << (int)thing->id << "\n"; - // allThings[thing->id] = nullptr; - allThings.remove_if([thing](Thing *obj) { return obj == thing; }); + Thing::allThings.remove_if([thing](Thing *obj) { return obj == thing; }); } void Thing::UpdateAll(unsigned long currentTimeMs) { // Not very efficient, but it works for now. - // for (uint16_t ix = 0; ix < THING_STORE_SIZE; ix++) { - // Thing *thing = allThings[ix]; - // if (thing != nullptr && - // thing->parent == nullptr) { // update all root things - // // std::cout << " update " << (int)ix << " thingid " << (int)thing->id - // // << "\n"; - // thing->Update(currentTimeMs); - // } - // } - for (Thing *thing : allThings) { + + for (Thing *thing : Thing::allThings) { if (thing != nullptr && thing->parent == nullptr) { // update all root things // std::cout << " update " << (int)ix << " thingid " << (int)thing->id diff --git a/Thing.h b/Thing.h index ba2be8f..41431c2 100644 --- a/Thing.h +++ b/Thing.h @@ -129,17 +129,15 @@ protected: //------------ All things public: - // static Thing **GetAllThings(); static Thing *Get(unsigned char networkId, unsigned char thingId); static int Add(Thing *thing); static void Remove(Thing *thing); static void UpdateAll(unsigned long currentTimeMs); - // private: - // static Thing *allThings[]; + static std::list allThings; }; -static std::list allThings; +// static std::list allThings; } // namespace Control } // namespace Passer