2016-07-11 22:12:58 -04:00
|
|
|
//
|
|
|
|
// DigitalPhaseLockedLoop.hpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 11/07/2016.
|
|
|
|
// Copyright © 2016 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef DigitalPhaseLockedLoop_hpp
|
|
|
|
#define DigitalPhaseLockedLoop_hpp
|
|
|
|
|
2016-07-12 20:23:56 -04:00
|
|
|
#include <memory>
|
2016-07-28 11:32:14 -04:00
|
|
|
#include <vector>
|
2017-07-25 20:20:55 -04:00
|
|
|
|
2017-09-22 22:39:23 -04:00
|
|
|
#include "../../../ClockReceiver/ClockReceiver.hpp"
|
2016-07-12 20:23:56 -04:00
|
|
|
|
2016-07-11 22:12:58 -04:00
|
|
|
namespace Storage {
|
|
|
|
|
2017-07-27 07:40:02 -04:00
|
|
|
class DigitalPhaseLockedLoop {
|
2016-07-11 22:12:58 -04:00
|
|
|
public:
|
|
|
|
/*!
|
|
|
|
Instantiates a @c DigitalPhaseLockedLoop.
|
|
|
|
|
|
|
|
@param clocks_per_bit The expected number of cycles between each bit of input.
|
2016-07-12 20:23:56 -04:00
|
|
|
@param length_of_history The number of historic pulses to consider in locking to phase.
|
2016-07-11 22:12:58 -04:00
|
|
|
*/
|
2017-07-16 19:03:50 -04:00
|
|
|
DigitalPhaseLockedLoop(int clocks_per_bit, size_t length_of_history);
|
2016-07-11 22:12:58 -04:00
|
|
|
|
|
|
|
/*!
|
|
|
|
Runs the loop, impliedly posting no pulses during that period.
|
|
|
|
|
|
|
|
@c number_of_cycles The time to run the loop for.
|
|
|
|
*/
|
2017-07-27 22:05:29 -04:00
|
|
|
void run_for(const Cycles cycles);
|
2016-07-11 22:12:58 -04:00
|
|
|
|
|
|
|
/*!
|
|
|
|
Announces a pulse at the current time.
|
|
|
|
*/
|
|
|
|
void add_pulse();
|
|
|
|
|
|
|
|
/*!
|
|
|
|
A receiver for PCM output data; called upon every recognised bit.
|
|
|
|
*/
|
|
|
|
class Delegate {
|
|
|
|
public:
|
2016-07-12 21:42:23 -04:00
|
|
|
virtual void digital_phase_locked_loop_output_bit(int value) = 0;
|
2016-07-11 22:12:58 -04:00
|
|
|
};
|
2017-03-26 14:34:47 -04:00
|
|
|
void set_delegate(Delegate *delegate) {
|
2016-12-03 11:59:28 -05:00
|
|
|
delegate_ = delegate;
|
2016-07-11 22:12:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-10-17 22:13:37 -04:00
|
|
|
Delegate *delegate_ = nullptr;
|
2016-07-12 20:23:56 -04:00
|
|
|
|
2017-07-16 16:49:04 -04:00
|
|
|
void post_phase_offset(int phase, int offset);
|
2016-07-12 20:23:56 -04:00
|
|
|
|
2017-07-16 16:49:04 -04:00
|
|
|
std::vector<int> offset_history_;
|
2017-10-17 22:13:37 -04:00
|
|
|
size_t offset_history_pointer_ = 0;
|
|
|
|
int offset_ = 0;
|
2017-07-16 16:49:04 -04:00
|
|
|
|
2017-10-17 22:13:37 -04:00
|
|
|
int phase_ = 0;
|
|
|
|
int window_length_ = 0;
|
|
|
|
bool window_was_filled_ = false;
|
2016-07-12 20:23:56 -04:00
|
|
|
|
2017-10-17 22:13:37 -04:00
|
|
|
int clocks_per_bit_ = 0;
|
|
|
|
int tolerance_ = 0;
|
2016-07-11 22:12:58 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* DigitalPhaseLockedLoop_hpp */
|