From ada0e6abcdfe623c5888c7ca58ab4e264b3ba85f Mon Sep 17 00:00:00 2001 From: strange_v Date: Sun, 10 Jul 2022 17:55:33 +0300 Subject: [PATCH 1/2] Added ISR callback --- RFM69.cpp | 13 ++++++++++++- RFM69.h | 3 +++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/RFM69.cpp b/RFM69.cpp index aac4530..8e18045 100644 --- a/RFM69.cpp +++ b/RFM69.cpp @@ -37,8 +37,10 @@ uint8_t RFM69::ACK_REQUESTED; uint8_t RFM69::ACK_RECEIVED; // should be polled immediately after sending a packet with ACK request int16_t RFM69::RSSI; // most accurate RSSI during reception (closest to the reception) volatile bool RFM69::_haveData; +RFM69 *RFM69::_instance = nullptr; RFM69::RFM69(uint8_t slaveSelectPin, uint8_t interruptPin, bool isRFM69HW_HCW, SPIClass *spi) { + _instance = this; _slaveSelectPin = slaveSelectPin; _interruptPin = interruptPin; _mode = RF69_MODE_STANDBY; @@ -152,6 +154,11 @@ bool RFM69::initialize(uint8_t freqBand, uint16_t nodeID, uint8_t networkID) return true; } +void RFM69::setIsrCallback(void (*callback)()) +{ + _isrCallback = callback; +} + // return the frequency (in Hz) uint32_t RFM69::getFrequency() { @@ -446,7 +453,11 @@ void RFM69::interruptHandler() { } // internal function -ISR_PREFIX void RFM69::isr0() { _haveData = true; } +ISR_PREFIX void RFM69::isr0() { + _haveData = true; + if (_instance->_isrCallback) + _instance->_isrCallback(); +} // internal function void RFM69::receiveBegin() { diff --git a/RFM69.h b/RFM69.h index 491fa7e..2d511e2 100644 --- a/RFM69.h +++ b/RFM69.h @@ -201,6 +201,7 @@ class RFM69 { RFM69(uint8_t slaveSelectPin=RF69_SPI_CS, uint8_t interruptPin=RF69_IRQ_PIN, bool isRFM69HW_HCW=false, SPIClass *spi=nullptr); bool initialize(uint8_t freqBand, uint16_t ID, uint8_t networkID=1); + void setIsrCallback(void (*callback)()); void setAddress(uint16_t addr); void setNetwork(uint8_t networkID); virtual bool canSend(); @@ -247,6 +248,8 @@ class RFM69 { void interruptHandler(); virtual void interruptHook(uint8_t CTLbyte __attribute__((unused))) {}; static volatile bool _haveData; + static RFM69 *_instance; + void (*_isrCallback)() = nullptr; virtual void sendFrame(uint16_t toAddress, const void* buffer, uint8_t size, bool requestACK=false, bool sendACK=false); // for ListenMode sleep/timer From 4b91c6c1b686b922b597260c11692d887c623fba Mon Sep 17 00:00:00 2001 From: strange_v Date: Mon, 11 Jul 2022 20:51:58 +0300 Subject: [PATCH 2/2] Restructured a bit --- RFM69.cpp | 11 ++++++----- RFM69.h | 4 ++-- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/RFM69.cpp b/RFM69.cpp index 8e18045..b3bc30d 100644 --- a/RFM69.cpp +++ b/RFM69.cpp @@ -154,11 +154,6 @@ bool RFM69::initialize(uint8_t freqBand, uint16_t nodeID, uint8_t networkID) return true; } -void RFM69::setIsrCallback(void (*callback)()) -{ - _isrCallback = callback; -} - // return the frequency (in Hz) uint32_t RFM69::getFrequency() { @@ -234,6 +229,12 @@ void RFM69::setNetwork(uint8_t networkID) writeReg(REG_SYNCVALUE2, networkID); } +//set user's ISR callback +void RFM69::setIsrCallback(void (*callback)()) +{ + _isrCallback = callback; +} + // Control transmitter output power (this is NOT a dBm value!) // the power configurations are explained in the SX1231H datasheet (Table 10 on p21; RegPaLevel p66): http://www.semtech.com/images/datasheet/sx1231h.pdf // valid powerLevel parameter values are 0-31 and result in a directly proportional effect on the output/transmission power diff --git a/RFM69.h b/RFM69.h index 2d511e2..c5f029c 100644 --- a/RFM69.h +++ b/RFM69.h @@ -201,9 +201,9 @@ class RFM69 { RFM69(uint8_t slaveSelectPin=RF69_SPI_CS, uint8_t interruptPin=RF69_IRQ_PIN, bool isRFM69HW_HCW=false, SPIClass *spi=nullptr); bool initialize(uint8_t freqBand, uint16_t ID, uint8_t networkID=1); - void setIsrCallback(void (*callback)()); void setAddress(uint16_t addr); void setNetwork(uint8_t networkID); + void setIsrCallback(void (*callback)()); virtual bool canSend(); virtual void send(uint16_t toAddress, const void* buffer, uint8_t bufferSize, bool requestACK=false); virtual bool sendWithRetry(uint16_t toAddress, const void* buffer, uint8_t bufferSize, uint8_t retries=2, uint8_t retryWaitTime=RFM69_ACK_TIMEOUT); @@ -249,7 +249,6 @@ class RFM69 { virtual void interruptHook(uint8_t CTLbyte __attribute__((unused))) {}; static volatile bool _haveData; static RFM69 *_instance; - void (*_isrCallback)() = nullptr; virtual void sendFrame(uint16_t toAddress, const void* buffer, uint8_t size, bool requestACK=false, bool sendACK=false); // for ListenMode sleep/timer @@ -263,6 +262,7 @@ class RFM69 { uint8_t _powerLevel; bool _isRFM69HW; SPIClass *_spi; + void (*_isrCallback)() = nullptr; #if defined (SPCR) && defined (SPSR) uint8_t _SPCR; uint8_t _SPSR;