Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 1 of 1

Thread: Link data from one struct to the other

  1. #1
    Junior Member
    Join Date
    Jan 2020
    Posts
    2

    Link data from one struct to the other

    Hi All,

    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:

    1. ArtNetNode: this contains all node identification information. IP and short/long name can off course change during the program.
    2. ArtPollReplyPack: this struct has the packed argument attached to it. I use this struct later on so send the udp packet. It really is the entire package as specified in the artnet protocol specifications.
    3. ArtDmxPack: a similar to point two, but is there to prove the point that data from the node struct is used in multiple packets.


    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.

    Code:
    #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();
    }
    Last edited by Thieu; 01-16-2020 at 09:56 AM. Reason: Added board details

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •