Merge pull request #8999 from AdmiralCurtiss/input-expression-math

Add a few input expression functions (trigonometry, sqrt, pow, min, max, clamp)
This commit is contained in:
Jordan Woyak 2020-08-07 14:40:29 -05:00 committed by GitHub
commit a77977d0aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 225 additions and 0 deletions

View File

@ -278,6 +278,18 @@ void IOWindow::CreateMainLayout()
m_functions_combo->addItem(QStringLiteral("tap")); m_functions_combo->addItem(QStringLiteral("tap"));
m_functions_combo->addItem(QStringLiteral("relative")); m_functions_combo->addItem(QStringLiteral("relative"));
m_functions_combo->addItem(QStringLiteral("pulse")); m_functions_combo->addItem(QStringLiteral("pulse"));
m_functions_combo->addItem(QStringLiteral("sin"));
m_functions_combo->addItem(QStringLiteral("cos"));
m_functions_combo->addItem(QStringLiteral("tan"));
m_functions_combo->addItem(QStringLiteral("asin"));
m_functions_combo->addItem(QStringLiteral("acos"));
m_functions_combo->addItem(QStringLiteral("atan"));
m_functions_combo->addItem(QStringLiteral("atan2"));
m_functions_combo->addItem(QStringLiteral("sqrt"));
m_functions_combo->addItem(QStringLiteral("pow"));
m_functions_combo->addItem(QStringLiteral("min"));
m_functions_combo->addItem(QStringLiteral("max"));
m_functions_combo->addItem(QStringLiteral("clamp"));
// Devices // Devices
m_main_layout->addWidget(m_devices_combo); m_main_layout->addWidget(m_devices_combo);

View File

