diff --git a/src/main/resources/com/ardublock/libraries/didacticNet.cpp b/src/main/resources/com/ardublock/libraries/DidacticNet.cpp similarity index 94% rename from src/main/resources/com/ardublock/libraries/didacticNet.cpp rename to src/main/resources/com/ardublock/libraries/DidacticNet.cpp index f14ace21992e70b7d533ffd00d95831966f92b66..380c12a0cbbd38ae648f239bb1bd96e48f73de42 100644 --- a/src/main/resources/com/ardublock/libraries/didacticNet.cpp +++ b/src/main/resources/com/ardublock/libraries/DidacticNet.cpp @@ -1,12 +1,12 @@ /**************************************************************************/ /*! - @file didacticNet.cpp + @file DidacticNet.cpp @author anian buehler @ letsgoING.org */ /**************************************************************************/ #include "Arduino.h" -#include "didacticNet.h" +#include "DidacticNet.h" //************************************************************************** //ROOT @@ -43,13 +43,15 @@ bool DidacticPSNet::handleNetwork(){ 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){ - if(_dataToSend && _waitingTime <= millis()){ + if(_dataToSend && _waitingTimeSend <= millis() && _waitingTimeCSMA <= millis()){ //send data to network //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(!sendData()){ return false; @@ -57,7 +59,7 @@ bool DidacticPSNet::handleNetwork(){ else{ _dataToSend = false; //_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(){ payloadLength = extractData(topicLength+3, MAX_LEN_PAYLOAD, _bufferPayload, MSG_DELIMITER); if( currentTopicNr >= 0){ 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; diff --git a/src/main/resources/com/ardublock/libraries/didacticNet.h b/src/main/resources/com/ardublock/libraries/DidacticNet.h similarity index 88% rename from src/main/resources/com/ardublock/libraries/didacticNet.h rename to src/main/resources/com/ardublock/libraries/DidacticNet.h index 596db882857b225191ab1d86fa05712d91df8856..498e953e4186f43160d0752fe72f74d8c6891177 100644 --- a/src/main/resources/com/ardublock/libraries/didacticNet.h +++ b/src/main/resources/com/ardublock/libraries/DidacticNet.h @@ -1,5 +1,5 @@ /************************************************************************** - @file didacticNet.h + @file DidacticNet.h @author anian buehler @ letsgoING **************************************************************************/ @@ -8,8 +8,14 @@ #define _DIDACTICNET_ #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_DELIMITER '>' @@ -18,10 +24,10 @@ //@ publish → on publish check topic, then send topic-update //? subscribe → subscribe starts update, topic filter @client //# update → update to specific topic Broker to client -#define MSG_PUBLISH '@' -#define MSG_SUBSCRIBE '?' -#define MSG_UPDATE '#' -#define MSG_TOPIC_MULTI '*' +#define MSG_PUBLISH '@' +#define MSG_SUBSCRIBE '?' +#define MSG_UPDATE '#' +#define MSG_TOPIC_MULTI '*' //<@topic|payload> #define LEN_OVERHEAD 4 @@ -34,7 +40,7 @@ #define INTERVAL_CLIENT 500L #define INTERVAL_BROKER 0L -#define MAX_NR_TOPICS_CLIENT 5 +#define MAX_NR_TOPICS_CLIENT 10 #define MAX_NR_TOPICS_BROKER 20 #define MAX_LEN_TOPICS 10 #define MAX_LEN_PAYLOAD 20 @@ -110,12 +116,14 @@ class DidacticPSNet char _sendBufferMessage[MAX_LEN_TOPICS + MAX_LEN_PAYLOAD + LEN_OVERHEAD +1]; 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; int _currentTopicLength = 0; int _currentPayloadLength = 0; DidacticPSNet& setCallback(PSNET_CALLBACK_SIGNATURE); + void setStream(Stream& _port); int checkData(); @@ -135,6 +143,7 @@ class DidacticPSNet void begin(Stream& _port); void begin(Stream& _port, PSNET_CALLBACK_SIGNATURE); + bool handleNetwork(); bool isDataToSend();