// --------------------------------------------------------------
#ifndef _RADIO_DESYMBOLIZER_HPP_
#define	_RADIO_DESYMBOLIZER_HPP_

#include <radio/radio_types.h>
#include <radio/cpx.h>
#include "DeFormatter.hpp"


class DeSymbolizer : public Processor::Block
{
	enum State
	{
		Idle,
		Acq,
		Trk
	};

	static const size_t SYNC_COUNTER_RELOAD = 3;

public:
	DeSymbolizer();
	virtual ~DeSymbolizer() = default;

	void setNumBitsPerSymbol(size_t nBitsPerSym);

	// Processor::Block
	void prepare() override;
	void process() override;

	size_t sym2bytes(symbol_t symbol, uint8_t *pBytes);

private:
	size_t m_nBitsPerSym;
	size_t m_dataBitCount;
	symbol_t m_lastSymbol;
	symbol_t m_symbolMask;
	symbol_t differentialDecode(symbol_t symbol);
	State m_state;
	size_t m_sync_dist;
	size_t m_sync_dist_last;
	size_t m_sync_counter;

	Processor::Buffer<symbol_t> *m_buf_in;
	Processor::Buffer<uint8_t> *m_buf_out;
	void process(symbol_t symbol);
	void sync(symbol_t symbol);
	bool is_preamble(uint16_t data);
};

// --------------------------------------------------------------
#endif // _RADIO_DESYMBOLIZER_HPP_
