Changed the lib architecture. Now you don't need to change a define in the SBNetwork_config.h to change the device type from Master to Client. You can do this in the constructor ob SBNetwork class. The code get a little bit bigger now, because the lib always compiles for both master and client device. But it is definitively better to use now.
This commit is contained in:
parent
1128d8c3c4
commit
a99d91c30b
@ -1,5 +1,5 @@
|
||||
#ifndef _SB_SENSOR_NETWORK_DEVCIE_
|
||||
#define _SB_SENSOR_NETWORK_DEVCIE_
|
||||
#ifndef _SB_NETWORK_DEVCIE_
|
||||
#define _SB_NETWORK_DEVCIE_
|
||||
|
||||
#include <EEPROM.h>
|
||||
|
||||
@ -12,8 +12,6 @@ public:
|
||||
uint32_t NetworkKey;
|
||||
};
|
||||
|
||||
#ifdef RUN_AS_MASTER
|
||||
|
||||
class SBMasterStorage{
|
||||
public:
|
||||
SBMasterStorage(){};
|
||||
@ -53,5 +51,3 @@ public:
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
@ -23,14 +23,13 @@ void printDeviceData(SBNetworkDevice &device){
|
||||
Serial.print(F("Master MAC = "));
|
||||
printAddress(device.MasterMAC.Bytes);
|
||||
Serial.println("");
|
||||
#ifdef RUN_AS_MASTER
|
||||
Serial.print(F("NetKey = "));
|
||||
Serial.print(device.NetworkKey, DEC);
|
||||
Serial.println("");
|
||||
#endif
|
||||
}
|
||||
|
||||
SBNetwork::SBNetwork(uint8_t cePin, uint8_t csPin) : radio(cePin, csPin){
|
||||
SBNetwork::SBNetwork(bool client, uint8_t cePin, uint8_t csPin) : radio(cePin, csPin){
|
||||
RunAsClient = client;
|
||||
}
|
||||
|
||||
void SBNetwork::initialize(SBMacAddress mac){
|
||||
@ -45,14 +44,14 @@ void SBNetwork::initialize(SBMacAddress mac){
|
||||
|
||||
this->initializeNetworkDevice(NetworkDevice, mac);
|
||||
|
||||
#if defined(RUN_AS_MASTER)
|
||||
this->_MasterStorage = SBMasterStorage::initialize();
|
||||
if (!this->RunAsClient) {
|
||||
this->MasterStorage = SBMasterStorage::initialize();
|
||||
for (uint8_t i = 0; i < MAX_CLIENTS; i++) {
|
||||
Serial.print("Masterstorage Slot "); Serial.print(i); Serial.print(" ");
|
||||
printAddress(_MasterStorage.Slaves[i]);
|
||||
printAddress(MasterStorage.Slaves[i]);
|
||||
Serial.println();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
Serial.print(F("Initializing NRF24L01 transmitter..."));
|
||||
this->radio.begin();
|
||||
@ -77,14 +76,14 @@ void SBNetwork::initialize(SBMacAddress mac){
|
||||
this->radio.startListening();
|
||||
Serial.println(F("Done"));
|
||||
|
||||
#ifndef RUN_AS_MASTER // In case of we defined a client device
|
||||
if (this->RunAsClient) {
|
||||
// Connect to a master
|
||||
_Connected = false;
|
||||
while (!_Connected) {
|
||||
_Connected = connectToNetwork();
|
||||
delay(500); // This can be an endless loop in case of no connection to master is available
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
void SBNetwork::initializeNetworkDevice(SBNetworkDevice &device, SBMacAddress mac){
|
||||
@ -303,8 +302,8 @@ bool SBNetwork::receiveMessage(void **message, uint8_t *messageSize, SBMacAddres
|
||||
return false;
|
||||
}
|
||||
|
||||
#ifndef RUN_AS_MASTER
|
||||
bool SBNetwork::connectToNetwork(){
|
||||
if (this->RunAsClient) {
|
||||
Serial.print(F("Try to connect to master..."));
|
||||
// First we have to check, if we already have a master stored
|
||||
if (!this->NetworkDevice.ConnectedToMaster) {
|
||||
@ -399,7 +398,10 @@ bool SBNetwork::connectToNetwork(){
|
||||
}
|
||||
return bMasterAvailable;
|
||||
}
|
||||
#endif
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool SBNetwork::pingDevice(SBMacAddress mac){
|
||||
SBNetworkHeader header;
|
||||
@ -419,12 +421,11 @@ bool SBNetwork::pingDevice(SBMacAddress mac){
|
||||
}
|
||||
|
||||
bool SBNetwork::handleCommandPackage(SBNetworkFrame *frame){
|
||||
|
||||
#if defined(RUN_AS_MASTER)
|
||||
if (!this->RunAsClient) {
|
||||
// First check, if the device is listed in the storage
|
||||
bool bFound = false;
|
||||
for (uint8_t i = 0; i < MAX_CLIENTS; i++) {
|
||||
if (this->_MasterStorage.Slaves[i].isEquals(frame->Header.FromAddress)){
|
||||
if (this->MasterStorage.Slaves[i].isEquals(frame->Header.FromAddress)) {
|
||||
_SlavePings[i] = _Uptime;
|
||||
bFound = true;
|
||||
break;
|
||||
@ -479,14 +480,14 @@ bool SBNetwork::handleCommandPackage(SBNetworkFrame *frame){
|
||||
}
|
||||
}
|
||||
return false;
|
||||
#else
|
||||
}
|
||||
else {
|
||||
return true;
|
||||
#endif
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(RUN_AS_MASTER)
|
||||
bool SBNetwork::sendMasterAck(SBMacAddress mac){
|
||||
if (!this->RunAsClient) {
|
||||
SBNetworkHeader header;
|
||||
header.ToAddress = mac;
|
||||
header.FromAddress = this->NetworkDevice.MAC;
|
||||
@ -500,9 +501,14 @@ bool SBNetwork::sendMasterAck(SBMacAddress mac){
|
||||
frame.MessageSize = sizeof(uint32_t);
|
||||
return this->sendToDevice(frame);
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool SBNetwork::sendPairingAck(SBMacAddress mac){
|
||||
if (!this->RunAsClient) {
|
||||
SBNetworkHeader header;
|
||||
header.ToAddress = mac;
|
||||
header.FromAddress = this->NetworkDevice.MAC;
|
||||
@ -517,10 +523,13 @@ bool SBNetwork::sendPairingAck(SBMacAddress mac){
|
||||
|
||||
return this->sendToDevice(frame);
|
||||
}
|
||||
#endif
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
#ifndef RUN_AS_MASTER
|
||||
bool SBNetwork::checkMaster(){
|
||||
if (this->RunAsClient) {
|
||||
if (this->pingDevice(this->NetworkDevice.MasterMAC)) {
|
||||
Serial.println("Master OK");
|
||||
return true;
|
||||
@ -530,7 +539,10 @@ bool SBNetwork::checkMaster(){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void SBNetwork::update(){
|
||||
|
||||
@ -545,7 +557,7 @@ void SBNetwork::update(){
|
||||
}
|
||||
_LastTime = millis();
|
||||
|
||||
#ifndef RUN_AS_MASTER
|
||||
if (this->RunAsClient) {
|
||||
if (NetworkDevice.ConnectedToMaster && MASTER_CHECK_INTERVAL) {
|
||||
if (_Uptime > _NextCheck) {
|
||||
// Now we have to check our sensors if they are still available
|
||||
@ -553,7 +565,7 @@ void SBNetwork::update(){
|
||||
checkMaster();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
_LastReceivedMessageSize = 0;
|
||||
_LastReceivedMessage = NULL;
|
||||
@ -564,36 +576,40 @@ void SBNetwork::update(){
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(RUN_AS_MASTER)
|
||||
uint8_t SBNetwork::addMac(SBMacAddress mac){
|
||||
|
||||
if (!this->RunAsClient) {
|
||||
// iterate through the storage and look if the mac already exists
|
||||
uint8_t iPos;
|
||||
for (iPos = 0; iPos < MAX_CLIENTS; iPos++) {
|
||||
if (_MasterStorage.Slaves[iPos].isEquals(mac)){
|
||||
if (MasterStorage.Slaves[iPos].isEquals(mac)) {
|
||||
return iPos;
|
||||
}
|
||||
}
|
||||
// Search the first free place and add the mac
|
||||
for (iPos = 0; iPos < MAX_CLIENTS; iPos++) {
|
||||
if (_MasterStorage.Slaves[iPos].isEquals(EMPTY_MAC)){
|
||||
_MasterStorage.Slaves[iPos] = mac;
|
||||
_MasterStorage.save();
|
||||
if (MasterStorage.Slaves[iPos].isEquals(EMPTY_MAC)) {
|
||||
MasterStorage.Slaves[iPos] = mac;
|
||||
MasterStorage.save();
|
||||
return iPos;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t SBNetwork::removeMac(SBMacAddress mac){
|
||||
if (!this->RunAsClient) {
|
||||
// iterate through the storage and look if the mac is in the list, if not, then return -1. If yes, remove it.
|
||||
for (uint8_t iPos = 0; iPos < MAX_CLIENTS; iPos++) {
|
||||
if (_MasterStorage.Slaves[iPos].isEquals(mac)){
|
||||
_MasterStorage.Slaves[iPos] = EMPTY_MAC;
|
||||
_MasterStorage.save();
|
||||
if (MasterStorage.Slaves[iPos].isEquals(mac)) {
|
||||
MasterStorage.Slaves[iPos] = EMPTY_MAC;
|
||||
MasterStorage.save();
|
||||
return iPos;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
@ -14,7 +14,6 @@
|
||||
|
||||
class SBNetwork{
|
||||
private:
|
||||
|
||||
/*
|
||||
* Stores the uptime of the device
|
||||
*/
|
||||
@ -27,12 +26,10 @@ class SBNetwork{
|
||||
* Stores the time when the device should ping to the master
|
||||
*/
|
||||
unsigned long _NextCheck;
|
||||
|
||||
/*
|
||||
* Stores the connection state to a master device in case of it is a client device
|
||||
*/
|
||||
bool _Connected;
|
||||
|
||||
/**
|
||||
* Here the payload will be stored for each frame receive
|
||||
*/
|
||||
@ -41,15 +38,11 @@ class SBNetwork{
|
||||
* Here the received message is stored after a full message receive (fragmented or not fragmented)
|
||||
*/
|
||||
uint8_t _ReadBuffer[MAX_FRAME_SIZE];
|
||||
|
||||
#if defined(RUN_AS_MASTER)
|
||||
/*
|
||||
* Store the times, when the slaves sent the last signal to the master
|
||||
*/
|
||||
unsigned long long _SlavePings[MAX_CLIENTS];
|
||||
#endif
|
||||
|
||||
/**
|
||||
/*
|
||||
Points to the last message which was received
|
||||
*/
|
||||
void* _LastReceivedMessage;
|
||||
@ -68,11 +61,9 @@ class SBNetwork{
|
||||
|
||||
bool handleCommandPackage(SBNetworkFrame *frame);
|
||||
|
||||
#if defined(RUN_AS_MASTER)
|
||||
bool sendMasterAck(SBMacAddress mac);
|
||||
|
||||
bool sendPairingAck(SBMacAddress mac);
|
||||
#endif
|
||||
|
||||
bool receive(SBNetworkFrame *frame);
|
||||
|
||||
@ -82,25 +73,26 @@ public:
|
||||
/*
|
||||
* Define the standard addresses for the sensor network
|
||||
*/
|
||||
//SBMacAddress _StandardSensorAddress = SBMacAddress(0x01, 0x01, 0x01, 0x01, 0x01);
|
||||
SBMacAddress _BroadcastAddress = BROADCAST_MAC;
|
||||
|
||||
SBNetworkDevice NetworkDevice;
|
||||
#if defined(RUN_AS_MASTER)
|
||||
SBMasterStorage _MasterStorage;
|
||||
#endif
|
||||
|
||||
SBMasterStorage MasterStorage;
|
||||
|
||||
RF24 radio;
|
||||
|
||||
//######################################################################################
|
||||
/*
|
||||
* Stores the runtime mode of the device true=Client, false=Master
|
||||
*/
|
||||
bool RunAsClient;
|
||||
|
||||
/*
|
||||
* Constructor with setting the used pins for commnicate with the NRF24L01(+) chip.
|
||||
*/
|
||||
SBNetwork(uint8_t cePin, uint8_t csPin);
|
||||
SBNetwork(bool client, uint8_t cePin, uint8_t csPin);
|
||||
|
||||
/*
|
||||
* Constructor with setting the used pins for commnicate with the NRF24L01(+) chip.
|
||||
* Constructor no settings. The used pins for commnicate with the NRF24L01(+) chip will be the standard pins.
|
||||
*/
|
||||
SBNetwork();
|
||||
|
||||
@ -130,24 +122,27 @@ public:
|
||||
*/
|
||||
void* getMessage() { return _LastReceivedMessage; }
|
||||
|
||||
#ifndef RUN_AS_MASTER
|
||||
bool connectToNetwork();
|
||||
|
||||
bool checkMaster();
|
||||
#else
|
||||
|
||||
// Adds a mac to the storage and returns the position in the storage.
|
||||
/*
|
||||
Adds a mac to the storage and returns the position in the storage.
|
||||
*/
|
||||
uint8_t addMac(SBMacAddress mac);
|
||||
|
||||
// Removes the mac from the storage. If the mac was stored, it returns the position of the mac, if not, it returns -1.
|
||||
/*
|
||||
Removes the mac from the storage. If the mac was stored, it returns the position of the mac, if not, it returns -1.
|
||||
*/
|
||||
uint8_t removeMac(SBMacAddress mac);
|
||||
#endif
|
||||
|
||||
bool pingDevice(SBMacAddress mac);
|
||||
|
||||
// Updates the uptime counter.
|
||||
// If this device is a sensor, it pings the master after a time. The time is definded in the config under SENSOR_CHECK_INTERVAL.
|
||||
// If SENSOR_CHECK_INTERVAL is set to 0, it will not ping the master.
|
||||
/*
|
||||
Updates the uptime counter.
|
||||
If this device is a sensor, it pings the master after a time. The time is definded in the config under SENSOR_CHECK_INTERVAL.
|
||||
If SENSOR_CHECK_INTERVAL is set to 0, it will not ping the master.
|
||||
*/
|
||||
void update();
|
||||
|
||||
unsigned long long uptime(){
|
||||
@ -155,6 +150,4 @@ public:
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
|
@ -3,7 +3,7 @@
|
||||
#define _SB_NETWORK_CONFIG_
|
||||
|
||||
// Uncomment the following line, to compile the library for a master device.
|
||||
#define RUN_AS_MASTER
|
||||
//#define RUN_AS_MASTER
|
||||
|
||||
#define _DEBUG
|
||||
|
||||
|
@ -3,11 +3,13 @@
|
||||
#define _SB_TYPES_
|
||||
|
||||
#include <arduino.h>
|
||||
|
||||
#define SBS_COMMAND_PING 0 // Will be sent to check, if a device is available
|
||||
// Will be sent to check, if a device is available
|
||||
#define SBS_COMMAND_PING 0
|
||||
#define SBS_COMMAND_NO_COMMAND 1
|
||||
#define SBS_COMMAND_SEARCH_MASTER 2 // Will be sent from a slave to find search a master
|
||||
#define SBS_COMMAND_MASTER_ACK 3 // Will be sent from a master after receiving a search master request
|
||||
// Will be sent from a slave to find search a master
|
||||
#define SBS_COMMAND_SEARCH_MASTER 2
|
||||
// Will be sent from a master after receiving a search master request
|
||||
#define SBS_COMMAND_MASTER_ACK 3
|
||||
#define SBS_COMMAND_REQUEST_PAIRING 4
|
||||
#define SBS_COMMAND_PAIRING_ACK 5
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user