Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added std::function callback #132

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/CANController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "CANController.h"

CANControllerClass::CANControllerClass() :
_onReceive(NULL),
_onReceivePointer(NULL),

_packetBegun(false),
_txId(-1),
Expand Down Expand Up @@ -182,7 +182,12 @@ void CANControllerClass::flush()

void CANControllerClass::onReceive(void(*callback)(int))
{
_onReceive = callback;
_onReceivePointer = callback;
}

void CANControllerClass::onReceive(std::function<void(int)> callback)
{
_onReceiveFunction = callback;
}

int CANControllerClass::filter(int /*id*/, int /*mask*/)
Expand Down
4 changes: 3 additions & 1 deletion src/CANController.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class CANControllerClass : public Stream {
virtual void flush();

virtual void onReceive(void(*callback)(int));
virtual void onReceive(std::function<void(int)> callback);

virtual int filter(int id) { return filter(id, 0x7ff); }
virtual int filter(int id, int mask);
Expand All @@ -49,7 +50,8 @@ class CANControllerClass : public Stream {
virtual ~CANControllerClass();

protected:
void (*_onReceive)(int);
void (*_onReceivePointer)(int);
std::function<void(int)> _onReceiveFunction;

bool _packetBegun;
long _txId;
Expand Down
21 changes: 20 additions & 1 deletion src/ESP32SJA1000.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,20 @@ void ESP32SJA1000Class::onReceive(void(*callback)(int))
}
}

void ESP32SJA1000Class::onReceive(std::function<void(int)> 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;
Expand Down Expand Up @@ -380,7 +394,12 @@ void ESP32SJA1000Class::handleInterrupt()
// received packet, parse and call callback
parsePacket();

_onReceive(available());
if (_onReceivePointer) {
_onReceivePointer(available());
}
if (_onReceiveFunction) {
_onReceiveFunction(available());
}
}
}

Expand Down
1 change: 1 addition & 0 deletions src/ESP32SJA1000.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class ESP32SJA1000Class : public CANControllerClass {
virtual int parsePacket();

virtual void onReceive(void(*callback)(int));
virtual void onReceive(std::function<void(int)> callback);

using CANControllerClass::filter;
virtual int filter(int id, int mask);
Expand Down
26 changes: 24 additions & 2 deletions src/MCP2515.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,23 @@ void MCP2515Class::onReceive(void(*callback)(int))
}
}

void MCP2515Class::onReceive(std::function<void(int)> 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;
Expand Down Expand Up @@ -442,8 +459,13 @@ void MCP2515Class::handleInterrupt()
return;
}

while (parsePacket() || _rxId != -1) {
_onReceive(available());
while (parsePacket()) {
if (_onReceivePointer) {
_onReceivePointer(available());
}
if (_onReceiveFunction) {
_onReceiveFunction(available());
}
}
}

Expand Down
1 change: 1 addition & 0 deletions src/MCP2515.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class MCP2515Class : public CANControllerClass {
virtual int parsePacket();

virtual void onReceive(void(*callback)(int));
virtual void onReceive(std::function<void(int)> callback);

using CANControllerClass::filter;
virtual int filter(int id, int mask);
Expand Down