From c936e097027f98d4630b8735dee189141271ce5f Mon Sep 17 00:00:00 2001 From: Jared Boone Date: Sat, 26 Sep 2015 14:14:39 -0700 Subject: [PATCH] Add bit unstuffing algorithm. For use in AIS. Probably useful elsewhere, too... --- firmware/baseband/symbol_coding.hpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/firmware/baseband/symbol_coding.hpp b/firmware/baseband/symbol_coding.hpp index d474f4b17..2f8d75b91 100644 --- a/firmware/baseband/symbol_coding.hpp +++ b/firmware/baseband/symbol_coding.hpp @@ -23,6 +23,7 @@ #define __SYMBOL_CODING_H__ #include +#include namespace symbol_coding { @@ -38,6 +39,32 @@ private: uint_fast8_t last { 0 }; }; +class Unstuff { +public: + uint_fast8_t is_stuffing_bit(const uint_fast8_t symbol) { + history = (history << 1) | (symbol & 1); + return (history & mask) == match; + } + + void configure( + const uint32_t pattern, + const size_t length + ) { + match = pattern; + mask = (1U << length) - 1; + reset(); + } + + void reset() { + history = 0; + } + +private: + uint32_t history { 0 }; + uint32_t match { 0b111110 }; + uint32_t mask { 0b111111 }; +}; + } /* namespace symbol_coding */ #endif/*__SYMBOL_CODING_H__*/