From 9d4ba486abd34007f194be4b1b2f4e0ae7aa41cc Mon Sep 17 00:00:00 2001 From: crteensy Date: Sat, 11 Oct 2014 20:19:12 +0200 Subject: [PATCH] Update SPI.h added SPI_TRANSACTION_BLOCK macro --- SPI.h | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/SPI.h b/SPI.h index b7f3b39..386bc31 100644 --- a/SPI.h +++ b/SPI.h @@ -19,6 +19,36 @@ // usingInterrupt(), and SPISetting(clock, bitOrder, dataMode) #define SPI_HAS_TRANSACTION 1 +// automatically matches pairs of beginTransaction() and endTransaction() +// http://forum.pjrc.com/threads/26808-Scoped-SPI-transactions +#define SPI_TRANSACTION_BLOCK(settings) \ +for( \ + struct \ + { \ + struct Helper \ + { \ + Helper() : done_(false) \ + { \ + SPI.beginTransaction(settings); \ + } \ + ~Helper() \ + { \ + SPI.endTransaction(); \ + } \ + bool done_; \ + }; \ + \ + bool done() const \ + { \ + return helper_.done_; \ + } \ + void exec() \ + { \ + helper_.done_ = true; \ + } \ + Helper helper_; \ + } scope; !scope.done() ; scope.exec()) + // Uncomment this line to add detection of mismatched begin/end transactions. // A mismatch occurs if other libraries fail to use SPI.endTransaction() for // each SPI.beginTransaction(). Connect a LED to this pin. The LED will turn