mirror of
https://github.com/portapack-mayhem/mayhem-firmware.git
synced 2024-12-13 11:44:31 +00:00
Decouple PacketBuilder PayloadHandler function from method type.
This commit is contained in:
parent
1bdad62496
commit
cf3bff3b03
@ -25,21 +25,29 @@
|
||||
#include <cstdint>
|
||||
#include <cstddef>
|
||||
#include <bitset>
|
||||
#include <functional>
|
||||
|
||||
#include "bit_pattern.hpp"
|
||||
|
||||
class PacketBuilder {
|
||||
public:
|
||||
using PayloadType = std::bitset<256>;
|
||||
using PayloadHandlerFunc = std::function<void(const PayloadType& payload, const size_t bits_received)>;
|
||||
|
||||
PacketBuilder(
|
||||
const PayloadHandlerFunc payload_handler
|
||||
) : payload_handler { payload_handler }
|
||||
{
|
||||
}
|
||||
|
||||
void configure(
|
||||
const BitPattern preamble,
|
||||
const BitPattern unstuffing,
|
||||
size_t new_payload_length
|
||||
);
|
||||
|
||||
template<typename PayloadHandler>
|
||||
void execute(
|
||||
const uint_fast8_t symbol,
|
||||
PayloadHandler payload_handler
|
||||
const uint_fast8_t symbol
|
||||
) {
|
||||
bit_history.add(symbol);
|
||||
|
||||
@ -89,6 +97,8 @@ private:
|
||||
return bit_history.matches(end_flag_pattern);
|
||||
}
|
||||
|
||||
const PayloadHandlerFunc payload_handler;
|
||||
|
||||
BitHistory bit_history;
|
||||
BitPattern preamble_pattern { 0b01010101010101010101111110, 26, 1 };
|
||||
BitPattern unstuff_pattern { 0b111110, 6 };
|
||||
@ -97,7 +107,7 @@ private:
|
||||
size_t payload_length { 0 };
|
||||
size_t bits_received { 0 };
|
||||
State state { State::Preamble };
|
||||
std::bitset<256> payload;
|
||||
PayloadType payload;
|
||||
|
||||
void reset_state();
|
||||
};
|
||||
|
@ -103,20 +103,10 @@ void FSKProcessor::execute(buffer_c8_t buffer) {
|
||||
void FSKProcessor::consume_symbol(
|
||||
const float raw_symbol
|
||||
) {
|
||||
const auto payload_handler_fn = [this](
|
||||
const std::bitset<256>& payload,
|
||||
const size_t bits_received
|
||||
) {
|
||||
this->payload_handler(payload, bits_received);
|
||||
};
|
||||
|
||||
const uint_fast8_t sliced_symbol = (raw_symbol >= 0.0f) ? 1 : 0;
|
||||
const auto decoded_symbol = nrzi_decode(sliced_symbol);
|
||||
|
||||
packet_builder.execute(
|
||||
decoded_symbol,
|
||||
payload_handler_fn
|
||||
);
|
||||
packet_builder.execute(decoded_symbol);
|
||||
}
|
||||
|
||||
void FSKProcessor::payload_handler(
|
||||
|
@ -84,7 +84,11 @@ private:
|
||||
[this](const float symbol) { this->consume_symbol(symbol); }
|
||||
};
|
||||
symbol_coding::NRZIDecoder nrzi_decode;
|
||||
PacketBuilder packet_builder;
|
||||
PacketBuilder packet_builder {
|
||||
[this](const PacketBuilder::PayloadType& payload, const size_t bits_received) {
|
||||
this->payload_handler(payload, bits_received);
|
||||
}
|
||||
};
|
||||
|
||||
MessageHandlerMap& message_handlers;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user