I'm doing some changes the ArtNet repository. I want to extend it a little but to do that efficiently I had a couple of question where I don't know the answer on. Please consider the piece of code bellow. As you can see I have three structs:
Current implemantation keeps a lot of coppies from specifiic information, I refer to IP address, mac, short and long name. If one of these parameters changes i need to change it on all three locations. Which is not very efficient. My question is how can I make this efficient, so I only have to change the node.ip instead of all three location. Searches led me to pointers, which really make sense. However, I this this will mess up the structure packing since ArtPollReplyPack and ArtDmxPack are really the full data set of the package. Can any help me to overcome this? Maybe by example.
Thank you for your time!
FYI: i'm implementing with Teensyduino on a Teensy 3.2 board with Wiz8xx Ethernet port.
#include <Ethernet.h>
#include <EthernetUdp.h>
EthernetUDP Udp;
byte mac[] = {0x04, 0xE9, 0xE5, 0x00, 0x69, 0xEC};
IPAddress controllerIP(192, 168, 0, 2);
struct ArtNetNode {
IPAddress ip;
uint8_t mac[6];
uint8_t oemH;
uint8_t oemL;
uint8_t shortname[18];
uint8_t longname[64];
};
struct ArtPollReplyPack {
uint8_t id[8];
uint16_t opCode;
uint8_t ip[4];
uint8_t mac[6];
uint16_t port;
uint8_t oemH;
uint8_t oemL;
uint8_t shortname[18];
uint8_t longname[64];
}__attribute__((packed));
struct ArtDmxPack {
uint8_t id[8];
uint16_t opCode;
uint8_t ip[4];
uint16_t port;
uint8_t DmxData[512];
}__attribute__((packed));
struct ArtNetNode node;
struct ArtPollReplyPack ArtPollReply;
struct ArtDmxPack ArtDmx;
void setup() {
ArtPollReply.oemH = node.oemH;
ArtPollReply.oemL = node.oemL;
memcpy(ArtPollReply.shortname, node.shortname, sizeof(node.shortname));
memcpy(ArtPollReply.longname, node.longname, sizeof(node.longname));
memcpy(node.mac, mac, sizeof(mac));
memcpy(ArtPollReply.mac, node.mac, sizeof(node.mac));
Ethernet.begin(mac);
Udp.begin(6454);
}
void loop() {
switch(Ethernet.maintain()) {
case 1:
case 3:
// rebind / renew failed.
break;
case 2:
case 4:
// update the node IP address.
memcpy(node.ip, Ethernet.localIP(), 4);
case 0:
default:
break;
}
Udp.beginPacket(controllerIP, 6454);
Udp.write((uint8_t *)&ArtPollReply, sizeof(ArtPollReply));
Udp.endPacket();
}
There is no way to change data of two or more structures at a time. But, you can attach them with one specific structure, so that whenever the data of that specific structure or we can say Base Structure changes, it will affect all other structures. Overall, just call Base Structure in all other structures. For example, refer the code below:
struct o{
int data=3;
char name='c';
}one;
struct t{
int data=one.data;
char name=one.name;
}two;
Now, when you call structure two, it will print data from structure one.