From b71858b9a38d4f69d086cfc7835dce39fe11171a Mon Sep 17 00:00:00 2001 From: Youen Toupin Date: Fri, 18 Dec 2015 23:25:02 +0100 Subject: [PATCH] - fixed synchronous write bugs - added possibility to log messages generated by OneWireSlave --- OneWireSlave.cpp | 10 +++++++--- OneWireSlave.h | 9 +++++++-- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/OneWireSlave.cpp b/OneWireSlave.cpp index 53d97e6..6b5baa3 100644 --- a/OneWireSlave.cpp +++ b/OneWireSlave.cpp @@ -57,8 +57,8 @@ short OneWireSlave::bufferPos_; void(*OneWireSlave::receiveBytesCallback_)(bool error); void(*OneWireSlave::sendBytesCallback_)(bool error); -bool OneWireSlave::waitingSynchronousWriteToComplete_; -bool OneWireSlave::synchronousWriteError_; +volatile bool OneWireSlave::waitingSynchronousWriteToComplete_; +volatile bool OneWireSlave::synchronousWriteError_; bool OneWireSlave::sendingClientBytes_; @@ -66,6 +66,8 @@ bool OneWireSlave::singleBit_; bool OneWireSlave::singleBitRepeat_; void(*OneWireSlave::singleBitSentCallback_)(bool error); +void(*OneWireSlave::logCallback_)(const char* message); + ISR(TIMER1_COMPA_vect) // timer1 interrupt { @@ -125,7 +127,7 @@ bool OneWireSlave::write(const byte* bytes, short numBytes) beginWrite(bytes, numBytes, &OneWireSlave::onSynchronousWriteComplete_); while (waitingSynchronousWriteToComplete_) delay(1); - return synchronousWriteError_; + return !synchronousWriteError_; } void OneWireSlave::onSynchronousWriteComplete_(bool error) @@ -247,6 +249,8 @@ void OneWireSlave::onLeaveInterrupt_() void OneWireSlave::error_(const char* message) { + if (logCallback_ != 0) + logCallback_(message); beginWaitReset_(); endClientWrite_(true); if (clientReceiveCallback_ != 0) diff --git a/OneWireSlave.h b/OneWireSlave.h index f8f6381..053f4ce 100644 --- a/OneWireSlave.h +++ b/OneWireSlave.h @@ -26,6 +26,9 @@ public: //! Sets (or replaces) a function to be called when a bit is received. The byte reception callback is called after that if the received bit was the last of a byte. The callback is executed from interrupts and should be as short as possible. Failure to return quickly can prevent the library from correctly reading the next bit. void setReceiveBitCallback(void(*callback)(bool bit)) { clientReceiveBitCallback_ = callback; } + //! Sets (or replaces) a function to be called when the library has a message to log, if the functionality is enabled in OneWireSlave.cpp. This is for debugging purposes. + void setLogCallback(void(*callback)(const char* message)) { logCallback_ = callback; } + //! Writes the specified bytes synchronously. This function blocks until the write operation has finished. Do not call from an interrupt handler! Returns true in case of success, false if an error occured. bool write(const byte* bytes, short numBytes); @@ -121,8 +124,8 @@ private: static void(*receiveBytesCallback_)(bool error); static void(*sendBytesCallback_)(bool error); - static bool waitingSynchronousWriteToComplete_; - static bool synchronousWriteError_; + static volatile bool waitingSynchronousWriteToComplete_; + static volatile bool synchronousWriteError_; static bool sendingClientBytes_; @@ -133,6 +136,8 @@ private: static void(*clientReceiveCallback_)(ReceiveEvent evt, byte data); static void(*clientReceiveBitCallback_)(bool bit); + + static void(*logCallback_)(const char* message); }; extern OneWireSlave OWSlave;