I have a problem, but I think its more a design flaw in the way I’m thinking/Implementing and I would like some suggestions or examples in how to do it the correct way.
my full code is over on github but below is a cut down version to see how I did what I did:
What I’m trying to do here is write UDP packets from a subclass. i.e. I have a vector of objects that I have pass a reference to a UDP object with which they can use to write their UDP commands. but when I get more than 1 object in the vector it start throwing error (works with one object).
I’m thinking I need to build some kind of UDP class with a stack that I add to then it can start sending messages if there is something on the stack. but I’m at a loss of where to start this one.
anyways, the cut down code:
#include "lifx.h"
uint16_t remotePort = 56700;
lifx LIFX;
UDP _udp;
void setup() {
IPAddress myIP = WiFi.localIP();
broadcastIP = IPAddress(myIP[0], myIP[1], myIP[2], 255);
_udp.begin(remotePort);
_udp.joinMulticast(broadcastIP);
LIFX = lifx();
LIFX.setUDP(_udp);
LIFX.setBroadcastIP(broadcastIP);
LIFX.setRemotePort(remotePort);
LIFX.togglePower();
}
---------------------------------------------------------------
#include "light.h"
class lifx{
std::vector<light> Lights;
void lifx::setUDP(UDP &udpRef)
{
_lifxUdp = udpRef;
}
void lifx::addLight(uint8_t mac[6], uint32_t port)
{
light _light = light();
Lights.push_back (_light);
Lights.back().setMAC(mac);
Lights.back().setPort(port);
Lights.back().setUDP(_lifxUdp);
Lights.back().setBroadcastIP(_broadcastIP);
Lights.back().setRemotePort(_remotePort);
}
void lifx::togglePower()
{
for(auto &Light : Lights)
{
uint16_t level = Light.getPowerLevel();
level = level > 0 ? 0 : 65535;
Light.setPower(level, 0);
}
}
}
---------------------------------------------------------------
class light{
void light::setUDP(UDP &udpRef)
{
_lightUdp = udpRef;
}
void light::setPower(uint16_t level, uint32_t duration)
{
if (WiFi.ready()) {
if(_lightUdp.sendPacket(udpPacket, sizeof(udpPacket), _broadcastIP, _lamp.port) < 0)
{
Serial.printlnf(Time.timeStr() + ":" + millis() + " - Light setPower - Error in sending UDP");
}
}
}
uint16_t light::getPowerLevel()
{
return _lamp.level;
}
}
Hopefully the above gave you and idea of how I got the UDP reference down to the light object. once again the full code can be found on github.
It would be greatly appreciated if anyone could give or point my too and example of multiplexing the send of UDP packest.
Thanks.