Skip to content

Commit

Permalink
Create before and after patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
evan1026 committed May 27, 2022
1 parent 6fd240f commit ace50d7
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 16 deletions.
8 changes: 8 additions & 0 deletions example.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
{
"name": "rollup_pattern",
"repeat": 1,
"before": [
{"color": "#000000", "led": 0, "time": 100},
{"time": 100}
],
"lines": [
{"color": "#FFFFFF", "led": 0, "time": 500},
{"time": 500},
Expand Down Expand Up @@ -58,6 +62,10 @@
{"color": "#0000FF", "led": 1, "time": 250},
{"color": "#FF0000", "led": 2, "time": 250},
{"time": 250}
],
"after": [
{"color": "#000000", "led": 0, "time": 100},
{"time": 100}
]
}
]
Expand Down
1 change: 1 addition & 0 deletions include/config/ConfigParser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ namespace blink1_control::config {
[[nodiscard]] static std::shared_ptr<ProcessMonitorConfig> parseProcessMonitor(const Json& json);
[[nodiscard]] static std::shared_ptr<RollupConfig> parseRollup(const Json& json);
[[nodiscard]] static blink1_lib::RGBN parseRgb(const std::string& rgbString);
static void readPattern(const Json& json, std::vector<std::unique_ptr<PatternCommand>>& commands);

public:
/**
Expand Down
10 changes: 10 additions & 0 deletions include/config/PatternConfig.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ namespace blink1_control::config {
*/
std::string name;

/**
* A list of pattern lines which get played before the pattern starts
*/
std::vector<std::unique_ptr<PatternCommand>> before;

/**
* A list of pattern lines which get played after the pattern is done
*/
std::vector<std::unique_ptr<PatternCommand>> after;

/**
* A list of pattern lines which make up the pattern
*/
Expand Down
42 changes: 26 additions & 16 deletions src/config/ConfigParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,26 @@ namespace blink1_control::config {
return parseArray(json, config, PATTERNS_STRING, [](const Json& ljson, Config& lconfig){return parsePattern(ljson, lconfig);});
}

void ConfigParser::readPattern(const Json& json, std::vector<std::unique_ptr<PatternCommand>>& commands) {
for (const Json& line : json) {
bool hasLed = line.contains("led");
bool hasColor = line.contains("color");

if (hasLed && hasColor) {
blink1_lib::PatternLineN patternLine;
patternLine.rgbn = parseRgb(line.at("color"));
patternLine.rgbn.n = line.at("led");
patternLine.fadeMillis = line.at("time");

commands.push_back(std::make_unique<FadeCommand>(patternLine));
} else if (hasLed || hasColor) {
throw std::runtime_error("Pattern line must contain both 'led' and 'color' or neither of them");
} else {
commands.push_back(std::make_unique<WaitCommand>(std::chrono::milliseconds(line.at("time"))));
}
}
}

bool ConfigParser::parsePattern(const Json& json, Config& config) {
bool success = true;

Expand All @@ -126,22 +146,12 @@ namespace blink1_control::config {
pattern->name = json.at("name");
pattern->repeat = json.at("repeat");

for (const Json& line : json.at("lines")) {
bool hasLed = line.contains("led");
bool hasColor = line.contains("color");

if (hasLed && hasColor) {
blink1_lib::PatternLineN patternLine;
patternLine.rgbn = parseRgb(line.at("color"));
patternLine.rgbn.n = line.at("led");
patternLine.fadeMillis = line.at("time");

pattern->pattern.push_back(std::make_unique<FadeCommand>(patternLine));
} else if (hasLed || hasColor) {
throw std::runtime_error("Pattern line must contain both 'led' and 'color' or neither of them");
} else {
pattern->pattern.push_back(std::make_unique<WaitCommand>(std::chrono::milliseconds(line.at("time"))));
}
readPattern(json.at("lines"), pattern->pattern);
if (json.contains("before")) {
readPattern(json.at("before"), pattern->before);
}
if (json.contains("after")) {
readPattern(json.at("after"), pattern->after);
}

config.patternConfigs.emplace(pattern->name, pattern);
Expand Down
12 changes: 12 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,25 @@ int main(int argc, const char* argv[]) {
PatternConfig& pattern = *it->second;
std::cout << "Playing " << pattern.name << "\n";

std::cout << " Playing before pattern\n";
for (auto& patternLine : pattern.before) {
std::cout << " Playing " << *patternLine << "\n";
patternLine->execute(blinkDevice);
}

for (int i = 0; i < pattern.repeat && LOOPING; ++i) {
std::cout << " Playing iteration " << i << "/" << pattern.repeat << "\n";
for (auto& patternLine : pattern.pattern) {
std::cout << " Playing " << *patternLine << "\n";
patternLine->execute(blinkDevice);
}
}

std::cout << " Playing after pattern\n";
for (auto& patternLine : pattern.after) {
std::cout << " Playing " << *patternLine << "\n";
patternLine->execute(blinkDevice);
}
}
}
}
33 changes: 33 additions & 0 deletions test/config/ConfigParser_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,20 @@ TEST_F(SUITE_NAME, TestParseConfig) {
{
"time": 1234
}
],
"before": [
{
"color": "#ABCDEF",
"led": 5,
"time": 10
}
],
"after": [
{
"color": "#FEDCBA",
"led": 6,
"time": 11
}
]
}
]
Expand Down Expand Up @@ -155,6 +169,25 @@ TEST_F(SUITE_NAME, TestParseConfig) {
EXPECT_EQ(typeid(WaitCommand&), typeid(patternLine3));
auto& waitCommand = dynamic_cast<WaitCommand&>(patternLine3);
EXPECT_EQ(std::chrono::milliseconds(1234), waitCommand.waitTime);

auto& beforeLine = *rollupPatternConfig->before[0];
auto& afterLine = *rollupPatternConfig->after[0];

EXPECT_EQ(typeid(FadeCommand&), typeid(beforeLine));
auto& beforeCommand = dynamic_cast<FadeCommand&>(beforeLine);
EXPECT_EQ(0xAB, beforeCommand.fadeParams.rgbn.r);
EXPECT_EQ(0xCD, beforeCommand.fadeParams.rgbn.g);
EXPECT_EQ(0xEF, beforeCommand.fadeParams.rgbn.b);
EXPECT_EQ(5, beforeCommand.fadeParams.rgbn.n);
EXPECT_EQ(10, beforeCommand.fadeParams.fadeMillis);

EXPECT_EQ(typeid(FadeCommand&), typeid(afterLine));
auto& afterCommand = dynamic_cast<FadeCommand&>(afterLine);
EXPECT_EQ(0xFE, afterCommand.fadeParams.rgbn.r);
EXPECT_EQ(0xDC, afterCommand.fadeParams.rgbn.g);
EXPECT_EQ(0xBA, afterCommand.fadeParams.rgbn.b);
EXPECT_EQ(6, afterCommand.fadeParams.rgbn.n);
EXPECT_EQ(11, afterCommand.fadeParams.fadeMillis);
}

TEST_F(SUITE_NAME, TestBadStreams) {
Expand Down

0 comments on commit ace50d7

Please sign in to comment.