Skip to content
Snippets Groups Projects
Verified Commit 5efedc7e authored by Anian Bühler's avatar Anian Bühler
Browse files

added updated DidacticNet library

parent b606eacb
No related branches found
No related tags found
1 merge request!1dev_prefereences to master
/**************************************************************************/ /**************************************************************************/
/*! /*!
@file didacticNet.cpp @file DidacticNet.cpp
@author anian buehler @ letsgoING.org @author anian buehler @ letsgoING.org
*/ */
/**************************************************************************/ /**************************************************************************/
#include "Arduino.h" #include "Arduino.h"
#include "didacticNet.h" #include "DidacticNet.h"
//************************************************************************** //**************************************************************************
//ROOT //ROOT
...@@ -43,13 +43,15 @@ bool DidacticPSNet::handleNetwork(){ ...@@ -43,13 +43,15 @@ bool DidacticPSNet::handleNetwork(){
handleData(); handleData();
} }
} }
_waitingTime = millis()+ random(CSMA_MIN_DELAY_MS, CSMA_MAX_DELAY_MS); _waitingTimeCSMA = millis()+ random(CSMA_MIN_DELAY_MS, CSMA_MAX_DELAY_MS);
} }
//else if(_dataToSend){ //else if(_dataToSend){
if(_dataToSend && _waitingTime <= millis()){ if(_dataToSend && _waitingTimeSend <= millis() && _waitingTimeCSMA <= millis()){
//send data to network //send data to network
//TODO: test added CSMA_CHECKDELAY + 2nd checkData() //TODO: test added CSMA_CHECKDELAY + 2nd checkData()
delayMicroseconds(CSMA_CHECK_DELAY_US); unsigned long delayStartTime = micros();
while(micros() < delayStartTime + CSMA_CHECK_DELAY_US);
//delayMicroseconds(CSMA_CHECK_DELAY_US); //removed: blocking SoftSerial-interrupts
if(!checkData()){ if(!checkData()){
if(!sendData()){ if(!sendData()){
return false; return false;
...@@ -57,7 +59,7 @@ bool DidacticPSNet::handleNetwork(){ ...@@ -57,7 +59,7 @@ bool DidacticPSNet::handleNetwork(){
else{ else{
_dataToSend = false; _dataToSend = false;
//_waitingTime = millis()+ random(CSMA_MID_DELAY_MS, CSMA_MAX_DELAY_MS); //_waitingTime = millis()+ random(CSMA_MID_DELAY_MS, CSMA_MAX_DELAY_MS);
_waitingTime = millis()+ _intervalTime;//random(CSMA_MID_DELAY_MS, CSMA_MAX_DELAY_MS); _waitingTimeSend = millis() + _intervalTime;//random(CSMA_MID_DELAY_MS, CSMA_MAX_DELAY_MS);
} }
} }
} }
...@@ -299,7 +301,13 @@ bool DidacticPSNetClient::handleData(){ ...@@ -299,7 +301,13 @@ bool DidacticPSNetClient::handleData(){
payloadLength = extractData(topicLength+3, MAX_LEN_PAYLOAD, _bufferPayload, MSG_DELIMITER); payloadLength = extractData(topicLength+3, MAX_LEN_PAYLOAD, _bufferPayload, MSG_DELIMITER);
if( currentTopicNr >= 0){ if( currentTopicNr >= 0){
savePayload( _bufferPayload, currentTopicNr); savePayload( _bufferPayload, currentTopicNr);
callback(_topic[currentTopicNr], topicLength, _payload[currentTopicNr], payloadLength); //callback(_topic[currentTopicNr], topicLength, _payload[currentTopicNr], payloadLength);
#ifdef CALLBACK_W_LENGTH
callback(_bufferTopic, topicLength, _bufferPayload, payloadLength);
#else
callback(_bufferTopic, _bufferPayload);
#endif
} }
} }
return true; return true;
......
/************************************************************************** /**************************************************************************
@file didacticNet.h @file DidacticNet.h
@author anian buehler @ letsgoING @author anian buehler @ letsgoING
**************************************************************************/ **************************************************************************/
...@@ -8,8 +8,14 @@ ...@@ -8,8 +8,14 @@
#define _DIDACTICNET_ #define _DIDACTICNET_
#include "Arduino.h" #include "Arduino.h"
//callback(topic, topicLength, data, dataLength)
#define PSNET_CALLBACK_SIGNATURE void (*callback)(char*, int, char*, int) #ifdef CALLBACK_W_LENGTH
//callback(topic, topicLength, payload, payloadLength)
#define PSNET_CALLBACK_SIGNATURE void (*callback)(char*, int, char*, int)
#else
//callback(topic, payload)
#define PSNET_CALLBACK_SIGNATURE void (*callback)(char*, char*)
#endif
#define MSG_PRELIMITER '<' #define MSG_PRELIMITER '<'
#define MSG_DELIMITER '>' #define MSG_DELIMITER '>'
...@@ -18,10 +24,10 @@ ...@@ -18,10 +24,10 @@
//@ publish → on publish check topic, then send topic-update //@ publish → on publish check topic, then send topic-update
//? subscribe → subscribe starts update, topic filter @client //? subscribe → subscribe starts update, topic filter @client
//# update → update to specific topic Broker to client //# update → update to specific topic Broker to client
#define MSG_PUBLISH '@' #define MSG_PUBLISH '@'
#define MSG_SUBSCRIBE '?' #define MSG_SUBSCRIBE '?'
#define MSG_UPDATE '#' #define MSG_UPDATE '#'
#define MSG_TOPIC_MULTI '*' #define MSG_TOPIC_MULTI '*'
//<@topic|payload> //<@topic|payload>
#define LEN_OVERHEAD 4 #define LEN_OVERHEAD 4
...@@ -34,7 +40,7 @@ ...@@ -34,7 +40,7 @@
#define INTERVAL_CLIENT 500L #define INTERVAL_CLIENT 500L
#define INTERVAL_BROKER 0L #define INTERVAL_BROKER 0L
#define MAX_NR_TOPICS_CLIENT 5 #define MAX_NR_TOPICS_CLIENT 10
#define MAX_NR_TOPICS_BROKER 20 #define MAX_NR_TOPICS_BROKER 20
#define MAX_LEN_TOPICS 10 #define MAX_LEN_TOPICS 10
#define MAX_LEN_PAYLOAD 20 #define MAX_LEN_PAYLOAD 20
...@@ -110,12 +116,14 @@ class DidacticPSNet ...@@ -110,12 +116,14 @@ class DidacticPSNet
char _sendBufferMessage[MAX_LEN_TOPICS + MAX_LEN_PAYLOAD + LEN_OVERHEAD +1]; char _sendBufferMessage[MAX_LEN_TOPICS + MAX_LEN_PAYLOAD + LEN_OVERHEAD +1];
bool _dataToSend = false; // int Data to send for queue? bool _dataToSend = false; // int Data to send for queue?
unsigned long _waitingTime = 0L; unsigned long _waitingTimeSend = 0L;
unsigned long _waitingTimeCSMA = 0L;
unsigned long _intervalTime = 0L; unsigned long _intervalTime = 0L;
int _currentTopicLength = 0; int _currentTopicLength = 0;
int _currentPayloadLength = 0; int _currentPayloadLength = 0;
DidacticPSNet& setCallback(PSNET_CALLBACK_SIGNATURE); DidacticPSNet& setCallback(PSNET_CALLBACK_SIGNATURE);
void setStream(Stream& _port); void setStream(Stream& _port);
int checkData(); int checkData();
...@@ -135,6 +143,7 @@ class DidacticPSNet ...@@ -135,6 +143,7 @@ class DidacticPSNet
void begin(Stream& _port); void begin(Stream& _port);
void begin(Stream& _port, PSNET_CALLBACK_SIGNATURE); void begin(Stream& _port, PSNET_CALLBACK_SIGNATURE);
bool handleNetwork(); bool handleNetwork();
bool isDataToSend(); bool isDataToSend();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment