Add packetCallback to RFPowerView class
This commit is contained in:
@@ -23,8 +23,8 @@ public:
|
|||||||
void loop();
|
void loop();
|
||||||
void read();
|
void read();
|
||||||
|
|
||||||
void setPacketCallback(std::function<void(const uint8_t*)> callback);
|
void setBufferCallback(std::function<void(const uint8_t*)> callback);
|
||||||
void setInvalidPacketCallback(std::function<void(const uint8_t*)> callback);
|
void setInvalidBufferCallback(std::function<void(const uint8_t*)> callback);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
RF24 *radio;
|
RF24 *radio;
|
||||||
@@ -35,13 +35,13 @@ private:
|
|||||||
CircularBuffer<uint8_t*, EMPTY_BUFFER_COUNT> freeBuffers;
|
CircularBuffer<uint8_t*, EMPTY_BUFFER_COUNT> freeBuffers;
|
||||||
CircularBuffer<uint8_t*, VALID_BUFFER_COUNT> receivedBuffers;
|
CircularBuffer<uint8_t*, VALID_BUFFER_COUNT> receivedBuffers;
|
||||||
|
|
||||||
std::function<void(const uint8_t*)> packetCallback;
|
std::function<void(const uint8_t*)> bufferCallback;
|
||||||
std::function<void(const uint8_t*)> invalidPacketCallback;
|
std::function<void(const uint8_t*)> invalidBufferCallback;
|
||||||
|
|
||||||
bool isSanePacket(uint8_t *buffer);
|
bool isSaneBuffer(uint8_t *buffer);
|
||||||
bool isValidPacket(uint8_t *buffer);
|
bool isValidBuffer(uint8_t *buffer);
|
||||||
bool isNewPacket(uint8_t *buffer);
|
bool isNewBuffer(uint8_t *buffer);
|
||||||
bool isEquivalentPacket(uint8_t *a, uint8_t *b);
|
bool isEquivalentBuffer(uint8_t *a, uint8_t *b);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // PACKET_RECEIVER_H
|
#endif // PACKET_RECEIVER_H
|
||||||
@@ -22,6 +22,9 @@ public:
|
|||||||
bool begin();
|
bool begin();
|
||||||
void loop();
|
void loop();
|
||||||
void startListening();
|
void startListening();
|
||||||
|
|
||||||
|
void setPacketCallback(std::function<void(const Packet*)> callback);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
RF24 radio;
|
RF24 radio;
|
||||||
PacketReceiver packetReceiver;
|
PacketReceiver packetReceiver;
|
||||||
@@ -29,10 +32,12 @@ private:
|
|||||||
uint8_t irqPin;
|
uint8_t irqPin;
|
||||||
uint8_t rfID[2];
|
uint8_t rfID[2];
|
||||||
|
|
||||||
|
std::function<void(const Packet*)> packetCallback;
|
||||||
|
|
||||||
void interruptHandler();
|
void interruptHandler();
|
||||||
|
|
||||||
void processPacketBuffer(const uint8_t *buffer);
|
void processBuffer(const uint8_t *buffer);
|
||||||
void processInvalidPacketBuffer(const uint8_t *buffer);
|
void processInvalidBuffer(const uint8_t *buffer);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // RFPOWERVIEW_H
|
#endif // RFPOWERVIEW_H
|
||||||
@@ -18,20 +18,20 @@ void PacketReceiver::loop() {
|
|||||||
while (!pendingBuffers.isEmpty()) {
|
while (!pendingBuffers.isEmpty()) {
|
||||||
uint8_t *p = pendingBuffers.pop();
|
uint8_t *p = pendingBuffers.pop();
|
||||||
|
|
||||||
bool isSanePacket = this->isSanePacket(p);
|
bool isSaneBuffer = this->isSaneBuffer(p);
|
||||||
bool isValidPacket = this->isValidPacket(p);
|
bool isValidBuffer = this->isValidBuffer(p);
|
||||||
bool isNewPacket = this->isNewPacket(p);
|
bool isNewBuffer = this->isNewBuffer(p);
|
||||||
|
|
||||||
// TODO: We don't keep old invalid packets around, so invalid packets will always be new for now
|
// TODO: We don't keep old invalid packets around, so invalid packets will always be new for now
|
||||||
if (isSanePacket && !isValidPacket && isNewPacket) {
|
if (isSaneBuffer && !isValidBuffer && isNewBuffer) {
|
||||||
if (invalidPacketCallback != nullptr) {
|
if (invalidBufferCallback != nullptr) {
|
||||||
invalidPacketCallback(p);
|
invalidBufferCallback(p);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isValidPacket && isNewPacket) {
|
if (isValidBuffer && isNewBuffer) {
|
||||||
if (packetCallback != nullptr) {
|
if (bufferCallback != nullptr) {
|
||||||
this->packetCallback(p);
|
this->bufferCallback(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
// If there is no space to store this new packet, return the oldest packet
|
// 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<void(const uint8_t*)> callback) {
|
void PacketReceiver::setBufferCallback(std::function<void(const uint8_t*)> callback) {
|
||||||
this->packetCallback = callback;
|
this->bufferCallback = callback;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PacketReceiver::setInvalidPacketCallback(std::function<void(const uint8_t*)> callback)
|
void PacketReceiver::setInvalidBufferCallback(std::function<void(const uint8_t*)> 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
|
// Sanity check that the buffer isn't just filled with noise
|
||||||
// First byte contains a header
|
// 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)
|
// 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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PacketReceiver::isValidPacket(uint8_t *buffer) {
|
bool PacketReceiver::isValidBuffer(uint8_t *buffer) {
|
||||||
if (isSanePacket(buffer)) {
|
if (isSaneBuffer(buffer)) {
|
||||||
if (packetCRC.check(buffer)) {
|
if (packetCRC.check(buffer)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -97,16 +97,16 @@ bool PacketReceiver::isValidPacket(uint8_t *buffer) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PacketReceiver::isNewPacket(uint8_t *buffer) {
|
bool PacketReceiver::isNewBuffer(uint8_t *buffer) {
|
||||||
for (int i = 0; i < receivedBuffers.size(); i++) {
|
for (int i = 0; i < receivedBuffers.size(); i++) {
|
||||||
if (isEquivalentPacket(buffer, receivedBuffers[i])) {
|
if (isEquivalentBuffer(buffer, receivedBuffers[i])) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PacketReceiver::isEquivalentPacket(uint8_t *a, uint8_t *b) {
|
bool PacketReceiver::isEquivalentBuffer(uint8_t *a, uint8_t *b) {
|
||||||
// Check length byte
|
// Check length byte
|
||||||
if (a[1] != b[1]) {
|
if (a[1] != b[1]) {
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -5,15 +5,16 @@ RFPowerView::RFPowerView(uint8_t cePin, uint8_t csPin, uint8_t irqPin, uint16_t
|
|||||||
radio(cePin, csPin),
|
radio(cePin, csPin),
|
||||||
packetReceiver(&radio),
|
packetReceiver(&radio),
|
||||||
irqPin(irqPin),
|
irqPin(irqPin),
|
||||||
rfID{static_cast<uint8_t>(rfID & 0xFF), static_cast<uint8_t>(rfID >> 8)} {}
|
rfID{static_cast<uint8_t>(rfID & 0xFF), static_cast<uint8_t>(rfID >> 8)},
|
||||||
|
packetCallback(nullptr) {}
|
||||||
|
|
||||||
bool RFPowerView::begin() {
|
bool RFPowerView::begin() {
|
||||||
if (!radio.begin()) {
|
if (!radio.begin()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
packetReceiver.setPacketCallback([this](const uint8_t* buffer) { this->processPacketBuffer(buffer); });
|
packetReceiver.setBufferCallback([this](const uint8_t* buffer) { this->processBuffer(buffer); });
|
||||||
packetReceiver.setInvalidPacketCallback([this](const uint8_t* buffer) { this->processInvalidPacketBuffer(buffer); });
|
packetReceiver.setInvalidBufferCallback([this](const uint8_t* buffer) { this->processInvalidBuffer(buffer); });
|
||||||
|
|
||||||
startListening();
|
startListening();
|
||||||
|
|
||||||
@@ -48,16 +49,20 @@ void RFPowerView::interruptHandler() {
|
|||||||
packetReceiver.read();
|
packetReceiver.read();
|
||||||
}
|
}
|
||||||
|
|
||||||
void RFPowerView::processPacketBuffer(const uint8_t *buffer) {
|
void RFPowerView::processBuffer(const uint8_t *buffer) {
|
||||||
Packet packet;
|
Packet packet;
|
||||||
bool result = packetParser.parsePacket(buffer, packet);
|
bool result = packetParser.parsePacket(buffer, packet);
|
||||||
if (result) {
|
if (result) {
|
||||||
Serial.print("Parsed packet! ");
|
if (packetCallback != nullptr) {
|
||||||
Serial.print((int)packet.type);
|
packetCallback(&packet);
|
||||||
Serial.println();
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void RFPowerView::processInvalidPacketBuffer(const uint8_t *buffer) {
|
void RFPowerView::processInvalidBuffer(const uint8_t *buffer) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void RFPowerView::setPacketCallback(std::function<void(const Packet*)> callback) {
|
||||||
|
packetCallback = callback;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user