Skip to content

Commit

Permalink
Allow static parent node to be set when calling begin (which also dis…
Browse files Browse the repository at this point in the history
…ables auto-parent-finding)
  • Loading branch information
henrikekblad committed Jul 3, 2014
1 parent 7b4bef5 commit 104fb4c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
15 changes: 11 additions & 4 deletions libraries/MySensors/MySensor.cpp
Expand Up @@ -31,7 +31,7 @@ MySensor::MySensor(uint8_t _cepin, uint8_t _cspin) : RF24(_cepin, _cspin) {
}


void MySensor::begin(void (*_msgCallback)(const MyMessage &), uint8_t _nodeId, boolean _repeaterMode, rf24_pa_dbm_e paLevel, uint8_t channel, rf24_datarate_e dataRate) {
void MySensor::begin(void (*_msgCallback)(const MyMessage &), uint8_t _nodeId, boolean _repeaterMode, uint8_t _parentNodeId, rf24_pa_dbm_e paLevel, uint8_t channel, rf24_datarate_e dataRate) {
Serial.begin(BAUD_RATE);
isGateway = false;
repeaterMode = _repeaterMode;
Expand All @@ -51,13 +51,20 @@ void MySensor::begin(void (*_msgCallback)(const MyMessage &), uint8_t _nodeId, b
cc.isMetric = 0x01;
}

if (_parentNodeId != AUTO) {
nc.parentNodeId = _parentNodeId;
autoFindParent = false;
} else {
autoFindParent = true;
}

if (_nodeId != AUTO) {
// Set static id
nc.nodeId = _nodeId;
}

// If no parent was found. Try to find one.
if (nc.parentNodeId == 0xff) {
// If no parent was found in eeprom. Try to find one.
if (autoFindParent && nc.parentNodeId == 0xff) {
findParentNode();
}

Expand Down Expand Up @@ -186,7 +193,7 @@ boolean MySensor::sendRoute(MyMessage &message) {
if (!ok) {
// Failure when sending to parent node. The parent node might be down and we
// need to find another route to gateway.
if (failedTransmissions > SEARCH_FAILURES) {
if (autoFindParent && failedTransmissions > SEARCH_FAILURES) {
findParentNode();
}
failedTransmissions++;
Expand Down
4 changes: 3 additions & 1 deletion libraries/MySensors/MySensor.h
Expand Up @@ -95,12 +95,13 @@ class MySensor : public RF24
* @param incomingMessageCallback Callback function for incoming messages from other nodes or controller and request responses. Default is NULL.
* @param nodeId The unique id (1-254) for this sensor. Default is AUTO(255) which means sensor tries to fetch an id from controller.
* @param repeaterMode Activate repeater mode. This node will forward messages to other nodes in the radio network. Make sure to call process() regularly. Default in false
* @param parentNodeId Use this to force node to always communicate with a certain parent node. Default is AUTO which means node automatically tries to find a parent.
* @param paLevel Radio PA Level for this sensor. Default RF24_PA_MAX
* @param channel Radio channel. Default is channel 76
* @param dataRate Radio transmission speed. Default RF24_1MBPS
*/

void begin(void (* msgCallback)(const MyMessage &)=NULL, uint8_t nodeId=AUTO, boolean repeaterMode=false, rf24_pa_dbm_e paLevel=RF24_PA_LEVEL, uint8_t channel=RF24_CHANNEL, rf24_datarate_e dataRate=RF24_DATARATE);
void begin(void (* msgCallback)(const MyMessage &)=NULL, uint8_t nodeId=AUTO, boolean repeaterMode=false, uint8_t parentNodeId=AUTO, rf24_pa_dbm_e paLevel=RF24_PA_LEVEL, uint8_t channel=RF24_CHANNEL, rf24_datarate_e dataRate=RF24_DATARATE);

/**
* Return the nodes nodeId.
Expand Down Expand Up @@ -237,6 +238,7 @@ class MySensor : public RF24
NodeConfig nc; // Essential settings for node to work
ControllerConfig cc; // Configuration coming from controller
bool repeaterMode;
bool autoFindParent;
bool isGateway;
MyMessage msg; // Buffer for incoming messages.
MyMessage ack; // Buffer for ack messages.
Expand Down

0 comments on commit 104fb4c

Please sign in to comment.