From 09fd491fdc3bb517d8a8b2ee9301f28288da024f Mon Sep 17 00:00:00 2001 From: Matt Way Date: Sat, 30 Dec 2023 23:34:06 +1100 Subject: [PATCH] Add packetCallback to RFPowerView class --- include/PacketReceiver.h | 16 ++++++++-------- include/RFPowerView.h | 9 +++++++-- src/PacketReceiver.cpp | 38 +++++++++++++++++++------------------- src/RFPowerView.cpp | 21 +++++++++++++-------- 4 files changed, 47 insertions(+), 37 deletions(-) diff --git a/include/PacketReceiver.h b/include/PacketReceiver.h index db1fdbf..6cdfc78 100644 --- a/include/PacketReceiver.h +++ b/include/PacketReceiver.h @@ -23,8 +23,8 @@ public: void loop(); void read(); - void setPacketCallback(std::function callback); - void setInvalidPacketCallback(std::function callback); + void setBufferCallback(std::function callback); + void setInvalidBufferCallback(std::function callback); private: RF24 *radio; @@ -35,13 +35,13 @@ private: CircularBuffer freeBuffers; CircularBuffer receivedBuffers; - std::function packetCallback; - std::function invalidPacketCallback; + std::function bufferCallback; + std::function invalidBufferCallback; - bool isSanePacket(uint8_t *buffer); - bool isValidPacket(uint8_t *buffer); - bool isNewPacket(uint8_t *buffer); - bool isEquivalentPacket(uint8_t *a, uint8_t *b); + bool isSaneBuffer(uint8_t *buffer); + bool isValidBuffer(uint8_t *buffer); + bool isNewBuffer(uint8_t *buffer); + bool isEquivalentBuffer(uint8_t *a, uint8_t *b); }; #endif // PACKET_RECEIVER_H \ No newline at end of file diff --git a/include/RFPowerView.h b/include/RFPowerView.h index b7c6351..f88ae39 100644 --- a/include/RFPowerView.h +++ b/include/RFPowerView.h @@ -22,6 +22,9 @@ public: bool begin(); void loop(); void startListening(); + + void setPacketCallback(std::function callback); + private: RF24 radio; PacketReceiver packetReceiver; @@ -29,10 +32,12 @@ private: uint8_t irqPin; uint8_t rfID[2]; + std::function packetCallback; + void interruptHandler(); - void processPacketBuffer(const uint8_t *buffer); - void processInvalidPacketBuffer(const uint8_t *buffer); + void processBuffer(const uint8_t *buffer); + void processInvalidBuffer(const uint8_t *buffer); }; #endif // RFPOWERVIEW_H \ No newline at end of file diff --git a/src/PacketReceiver.cpp b/src/PacketReceiver.cpp index ef1ba59..29f62b6 100644 --- a/src/PacketReceiver.cpp +++ b/src/PacketReceiver.cpp @@ -18,20 +18,20 @@ void PacketReceiver::loop() { while (!pendingBuffers.isEmpty()) { uint8_t *p = pendingBuffers.pop(); - bool isSanePacket = this->isSanePacket(p); - bool isValidPacket = this->isValidPacket(p); - bool isNewPacket = this->isNewPacket(p); + bool isSaneBuffer = this->isSaneBuffer(p); + bool isValidBuffer = this->isValidBuffer(p); + bool isNewBuffer = this->isNewBuffer(p); // TODO: We don't keep old invalid packets around, so invalid packets will always be new for now - if (isSanePacket && !isValidPacket && isNewPacket) { - if (invalidPacketCallback != nullptr) { - invalidPacketCallback(p); + if (isSaneBuffer && !isValidBuffer && isNewBuffer) { + if (invalidBufferCallback != nullptr) { + invalidBufferCallback(p); } } - if (isValidPacket && isNewPacket) { - if (packetCallback != nullptr) { - this->packetCallback(p); + if (isValidBuffer && isNewBuffer) { + if (bufferCallback != nullptr) { + this->bufferCallback(p); } // If there is no space to store this new packet, return the oldest packet @@ -69,16 +69,16 @@ void PacketReceiver::read() { } } -void PacketReceiver::setPacketCallback(std::function callback) { - this->packetCallback = callback; +void PacketReceiver::setBufferCallback(std::function callback) { + this->bufferCallback = callback; } -void PacketReceiver::setInvalidPacketCallback(std::function callback) +void PacketReceiver::setInvalidBufferCallback(std::function callback) { - this->invalidPacketCallback = callback; + this->invalidBufferCallback = callback; } -bool PacketReceiver::isSanePacket(uint8_t *buffer) { +bool PacketReceiver::isSaneBuffer(uint8_t *buffer) { // Sanity check that the buffer isn't just filled with noise // First byte contains a header // Second byte contains the packet length which cannot be larger than 28 bytes (32 bytes - 1 header byte - 1 length byte - 2 CRC bytes) @@ -88,8 +88,8 @@ bool PacketReceiver::isSanePacket(uint8_t *buffer) { return false; } -bool PacketReceiver::isValidPacket(uint8_t *buffer) { - if (isSanePacket(buffer)) { +bool PacketReceiver::isValidBuffer(uint8_t *buffer) { + if (isSaneBuffer(buffer)) { if (packetCRC.check(buffer)) { return true; } @@ -97,16 +97,16 @@ bool PacketReceiver::isValidPacket(uint8_t *buffer) { return false; } -bool PacketReceiver::isNewPacket(uint8_t *buffer) { +bool PacketReceiver::isNewBuffer(uint8_t *buffer) { for (int i = 0; i < receivedBuffers.size(); i++) { - if (isEquivalentPacket(buffer, receivedBuffers[i])) { + if (isEquivalentBuffer(buffer, receivedBuffers[i])) { return false; } } return true; } -bool PacketReceiver::isEquivalentPacket(uint8_t *a, uint8_t *b) { +bool PacketReceiver::isEquivalentBuffer(uint8_t *a, uint8_t *b) { // Check length byte if (a[1] != b[1]) { return false; diff --git a/src/RFPowerView.cpp b/src/RFPowerView.cpp index 955e255..8ac1016 100644 --- a/src/RFPowerView.cpp +++ b/src/RFPowerView.cpp @@ -5,15 +5,16 @@ RFPowerView::RFPowerView(uint8_t cePin, uint8_t csPin, uint8_t irqPin, uint16_t radio(cePin, csPin), packetReceiver(&radio), irqPin(irqPin), - rfID{static_cast(rfID & 0xFF), static_cast(rfID >> 8)} {} + rfID{static_cast(rfID & 0xFF), static_cast(rfID >> 8)}, + packetCallback(nullptr) {} bool RFPowerView::begin() { if (!radio.begin()) { return false; } - packetReceiver.setPacketCallback([this](const uint8_t* buffer) { this->processPacketBuffer(buffer); }); - packetReceiver.setInvalidPacketCallback([this](const uint8_t* buffer) { this->processInvalidPacketBuffer(buffer); }); + packetReceiver.setBufferCallback([this](const uint8_t* buffer) { this->processBuffer(buffer); }); + packetReceiver.setInvalidBufferCallback([this](const uint8_t* buffer) { this->processInvalidBuffer(buffer); }); startListening(); @@ -48,16 +49,20 @@ void RFPowerView::interruptHandler() { packetReceiver.read(); } -void RFPowerView::processPacketBuffer(const uint8_t *buffer) { +void RFPowerView::processBuffer(const uint8_t *buffer) { Packet packet; bool result = packetParser.parsePacket(buffer, packet); if (result) { - Serial.print("Parsed packet! "); - Serial.print((int)packet.type); - Serial.println(); + if (packetCallback != nullptr) { + packetCallback(&packet); + } } } -void RFPowerView::processInvalidPacketBuffer(const uint8_t *buffer) { +void RFPowerView::processInvalidBuffer(const uint8_t *buffer) { +} + +void RFPowerView::setPacketCallback(std::function callback) { + packetCallback = callback; } \ No newline at end of file