From 8349005c4b8c4e21cece4adbc7747e0e18e3c282 Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Sun, 19 Jan 2020 23:52:47 -0500 Subject: [PATCH] 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. --- Machines/CRTMachine.hpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Machines/CRTMachine.hpp b/Machines/CRTMachine.hpp index 64be17500..8d6d6a99c 100644 --- a/Machines/CRTMachine.hpp +++ b/Machines/CRTMachine.hpp @@ -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(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 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;