From fae8b15db1042485226ed9ed2d6e9e09a4a54377 Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Sat, 26 Jan 2019 16:34:28 -0600 Subject: [PATCH] ExpressionParser: Add !pulse function that evaluates to 1.0 for N seconds for each press. --- .../ControlReference/FunctionExpression.cpp | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/Source/Core/InputCommon/ControlReference/FunctionExpression.cpp b/Source/Core/InputCommon/ControlReference/FunctionExpression.cpp index 8596b64221..c18ca3e582 100644 --- a/Source/Core/InputCommon/ControlReference/FunctionExpression.cpp +++ b/Source/Core/InputCommon/ControlReference/FunctionExpression.cpp @@ -438,6 +438,61 @@ private: mutable Clock::time_point m_last_update = Clock::now(); }; +// usage: !pulse(input, seconds) +class PulseExpression : public FunctionExpression +{ + virtual bool ValidateArguments(const std::vector>& args) override + { + return 2 == args.size(); + } + + ControlState GetValue() const override + { + const auto now = Clock::now(); + + const ControlState input = GetArg(0).GetValue(); + + if (input < CONDITION_THRESHOLD) + { + m_released = true; + } + else if (m_released) + { + m_released = false; + + using FSec = std::chrono::duration; + const auto seconds = std::chrono::duration_cast(FSec(GetArg(1).GetValue())); + + if (m_state) + { + m_release_time += seconds; + } + else + { + m_state = true; + m_release_time = now + seconds; + } + } + + if (m_state && now >= m_release_time) + { + m_state = false; + } + + return m_state; + } + + void SetValue(ControlState value) override {} + std::string GetFuncName() const override { return "pulse"; } + +private: + using Clock = std::chrono::steady_clock; + + mutable bool m_released = false; + mutable bool m_state = false; + mutable Clock::time_point m_release_time = Clock::now(); +}; + std::unique_ptr MakeFunctionExpression(std::string name) { if (name.empty()) @@ -464,6 +519,8 @@ std::unique_ptr MakeFunctionExpression(std::string name) return std::make_unique(); else if ("relative" == name) return std::make_unique(); + else if ("pulse" == name) + return std::make_unique(); else return std::make_unique(); }