From 395ada379951ae0e6e9246e823cc7825bead52fc Mon Sep 17 00:00:00 2001 From: Jan Karssies <43203120+hjkarssies@users.noreply.github.com> Date: Wed, 27 Mar 2024 15:52:43 +0100 Subject: [PATCH] Added std::function callback Added std::function callback next to callback pointer to support lambda functions. This way, non-static functions can be used. --- src/CANController.cpp | 9 +++++++-- src/CANController.h | 4 +++- src/ESP32SJA1000.cpp | 21 ++++++++++++++++++++- src/ESP32SJA1000.h | 1 + src/MCP2515.cpp | 26 ++++++++++++++++++++++++-- src/MCP2515.h | 1 + 6 files changed, 56 insertions(+), 6 deletions(-) diff --git a/src/CANController.cpp b/src/CANController.cpp index 0890eec..3140e47 100644 --- a/src/CANController.cpp +++ b/src/CANController.cpp @@ -4,7 +4,7 @@ #include "CANController.h" CANControllerClass::CANControllerClass() : - _onReceive(NULL), + _onReceivePointer(NULL), _packetBegun(false), _txId(-1), @@ -182,7 +182,12 @@ void CANControllerClass::flush() void CANControllerClass::onReceive(void(*callback)(int)) { - _onReceive = callback; + _onReceivePointer = callback; +} + +void CANControllerClass::onReceive(std::function callback) +{ + _onReceiveFunction = callback; } int CANControllerClass::filter(int /*id*/, int /*mask*/) diff --git a/src/CANController.h b/src/CANController.h index cdaeb94..8d39e08 100644 --- a/src/CANController.h +++ b/src/CANController.h @@ -33,6 +33,7 @@ class CANControllerClass : public Stream { virtual void flush(); virtual void onReceive(void(*callback)(int)); + virtual void onReceive(std::function callback); virtual int filter(int id) { return filter(id, 0x7ff); } virtual int filter(int id, int mask); @@ -49,7 +50,8 @@ class CANControllerClass : public Stream { virtual ~CANControllerClass(); protected: - void (*_onReceive)(int); + void (*_onReceivePointer)(int); + std::function _onReceiveFunction; bool _packetBegun; long _txId; diff --git a/src/ESP32SJA1000.cpp b/src/ESP32SJA1000.cpp index 309e030..d7accb0 100644 --- a/src/ESP32SJA1000.cpp +++ b/src/ESP32SJA1000.cpp @@ -272,6 +272,20 @@ void ESP32SJA1000Class::onReceive(void(*callback)(int)) } } +void ESP32SJA1000Class::onReceive(std::function callback) +{ + CANControllerClass::onReceive(callback); + + if (_intrHandle) { + esp_intr_free(_intrHandle); + _intrHandle = NULL; + } + + if (callback) { + esp_intr_alloc(ETS_CAN_INTR_SOURCE, 0, ESP32SJA1000Class::onInterrupt, this, &_intrHandle); + } +} + int ESP32SJA1000Class::filter(int id, int mask) { id &= 0x7ff; @@ -380,7 +394,12 @@ void ESP32SJA1000Class::handleInterrupt() // received packet, parse and call callback parsePacket(); - _onReceive(available()); + if (_onReceivePointer) { + _onReceivePointer(available()); + } + if (_onReceiveFunction) { + _onReceiveFunction(available()); + } } } diff --git a/src/ESP32SJA1000.h b/src/ESP32SJA1000.h index b83cddc..ce96449 100644 --- a/src/ESP32SJA1000.h +++ b/src/ESP32SJA1000.h @@ -25,6 +25,7 @@ class ESP32SJA1000Class : public CANControllerClass { virtual int parsePacket(); virtual void onReceive(void(*callback)(int)); + virtual void onReceive(std::function callback); using CANControllerClass::filter; virtual int filter(int id, int mask); diff --git a/src/MCP2515.cpp b/src/MCP2515.cpp index a153a76..8a9bd5b 100644 --- a/src/MCP2515.cpp +++ b/src/MCP2515.cpp @@ -275,6 +275,23 @@ void MCP2515Class::onReceive(void(*callback)(int)) } } +void MCP2515Class::onReceive(std::function callback) +{ + CANControllerClass::onReceive(callback); + + pinMode(_intPin, INPUT); + + if (callback) { + SPI.usingInterrupt(digitalPinToInterrupt(_intPin)); + attachInterrupt(digitalPinToInterrupt(_intPin), MCP2515Class::onInterrupt, LOW); + } else { + detachInterrupt(digitalPinToInterrupt(_intPin)); +#ifdef SPI_HAS_NOTUSINGINTERRUPT + SPI.notUsingInterrupt(digitalPinToInterrupt(_intPin)); +#endif + } +} + int MCP2515Class::filter(int id, int mask) { id &= 0x7ff; @@ -442,8 +459,13 @@ void MCP2515Class::handleInterrupt() return; } - while (parsePacket() || _rxId != -1) { - _onReceive(available()); + while (parsePacket()) { + if (_onReceivePointer) { + _onReceivePointer(available()); + } + if (_onReceiveFunction) { + _onReceiveFunction(available()); + } } } diff --git a/src/MCP2515.h b/src/MCP2515.h index 2f0444f..3615623 100644 --- a/src/MCP2515.h +++ b/src/MCP2515.h @@ -35,6 +35,7 @@ class MCP2515Class : public CANControllerClass { virtual int parsePacket(); virtual void onReceive(void(*callback)(int)); + virtual void onReceive(std::function callback); using CANControllerClass::filter; virtual int filter(int id, int mask);