Initial code
This commit is contained in:
18
include/PacketCRC.h
Normal file
18
include/PacketCRC.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#ifndef PACKET_CRC_H
|
||||
#define PACKET_CRC_H
|
||||
|
||||
#include <CRC16.h>
|
||||
|
||||
class PacketCRC {
|
||||
public:
|
||||
PacketCRC();
|
||||
~PacketCRC();
|
||||
|
||||
// Method to calculate CRC for a packet
|
||||
bool check(const uint8_t* packet);
|
||||
uint16_t calculate(const uint8_t* packet);
|
||||
private:
|
||||
CRC16 crc;
|
||||
};
|
||||
|
||||
#endif // PACKET_CRC_H
|
||||
56
include/PacketParser.h
Normal file
56
include/PacketParser.h
Normal file
@@ -0,0 +1,56 @@
|
||||
#ifndef PACKET_PARSER_H
|
||||
#define PACKET_PARSER_H
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <variant>
|
||||
|
||||
// Define packet types
|
||||
enum class PacketType {
|
||||
OPEN,
|
||||
CLOSE,
|
||||
STOP,
|
||||
FIELDS,
|
||||
FIELD_COMMAND,
|
||||
UNKNOWN
|
||||
};
|
||||
|
||||
enum class FieldType {
|
||||
VALUE,
|
||||
SET,
|
||||
FETCH,
|
||||
UNKNOWN
|
||||
};
|
||||
|
||||
struct Field {
|
||||
uint8_t identifier;
|
||||
FieldType type;
|
||||
bool hasValue;
|
||||
std::variant<std::monostate, uint8_t, uint16_t> value;
|
||||
};
|
||||
|
||||
struct FieldsParameters {
|
||||
std::vector<Field> fields;
|
||||
};
|
||||
|
||||
using PacketParameters = std::variant<std::monostate, FieldsParameters>;
|
||||
|
||||
|
||||
// Define Message structure
|
||||
struct Packet {
|
||||
uint16_t source;
|
||||
uint16_t destination;
|
||||
PacketType type;
|
||||
PacketParameters parameters;
|
||||
};
|
||||
|
||||
class PacketParser {
|
||||
public:
|
||||
PacketParser();
|
||||
~PacketParser();
|
||||
|
||||
bool parsePacket(const uint8_t *buffer, Packet& message);
|
||||
private:
|
||||
bool parseFields(const uint8_t *buffer, std::vector<Field>& fields);
|
||||
};
|
||||
|
||||
#endif // PACKET_PARSER_H
|
||||
46
include/PacketReceiver.h
Normal file
46
include/PacketReceiver.h
Normal file
@@ -0,0 +1,46 @@
|
||||
#ifndef PACKET_RECEIVER_H
|
||||
#define PACKET_RECEIVER_H
|
||||
|
||||
#define CIRCULAR_BUFFER_INT_SAFE
|
||||
|
||||
#include <RF24.h>
|
||||
#include <CircularBuffer.h>
|
||||
#include "PacketCRC.h"
|
||||
|
||||
#define BUFFER_SIZE 32
|
||||
|
||||
#define TOTAL_BUFFER_COUNT 30
|
||||
#define RX_BUFFER_COUNT 3
|
||||
#define EMPTY_BUFFER_COUNT 30
|
||||
#define VALID_BUFFER_COUNT 5
|
||||
|
||||
class PacketReceiver {
|
||||
public:
|
||||
PacketReceiver(RF24 *radio);
|
||||
~PacketReceiver();
|
||||
|
||||
void loop();
|
||||
void read();
|
||||
|
||||
void setPacketCallback(void (*callback)(const uint8_t *buffer));
|
||||
void setInvalidPacketCallback(void (*callback)(const uint8_t *buffer));
|
||||
|
||||
private:
|
||||
RF24 *radio;
|
||||
PacketCRC packetCRC;
|
||||
|
||||
uint8_t buffers[TOTAL_BUFFER_COUNT][BUFFER_SIZE];
|
||||
CircularBuffer<uint8_t*, RX_BUFFER_COUNT> pendingBuffers;
|
||||
CircularBuffer<uint8_t*, EMPTY_BUFFER_COUNT> freeBuffers;
|
||||
CircularBuffer<uint8_t*, VALID_BUFFER_COUNT> receivedBuffers;
|
||||
|
||||
void (*packetCallback)(const uint8_t *buffer);
|
||||
void (*invalidPacketCallback)(const uint8_t *buffer);
|
||||
|
||||
bool isSanePacket(uint8_t *buffer);
|
||||
bool isValidPacket(uint8_t *buffer);
|
||||
bool isNewPacket(uint8_t *buffer);
|
||||
bool isEquivalentPacket(uint8_t *a, uint8_t *b);
|
||||
};
|
||||
|
||||
#endif // PACKET_RECEIVER_H
|
||||
Reference in New Issue
Block a user