Skip to content

Commit

Permalink
ParamMetaData Polarity API (#74)
Browse files Browse the repository at this point in the history
gets a polarity api and an inferred default and isBi and isUni
methods.

Addresses #73
  • Loading branch information
baconpaul authored Dec 4, 2023
1 parent 0960274 commit aa18af8
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
37 changes: 37 additions & 0 deletions include/sst/basic-blocks/params/ParamMetadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,36 @@ struct ParamMetaData

bool supportsStringConversion{false};

// Polarity can either be explicit set or inferred from the underling min max
enum struct Polarity
{
INFERRED, // figure out from min-max
UNIPOLAR_POSITIVE, // 0 .. x
UNIPOLAR_NEGATIVE, // -x .. 0
BIPOLAR, // -x .. x
NO_POLARITY // x .. y not meeting above conditions
} polarity{Polarity::INFERRED};

Polarity getPolarity() const
{
if (polarity != Polarity::INFERRED)
return polarity;
if (minVal == 0 && maxVal > 0)
return Polarity::UNIPOLAR_POSITIVE;
if (minVal < 0 && maxVal == 0)
return Polarity::UNIPOLAR_NEGATIVE;
if (minVal == -maxVal)
return Polarity::BIPOLAR;
return Polarity::NO_POLARITY;
}

bool isBipolar() const { return getPolarity() == Polarity::BIPOLAR; }
bool isUnipolar() const
{
auto p = getPolarity();
return p == Polarity::UNIPOLAR_NEGATIVE || p == Polarity::UNIPOLAR_POSITIVE;
}

/*
* To String and From String conversion functions require information about the
* parameter to execute. The primary driver is the value so the API takes the form
Expand Down Expand Up @@ -323,6 +353,13 @@ struct ParamMetaData
res.defaultVal = t;
return res;
}
ParamMetaData withPolarity(Polarity p)
{
auto res = *this;
res.polarity = p;
return res;
}

ParamMetaData withTemposyncMultiplier(float f)
{
auto res = *this;
Expand Down
32 changes: 31 additions & 1 deletion tests/param_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ TEST_CASE("Percent and BiPolar Percent")
REQUIRE(*(p.valueFromString("440", ems)) == 0);
REQUIRE(*(p.valueFromString("220", ems)) == -12);
}

}
TEST_CASE("Parameter Constructability")
{
SECTION("Default Constructors")
{
static_assert(std::is_copy_constructible_v<pmd::ParamMetaData::FeatureState>);
Expand All @@ -83,3 +85,31 @@ TEST_CASE("Percent and BiPolar Percent")
static_assert(std::is_move_assignable_v<pmd::ParamMetaData::FeatureState>);
}
}
TEST_CASE("Parameter Polarity")
{
SECTION("Polarity Test")
{
auto p = pmd::ParamMetaData().withRange(0, 4);
REQUIRE(p.getPolarity() == pmd::ParamMetaData::Polarity::UNIPOLAR_POSITIVE);
REQUIRE(p.isUnipolar());
REQUIRE(!p.isBipolar());
p = pmd::ParamMetaData().withRange(-4, 4);
REQUIRE(p.getPolarity() == pmd::ParamMetaData::Polarity::BIPOLAR);
REQUIRE(p.isBipolar());
REQUIRE(!p.isUnipolar());

p = pmd::ParamMetaData().withRange(-4, 0);
REQUIRE(p.getPolarity() == pmd::ParamMetaData::Polarity::UNIPOLAR_NEGATIVE);
REQUIRE(p.isUnipolar());
REQUIRE(!p.isBipolar());

p = pmd::ParamMetaData().withRange(-4, 7);
REQUIRE(p.getPolarity() == pmd::ParamMetaData::Polarity::NO_POLARITY);
REQUIRE(!p.isBipolar());
REQUIRE(!p.isUnipolar());

p = pmd::ParamMetaData().withRange(-4, 7).withPolarity(
pmd::ParamMetaData::Polarity::BIPOLAR);
REQUIRE(p.getPolarity() == pmd::ParamMetaData::Polarity::BIPOLAR);
}
}

0 comments on commit aa18af8

Please sign in to comment.