Browse Source

- fixed synchronous write bugs

- added possibility to log messages generated by OneWireSlave
timer1
Youen Toupin 9 years ago
parent
commit
b71858b9a3
  1. 10
      OneWireSlave.cpp
  2. 9
      OneWireSlave.h

10
OneWireSlave.cpp

@ -57,8 +57,8 @@ short OneWireSlave::bufferPos_;
void(*OneWireSlave::receiveBytesCallback_)(bool error); void(*OneWireSlave::receiveBytesCallback_)(bool error);
void(*OneWireSlave::sendBytesCallback_)(bool error); void(*OneWireSlave::sendBytesCallback_)(bool error);
bool OneWireSlave::waitingSynchronousWriteToComplete_; volatile bool OneWireSlave::waitingSynchronousWriteToComplete_;
bool OneWireSlave::synchronousWriteError_; volatile bool OneWireSlave::synchronousWriteError_;
bool OneWireSlave::sendingClientBytes_; bool OneWireSlave::sendingClientBytes_;
@ -66,6 +66,8 @@ bool OneWireSlave::singleBit_;
bool OneWireSlave::singleBitRepeat_; bool OneWireSlave::singleBitRepeat_;
void(*OneWireSlave::singleBitSentCallback_)(bool error); void(*OneWireSlave::singleBitSentCallback_)(bool error);
void(*OneWireSlave::logCallback_)(const char* message);
ISR(TIMER1_COMPA_vect) // timer1 interrupt ISR(TIMER1_COMPA_vect) // timer1 interrupt
{ {
@ -125,7 +127,7 @@ bool OneWireSlave::write(const byte* bytes, short numBytes)
beginWrite(bytes, numBytes, &OneWireSlave::onSynchronousWriteComplete_); beginWrite(bytes, numBytes, &OneWireSlave::onSynchronousWriteComplete_);
while (waitingSynchronousWriteToComplete_) while (waitingSynchronousWriteToComplete_)
delay(1); delay(1);
return synchronousWriteError_; return !synchronousWriteError_;
} }
void OneWireSlave::onSynchronousWriteComplete_(bool error) void OneWireSlave::onSynchronousWriteComplete_(bool error)
@ -247,6 +249,8 @@ void OneWireSlave::onLeaveInterrupt_()
void OneWireSlave::error_(const char* message) void OneWireSlave::error_(const char* message)
{ {
if (logCallback_ != 0)
logCallback_(message);
beginWaitReset_(); beginWaitReset_();
endClientWrite_(true); endClientWrite_(true);
if (clientReceiveCallback_ != 0) if (clientReceiveCallback_ != 0)

9
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. //! 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; } 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. //! 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); bool write(const byte* bytes, short numBytes);
@ -121,8 +124,8 @@ private:
static void(*receiveBytesCallback_)(bool error); static void(*receiveBytesCallback_)(bool error);
static void(*sendBytesCallback_)(bool error); static void(*sendBytesCallback_)(bool error);
static bool waitingSynchronousWriteToComplete_; static volatile bool waitingSynchronousWriteToComplete_;
static bool synchronousWriteError_; static volatile bool synchronousWriteError_;
static bool sendingClientBytes_; static bool sendingClientBytes_;
@ -133,6 +136,8 @@ private:
static void(*clientReceiveCallback_)(ReceiveEvent evt, byte data); static void(*clientReceiveCallback_)(ReceiveEvent evt, byte data);
static void(*clientReceiveBitCallback_)(bool bit); static void(*clientReceiveBitCallback_)(bool bit);
static void(*logCallback_)(const char* message);
}; };
extern OneWireSlave OWSlave; extern OneWireSlave OWSlave;

Loading…
Cancel
Save