diff --git a/examples/sPSN_Broker/sPSN_Broker.ino b/examples/sPSN_Broker/sPSN_Broker.ino index da6837f74112ae3b78ef990c120bb7e2b4a56c8f..662ecf03641f9d69a3289d9f9be861024f9514bd 100644 --- a/examples/sPSN_Broker/sPSN_Broker.ino +++ b/examples/sPSN_Broker/sPSN_Broker.ino @@ -48,7 +48,8 @@ void setup() { sSerial.begin(SERIAL_BAUD); //Lege fest welche Serielle Schnittstelle für sPSN verwendet werden soll - psnBroker.setStream(sSerial); + //psnBroker.setStream(sSerial); + psnBroker.begin(sSerial); } void loop() { diff --git a/examples/sPSN_Chat/sPSN_Chat.ino b/examples/sPSN_Chat/sPSN_Chat.ino index cdf02778539f8fd9cb1c5e344ac02cc03bef2a27..df7dbe6bdd0abbdab2821ce70bb16757a8f9e34f 100644 --- a/examples/sPSN_Chat/sPSN_Chat.ino +++ b/examples/sPSN_Chat/sPSN_Chat.ino @@ -67,10 +67,12 @@ void setup() { sSerial.begin(SERIAL_BAUD); //Lege fest welche Serielle Schnittstelle für sPSN verwendet werden soll - psnClient.setStream(sSerial); + //psnClient.setStream(sSerial); //Lege Callback-Funktion fest (wird ausgeführt wenn neue Daten ankommen) - psnClient.setCallback(myCallback); + //psnClient.setCallback(myCallback); + + psnClient.begin(sSerial, myCallback); Serial.print("Bitte den eigenen Namen mit einem # eingeben\nund mit Enter bestaetigen. -> "); Serial.println("#DeinName"); diff --git a/examples/sPSN_Client1/sPSN_Client1.ino b/examples/sPSN_Client1/sPSN_Client1.ino index 344d9ac93c04c4f347d08a2b2a65c0703c5af1b2..61997358952e0f61d7bcbb755b57b0f1135b7650 100644 --- a/examples/sPSN_Client1/sPSN_Client1.ino +++ b/examples/sPSN_Client1/sPSN_Client1.ino @@ -85,10 +85,12 @@ void setup() { pinMode(ledPin, OUTPUT); //Lege fest welche Serielle Schnittstelle für sPSN verwendet werden soll - psnClient.setStream(sSerial); + //psnClient.setStream(sSerial); //Lege Callback-Funktion fest (wird ausgeführt wenn neue Daten ankommen) - psnClient.setCallback(myCallback); + //psnClient.setCallback(myCallback); + + psnClient.begin(sSerial, myCallback); //Lege fest zu welchem Topic Daten empfangen werden sollen psnClient.subscribe(topicSub); diff --git a/examples/sPSN_Client2/sPSN_Client2.ino b/examples/sPSN_Client2/sPSN_Client2.ino index e75ce0a7ee64f6189c91df69910e5ae12a1b114b..dc3be3bf1df9dec594464c653d6dfe7714bc963f 100644 --- a/examples/sPSN_Client2/sPSN_Client2.ino +++ b/examples/sPSN_Client2/sPSN_Client2.ino @@ -84,10 +84,12 @@ void setup() { pinMode(buttonPin, INPUT); //Lege fest welche Serielle Schnittstelle für sPSN verwendet werden soll - psnClient.setStream(sSerial); + //psnClient.setStream(sSerial); //Lege Callback-Funktion fest (wird ausgeführt wenn neue Daten ankommen) - psnClient.setCallback(myCallback); + //psnClient.setCallback(myCallback); + + psnClient.begin(sSerial, myCallback); //Lege fest zu welchem Topic Daten empfangen werden sollen psnClient.subscribe(topicSub); diff --git a/examples/sPSN_Server/sPSN_Server.ino b/examples/sPSN_Server/sPSN_Server.ino index 8b03893b3b5c8bfe7de734909a24a114ab9a014e..849fcb72e38200df68acac04fe11957d251b610e 100644 --- a/examples/sPSN_Server/sPSN_Server.ino +++ b/examples/sPSN_Server/sPSN_Server.ino @@ -48,7 +48,8 @@ void setup() { sSerial.begin(SERIAL_BAUD); //Lege fest welche Serielle Schnittstelle für sPSN verwendet werden soll - psnBroker.setStream(sSerial); + //psnBroker.setStream(sSerial); + psnClient.begin(sSerial); } void loop() { diff --git a/src/didacticNet.cpp b/src/didacticNet.cpp index 17452d0138a6ad0fcb34cb579098c9997a967392..72f2622052c7b322fc26d95d6ad2fe640414a3cd 100644 --- a/src/didacticNet.cpp +++ b/src/didacticNet.cpp @@ -16,7 +16,16 @@ didacticPSNet::didacticPSNet(){} didacticPSNet::~didacticPSNet(){} -didacticPSNet& didacticPSNet::setCallback(void(*callback)(char*, int, char*, int)){ +didacticPSNet::begin(Stream& _port){ + setStream(_port); +} + +didacticPSNet::begin(Stream& _port, PSNET_CALLBACK_SIGNATURE){ + setStream(_port); + setCallback(callback); +} + +didacticPSNet& didacticPSNet::setCallback(PSNET_CALLBACK_SIGNATURE){ this->callback = callback; return *this; } @@ -39,12 +48,16 @@ bool didacticPSNet::handleNetwork(){ } else if(_dataToSend){ //send data to network - if(!sendData()){ - return false; - } - else{ - _dataToSend = false; - _waitingTime = millis()+ random(CSMA_MID_DELAY_MS, CSMA_MAX_DELAY_MS); + //TODO: test added CSMA_CHECKDELAY + 2nd checkData() + delayMicroseconds(CSMA_CHECK_DELAY_US); + if(!checkData()){ + if(!sendData()){ + return false; + } + else{ + _dataToSend = false; + _waitingTime = millis()+ random(CSMA_MID_DELAY_MS, CSMA_MAX_DELAY_MS); + } } } } diff --git a/src/didacticNet.h b/src/didacticNet.h index 326a65f872104a3395f2f0c1c7ca048f7a71f2fe..3f519b633a2e76e3681d83bebf6e623ad76e9aa7 100644 --- a/src/didacticNet.h +++ b/src/didacticNet.h @@ -26,7 +26,7 @@ #define MSG_TOPIC_MULTI '*' -#define CSMA_CHECK_DELAY_US 400 //unused? +#define CSMA_CHECK_DELAY_US 400 #define CSMA_MIN_DELAY_MS 10 #define CSMA_MID_DELAY_MS 20 #define CSMA_MAX_DELAY_MS 30 @@ -53,6 +53,9 @@ class didacticPSNet int _currentTopicLength = 0; int _currentDataLength = 0; + didacticPSNet& setCallback(PSNET_CALLBACK_SIGNATURE); + void setStream(Stream& _port); + int checkData(); bool recieveData(); bool sendData(); @@ -68,8 +71,8 @@ class didacticPSNet didacticPSNet(); ~didacticPSNet(); - didacticPSNet& setCallback(PSNET_CALLBACK_SIGNATURE); - void setStream(Stream& _port); + begin(Stream& _port); + begin(Stream& _port, PSNET_CALLBACK_SIGNATURE); bool handleNetwork(); bool isDataToSend();