1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-11-26 08:49:37 +00:00

Adds CRTMachine::run_until, which will run until a condition is true.

I want to get to being able to say "run until the beam is 60% of the way down", "run until a new packet of audio has been delivered", etc.
This commit is contained in:
Thomas Harte 2020-01-19 23:52:47 -05:00
parent a2847f4f8e
commit 8349005c4b

View File

@ -45,12 +45,20 @@ class Machine {
virtual std::string debug_type() { return ""; }
/// Runs the machine for @c duration seconds.
virtual void run_for(Time::Seconds duration) {
void run_for(Time::Seconds duration) {
const double cycles = (duration * clock_rate_) + clock_conversion_error_;
clock_conversion_error_ = std::fmod(cycles, 1.0);
run_for(Cycles(static_cast<int>(cycles)));
}
/// Runs for the machine for at least @c duration seconds, and then until @c condition is true.
void run_until(Time::Seconds minimum_duration, std::function<bool()> condition) {
run_for(minimum_duration);
while(!condition()) {
run_for(0.002);
}
}
protected:
/// Runs the machine for @c cycles.
virtual void run_for(const Cycles cycles) = 0;