From 40a2edc0e77093f7d02cb8a126eedd1bf8bf9329 Mon Sep 17 00:00:00 2001 From: Brendan <2bndy5@gmail.com> Date: Fri, 21 Jun 2024 15:32:19 -0700 Subject: [PATCH 1/2] move allocation of addrList to setNodeID() resolves #221 as proposed --- RF24Mesh.cpp | 17 ++++++++--------- RF24Mesh.h | 2 -- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/RF24Mesh.cpp b/RF24Mesh.cpp index be6ef20..d781ee3 100644 --- a/RF24Mesh.cpp +++ b/RF24Mesh.cpp @@ -16,7 +16,8 @@ ESBMesh::ESBMesh(radio_t& _radio, network_t& _network) : rad setCallback(NULL); meshStarted = false; #if !defined(MESH_NOMASTER) - addrMemAllocated = false; + addrList = nullptr; + addrListTop = 0; #endif } @@ -42,14 +43,6 @@ bool ESBMesh::begin(uint8_t channel, rf24_datarate_e data_ra } } else { -#if !defined(MESH_NOMASTER) - if (!addrMemAllocated) { - addrMemAllocated = true; - addrList = (addrListStruct*)malloc((MESH_MEM_ALLOC_SIZE * sizeof(addrListStruct))); - addrListTop = 0; - loadDHCP(); - } -#endif mesh_address = 0; network.begin(mesh_address); } @@ -438,6 +431,12 @@ template void ESBMesh::setNodeID(uint8_t nodeID) { _nodeID = nodeID; +#if !defined(MESH_NOMASTER) + if (!nodeID && addrList == nullptr) { + addrList = (addrListStruct*)malloc((MESH_MEM_ALLOC_SIZE * sizeof(addrListStruct))); + loadDHCP(); + } +#endif } /*****************************************************/ diff --git a/RF24Mesh.h b/RF24Mesh.h index 603adcb..1673306 100644 --- a/RF24Mesh.h +++ b/RF24Mesh.h @@ -369,8 +369,6 @@ class ESBMesh #if !defined(MESH_NOMASTER) /** Indicator that an address request is available. */ bool doDHCP; - /** Just ensures we don't re-allocate the memory buffer if restarting the mesh on master. **/ - bool addrMemAllocated; #endif /** Starts up the network layer with default address. */ From 04ca777cc0096266188de552b4b02a0d73624ba9 Mon Sep 17 00:00:00 2001 From: Brendan <2bndy5@gmail.com> Date: Fri, 21 Jun 2024 15:51:43 -0700 Subject: [PATCH 2/2] reviewed examples --- .../RF24Mesh_Example_Master_Statics.ino | 23 ++++++++++--------- .../RF24Mesh_SerialConfig.ino | 2 +- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/examples/RF24Mesh_Example_Master_Statics/RF24Mesh_Example_Master_Statics.ino b/examples/RF24Mesh_Example_Master_Statics/RF24Mesh_Example_Master_Statics.ino index e036d96..4b2d63e 100644 --- a/examples/RF24Mesh_Example_Master_Statics/RF24Mesh_Example_Master_Statics.ino +++ b/examples/RF24Mesh_Example_Master_Statics/RF24Mesh_Example_Master_Statics.ino @@ -32,16 +32,7 @@ void setup() { } // Set the nodeID to 0 for the master node - mesh.setNodeID(0); - Serial.println(mesh.getNodeID()); - // Connect to the mesh - if (!mesh.begin()) { - // if mesh.begin() returns false for a master node, then radio.begin() returned false. - Serial.println(F("Radio hardware not responding.")); - while (1) { - // hold in an infinite loop - } - } + mesh.setNodeID(0); // must be called before setStaticAddress() // In this case, assign a static address to nodeIDs 23,24 at RF24Network address 02 && 03 // This will prevent this master node from assigning the address to another node @@ -51,10 +42,20 @@ void setup() { // With this example, assign nodes 02 and 03 statically. This allows child nodes to join // the network as children of 00(master), node 02, or node 03, or as children of other // mesh nodes. If nodes 02 and 03 are placed in proximity to a group of mesh nodes, the - // mesh nodes can attatch to the network via the static nodes, and route traffic through + // mesh nodes can attach to the network via the static nodes, and route traffic through // either node, to the master node. mesh.setStaticAddress(23, 02); mesh.setStaticAddress(24, 03); + + Serial.println(mesh.getNodeID()); + // Connect to the mesh + if (!mesh.begin()) { + // if mesh.begin() returns false for a master node, then radio.begin() returned false. + Serial.println(F("Radio hardware not responding.")); + while (1) { + // hold in an infinite loop + } + } } uint32_t displayTimer = 0; diff --git a/examples/RF24Mesh_SerialConfig/RF24Mesh_SerialConfig.ino b/examples/RF24Mesh_SerialConfig/RF24Mesh_SerialConfig.ino index deb31be..1c5489d 100644 --- a/examples/RF24Mesh_SerialConfig/RF24Mesh_SerialConfig.ino +++ b/examples/RF24Mesh_SerialConfig/RF24Mesh_SerialConfig.ino @@ -41,7 +41,7 @@ void setup() { while (!mesh.getNodeID()) { // Wait for the nodeID to be set via Serial if (Serial.available()) { - mesh.setNodeID(Serial.read()); + mesh.setNodeID(Serial.parseInt() & 0xFF); Serial.print("Set NodeID: "); Serial.println(mesh.getNodeID()); }