/*
 *
 *
 *    decoder.h
 *
 *    Experimental NOAA NWS / EAS data decoder.
 *
 *    Copyright (C) 2018 by Matt Roberts.
 *    License: GNU GPL3 (www.gnu.org)
 *
 *
 */

#ifndef __NWS_DECODER_H
#define __NWS_DECODER_H

#include <stdint.h>
#include <iostream>
#include <iomanip>

#include "bitclock.h"
#include "localtypes.h"

//
//  Decoder class -- responsible for clock restoration and ASCII decoding
//                   of the raw bit stream from the discriminator
//
class Decoder {
	public:
		// the clock recovery PLL
		KK5JY::DSP::BitClock<> bc;

		uint16_t sinceLast; // the number of samples since the last edge
		uint16_t next;      // samples until the next expected clock edge
		uint16_t period;    // the current phase-locked period
		uint16_t get;       // the value of 'next' at which to grab the next bit level
		bool last_out;      // the most recent output of the discriminator; used to track
		                    //   data edges
		#ifndef RAW_OUTPUT
		uint32_t shift_reg; // the shift register used for assembling data bytes
		uint16_t shift_ct;  // the number of bits shifted into the register
		bool lastPrint;     // indicates that the last character was printable
		bool mute;          // indicates that output is muted
		bool lastCR;        // indicates that the last character out was newline
		#endif

	public:
		// ctor
		Decoder(size_t bc_period)
			: bc(bc_period) {
			sinceLast = 0;
			next = bc.Period();
			period = bc.Period();
			get = bc.Period() / 2;
			last_out = false;
			#ifndef RAW_OUTPUT
			shift_reg = 0;
			shift_ct = 0;
			lastPrint = false;
			mute = true;
			lastCR = true;
			#endif
		}

		// process_sample(bool)
		void process_sample(bool output);
};

#endif // __NWS_DECODER_H
