Use pointers inernally instead of passing objects to save space.
This commit is contained in:
@@ -156,7 +156,7 @@ void MyGateway::parseAndSend(char *commandBuffer) {
|
||||
msg.type = type;
|
||||
mSetCommand(msg,command);
|
||||
msg.set(value);
|
||||
ok = sendRoute(msg);
|
||||
ok = sendRoute(&msg);
|
||||
if (!ok) {
|
||||
errBlink(1);
|
||||
}
|
||||
@@ -177,7 +177,7 @@ void MyGateway::setInclusionMode(boolean newMode) {
|
||||
|
||||
|
||||
// Override normal validate to add error blink if crc check fails
|
||||
uint8_t MyGateway::validate(MyMessage message) {
|
||||
uint8_t MyGateway::validate(MyMessage *message) {
|
||||
uint8_t res = MySensor::validate(message);
|
||||
if (res == VALIDATE_BAD_CRC) {
|
||||
errBlink(1);
|
||||
@@ -188,8 +188,8 @@ uint8_t MyGateway::validate(MyMessage message) {
|
||||
void MyGateway::processRadioMessage() {
|
||||
if (process()) {
|
||||
// A new message was received from one of the sensors
|
||||
MyMessage message = getLastMessage();
|
||||
if (mGetCommand(message) == C_PRESENTATION && inclusionMode) {
|
||||
MyMessage *message = getLastMessage();
|
||||
if (mGetCommandP(message) == C_PRESENTATION && inclusionMode) {
|
||||
rxBlink(3);
|
||||
} else {
|
||||
rxBlink(1);
|
||||
@@ -214,8 +214,8 @@ void MyGateway::serial(const char *fmt, ... ) {
|
||||
}
|
||||
}
|
||||
|
||||
void MyGateway::serial(MyMessage msg) {
|
||||
serial(PSTR("%d;%d;%d;%d;%s\n"),msg.sender, msg.sensor, mGetCommand(msg), msg.type, msg.getString(convBuf));
|
||||
void MyGateway::serial(MyMessage *msg) {
|
||||
serial(PSTR("%d;%d;%d;%d;%s\n"),msg->sender, msg->sensor, mGetCommandP(msg), msg->type, msg->getString(convBuf));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -69,8 +69,8 @@ class MyGateway : public MySensor
|
||||
uint8_t pinEr;
|
||||
|
||||
void serial(const char *fmt, ... );
|
||||
void serial(MyMessage msg);
|
||||
uint8_t validate(MyMessage message);
|
||||
void serial(MyMessage *msg);
|
||||
uint8_t validate(MyMessage *message);
|
||||
void interruptStartInclusion();
|
||||
void checkButtonTriggeredInclusion();
|
||||
void setInclusionMode(boolean newMode);
|
||||
|
||||
@@ -14,7 +14,16 @@ void* MyMessage::getCustom() {
|
||||
return (void *)data;
|
||||
}
|
||||
|
||||
/* NOTE: Calling this method might affect the payload by converting it to its string representation */
|
||||
char* MyMessage::getString() {
|
||||
uint8_t payloadType = miGetPayloadType();
|
||||
if (payloadType == P_STRING) {
|
||||
data[miGetLength()] = '\0';
|
||||
return data;
|
||||
} else {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
char* MyMessage::getString(char *buffer) {
|
||||
uint8_t payloadType = miGetPayloadType();
|
||||
|
||||
@@ -116,85 +125,85 @@ unsigned int MyMessage::getUInt() {
|
||||
}
|
||||
|
||||
|
||||
MyMessage MyMessage::setType(uint8_t _type) {
|
||||
MyMessage* MyMessage::setType(uint8_t _type) {
|
||||
type = _type;
|
||||
return *this;
|
||||
return this;
|
||||
}
|
||||
|
||||
MyMessage MyMessage::setSensor(uint8_t _sensor) {
|
||||
MyMessage* MyMessage::setSensor(uint8_t _sensor) {
|
||||
sensor = _sensor;
|
||||
return *this;
|
||||
return this;
|
||||
}
|
||||
|
||||
MyMessage MyMessage::setDestination(uint8_t _destination) {
|
||||
MyMessage* MyMessage::setDestination(uint8_t _destination) {
|
||||
destination = _destination;
|
||||
return *this;
|
||||
return this;
|
||||
}
|
||||
|
||||
// Set payload
|
||||
MyMessage MyMessage::set(void* value, uint8_t length) {
|
||||
MyMessage* MyMessage::set(void* value, uint8_t length) {
|
||||
miSetPayloadType(P_CUSTOM);
|
||||
miSetLength(length);
|
||||
memcpy(data, value, min(length, MAX_PAYLOAD));
|
||||
return *this;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
MyMessage MyMessage::set(const char* value) {
|
||||
MyMessage* MyMessage::set(const char* value) {
|
||||
uint8_t length = strlen(value);
|
||||
miSetLength(length);
|
||||
miSetPayloadType(P_STRING);
|
||||
strncpy(data, value, min(length, MAX_PAYLOAD));
|
||||
return *this;
|
||||
return this;
|
||||
}
|
||||
|
||||
MyMessage MyMessage::set(uint8_t value) {
|
||||
MyMessage* MyMessage::set(uint8_t value) {
|
||||
miSetLength(1);
|
||||
miSetPayloadType(P_BYTE);
|
||||
data[0] = value;
|
||||
return *this;
|
||||
return this;
|
||||
}
|
||||
|
||||
MyMessage MyMessage::set(bool value) {
|
||||
MyMessage* MyMessage::set(bool value) {
|
||||
miSetLength(1);
|
||||
miSetPayloadType(P_STRING);
|
||||
data[0] = value?'1':'0';
|
||||
return *this;
|
||||
return this;
|
||||
}
|
||||
|
||||
MyMessage MyMessage::set(double value, uint8_t decimals) {
|
||||
MyMessage* MyMessage::set(double value, uint8_t decimals) {
|
||||
dtostrf(value,2,decimals,data);
|
||||
miSetLength(strlen(data));
|
||||
miSetPayloadType(P_STRING);
|
||||
return *this;
|
||||
return this;
|
||||
}
|
||||
|
||||
MyMessage MyMessage::set(unsigned long value) {
|
||||
MyMessage* MyMessage::set(unsigned long value) {
|
||||
miSetPayloadType(P_ULONG32);
|
||||
miSetLength(4);
|
||||
ulValue = value;
|
||||
return *this;
|
||||
return this;
|
||||
}
|
||||
|
||||
MyMessage MyMessage::set(long value) {
|
||||
MyMessage* MyMessage::set(long value) {
|
||||
miSetPayloadType(P_LONG32);
|
||||
miSetLength(4);
|
||||
lValue = value;
|
||||
return *this;
|
||||
return this;
|
||||
}
|
||||
|
||||
MyMessage MyMessage::set(unsigned int value) {
|
||||
MyMessage* MyMessage::set(unsigned int value) {
|
||||
miSetPayloadType(P_INT16);
|
||||
miSetLength(2);
|
||||
uiValue = value;
|
||||
return *this;
|
||||
return this;
|
||||
}
|
||||
|
||||
MyMessage MyMessage::set(int value) {
|
||||
MyMessage* MyMessage::set(int value) {
|
||||
miSetPayloadType(P_UINT16);
|
||||
miSetLength(2);
|
||||
iValue = value;
|
||||
return *this;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -85,11 +85,20 @@ typedef enum {
|
||||
#define mSetLength(_msg,_length) BF_SET(_msg.version_length, _length, 3, 5)
|
||||
#define mSetCommand(_msg,_command) BF_SET(_msg.command_payload, _command, 0, 4)
|
||||
#define mSetPayloadType(_msg, _pt) BF_SET(_msg.command_payload, _pt, 4, 4)
|
||||
#define mSetVersionP(_msg,_version) BF_SET(_msg->version_length, _version, 0, 3)
|
||||
#define mSetLengthP(_msg,_length) BF_SET(_msg->version_length, _length, 3, 5)
|
||||
#define mSetCommandP(_msg,_command) BF_SET(_msg->command_payload, _command, 0, 4)
|
||||
#define mSetPayloadTypeP(_msg, _pt) BF_SET(_msg->command_payload, _pt, 4, 4)
|
||||
|
||||
#define mGetVersion(_msg) BF_GET(_msg.version_length, 0, 3)
|
||||
#define mGetLength(_msg) BF_GET(_msg.version_length, 3, 5)
|
||||
#define mGetCommand(_msg) BF_GET(_msg.command_payload, 0, 4)
|
||||
#define mGetPayloadType(_msg) BF_GET(_msg.command_payload, 4, 4)
|
||||
#define mGetVersionP(_msg) BF_GET(_msg->version_length, 0, 3)
|
||||
#define mGetLengthP(_msg) BF_GET(_msg->version_length, 3, 5)
|
||||
#define mGetCommandP(_msg) BF_GET(_msg->command_payload, 0, 4)
|
||||
#define mGetPayloadTypeP(_msg) BF_GET(_msg->command_payload, 4, 4)
|
||||
|
||||
|
||||
// internal access for special fileds
|
||||
#define miGetPayloadType() BF_GET(command_payload, 4, 4)
|
||||
@@ -105,12 +114,14 @@ public:
|
||||
// Constructors
|
||||
MyMessage();
|
||||
|
||||
|
||||
char* getString();
|
||||
/**
|
||||
* If payload is something else than P_STRING you can have payload the value converted
|
||||
* to string representation by supplying a buffer with the minimum size of
|
||||
* If payload is something else than P_STRING you can have the payload value converted
|
||||
* into string representation by supplying a buffer with the minimum size of
|
||||
* 2*MAX_PAYLOAD+1. This is to be able to fit hex-conversion of a full binary payload.
|
||||
*/
|
||||
char* getString(char *buffer = NULL);
|
||||
char* getString(char *buffer);
|
||||
void* getCustom();
|
||||
uint8_t getByte();
|
||||
bool getBool();
|
||||
@@ -121,20 +132,20 @@ public:
|
||||
unsigned int getUInt();
|
||||
|
||||
// Setters for building message "on the fly"
|
||||
MyMessage setType(uint8_t type);
|
||||
MyMessage setSensor(uint8_t sensor);
|
||||
MyMessage setDestination(uint8_t destination);
|
||||
MyMessage* setType(uint8_t type);
|
||||
MyMessage* setSensor(uint8_t sensor);
|
||||
MyMessage* setDestination(uint8_t destination);
|
||||
|
||||
// Setters for payload
|
||||
MyMessage set(void* payload, uint8_t length);
|
||||
MyMessage set(const char* value);
|
||||
MyMessage set(uint8_t value);
|
||||
MyMessage set(bool value);
|
||||
MyMessage set(double value, uint8_t decimals);
|
||||
MyMessage set(unsigned long value);
|
||||
MyMessage set(long value);
|
||||
MyMessage set(unsigned int value);
|
||||
MyMessage set(int value);
|
||||
MyMessage* set(void* payload, uint8_t length);
|
||||
MyMessage* set(const char* value);
|
||||
MyMessage* set(uint8_t value);
|
||||
MyMessage* set(bool value);
|
||||
MyMessage* set(double value, uint8_t decimals);
|
||||
MyMessage* set(unsigned long value);
|
||||
MyMessage* set(long value);
|
||||
MyMessage* set(unsigned int value);
|
||||
MyMessage* set(int value);
|
||||
|
||||
|
||||
uint8_t version_length; // 3 bit - Protocol version + 5 bit - Length of payload
|
||||
|
||||
@@ -12,25 +12,15 @@
|
||||
#include "MySensor.h"
|
||||
|
||||
// Inline function and macros
|
||||
inline MyMessage build (MyMessage msg, uint8_t sender, uint8_t destination, uint8_t sensor, uint8_t command, uint8_t type) {
|
||||
msg.sender = sender;
|
||||
msg.destination = destination;
|
||||
msg.sensor = sensor;
|
||||
msg.type = type;
|
||||
mSetCommand(msg,command);
|
||||
inline MyMessage* build (MyMessage* msg, uint8_t sender, uint8_t destination, uint8_t sensor, uint8_t command, uint8_t type) {
|
||||
msg->sender = sender;
|
||||
msg->destination = destination;
|
||||
msg->sensor = sensor;
|
||||
msg->type = type;
|
||||
mSetCommandP(msg,command);
|
||||
return msg;
|
||||
}
|
||||
|
||||
// Created macros for these, compiler seems to do a bad job when doing sendRoute(build(...))
|
||||
#define buildSend(_msg, _sender, _destination, _sensor, _cmd, _type) \
|
||||
build(_msg, _sender, _destination, _sensor, _cmd, _type);\
|
||||
sendRoute(_msg);
|
||||
|
||||
#define buildSendPayload(_msg, _sender, _destination, _sensor, _cmd, _type, _value) \
|
||||
build(_msg, _sender, _destination, _sensor, _cmd, _type).set(_value);\
|
||||
_msg.set(_value);\
|
||||
sendRoute(_msg);
|
||||
|
||||
|
||||
MySensor::MySensor(uint8_t _cepin, uint8_t _cspin) : RF24(_cepin, _cspin) {
|
||||
}
|
||||
@@ -108,7 +98,9 @@ void MySensor::begin(void (*_msgCallback)(MyMessage), boolean _relayMode, uint8_
|
||||
// Send a configuration exchange request to controller
|
||||
// Node sends parent node. Controller answers with latest node configuration
|
||||
// which is picked up in process()
|
||||
buildSendPayload(msg, nc.nodeId, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_CONFIG, nc.parentNodeId);
|
||||
sendRoute(build(&msg, nc.nodeId, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_CONFIG)->set(nc.parentNodeId));
|
||||
Serial.print("XX");
|
||||
Serial.println(nc.parentNodeId);
|
||||
}
|
||||
|
||||
|
||||
@@ -123,7 +115,7 @@ ControllerConfig MySensor::getConfig() {
|
||||
void MySensor::requestNodeId() {
|
||||
debug(PSTR("req node id\n"));
|
||||
RF24::openReadingPipe(CURRENT_NODE_PIPE, TO_ADDR(nc.nodeId));
|
||||
buildSend(msg, nc.nodeId, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_REQUEST_ID);
|
||||
sendRoute(build(&msg, nc.nodeId, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_REQUEST_ID));
|
||||
}
|
||||
|
||||
|
||||
@@ -141,8 +133,8 @@ void MySensor::findParentNode() {
|
||||
nc.distance = 255;
|
||||
|
||||
// Send ping message to BROADCAST_ADDRESS (to which all relaying nodes and gateway listens and should reply to)
|
||||
build(msg, nc.nodeId, BROADCAST_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_PING);
|
||||
sendWrite(BROADCAST_ADDRESS, msg, true);
|
||||
build(&msg, nc.nodeId, BROADCAST_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_PING);
|
||||
sendWrite(BROADCAST_ADDRESS, &msg, true);
|
||||
|
||||
unsigned long enter = millis();
|
||||
// Wait for ack responses slightly more than 1 second
|
||||
@@ -152,18 +144,18 @@ void MySensor::findParentNode() {
|
||||
|
||||
}
|
||||
|
||||
boolean MySensor::sendRoute(MyMessage message) {
|
||||
boolean MySensor::sendRoute(MyMessage *message) {
|
||||
// Make sure to process any incoming messages before sending (could this end up in recursive loop?)
|
||||
// process();
|
||||
|
||||
// If we still don't have any node id, re-request and skip this message.
|
||||
if (nc.nodeId == AUTO && !(mGetCommand(message) == C_INTERNAL && message.type == I_REQUEST_ID)) {
|
||||
if (nc.nodeId == AUTO && !(mGetCommandP(message) == C_INTERNAL && message->type == I_REQUEST_ID)) {
|
||||
requestNodeId();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (relayMode) {
|
||||
uint8_t dest = message.destination;
|
||||
uint8_t dest = message->destination;
|
||||
uint8_t route = getChildRoute(dest);
|
||||
if (route>0 && route<255 && dest != GATEWAY_ADDRESS) {
|
||||
debug(PSTR("route %d.\n"), route);
|
||||
@@ -193,61 +185,61 @@ boolean MySensor::sendRoute(MyMessage message) {
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean MySensor::sendWrite(uint8_t next, MyMessage message, bool broadcast) {
|
||||
uint8_t length = mGetLength(message);
|
||||
message.last = nc.nodeId;
|
||||
mSetVersion(message, PROTOCOL_VERSION);
|
||||
message.crc = crc8Message(message);
|
||||
boolean MySensor::sendWrite(uint8_t next, MyMessage *message, bool broadcast) {
|
||||
uint8_t length = mGetLengthP(message);
|
||||
message->last = nc.nodeId;
|
||||
mSetVersionP(message, PROTOCOL_VERSION);
|
||||
message->crc = crc8Message(message);
|
||||
|
||||
// Make sure radio has powered up
|
||||
RF24::powerUp();
|
||||
RF24::stopListening();
|
||||
RF24::openWritingPipe(TO_ADDR(next));
|
||||
bool ok = RF24::write(&message, min(MAX_MESSAGE_LENGTH, HEADER_SIZE + length), broadcast);
|
||||
bool ok = RF24::write(message, min(MAX_MESSAGE_LENGTH, HEADER_SIZE + length), broadcast);
|
||||
RF24::startListening();
|
||||
|
||||
// Only debug print payload if it is of string type
|
||||
debug(PSTR("tx: %d-%d-%d-%d s=%d,c=%d,t=%d, st=%s:%s\n"),
|
||||
message.sender,message.last, next, message.destination, message.sensor, mGetCommand(message), message.type, ok?"ok":"fail", message.getString());
|
||||
message->sender,message->last, next, message->destination, message->sensor, mGetCommandP(message), message->type, ok?"ok":"fail", message->getString());
|
||||
|
||||
return ok;
|
||||
}
|
||||
|
||||
|
||||
bool MySensor::send(MyMessage message, bool enableAck) {
|
||||
message.sender = nc.nodeId;
|
||||
bool MySensor::send(MyMessage *message, bool enableAck) {
|
||||
message->sender = nc.nodeId;
|
||||
if (enableAck) {
|
||||
mSetCommand(message,C_SET_WITH_ACK);
|
||||
mSetCommandP(message,C_SET_WITH_ACK);
|
||||
} else {
|
||||
mSetCommand(message,C_SET);
|
||||
mSetCommandP(message,C_SET);
|
||||
}
|
||||
return sendRoute(message);
|
||||
}
|
||||
|
||||
void MySensor::sendBatteryLevel(uint8_t value) {
|
||||
buildSendPayload(msg, nc.nodeId, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_BATTERY_LEVEL, value);
|
||||
sendRoute(build(&msg, nc.nodeId, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_BATTERY_LEVEL)->set(value));
|
||||
}
|
||||
|
||||
void MySensor::present(uint8_t childSensorId, uint8_t sensorType) {
|
||||
buildSendPayload(msg, nc.nodeId, GATEWAY_ADDRESS, childSensorId, C_PRESENTATION, sensorType, LIBRARY_VERSION);
|
||||
sendRoute(build(&msg, nc.nodeId, GATEWAY_ADDRESS, childSensorId, C_PRESENTATION, sensorType)->set(LIBRARY_VERSION));
|
||||
}
|
||||
|
||||
void MySensor::sendSketchInfo(const char *name, const char *version) {
|
||||
if (name != NULL) {
|
||||
buildSendPayload(msg, nc.nodeId, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_SKETCH_NAME, name);
|
||||
sendRoute(build(&msg, nc.nodeId, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_SKETCH_NAME)->set(name));
|
||||
}
|
||||
if (version != NULL) {
|
||||
buildSendPayload(msg, nc.nodeId, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_SKETCH_VERSION, version);
|
||||
sendRoute(build(&msg, nc.nodeId, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_SKETCH_VERSION)->set(version));
|
||||
}
|
||||
}
|
||||
|
||||
void MySensor::request(uint8_t childSensorId, uint8_t variableType, uint8_t destination) {
|
||||
buildSend(msg, nc.nodeId, destination, childSensorId, C_REQ, variableType);
|
||||
sendRoute(build(&msg, nc.nodeId, destination, childSensorId, C_REQ, variableType));
|
||||
}
|
||||
|
||||
void MySensor::requestTime(void (* _timeCallback)(unsigned long)) {
|
||||
timeCallback = _timeCallback;
|
||||
buildSend(msg, nc.nodeId, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_TIME);
|
||||
sendRoute(build(&msg, nc.nodeId, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_TIME));
|
||||
}
|
||||
|
||||
|
||||
@@ -262,7 +254,7 @@ boolean MySensor::process() {
|
||||
RF24::read(&msg, len);
|
||||
RF24::writeAckPayload(pipe,&pipe, 1 );
|
||||
|
||||
uint8_t valid = validate(msg);
|
||||
uint8_t valid = validate(&msg);
|
||||
boolean ok = valid == VALIDATE_OK;
|
||||
|
||||
// Make sure string gets terminated ok for full string messages.
|
||||
@@ -284,9 +276,7 @@ boolean MySensor::process() {
|
||||
// Wait a random delay of 0-2 seconds to minimize collision
|
||||
// between ping ack messages from other relaying nodes
|
||||
delay(millis() & 0x3ff);
|
||||
build(msg, nc.nodeId, sender, NODE_SENSOR_ID, C_INTERNAL, I_PING_ACK);
|
||||
msg.set(nc.distance);
|
||||
sendWrite(sender, msg);
|
||||
sendWrite(sender, build(&msg, nc.nodeId, sender, NODE_SENSOR_ID, C_INTERNAL, I_PING_ACK)->set(nc.distance));
|
||||
return false;
|
||||
} else if (destination == nc.nodeId) {
|
||||
// This message is addressed to this node
|
||||
@@ -344,7 +334,7 @@ boolean MySensor::process() {
|
||||
for (uint8_t i=0;i< sizeof(childNodeTable); i++) {
|
||||
removeChildRoute(i);
|
||||
}
|
||||
buildSendPayload(msg, nc.nodeId, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_CHILDREN,"");
|
||||
sendRoute(build(&msg, nc.nodeId, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_CHILDREN)->set(""));
|
||||
}
|
||||
break;
|
||||
case I_TIME:
|
||||
@@ -358,11 +348,12 @@ boolean MySensor::process() {
|
||||
}
|
||||
// Check if sender requests an ack back.
|
||||
if (command == C_SET_WITH_ACK) {
|
||||
// Copy message
|
||||
ack = msg;
|
||||
mSetCommand(ack,C_SET);
|
||||
ack.sender = nc.nodeId;
|
||||
ack.destination = msg.sender;
|
||||
sendRoute(ack);
|
||||
sendRoute(&ack);
|
||||
// The library user should not need to care about this ack request. Just treat it as a normal SET.
|
||||
mSetCommand(msg,C_SET);
|
||||
}
|
||||
@@ -388,7 +379,7 @@ boolean MySensor::process() {
|
||||
// We're node C, Message comes from A and has destination D
|
||||
//
|
||||
// lookup route in table and send message there
|
||||
sendWrite(route, msg);
|
||||
sendWrite(route, &msg);
|
||||
} else if (pipe == CURRENT_NODE_PIPE) {
|
||||
// A message comes from a child node and we have no
|
||||
// route for it.
|
||||
@@ -402,7 +393,7 @@ boolean MySensor::process() {
|
||||
// Message should be passed to node A (this nodes relay)
|
||||
|
||||
// This message should be routed back towards sensor net gateway
|
||||
sendWrite(nc.parentNodeId, msg);
|
||||
sendWrite(nc.parentNodeId, &msg);
|
||||
// Add this child to our "routing table" if it not already exist
|
||||
addChildRoute(sender, last);
|
||||
} else {
|
||||
@@ -441,35 +432,35 @@ boolean MySensor::process() {
|
||||
}
|
||||
|
||||
|
||||
MyMessage MySensor::getLastMessage() {
|
||||
return msg;
|
||||
MyMessage* MySensor::getLastMessage() {
|
||||
return &msg;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* calculate CRC8 on MyMessage data taking care of data structure and protocol version
|
||||
*/
|
||||
uint8_t MySensor::crc8Message(MyMessage message) {
|
||||
uint8_t len = mGetLength(message);
|
||||
uint8_t MySensor::crc8Message(MyMessage *message) {
|
||||
uint8_t len = mGetLengthP(message);
|
||||
uint8_t crc = 0x00;
|
||||
uint8_t loop_count;
|
||||
uint8_t bit_counter;
|
||||
uint8_t data;
|
||||
uint8_t feedback_bit;
|
||||
uint8_t number_of_bytes_to_read = (uint8_t)sizeof(message);
|
||||
uint8_t number_of_bytes_to_read = (uint8_t)sizeof(MyMessage);
|
||||
|
||||
// Must set crc to a constant value.
|
||||
message.crc = 0;
|
||||
message->crc = 0;
|
||||
|
||||
// fill unused space by zeroes for string data only
|
||||
if(len>=0 && len < sizeof(message.data)-1) {
|
||||
memset(&message.data[len], 0, sizeof(message.data) - 1 - len);
|
||||
if(len>=0 && len < sizeof(message->data)-1) {
|
||||
memset(&message->data[len], 0, sizeof(message->data) - 1 - len);
|
||||
}
|
||||
|
||||
|
||||
for (loop_count = 0; loop_count != number_of_bytes_to_read; loop_count++)
|
||||
{
|
||||
data = ((uint8_t*)&message)[loop_count];
|
||||
data = ((uint8_t*)message)[loop_count];
|
||||
|
||||
bit_counter = 8;
|
||||
do {
|
||||
@@ -492,11 +483,11 @@ uint8_t MySensor::crc8Message(MyMessage message) {
|
||||
}
|
||||
|
||||
|
||||
uint8_t MySensor::validate(MyMessage message) {
|
||||
uint8_t oldCrc = message.crc;
|
||||
uint8_t MySensor::validate(MyMessage *message) {
|
||||
uint8_t oldCrc = message->crc;
|
||||
uint8_t newCrc = crc8Message(message);
|
||||
|
||||
if(!(mGetVersion(message) == PROTOCOL_VERSION)) return VALIDATE_BAD_VERSION;
|
||||
if(!(mGetVersionP(message) == PROTOCOL_VERSION)) return VALIDATE_BAD_VERSION;
|
||||
|
||||
if(!(oldCrc == newCrc)) return VALIDATE_BAD_CRC;
|
||||
return VALIDATE_OK;
|
||||
|
||||
@@ -130,7 +130,7 @@ class MySensor : public RF24
|
||||
* @param ack Set this to true if you want destination node to send ack back to this node. Default is not to request any ack.
|
||||
* @return true Returns true if message reached the first stop on its way to destination.
|
||||
*/
|
||||
bool send(MyMessage msg, bool ack=false);
|
||||
bool send(MyMessage *msg, bool ack=false);
|
||||
|
||||
/**
|
||||
* Send this nodes battery level to gateway.
|
||||
@@ -191,7 +191,7 @@ class MySensor : public RF24
|
||||
/**
|
||||
* Returns the last received message
|
||||
*/
|
||||
MyMessage getLastMessage(void);
|
||||
MyMessage* getLastMessage(void);
|
||||
|
||||
|
||||
|
||||
@@ -209,9 +209,9 @@ class MySensor : public RF24
|
||||
|
||||
void setupRelayMode();
|
||||
void setupRadio(rf24_pa_dbm_e paLevel, uint8_t channel, rf24_datarate_e dataRate);
|
||||
boolean sendRoute(MyMessage message);
|
||||
boolean sendWrite(uint8_t dest, MyMessage message, bool broadcast=false);
|
||||
uint8_t validate(MyMessage message);
|
||||
boolean sendRoute(MyMessage *message);
|
||||
boolean sendWrite(uint8_t dest, MyMessage *message, bool broadcast=false);
|
||||
uint8_t validate(MyMessage *message);
|
||||
|
||||
private:
|
||||
uint8_t failedTransmissions;
|
||||
@@ -221,7 +221,7 @@ class MySensor : public RF24
|
||||
|
||||
void requestNodeId();
|
||||
void findParentNode();
|
||||
uint8_t crc8Message(MyMessage);
|
||||
uint8_t crc8Message(MyMessage *message);
|
||||
uint8_t getChildRoute(uint8_t childId);
|
||||
void addChildRoute(uint8_t childId, uint8_t route);
|
||||
void removeChildRoute(uint8_t childId);
|
||||
|
||||
@@ -61,7 +61,7 @@ void loop()
|
||||
if (lastTemperature[i] != temperature && temperature != -127.00) {
|
||||
|
||||
// Send in the new temperature
|
||||
gw.send(out.setSensor(i).set(temperature,1));
|
||||
gw.send(out.setSensor(i)->set(temperature,1));
|
||||
lastTemperature[i]=temperature;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ void setup()
|
||||
gw.sendSketchInfo("Relay", "1.0");
|
||||
|
||||
// Fetch relay status
|
||||
for (int sensor=0, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
|
||||
for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
|
||||
// Register all sensors to gw (they will be created as child devices)
|
||||
gw.present(sensor, S_LIGHT);
|
||||
// Then set relay pins in output mode
|
||||
|
||||
Reference in New Issue
Block a user