added some message builder helpers. Optimized libray a bit using inline functions and macros.

Converted Dallas example to use new library api.
This commit is contained in:
Henrik Ekblad
2014-05-16 10:35:26 +02:00
parent 7080520a3f
commit 9291a97d45
6 changed files with 104 additions and 48 deletions
+17
View File
@@ -4,6 +4,7 @@
MyMessage::MyMessage() {
destination = 0; // Gateway is default destination
}
@@ -60,6 +61,7 @@ uint8_t MyMessage::getByte() {
return 0;
}
}
bool MyMessage::getBool() {
return !(data[0]-'0'==0);
}
@@ -114,6 +116,21 @@ unsigned int MyMessage::getUInt() {
}
MyMessage MyMessage::setType(uint8_t _type) {
type = _type;
return *this;
}
MyMessage MyMessage::setSensor(uint8_t _sensor) {
sensor = _sensor;
return *this;
}
MyMessage MyMessage::setDestination(uint8_t _destination) {
destination = _destination;
return *this;
}
// Set payload
MyMessage MyMessage::set(void* value, uint8_t length) {
miSetPayloadType(P_CUSTOM);
+6 -2
View File
@@ -100,8 +100,8 @@ typedef enum {
// Check and fetch data from special messages
#define isTimeResponse(_msg) (mGetCommand(_msg)==C_INTERN && _msg.type==I_TIME)
#define isConfigResponse(_msg) (mGetCommand(_msg)==C_INTERN && _msg.type==I_CONFIG)
#define isTimeResponse(_msg) (mGetCommand(_msg)==C_INTERNAL && _msg.type==I_TIME)
#define isConfigResponse(_msg) (mGetCommand(_msg)==C_INTERNAL && _msg.type==I_CONFIG)
#define mIsMetric(_msg) (_msg.getByte() == 'M')
@@ -126,6 +126,10 @@ public:
int getInt();
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);
// Setters for payload
MyMessage set(void* payload, uint8_t length);
+22 -1
View File
@@ -11,6 +11,27 @@
#include "MySensor.h"
// Inline 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);
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) {
}
@@ -109,7 +130,7 @@ void MySensor::findParentNode() {
s.distance = 255;
// Send ping message to BROADCAST_ADDRESS (to which all relaying nodes and gateway listens and should reply to)
build(msg, s.nodeId, BROADCAST_ADDRESS, NODE_CHILD_ID, C_INTERNAL, I_PING)
build(msg, s.nodeId, BROADCAST_ADDRESS, NODE_CHILD_ID, C_INTERNAL, I_PING);
sendWrite(BROADCAST_ADDRESS, msg, true);
unsigned long enter = millis();
-16
View File
@@ -56,22 +56,6 @@
#define FIND_RELAY_AFTER_FAILED_TRANSMISSIONS 5
#define build(_msg, _sender, _destination, _sensor, _cmd, _type) \
_msg.sender = _sender; \
_msg.destination = _destination;\
_msg.sensor = _sensor; \
_msg.type = _type; \
mSetCommand(_msg,_cmd); \
#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) \
_msg.set(_value); \
sendRoute(_msg);
class MySensor : public RF24
{
@@ -1,61 +1,93 @@
// Example sketch showing how to send in OneWire temperature readings
#include <MySensor.h>
#include <Sleep_n0m1.h>
#include <SPI.h>
#include <EEPROM.h>
#include <DallasTemperature.h>
#include <OneWire.h>
#include <RF24.h>
#include <Sensor.h>
#define ONE_WIRE_BUS 3 // Pin where dallase sensor is connected
#define MAX_ATTACHED_DS18B20 16
unsigned long SLEEP_TIME = 30; // Sleep time between reads (in seconds)
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
Sensor gw;
MySensor gw;
Sleep sleep;
float lastTemperature[MAX_ATTACHED_DS18B20];
int numSensors=0;
boolean receivedConfig = false;
boolean metric = true;
MyMessage out;
void setup()
{
// Startup OneWire
sensors.begin();
gw.begin();
// Startup and initialize MySensors library. Set callback for incoming messages.
gw.begin(response);
// Send the sketch version information to the gateway and Controller
gw.sendSketchInfo("Temperature Sensor", "1.0");
gw.sketchInfo("Temperature Sensor", "1.0");
// Fetch the number of attached sensors
// Fetch the number of attached temperature sensors
numSensors = sensors.getDeviceCount();
// Register all sensors to gw (they will be created as child devices)
// Present all sensors to controller
for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {
gw.sendSensorPresentation(i, S_TEMP);
gw.present(i, S_TEMP);
}
metric = gw.isMetricSystem();
// Request configuration from controller (use this to determine if we're supposed to send in Ceclscius or Farenheit)
gw.requestConfig();
// Initialize outgoing message to always send temperature
out.type = V_TEMP;
}
void loop()
{
sensors.requestTemperatures(); // Fetch temperatures from Dallas
delay(100);
for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {
// Fetch and round temperature to one decimal
float temperature = static_cast<float>(static_cast<int>((metric?sensors.getTempCByIndex(i):sensors.getTempFByIndex(i)) * 10.)) / 10.;
// Only send data if temperature has changed and no error
if (lastTemperature[i] != temperature && temperature != -127.00) {
gw.powerUp(); // Powerup introduces a small delay (which is missing in radio.write powerup)
// Send variable (using registered shortcut) to gw
gw.sendVariable(i, V_TEMP, temperature, 1);
lastTemperature[i]=temperature;
// Process incoming messages
gw.process();
if (receivedConfig) {
// Fetch temperatures from Dallas sensors
sensors.requestTemperatures();
// Read temperatures and send them to controller
for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {
// Fetch and round temperature to one decimal
float temperature = static_cast<float>(static_cast<int>((metric?sensors.getTempCByIndex(i):sensors.getTempFByIndex(i)) * 10.)) / 10.;
// Only send data if temperature has changed and no error
if (lastTemperature[i] != temperature && temperature != -127.00) {
// Send in the new temperature
gw.send(out.setSensor(i).set(temperature,1));
lastTemperature[i]=temperature;
}
}
// Power down the radio and sleep Arduino to save batteries.
delay(500);
gw.powerDown();
sleep.pwrDownMode(); //set sleep mode
sleep.sleepDelay(SLEEP_TIME * 1000); //sleep for: sleepTime
} else {
// We're still missing the configuration. Wait a little bit and request it again.
delay(1000);
gw.requestConfig();
}
}
void response(MyMessage msg) {
// Check that received message is a configuration
if (isConfigResponse(msg)) {
// Read config from message
metric = mIsMetric(msg);
receivedConfig = true;
}
// Power down the radio. Note that the radio will get powered back up
// on the next write() call.
delay(500);
gw.powerDown();
sleep.pwrDownMode(); //set sleep mode
sleep.sleepDelay(SLEEP_TIME * 1000); //sleep for: sleepTime
}
@@ -1,7 +1,5 @@
// Example sketch showing how to control physical relays.
#include <MyConfig.h>
#include <MyMessage.h>
#include <MySensor.h>
#include <SPI.h>
#include <RF24.h>