@ -86,6 +86,197 @@ private:
ControlState GetValue() const override { return std::sin(GetArg(0).GetValue()); } ControlState GetValue() const override { return std::sin(GetArg(0).GetValue()); }
}; };
// usage: cos(expression)
class CosExpression : public FunctionExpression
{
private:
ArgumentValidation
ValidateArguments(const std::vector<std::unique_ptr<Expression>>& args) override
{
if (args.size() == 1)
return ArgumentsAreValid{};
else
return ExpectedArguments{"expression"};
}
ControlState GetValue() const override { return std::cos(GetArg(0).GetValue()); }
};
// usage: tan(expression)
class TanExpression : public FunctionExpression
{
private:
ArgumentValidation
ValidateArguments(const std::vector<std::unique_ptr<Expression>>& args) override
{
if (args.size() == 1)
return ArgumentsAreValid{};
else
return ExpectedArguments{"expression"};
}
ControlState GetValue() const override { return std::tan(GetArg(0).GetValue()); }
};
// usage: asin(expression)
class ASinExpression : public FunctionExpression
{
private:
ArgumentValidation
ValidateArguments(const std::vector<std::unique_ptr<Expression>>& args) override
{
if (args.size() == 1)
return ArgumentsAreValid{};
else
return ExpectedArguments{"expression"};
}
ControlState GetValue() const override { return std::asin(GetArg(0).GetValue()); }
};
// usage: acos(expression)
class ACosExpression : public FunctionExpression
{
private:
ArgumentValidation
ValidateArguments(const std::vector<std::unique_ptr<Expression>>& args) override
{
if (args.size() == 1)
return ArgumentsAreValid{};
else
return ExpectedArguments{"expression"};
}
ControlState GetValue() const override { return std::acos(GetArg(0).GetValue()); }
};
// usage: atan(expression)
class ATanExpression : public FunctionExpression
{
private:
ArgumentValidation
ValidateArguments(const std::vector<std::unique_ptr<Expression>>& args) override
{
if (args.size() == 1)
return ArgumentsAreValid{};
else
return ExpectedArguments{"expression"};
}
ControlState GetValue() const override { return std::atan(GetArg(0).GetValue()); }
};
// usage: atan2(y, x)
class ATan2Expression : public FunctionExpression
{
private:
ArgumentValidation
ValidateArguments(const std::vector<std::unique_ptr<Expression>>& args) override
{
if (args.size() == 2)
return ArgumentsAreValid{};
else
return ExpectedArguments{"y, x"};
}
ControlState GetValue() const override
{
return std::atan2(GetArg(0).GetValue(), GetArg(1).GetValue());
}
};
// usage: sqrt(expression)
class SqrtExpression : public FunctionExpression
{
private:
ArgumentValidation
ValidateArguments(const std::vector<std::unique_ptr<Expression>>& args) override
{
if (args.size() == 1)
return ArgumentsAreValid{};
else
return ExpectedArguments{"expression"};
}
ControlState GetValue() const override { return std::sqrt(GetArg(0).GetValue()); }
};
// usage: pow(base, exponent)
class PowExpression : public FunctionExpression
{
private:
ArgumentValidation
ValidateArguments(const std::vector<std::unique_ptr<Expression>>& args) override
{
if (args.size() == 2)
return ArgumentsAreValid{};
else
return ExpectedArguments{"base, exponent"};
}
ControlState GetValue() const override
{
return std::pow(GetArg(0).GetValue(), GetArg(1).GetValue());
}
};
// usage: min(a, b)
class MinExpression : public FunctionExpression
{
private:
ArgumentValidation
ValidateArguments(const std::vector<std::unique_ptr<Expression>>& args) override
{
if (args.size() == 2)
return ArgumentsAreValid{};
else
return ExpectedArguments{"a, b"};
}
ControlState GetValue() const override
{
return std::min(GetArg(0).GetValue(), GetArg(1).GetValue());
}
};
// usage: max(a, b)
class MaxExpression : public FunctionExpression
{
private:
ArgumentValidation
ValidateArguments(const std::vector<std::unique_ptr<Expression>>& args) override
{
if (args.size() == 2)
return ArgumentsAreValid{};
else
return ExpectedArguments{"a, b"};
}
ControlState GetValue() const override
{
return std::max(GetArg(0).GetValue(), GetArg(1).GetValue());
}
};
// usage: clamp(value, min, max)
class ClampExpression : public FunctionExpression
{
private:
ArgumentValidation
ValidateArguments(const std::vector<std::unique_ptr<Expression>>& args) override
{
if (args.size() == 3)
return ArgumentsAreValid{};
else
return ExpectedArguments{"value, min, max"};
}
ControlState GetValue() const override
{
return std::clamp(GetArg(0).GetValue(), GetArg(1).GetValue(), GetArg(2).GetValue());
}
};
// usage: timer(seconds) // usage: timer(seconds)
class TimerExpression : public FunctionExpression class TimerExpression : public FunctionExpression
{ {
@ -453,6 +644,28 @@ std::unique_ptr<FunctionExpression> MakeFunctionExpression(std::string_view name
return std::make_unique<IfExpression>(); return std::make_unique<IfExpression>();
if (name == "sin") if (name == "sin")
return std::make_unique<SinExpression>(); return std::make_unique<SinExpression>();
if (name == "cos")
return std::make_unique<CosExpression>();
if (name == "tan")
return std::make_unique<TanExpression>();
if (name == "asin")
return std::make_unique<ASinExpression>();
if (name == "acos")
return std::make_unique<ACosExpression>();
if (name == "atan")
return std::make_unique<ATanExpression>();
if (name == "atan2")
return std::make_unique<ATan2Expression>();
if (name == "sqrt")
return std::make_unique<SqrtExpression>();
if (name == "pow")
return std::make_unique<PowExpression>();
if (name == "min")
return std::make_unique<MinExpression>();
if (name == "max")
return std::make_unique<MaxExpression>();
if (name == "clamp")
return std::make_unique<ClampExpression>();
if (name == "timer") if (name == "timer")
return std::make_unique<TimerExpression>(); return std::make_unique<TimerExpression>();
if (name == "toggle") if (name == "toggle")