From d75f601b54429413fc89bf9ff8d5ae48eeaccc06 Mon Sep 17 00:00:00 2001 From: Jared Boone Date: Tue, 1 Dec 2015 12:05:42 -0800 Subject: [PATCH] Manchester decoder into separate class. Now operates directly on data, doesn't make a new pair of data/error bitsets. --- firmware/application/ui_receiver.cpp | 85 +++++++++++++++------------- 1 file changed, 46 insertions(+), 39 deletions(-) diff --git a/firmware/application/ui_receiver.cpp b/firmware/application/ui_receiver.cpp index b82ed146..0264952b 100644 --- a/firmware/application/ui_receiver.cpp +++ b/firmware/application/ui_receiver.cpp @@ -544,30 +544,60 @@ void ReceiverView::on_packet_ais(const AISPacketMessage& message) { static FIL fil_tpms; +class ManchesterDecoder { +public: + struct DecodedSymbol { + bool value; + bool error; + }; + + constexpr ManchesterDecoder( + const std::bitset<1024>& encoded, + const size_t count, + const size_t sense = 0 + ) : encoded { encoded }, + count { count }, + sense { sense } + { + } + + DecodedSymbol operator[](const size_t index) const { + const auto value = encoded[index * 2 + sense]; + const auto error = encoded[index * 2 + 0] == encoded[index * 2 + 1]; + return { value, error }; + } + + size_t symbols_count() const { + return count / 2; + } + +private: + const std::bitset<1024>& encoded; + const size_t count; + const size_t sense; +}; + static std::pair format_manchester( - const uint_fast8_t sense, - const std::bitset<1024>& payload, - const size_t payload_length + const ManchesterDecoder& decoder ) { - const size_t payload_length_decoded = payload_length / 2; + const size_t payload_length_decoded = decoder.symbols_count(); const size_t payload_length_bytes = (payload_length_decoded + 7) / 8; - const size_t payload_length_symbols_rounded = payload_length_bytes * 8 * 2; + const size_t payload_length_symbols_rounded = payload_length_bytes * 8; std::string hex_data; std::string hex_error; uint8_t byte_data = 0; uint8_t byte_error = 0; - for(size_t i=0; i> 1) & 7) == 7 ) { + if( (i & 7) == 7 ) { hex_data += to_string_hex(byte_data, 2); hex_error += to_string_hex(byte_error, 2); } @@ -577,10 +607,8 @@ static std::pair format_manchester( } void ReceiverView::on_packet_tpms(const TPMSPacketMessage& message) { - auto payload = message.packet.payload; - auto payload_length = message.packet.bits_received; - - const auto hex_formatted = format_manchester(1, payload, payload_length); + const ManchesterDecoder decoder(message.packet.payload, message.packet.bits_received, 1); + const auto hex_formatted = format_manchester(decoder); auto console = reinterpret_cast(widget_content.get()); console->writeln(hex_formatted.first.substr(0, 240 / 8)); @@ -606,29 +634,6 @@ void ReceiverView::on_packet_tpms(const TPMSPacketMessage& message) { } } -static std::bitset<512> manchester_decode( - const size_t sense, - const std::bitset<1024>& encoded, - const size_t count -) { - std::bitset<512> result; - for(size_t i=0; i> 1] = encoded[i+sense]; - } - return result; -} - -static std::bitset<512> manchester_errors( - const std::bitset<1024>& encoded, - const size_t count -) { - std::bitset<512> result; - for(size_t i=0; i> 1] = (encoded[i+0] == encoded[i+1]); - } - return result; -} - void ReceiverView::on_packet_ert(const ERTPacketMessage& message) { auto console = reinterpret_cast(widget_content.get()); @@ -639,7 +644,9 @@ void ReceiverView::on_packet_ert(const ERTPacketMessage& message) { console->writeln("SCM"); } - const auto hex_formatted = format_manchester(0, message.packet.payload, message.packet.bits_received); + const ManchesterDecoder decoder(message.packet.payload, message.packet.bits_received); + + const auto hex_formatted = format_manchester(decoder); console->writeln(hex_formatted.first); console->writeln(hex_formatted.second); }