We are receiving temperatures

This commit is contained in:
Pascal Serrarens 2025-01-11 14:48:50 +01:00
parent c092d029fc
commit 54ab547d5d
6 changed files with 23 additions and 46 deletions

View File

@ -1,8 +1,6 @@
#include "Participant.h" #include "Participant.h"
// #include <ws2tcpip.h> #include "Thing.h"
#define BUF_SIZE 1024
#include "UdpArduino.h" #include "UdpArduino.h"
#include "UdpPosix.h" #include "UdpPosix.h"
@ -211,7 +209,7 @@ void Participant::Process(Participant *sender, NetworkIdMsg *msg) {
// sender->SendThingInfo(thing); // sender->SendThingInfo(thing);
// } // }
for (Thing *thing : allThings) { for (Thing *thing : Thing::allThings) {
sender->SendThingInfo(thing); sender->SendThingInfo(thing);
} }
} }

View File

@ -18,5 +18,10 @@ void TemperatureSensor::SendBytes(char *buffer, unsigned char *ix) {
LowLevelMessages::SendFloat16(buffer, ix, this->temp); LowLevelMessages::SendFloat16(buffer, ix, this->temp);
} }
void TemperatureSensor::ProcessBytes(char *bytes) {
unsigned char ix = 0;
this->temp = LowLevelMessages::ReceiveFloat16(bytes, &ix);
}
} // namespace Control } // namespace Control
} // namespace Passer } // namespace Passer

View File

@ -12,6 +12,7 @@ public:
virtual void SetTemperature(float temp); virtual void SetTemperature(float temp);
void SendBytes(char *buffer, unsigned char *ix) override; void SendBytes(char *buffer, unsigned char *ix) override;
virtual void ProcessBytes(char *bytes) override;
protected: protected:
float temp = 0; float temp = 0;

View File

@ -34,6 +34,7 @@ void SiteServer::Process(Participant *sender, ClientMsg *msg) {
void SiteServer::Process(Participant *sender, NetworkIdMsg *msg) {} void SiteServer::Process(Participant *sender, NetworkIdMsg *msg) {}
void SiteServer::Process(ThingMsg *msg) { void SiteServer::Process(ThingMsg *msg) {
Thing *thing = Thing::Get(msg->networkId, msg->thingId); Thing *thing = Thing::Get(msg->networkId, msg->thingId);
if (thing == nullptr) { if (thing == nullptr) {
switch ((Thing::Type)msg->thingType) { switch ((Thing::Type)msg->thingType) {

View File

@ -46,6 +46,8 @@ Passer::Control::Thing::Thing(Participant *participant, unsigned char networkId,
this->linearVelocity = Spherical16::zero; this->linearVelocity = Spherical16::zero;
this->angularVelocity = Spherical16::zero; this->angularVelocity = Spherical16::zero;
std::cout << "Added thing " << (int)this->networkId << "/" << (int)this->id
<< "\n";
} }
void Thing::Terminate() { Thing::Remove(this); } void Thing::Terminate() { Thing::Remove(this); }
@ -176,37 +178,19 @@ Spherical16 Thing::GetLinearVelocity() { return this->linearVelocity; }
Spherical16 Thing::GetAngularVelocity() { return this->angularVelocity; } Spherical16 Thing::GetAngularVelocity() { return this->angularVelocity; }
// All things // All things
// Thing *Thing::allThings[THING_STORE_SIZE] = {nullptr}; std::list<Thing *> Thing::allThings;
// Thing **Passer::Control::Thing::GetAllThings() { return allThings; }
Thing *Thing::Get(unsigned char networkId, unsigned char thingId) { Thing *Thing::Get(unsigned char networkId, unsigned char thingId) {
// for (uint16_t ix = 0; ix < THING_STORE_SIZE; ix++) {
// Thing *thing = allThings[ix]; for (auto &thing : allThings) {
// if (thing == nullptr) if (thing->networkId == networkId && thing->id == thingId) {
// continue; return thing;
// 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;
});
return nullptr; return nullptr;
} }
int Thing::Add(Thing *newThing) { 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) { for (Thing *thing : allThings) {
if (thing == newThing) if (thing == newThing)
return thing->id; return thing->id;
@ -216,23 +200,13 @@ int Thing::Add(Thing *newThing) {
} }
void Thing::Remove(Thing *thing) { void Thing::Remove(Thing *thing) {
// std::cout << " remove " << (int)thing->id << "\n"; Thing::allThings.remove_if([thing](Thing *obj) { return obj == thing; });
// allThings[thing->id] = nullptr;
allThings.remove_if([thing](Thing *obj) { return obj == thing; });
} }
void Thing::UpdateAll(unsigned long currentTimeMs) { void Thing::UpdateAll(unsigned long currentTimeMs) {
// Not very efficient, but it works for now. // Not very efficient, but it works for now.
// for (uint16_t ix = 0; ix < THING_STORE_SIZE; ix++) {
// Thing *thing = allThings[ix]; for (Thing *thing : Thing::allThings) {
// 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) {
if (thing != nullptr && if (thing != nullptr &&
thing->parent == nullptr) { // update all root things thing->parent == nullptr) { // update all root things
// std::cout << " update " << (int)ix << " thingid " << (int)thing->id // std::cout << " update " << (int)ix << " thingid " << (int)thing->id

View File

@ -129,17 +129,15 @@ protected:
//------------ All things //------------ All things
public: public:
// static Thing **GetAllThings();
static Thing *Get(unsigned char networkId, unsigned char thingId); static Thing *Get(unsigned char networkId, unsigned char thingId);
static int Add(Thing *thing); static int Add(Thing *thing);
static void Remove(Thing *thing); static void Remove(Thing *thing);
static void UpdateAll(unsigned long currentTimeMs); static void UpdateAll(unsigned long currentTimeMs);
// private: static std::list<Thing *> allThings;
// static Thing *allThings[];
}; };
static std::list<Thing *> allThings; // static std::list<Thing *> allThings;
} // namespace Control } // namespace Control
} // namespace Passer } // namespace Passer