mirror of
https://github.com/portapack-mayhem/mayhem-firmware.git
synced 2026-09-14 02:29:29 +00:00
SYNC
This commit is contained in:
@@ -28,12 +28,26 @@
|
||||
#include <functional>
|
||||
|
||||
#include "bit_pattern.hpp"
|
||||
#include "baseband_packet.hpp"
|
||||
|
||||
struct NeverMatch {
|
||||
bool operator()(const BitHistory&, const size_t) const {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
struct FixedLength {
|
||||
bool operator()(const BitHistory&, const size_t symbols_received) const {
|
||||
return symbols_received >= length;
|
||||
}
|
||||
|
||||
const size_t length;
|
||||
};
|
||||
|
||||
template<typename PreambleMatcher, typename UnstuffMatcher, typename EndMatcher>
|
||||
class PacketBuilder {
|
||||
public:
|
||||
using PayloadType = std::bitset<1024>;
|
||||
using PayloadHandlerFunc = std::function<void(const PayloadType& payload, const size_t bits_received)>;
|
||||
using PayloadHandlerFunc = std::function<void(const baseband::Packet& packet)>;
|
||||
|
||||
PacketBuilder(
|
||||
const PreambleMatcher preamble_matcher,
|
||||
@@ -64,18 +78,19 @@ public:
|
||||
|
||||
switch(state) {
|
||||
case State::Preamble:
|
||||
if( preamble(bit_history, bits_received) ) {
|
||||
if( preamble(bit_history, packet.size()) ) {
|
||||
state = State::Payload;
|
||||
}
|
||||
break;
|
||||
|
||||
case State::Payload:
|
||||
if( !unstuff(bit_history, bits_received) ) {
|
||||
payload[bits_received++] = symbol;
|
||||
if( !unstuff(bit_history, packet.size()) ) {
|
||||
packet.add(symbol);
|
||||
}
|
||||
|
||||
if( end(bit_history, bits_received) ) {
|
||||
payload_handler(payload, bits_received);
|
||||
if( end(bit_history, packet.size()) ) {
|
||||
packet.set_timestamp(Timestamp::now());
|
||||
payload_handler(packet);
|
||||
reset_state();
|
||||
} else {
|
||||
if( packet_truncated() ) {
|
||||
@@ -97,7 +112,7 @@ private:
|
||||
};
|
||||
|
||||
bool packet_truncated() const {
|
||||
return bits_received >= payload.size();
|
||||
return packet.size() >= packet.capacity();
|
||||
}
|
||||
|
||||
const PayloadHandlerFunc payload_handler;
|
||||
@@ -107,12 +122,11 @@ private:
|
||||
UnstuffMatcher unstuff;
|
||||
EndMatcher end;
|
||||
|
||||
size_t bits_received { 0 };
|
||||
State state { State::Preamble };
|
||||
PayloadType payload;
|
||||
baseband::Packet packet;
|
||||
|
||||
void reset_state() {
|
||||
bits_received = 0;
|
||||
packet.clear();
|
||||
state = State::Preamble;
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user