diff --git a/RF24Mesh.cpp b/RF24Mesh.cpp index c9ec79c..b2cbe84 100644 --- a/RF24Mesh.cpp +++ b/RF24Mesh.cpp @@ -327,12 +327,17 @@ bool ESBMesh::requestAddress(uint8_t level) network.multicast(header, 0, 0, level); uint32_t timeout = millis() + 55; -#define MESH_MAXPOLLS 4 +#ifndef MESH_MAXPOLLS + #define MESH_MAXPOLLS 4 +#endif uint16_t contactNode[MESH_MAXPOLLS]; +#if defined NRF52_RADIO_LIBRARY + bool signalArray[MESH_MAXPOLLS]; +#endif uint8_t pollCount = 0; while (millis() < timeout && pollCount < MESH_MAXPOLLS) { -#if defined(RF24MESH_DEBUG) +#if defined NRF52_RADIO_LIBRARY || defined RF24MESH_DEBUG bool goodSignal = radio.testRPD(); #endif if (network.update() == NETWORK_POLL) { @@ -349,6 +354,9 @@ bool ESBMesh::requestAddress(uint8_t level) } if (!isDupe) { contactNode[pollCount] = contact; +#if defined NRF52_RADIO_LIBRARY + signalArray[pollCount] = goodSignal; +#endif ++pollCount; IF_RF24MESH_DEBUG(printf_P(PSTR("MSH Poll %c -64dbm from 0%o \n"), (goodSignal ? '>' : '<'), contact)); } @@ -362,61 +370,73 @@ bool ESBMesh::requestAddress(uint8_t level) if (!pollCount) return 0; - for (uint8_t i = 0; i < pollCount; i++) { +#if defined NRF52_RADIO_LIBRARY + for (uint8_t h = 0; h < 2; h++) { +#endif + for (uint8_t i = 0; i < pollCount; i++) { +#if defined NRF52_RADIO_LIBRARY + // If signal is weak on first run, continue, if signal is strong on second run, continue + if ((!h && !signalArray[i]) || (h && signalArray[i])) { + continue; + } + IF_RF24MESH_DEBUG(printf_P(PSTR("Req address: %s signal\n"), signalArray[i] ? "Strong" : "Weak")); +#endif - bool gotResponse = 0; + bool gotResponse = 0; - // Request an address via the contact node - header.type = NETWORK_REQ_ADDRESS; - header.reserved = _nodeID; - header.to_node = contactNode[i]; + // Request an address via the contact node + header.type = NETWORK_REQ_ADDRESS; + header.reserved = _nodeID; + header.to_node = contactNode[i]; - // Do a direct write (no ack) to the contact node. Include the nodeId and address. - network.write(header, 0, 0, contactNode[i]); + // Do a direct write (no ack) to the contact node. Include the nodeId and address. + network.write(header, 0, 0, contactNode[i]); - IF_RF24MESH_DEBUG(printf_P(PSTR("MSH Request address from: 0%o\n"), contactNode[i])); + IF_RF24MESH_DEBUG(printf_P(PSTR("MSH Request address from: 0%o\n"), contactNode[i])); - timeout = millis() + 225; + timeout = millis() + 225; - while (millis() < timeout) { - if (network.update() == NETWORK_ADDR_RESPONSE) { - if (network.frame_buffer[7] == _nodeID) { - uint16_t newAddy = 0; - memcpy(&newAddy, &network.frame_buffer[sizeof(RF24NetworkHeader)], sizeof(newAddy)); - uint16_t mask = 0xFFFF; - newAddy &= ~(mask << (3 * getLevel(contactNode[i]))); // Get the level of contact node. Multiply by 3 to get the number of bits to shift (3 per digit) - if (newAddy == contactNode[i]) { // Then shift the mask by this much, and invert it bitwise. Apply the mask to the newly received - gotResponse = 1; // address to evalute whether 'subnet' of the assigned address matches the contact node address. - break; + while (millis() < timeout) { + if (network.update() == NETWORK_ADDR_RESPONSE) { + if (network.frame_buffer[7] == _nodeID) { + uint16_t newAddy = 0; + memcpy(&newAddy, &network.frame_buffer[sizeof(RF24NetworkHeader)], sizeof(newAddy)); + uint16_t mask = 0xFFFF; + newAddy &= ~(mask << (3 * getLevel(contactNode[i]))); // Get the level of contact node. Multiply by 3 to get the number of bits to shift (3 per digit) + if (newAddy == contactNode[i]) { // Then shift the mask by this much, and invert it bitwise. Apply the mask to the newly received + gotResponse = 1; // address to evalute whether 'subnet' of the assigned address matches the contact node address. + break; + } } } + MESH_CALLBACK } - MESH_CALLBACK - } - if (!gotResponse) { - continue; - } + if (!gotResponse) { + continue; + } - uint16_t newAddress = 0; - memcpy(&newAddress, network.frame_buffer + sizeof(RF24NetworkHeader), sizeof(newAddress)); + uint16_t newAddress = 0; + memcpy(&newAddress, network.frame_buffer + sizeof(RF24NetworkHeader), sizeof(newAddress)); - IF_RF24MESH_DEBUG(printf_P(PSTR("Set address: Current: 0%o New: 0%o\n"), mesh_address, newAddress)); - mesh_address = newAddress; + IF_RF24MESH_DEBUG(printf_P(PSTR("Set address: Current: 0%o New: 0%o\n"), mesh_address, newAddress)); + mesh_address = newAddress; - radio.stopListening(); - network.begin(mesh_address); + radio.stopListening(); + network.begin(mesh_address); - // getNodeID() doesn't use auto-ack; do a double-check to manually retry 1 more time - if (getNodeID(mesh_address) != _nodeID) { + // getNodeID() doesn't use auto-ack; do a double-check to manually retry 1 more time if (getNodeID(mesh_address) != _nodeID) { - beginDefault(); - continue; + if (getNodeID(mesh_address) != _nodeID) { + beginDefault(); + continue; + } } - } - return 1; - } // end for - + return 1; + } // end for +#if defined NRF52_RADIO_LIBRARY + } +#endif return 0; }