2016-07-29 11:15:46 +00:00
|
|
|
//
|
|
|
|
// TimedEventLoop.hpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 29/07/2016.
|
2018-05-13 19:19:52 +00:00
|
|
|
// Copyright 2016 Thomas Harte. All rights reserved.
|
2016-07-29 11:15:46 +00:00
|
|
|
//
|
|
|
|
|
2024-01-17 04:34:46 +00:00
|
|
|
#pragma once
|
2016-07-29 11:15:46 +00:00
|
|
|
|
|
|
|
#include "Storage.hpp"
|
2017-07-26 00:20:55 +00:00
|
|
|
#include "../ClockReceiver/ClockReceiver.hpp"
|
2016-07-29 11:15:46 +00:00
|
|
|
#include "../SignalProcessing/Stepper.hpp"
|
2017-07-26 00:20:55 +00:00
|
|
|
|
|
|
|
#include <memory>
|
2016-07-29 11:15:46 +00:00
|
|
|
|
|
|
|
namespace Storage {
|
|
|
|
|
2016-08-01 10:04:55 +00:00
|
|
|
/*!
|
|
|
|
Provides a mechanism for arbitrarily timed events to be processed according to a fixed-base
|
|
|
|
discrete clock signal, ensuring correct timing.
|
|
|
|
|
|
|
|
Subclasses are responsible for calling @c set_next_event_time_interval to establish the time
|
|
|
|
until a next event; @c process_next_event will be called when that event occurs, with progression
|
2017-07-25 01:51:22 +00:00
|
|
|
determined via @c run_for.
|
2016-08-01 10:04:55 +00:00
|
|
|
|
2018-05-13 19:34:31 +00:00
|
|
|
Due to the aggregation of total timing information between events, e.g. if an event loop has
|
2016-08-01 10:04:55 +00:00
|
|
|
a clock rate of 1000 ticks per second and a steady stream of events that occur 10,000 times a second,
|
2018-05-13 19:34:31 +00:00
|
|
|
bookkeeping is necessary to ensure that 10 events are triggered per tick. Subclasses should call
|
2016-08-01 10:04:55 +00:00
|
|
|
@c reset_timer if there is a discontinuity in events.
|
|
|
|
|
|
|
|
Subclasses may also call @c jump_to_next_event to cause the next event to be communicated instantly.
|
|
|
|
|
|
|
|
Subclasses are therefore expected to call @c set_next_event_time_interval upon obtaining an event stream,
|
|
|
|
and again in response to each call to @c process_next_event while events are ongoing. They may use
|
|
|
|
@c reset_timer to initiate a distinctly-timed stream or @c jump_to_next_event to short-circuit the timing
|
|
|
|
loop and fast forward immediately to the next event.
|
|
|
|
*/
|
2017-07-27 11:40:02 +00:00
|
|
|
class TimedEventLoop {
|
2016-07-29 11:15:46 +00:00
|
|
|
public:
|
2016-08-01 10:04:55 +00:00
|
|
|
/*!
|
|
|
|
Constructs a timed event loop that will be clocked at @c input_clock_rate.
|
|
|
|
*/
|
2019-10-30 02:36:29 +00:00
|
|
|
TimedEventLoop(Cycles::IntType input_clock_rate);
|
2016-08-01 10:04:55 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
Advances the event loop by @c number_of_cycles cycles.
|
|
|
|
*/
|
2017-07-28 02:05:29 +00:00
|
|
|
void run_for(const Cycles cycles);
|
2016-07-29 11:15:46 +00:00
|
|
|
|
2016-09-18 14:30:52 +00:00
|
|
|
/*!
|
|
|
|
@returns the number of whole cycles remaining until the next event is triggered.
|
|
|
|
*/
|
2019-12-25 01:53:37 +00:00
|
|
|
Cycles::IntType get_cycles_until_next_event() const;
|
2016-09-17 23:52:27 +00:00
|
|
|
|
2017-09-10 21:31:43 +00:00
|
|
|
/*!
|
|
|
|
@returns the input clock rate.
|
|
|
|
*/
|
2019-12-25 01:53:37 +00:00
|
|
|
Cycles::IntType get_input_clock_rate() const;
|
2017-09-10 21:31:43 +00:00
|
|
|
|
2016-07-29 11:15:46 +00:00
|
|
|
protected:
|
2016-08-01 10:04:55 +00:00
|
|
|
/*!
|
|
|
|
Sets the time interval, as a proportion of a second, until the next event should be triggered.
|
|
|
|
*/
|
2016-07-29 11:15:46 +00:00
|
|
|
void set_next_event_time_interval(Time interval);
|
2019-07-02 19:43:03 +00:00
|
|
|
void set_next_event_time_interval(float interval);
|
2016-07-29 11:15:46 +00:00
|
|
|
|
2016-08-01 10:04:55 +00:00
|
|
|
/*!
|
|
|
|
Communicates that the next event is triggered. A subclass will idiomatically process that event
|
|
|
|
and make a fresh call to @c set_next_event_time_interval to keep the event loop running.
|
|
|
|
*/
|
2016-07-29 11:15:46 +00:00
|
|
|
virtual void process_next_event() = 0;
|
|
|
|
|
2017-09-10 18:44:38 +00:00
|
|
|
/*!
|
|
|
|
Optionally allows a subclass to track time within run_for periods; if a subclass implements
|
|
|
|
advnace then it will receive advance increments that add up to the number of cycles supplied
|
|
|
|
to run_for, but calls to process_next_event will be precisely interspersed. No time will carry
|
|
|
|
forward between calls into run_for; a subclass can receive arbitrarily many instructions to
|
|
|
|
advance before receiving a process_next_event.
|
|
|
|
*/
|
2020-05-30 04:37:06 +00:00
|
|
|
virtual void advance([[maybe_unused]] const Cycles cycles) {};
|
2017-09-10 18:44:38 +00:00
|
|
|
|
2016-08-01 10:04:55 +00:00
|
|
|
/*!
|
|
|
|
Resets timing, throwing away any current internal state. So clears any fractional ticks
|
|
|
|
that the event loop is currently tracking.
|
|
|
|
*/
|
|
|
|
void reset_timer();
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Causes an immediate call to @c process_next_event and a call to @c reset_timer with the
|
|
|
|
net effect of processing the current event immediately and fast forwarding exactly to the
|
|
|
|
start of the interval prior to the next event.
|
|
|
|
*/
|
|
|
|
void jump_to_next_event();
|
|
|
|
|
2016-08-03 11:49:00 +00:00
|
|
|
/*!
|
|
|
|
@returns the amount of time that has passed since the last call to @c set_next_time_interval,
|
|
|
|
which will always be less than or equal to the time that was supplied to @c set_next_time_interval.
|
|
|
|
*/
|
|
|
|
Time get_time_into_next_event();
|
|
|
|
|
2016-07-29 11:15:46 +00:00
|
|
|
private:
|
2019-10-30 02:36:29 +00:00
|
|
|
Cycles::IntType input_clock_rate_ = 0;
|
|
|
|
Cycles::IntType cycles_until_event_ = 0;
|
2020-07-18 03:18:41 +00:00
|
|
|
float subcycles_until_event_ = 0.0f;
|
2016-07-29 11:15:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|