2016-07-29 11:15:46 +00:00
|
|
|
//
|
|
|
|
// TimedEventLoop.hpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 29/07/2016.
|
|
|
|
// Copyright © 2016 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef TimedEventLoop_hpp
|
|
|
|
#define TimedEventLoop_hpp
|
|
|
|
|
|
|
|
#include "Storage.hpp"
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include "../SignalProcessing/Stepper.hpp"
|
|
|
|
|
|
|
|
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
|
|
|
|
determined via @c run_for_cycles.
|
|
|
|
|
|
|
|
Due to the aggregation of total timing information between events — e.g. if an event loop has
|
|
|
|
a clock rate of 1000 ticks per second and a steady stream of events that occur 10,000 times a second,
|
|
|
|
bookkeeping is necessary to ensure that 10 events are triggered per tick — subclasses should call
|
|
|
|
@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.
|
|
|
|
*/
|
2016-07-29 11:15:46 +00:00
|
|
|
class TimedEventLoop {
|
|
|
|
public:
|
2016-08-01 10:04:55 +00:00
|
|
|
/*!
|
|
|
|
Constructs a timed event loop that will be clocked at @c input_clock_rate.
|
|
|
|
*/
|
2016-07-29 11:15:46 +00:00
|
|
|
TimedEventLoop(unsigned int input_clock_rate);
|
2016-08-01 10:04:55 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
Advances the event loop by @c number_of_cycles cycles.
|
|
|
|
*/
|
2016-07-29 15:03:09 +00:00
|
|
|
void run_for_cycles(int number_of_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.
|
|
|
|
*/
|
2016-09-17 23:52:27 +00:00
|
|
|
unsigned int get_cycles_until_next_event();
|
|
|
|
|
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);
|
|
|
|
|
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;
|
|
|
|
|
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();
|
|
|
|
|
2016-08-03 12:16:23 +00:00
|
|
|
/*!
|
|
|
|
Sets the amount of time into the current event to @c offset.
|
|
|
|
*/
|
|
|
|
void reset_timer_to_offset(Time offset);
|
|
|
|
|
2016-08-01 10:04:55 +00:00
|
|
|
/*!
|
|
|
|
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:
|
|
|
|
unsigned int _input_clock_rate;
|
2016-09-17 23:52:27 +00:00
|
|
|
int _cycles_until_event;
|
2016-09-18 14:24:09 +00:00
|
|
|
Time _subcycles_until_event;
|
2016-07-29 11:15:46 +00:00
|
|
|
Time _event_interval;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* TimedEventLoop_hpp */
|