From 0e02cf820b868803316c90cff61acad826578d9f Mon Sep 17 00:00:00 2001 From: Damian Philipp Date: Mon, 4 Nov 2019 21:52:26 +0100 Subject: [PATCH 1/3] Add raw access to RX buffer. Allows for improved performance when accessing the entire payload rathrer than copying byte-by-byte with the Stream API. --- src/CANController.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/CANController.h b/src/CANController.h index cdaeb94..8f22924 100644 --- a/src/CANController.h +++ b/src/CANController.h @@ -44,6 +44,9 @@ class CANControllerClass : public Stream { virtual int sleep(); virtual int wakeup(); + /// Return the raw RX buffer for more efficient data access in high-performance scenarios. + uint8_t* getRxBuf() { return _rxData; } + protected: CANControllerClass(); virtual ~CANControllerClass(); From f55c2ceebf99e9c354a9ff4b0f5469c3e3de6f68 Mon Sep 17 00:00:00 2001 From: Damian Philipp Date: Tue, 5 Nov 2019 07:16:16 +0100 Subject: [PATCH 2/3] Add readBytes method from Stream API. --- src/CANController.cpp | 17 +++++++++++++++++ src/CANController.h | 4 ++++ 2 files changed, 21 insertions(+) diff --git a/src/CANController.cpp b/src/CANController.cpp index 0890eec..8bbbf4e 100644 --- a/src/CANController.cpp +++ b/src/CANController.cpp @@ -167,6 +167,23 @@ int CANControllerClass::read() return _rxData[_rxIndex++]; } +size_t CANControllerClass::readBytes(char *buffer, size_t length) { + int bytesAvailable = available(); + if (bytesAvailable <= 0) { + // No data left to consume. + return 0; + } + + if (bytesAvailable < length) { + // Limit to actually available data. + length = bytesAvailable; + } + + memcpy(buffer, _rxData, length); + _rxIndex += length; // Consume the data. + return length; +} + int CANControllerClass::peek() { if (!available()) { diff --git a/src/CANController.h b/src/CANController.h index 8f22924..88b100a 100644 --- a/src/CANController.h +++ b/src/CANController.h @@ -31,6 +31,10 @@ class CANControllerClass : public Stream { virtual int read(); virtual int peek(); virtual void flush(); + virtual size_t readBytes(char *buffer, size_t length); // read chars from stream into buffer + virtual size_t readBytes(uint8_t *buffer, size_t length) { + return readBytes((char*)buffer, length); + } virtual void onReceive(void(*callback)(int)); From 47c36a5b624c412cb620c9fe1177e79fef756999 Mon Sep 17 00:00:00 2001 From: Damian Philipp Date: Tue, 5 Nov 2019 20:59:06 +0100 Subject: [PATCH 3/3] Added method to get the receive message count. --- src/ESP32SJA1000.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/ESP32SJA1000.h b/src/ESP32SJA1000.h index b83cddc..f3d0e5e 100644 --- a/src/ESP32SJA1000.h +++ b/src/ESP32SJA1000.h @@ -26,6 +26,10 @@ class ESP32SJA1000Class : public CANControllerClass { virtual void onReceive(void(*callback)(int)); + uint8_t getRmc() { + return readRegister(29); + } + using CANControllerClass::filter; virtual int filter(int id, int mask); using CANControllerClass::filterExtended;