Skip to content

Add raw access to RX buffer. #27

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

Open
wants to merge 3 commits 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
17 changes: 17 additions & 0 deletions src/CANController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
7 changes: 7 additions & 0 deletions src/CANController.h
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand All @@ -44,6 +48,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();
Expand Down
4 changes: 4 additions & 0 deletions src/ESP32SJA1000.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down