Split Application and Driver Layer

This commit is contained in:
ToSa27
2014-09-29 00:24:57 +02:00
parent c3dbcd1fea
commit 39bab3dfba
10 changed files with 200 additions and 104 deletions
+9 -7
View File
@@ -4,16 +4,18 @@
/***
* Configure Sensor Network
*/
#define COMMDRIVER_NRF24
#ifdef COMMDRIVER_NRF24
#include "MyDriverNRF24.h"
typedef class MyDriverNRF24 MyDriverClass;
#define RF24_CHANNEL 76 //RF channel for the sensor net, 0-127
#define RF24_DATARATE RF24_250KBPS //RF24_250KBPS for 250kbs, RF24_1MBPS for 1Mbps, or RF24_2MBPS for 2Mbps
#define RF24_PA_LEVEL RF24_PA_MAX //Sensor PA Level == RF24_PA_MIN=-18dBm, RF24_PA_LOW=-12dBm, RF24_PA_HIGH=-6dBM, and RF24_PA_MAX=0dBm
#define RF24_PA_LEVEL_GW RF24_PA_LOW //Gateway PA Level, defaults to Sensor net PA Level. Tune here if using an amplified nRF2401+ in your gateway.
#define BASE_RADIO_ID ((uint64_t)0xA8A8E1FC00LL) // This is also act as base value for sensor nodeId addresses. Change this (or channel) if you have more than one sensor network.
// MySensors online examples defaults
#define DEFAULT_CE_PIN 9
#define DEFAULT_CS_PIN 10
#define RF24_BASE_RADIO_ID ((uint64_t)0xA8A8E1FC00LL) // This is also act as base value for sensor nodeId addresses. Change this (or channel) if you have more than one sensor network.
#define RF24_CE_PIN 9
#define RF24_CS_PIN 10
#endif
/***
* Enable/Disable debug logging
+4
View File
@@ -0,0 +1,4 @@
#include "MyDriver.h"
MyDriver::MyDriver() {
}
+52
View File
@@ -0,0 +1,52 @@
#ifndef MyDriver_h
#define MyDriver_h
#include <stdint.h>
#ifdef DEBUG
#define debug(x,...) debugPrint(x, ##__VA_ARGS__)
#else
#define debug(x,...)
#endif
#define AUTO 0xFF // 0-254. Id 255 is reserved for auto initialization of nodeId.
#define NODE_SENSOR_ID 0xFF // Node child id is always created for when a node
// This is the nodeId for sensor net gateway receiver sketch (where all sensors should send their data).
#define GATEWAY_ADDRESS ((uint8_t)0)
#define BROADCAST_ADDRESS ((uint8_t)0xFF)
class MyDriver
{
public:
// MyDriver constructor
// different parameters would be needed depending on radio module (e.g. pins connected etc.)
// keeping these parameters as #define's in MyConfig to streamline the driver interface
MyDriver();
// init()
// different parameters would be needed depending on radio module (e.g. channel, power, crc, etc.)
// keeping these parameters as #define's in MyConfig to streamline the driver interface
virtual void init() = 0;
// setAddress(address)
// set the node address and start listening to that address (and the broadcast address)
virtual void setAddress(uint8_t address) = 0;
// getAddress()
// returns the current address from the radio driver
virtual uint8_t getAddress() = 0;
// send(to, data, len)
// reliable transmission of the data with given length (in bytes) to the destination address
// returns true if successfully submitted
virtual bool send(uint8_t to, const void* data, uint8_t len) = 0;
// available(to)
// returns true if a new packet arrived in the rx buffer
// populates "to" parameter with the address the packet was sent to (either own address or broadcast)
virtual bool available(uint8_t *to) = 0;
// receive(data)
// returns length of received packet
// populates "data" parameter with received data
virtual uint8_t receive(void* data) = 0;
// powers down the radio
virtual void powerDown() = 0;
};
#endif
+68
View File
@@ -0,0 +1,68 @@
#include "MyDriver.h"
#include "MyDriverNRF24.h"
MyDriverNRF24::MyDriverNRF24() : MyDriver() {
rf24 = new RF24(RF24_CE_PIN, RF24_CS_PIN);
}
void MyDriverNRF24::init() {
// Start up the radio library
rf24->begin();
if (!rf24->isPVariant()) {
debug(PSTR("check wires\n"));
while(1);
}
rf24->setAutoAck(1);
rf24->setAutoAck(BROADCAST_PIPE,false); // Turn off auto ack for broadcast
rf24->enableAckPayload();
rf24->setChannel(RF24_CHANNEL);
rf24->setPALevel(RF24_PA_LEVEL);
rf24->setDataRate(RF24_DATARATE);
rf24->setRetries(5,15);
rf24->setCRCLength(RF24_CRC_16);
rf24->enableDynamicPayloads();
// All nodes listen to broadcast pipe (for FIND_PARENT_RESPONSE messages)
rf24->openReadingPipe(BROADCAST_PIPE, TO_ADDR(BROADCAST_ADDRESS));
}
void MyDriverNRF24::setAddress(uint8_t address) {
_address = address;
rf24->openReadingPipe(WRITE_PIPE, TO_ADDR(address));
rf24->openReadingPipe(CURRENT_NODE_PIPE, TO_ADDR(address));
rf24->startListening();
}
uint8_t MyDriverNRF24::getAddress() {
return _address;
}
bool MyDriverNRF24::send(uint8_t to, const void* data, uint8_t len) {
// Make sure radio has powered up
rf24->powerUp();
rf24->stopListening();
rf24->openWritingPipe(TO_ADDR(to));
bool ok = rf24->write(data, len, to == BROADCAST_ADDRESS);
rf24->startListening();
return ok;
}
bool MyDriverNRF24::available(uint8_t *to) {
uint8_t pipe = 255;
boolean avail = rf24->available(&pipe);
if (pipe == CURRENT_NODE_PIPE)
*to = _address;
else if (pipe == BROADCAST_PIPE)
*to = BROADCAST_ADDRESS;
return (rf24->available() && pipe < 6);
}
uint8_t MyDriverNRF24::receive(void* data) {
uint8_t len = rf24->getDynamicPayloadSize();
rf24->read(data, len);
}
void MyDriverNRF24::powerDown() {
rf24->powerDown();
}
+32
View File
@@ -0,0 +1,32 @@
#ifndef MyCommDriver_h
#define MyCommDriver_h
#include "MyConfig.h"
#include "MyDriver.h"
#include <stdint.h>
#include "utility/RF24.h"
#include "utility/RF24_config.h"
#define TO_ADDR(x) (RF24_BASE_RADIO_ID + x)
#define WRITE_PIPE ((uint8_t)0)
#define CURRENT_NODE_PIPE ((uint8_t)1)
#define BROADCAST_PIPE ((uint8_t)2)
class MyDriverNRF24 : public MyDriver
{
public:
MyDriverNRF24();
void init();
void setAddress(uint8_t address);
uint8_t getAddress();
bool send(uint8_t to, const void* data, uint8_t len);
bool available(uint8_t *to);
uint8_t receive(void* data);
void powerDown();
private:
RF24 *rf24;
uint8_t _address;
};
#endif
+19 -49
View File
@@ -10,9 +10,6 @@
*/
#include "MySensor.h"
#include "utility/LowPower.h"
#include "utility/RF24.h"
#include "utility/RF24_config.h"
#define DISTANCE_INVALID (0xFF)
@@ -36,12 +33,12 @@ static inline bool isValidDistance( const uint8_t distance ) {
return distance != DISTANCE_INVALID;
}
MySensor::MySensor(uint8_t _cepin, uint8_t _cspin) : RF24(_cepin, _cspin) {
}
MySensor::MySensor() {
driver = (MyDriver*) new MyDriverClass();
}
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) {
void MySensor::begin(void (*_msgCallback)(const MyMessage &), uint8_t _nodeId, boolean _repeaterMode, uint8_t _parentNodeId) {
Serial.begin(BAUD_RATE);
repeaterMode = _repeaterMode;
msgCallback = _msgCallback;
@@ -52,7 +49,7 @@ void MySensor::begin(void (*_msgCallback)(const MyMessage &), uint8_t _nodeId, b
if (repeaterMode) {
setupRepeaterMode();
}
setupRadio(paLevel, channel, dataRate);
setupRadio();
// Read settings from EEPROM
eeprom_read_block((void*)&nc, (void*)EEPROM_NODE_ID_ADDRESS, sizeof(NodeConfig));
@@ -92,8 +89,7 @@ void MySensor::begin(void (*_msgCallback)(const MyMessage &), uint8_t _nodeId, b
}
// Open reading pipe for messages directed to this node (set write pipe to same)
RF24::openReadingPipe(WRITE_PIPE, TO_ADDR(nc.nodeId));
RF24::openReadingPipe(CURRENT_NODE_PIPE, TO_ADDR(nc.nodeId));
driver->setAddress(nc.nodeId);
// Send presentation for this radio node (attach
present(NODE_SENSOR_ID, repeaterMode? S_ARDUINO_REPEATER_NODE : S_ARDUINO_NODE);
@@ -107,26 +103,9 @@ void MySensor::begin(void (*_msgCallback)(const MyMessage &), uint8_t _nodeId, b
waitForReply();
}
void MySensor::setupRadio(rf24_pa_dbm_e paLevel, uint8_t channel, rf24_datarate_e dataRate) {
// Start up the radio library
RF24::begin();
if (!RF24::isPVariant()) {
debug(PSTR("check wires\n"));
while(1);
}
RF24::setAutoAck(1);
RF24::setAutoAck(BROADCAST_PIPE,false); // Turn off auto ack for broadcast
RF24::setChannel(channel);
RF24::setPALevel(paLevel);
RF24::setDataRate(dataRate);
RF24::setRetries(5,15);
RF24::setCRCLength(RF24_CRC_16);
RF24::enableDynamicPayloads();
RF24::enableDynamicAck(); // Required to disable ack-sending for broadcast messages
// All nodes listen to broadcast pipe (for FIND_PARENT_RESPONSE messages)
RF24::openReadingPipe(BROADCAST_PIPE, TO_ADDR(BROADCAST_ADDRESS));
void MySensor::setupRadio() {
failedTransmissions = 0;
driver->init();
}
void MySensor::setupRepeaterMode(){
@@ -145,7 +124,7 @@ ControllerConfig MySensor::getConfig() {
void MySensor::requestNodeId() {
debug(PSTR("req node id\n"));
RF24::openReadingPipe(CURRENT_NODE_PIPE, TO_ADDR(nc.nodeId));
driver->setAddress(nc.nodeId);
sendRoute(build(msg, nc.nodeId, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_ID_REQUEST, false).set(""));
waitForReply();
}
@@ -223,13 +202,7 @@ boolean MySensor::sendWrite(uint8_t next, MyMessage &message, const bool allowFi
uint8_t length = mGetLength(message);
message.last = nc.nodeId;
mSetVersion(message, PROTOCOL_VERSION);
// Make sure radio has powered up
RF24::powerUp();
RF24::stopListening();
RF24::openWritingPipe(TO_ADDR(next));
// Send message. Disable auto-ack for broadcasts.
ok = RF24::write(&message, min(MAX_MESSAGE_LENGTH, HEADER_SIZE + length), broadcast);
RF24::startListening();
bool ok = driver->send(next, &message, min(MAX_MESSAGE_LENGTH, HEADER_SIZE + length));
debug(PSTR("send: %d-%d-%d-%d s=%d,c=%d,t=%d,pt=%d,l=%d,st=%s:%s\n"),
message.sender,message.last, next, message.destination, message.sensor, mGetCommand(message), message.type,
@@ -290,14 +263,11 @@ void MySensor::requestTime(void (* _timeCallback)(unsigned long)) {
boolean MySensor::process() {
uint8_t pipe;
boolean available = RF24::available(&pipe);
if (!available || pipe>6)
uint8_t to = 0;
if (!driver->available(&to))
return false;
uint8_t len = RF24::getDynamicPayloadSize();
RF24::read(&msg, len);
uint8_t len = driver->receive((uint8_t *)&msg);
// Add string termination, good if we later would want to print it.
msg.data[mGetLength(msg)] = '\0';
@@ -380,7 +350,7 @@ boolean MySensor::process() {
debug(PSTR("full\n"));
while (1); // Wait here. Nothing else we can do...
} else {
RF24::openReadingPipe(CURRENT_NODE_PIPE, TO_ADDR(nc.nodeId));
driver->setAddress(nc.nodeId);
eeprom_write_byte((uint8_t*)EEPROM_NODE_ID_ADDRESS, nc.nodeId);
}
debug(PSTR("id=%d\n"), nc.nodeId);
@@ -412,7 +382,7 @@ boolean MySensor::process() {
}
// Return true if message was addressed for this node...
return true;
} else if (repeaterMode && pipe == CURRENT_NODE_PIPE) {
} else if (repeaterMode && to == nc.nodeId) {
// We should try to relay this message to another node
uint8_t route = getChildRoute(msg.destination);
@@ -511,7 +481,7 @@ void MySensor::internalSleep(unsigned long ms) {
void MySensor::sleep(unsigned long ms) {
// Let serial prints finish (debug, log etc)
Serial.flush();
RF24::powerDown();
driver->powerDown();
pinIntTrigger = 0;
internalSleep(ms);
}
@@ -520,7 +490,7 @@ bool MySensor::sleep(uint8_t interrupt, uint8_t mode, unsigned long ms) {
// Let serial prints finish (debug, log etc)
bool pinTriggeredWakeup = true;
Serial.flush();
RF24::powerDown();
driver->powerDown();
attachInterrupt(interrupt, wakeUp, mode);
if (ms>0) {
pinIntTrigger = 0;
@@ -539,7 +509,7 @@ bool MySensor::sleep(uint8_t interrupt, uint8_t mode, unsigned long ms) {
int8_t MySensor::sleep(uint8_t interrupt1, uint8_t mode1, uint8_t interrupt2, uint8_t mode2, unsigned long ms) {
int8_t retVal = 1;
Serial.flush(); // Let serial prints finish (debug, log etc)
RF24::powerDown();
driver->powerDown();
attachInterrupt(interrupt1, wakeUp, mode1);
attachInterrupt(interrupt2, wakeUp2, mode2);
if (ms>0) {
+8 -27
View File
@@ -25,8 +25,6 @@
#include <Arduino.h>
#include <SPI.h>
#include "utility/LowPower.h"
#include "utility/RF24.h"
#include "utility/RF24_config.h"
#endif
#ifdef DEBUG
@@ -37,9 +35,6 @@
#define BAUD_RATE 115200
#define AUTO 0xFF // 0-254. Id 255 is reserved for auto initialization of nodeId.
#define NODE_SENSOR_ID 0xFF // Node child id is always created for when a node
// EEPROM start address for mysensors library data
#define EEPROM_START 0
// EEPROM location of node id
@@ -56,15 +51,6 @@
#define EEPROM_FIRMWARE_CRC_ADDRESS (EEPROM_FIRMWARE_BLOCKS_ADDRESS+2)
#define EEPROM_LOCAL_CONFIG_ADDRESS (EEPROM_FIRMWARE_CRC_ADDRESS+2) // First free address for sketch static configuration
// This is the nodeId for sensor net gateway receiver sketch (where all sensors should send their data).
#define GATEWAY_ADDRESS ((uint8_t)0)
#define BROADCAST_ADDRESS ((uint8_t)0xFF)
#define TO_ADDR(x) (BASE_RADIO_ID + x)
#define WRITE_PIPE ((uint8_t)0)
#define CURRENT_NODE_PIPE ((uint8_t)1)
#define BROADCAST_PIPE ((uint8_t)2)
// Search for a new parent node after this many transmission failures
#define SEARCH_FAILURES 5
@@ -80,7 +66,7 @@ struct ControllerConfig {
};
#ifdef __cplusplus
class MySensor : public RF24
class MySensor
{
public:
/**
@@ -88,10 +74,8 @@ class MySensor : public RF24
*
* Creates a new instance of Sensor class.
*
* @param _cepin The pin attached to RF24 Chip Enable on the RF module (default 9)
* @param _cspin The pin attached to RF24 Chip Select (default 10)
*/
MySensor(uint8_t _cepin=DEFAULT_CE_PIN, uint8_t _cspin=DEFAULT_CS_PIN);
MySensor();
/**
* Begin operation of the MySensors library
@@ -101,12 +85,9 @@ class MySensor : public RF24
* @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, uint8_t parentNodeId=AUTO, 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);
/**
* Return the nodes nodeId.
@@ -141,6 +122,8 @@ class MySensor : public RF24
*/
bool send(MyMessage &msg, bool ack=false);
boolean sendRoute(MyMessage &message);
/**
* Send this nodes battery level to gateway.
* @param level Level between 0-100(%)
@@ -234,10 +217,6 @@ class MySensor : public RF24
*/
int8_t sleep(uint8_t interrupt1, uint8_t mode1, uint8_t interrupt2, uint8_t mode2, unsigned long ms=0);
boolean sendRoute(MyMessage &message);
#ifdef DEBUG
void debugPrint(const char *fmt, ... );
int freeRam();
@@ -252,8 +231,10 @@ class MySensor : public RF24
MyMessage msg; // Buffer for incoming messages.
MyMessage ack; // Buffer for ack messages.
MyDriver *driver;
void setupRepeaterMode();
void setupRadio(rf24_pa_dbm_e paLevel, uint8_t channel, rf24_datarate_e dataRate);
void setupRadio();
boolean sendWrite(uint8_t dest, MyMessage &message, const bool allowFindParent = true );
private:
@@ -58,10 +58,10 @@
// Use this if you have attached a Ethernet ENC28J60 shields
//#include <UIPEthernet.h>
#include <UIPEthernet.h>
// Use this fo WizNET module and Arduino Ethernet Shield
#include <Ethernet.h>
//#include <Ethernet.h>
#define INCLUSION_MODE_TIME 1 // Number of minutes inclusion mode is enabled
@@ -75,7 +75,7 @@
#define RADIO_TX_LED_PIN 9 // the PCB, on board LED
MySensor gw(RADIO_CE_PIN, RADIO_SPI_SS_PIN);
MySensor gw;
#define IP_PORT 5003 // The port you want to open
@@ -107,11 +107,7 @@ void output(const char *fmt, ... ) {
void setup()
{
// Initialize gateway at maximum PA level, channel 70 and callback for write operations
gw.begin(incomingMessage, 0, true, 0, RF24_PA_LEVEL_GW, RF24_CHANNEL, RF24_DATARATE);
// Setup pipes for radio library
gw.openReadingPipe(WRITE_PIPE, BASE_RADIO_ID);
gw.openReadingPipe(CURRENT_NODE_PIPE, BASE_RADIO_ID);
gw.startListening();
gw.begin(incomingMessage, 0, true, 0);
Ethernet.begin(mac, myIp);
setupGateway(RADIO_RX_LED_PIN, RADIO_TX_LED_PIN, RADIO_ERROR_LED_PIN, INCLUSION_MODE_PIN, INCLUSION_MODE_TIME, output);
@@ -89,7 +89,7 @@ uint8_t TCP_MAC[] = { 0x02, 0xDE, 0xAD, 0x00, 0x00, 0x42 }; // Mac-address - You
//////////////////////////////////////////////////////////////////
MySensor gw(RADIO_CE_PIN, RADIO_SPI_SS_PIN);
MySensor gw;
EthernetServer server = EthernetServer(TCP_PORT);
MyMessage msg;
char convBuf[MAX_PAYLOAD*2+1];
@@ -143,12 +143,7 @@ void incomingMessage(const MyMessage &message) {
void setup()
{
// Initialize gateway at maximum PA level, channel 70 and callback for write operations
gw.begin(incomingMessage, 0, true, 0, RF24_PA_LEVEL_GW, RF24_CHANNEL, RF24_DATARATE);
// Setup pipes for radio library
gw.openReadingPipe(WRITE_PIPE, BASE_RADIO_ID);
gw.openReadingPipe(CURRENT_NODE_PIPE, BASE_RADIO_ID);
gw.startListening();
gw.begin(incomingMessage, 0, true, 0);
Ethernet.begin(TCP_MAC, TCP_IP);
@@ -38,7 +38,7 @@
#define RADIO_TX_LED_PIN 5 // the PCB, on board LED
MySensor gw(DEFAULT_CE_PIN, DEFAULT_CS_PIN);
MySensor gw;
char inputString[MAX_RECEIVE_LENGTH] = ""; // A string to hold incoming commands from serial/ethernet interface
int inputPos = 0;
@@ -57,11 +57,7 @@ void output(const char *fmt, ... ) {
void setup()
{
gw.begin(incomingMessage, 0, true, 0, RF24_PA_LEVEL_GW, RF24_CHANNEL, RF24_DATARATE);
// Stetup pipes for radio library
gw.openReadingPipe(WRITE_PIPE, BASE_RADIO_ID);
gw.openReadingPipe(CURRENT_NODE_PIPE, BASE_RADIO_ID);
gw.startListening();
gw.begin(incomingMessage, 0, true, 0);
setupGateway(RADIO_RX_LED_PIN, RADIO_TX_LED_PIN, RADIO_ERROR_LED_PIN, INCLUSION_MODE_PIN, INCLUSION_MODE_TIME, output);