Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ operations:
params:
enabled: false
duration: 5000
knightrider:
type: knightrider
params:
duration: 5000
width: 0.2
rotate:
type: rotate
params:
Expand Down
2 changes: 2 additions & 0 deletions src/config_sequence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "operations/FlashOperation.h"
#include "operations/GameOfLifeOperation.h"
#include "operations/HSVUDPInputOperation.h"
#include "operations/KnightRider.h"
#include "operations/RainbowOperation.h"
#include "operations/RaindropOperation.h"
#include "operations/RotateOperation.h"
Expand Down Expand Up @@ -45,6 +46,7 @@ std::unique_ptr<Operation> generateSequenceStep(
{"gameoflife", &generator<GameOfLifeOperation>},
{"hsvudpinput", &generator<HSVUDPInputOperation>},
{"lua", &generator<LuaOperation>},
{"knightrider", &generator<KnightRiderOperation>},
{"rainbow", &generator<RainbowOperation>},
{"raindrop", &generator<RaindropOperation>},
{"rotate", &generator<RotateOperation>},
Expand Down
82 changes: 82 additions & 0 deletions src/operations/KnightRider.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#include "KnightRider.h"

#include <algorithm>
#include <chrono>
#include <cmath>

#include <iomanip>
#include <iostream>

KnightRiderOperation::KnightRiderOperation(const std::string& name,
std::shared_ptr<VariableStore> store,
YAML::const_iterator begin,
YAML::const_iterator end)
: Operation(name, store, begin, end),
state(State::IDLE),
value(name + "/value",
Operation::HSV_VALUE,
store,
getValueByKey<float>("value", begin, end, 1.0f)),
duration_milliseconds(name + "/duration",
Operation::INT,
store,
getValueByKey<int>("duration", begin, end, 1000)),
width(name + "/width",
Operation::FLOAT,
store,
getValueByKey<float>("width", begin, end, 0.1f)) {}

Operation::BufferType KnightRiderOperation::operator()(
Operation::BufferType& buffer) {
if (!isEnabled())
return buffer;

int size = (*buffer).size();

for (int i = 0; i < size; i++) {
if (direction == Direction::ASCENDING && i > currentCenter) {
(*buffer).at(i) = HSV{0, 0, 0};
continue;
}

if (direction == Direction::DESCENDING && i < currentCenter) {
(*buffer).at(i) = HSV{0, 0, 0};
continue;
}

int distance = std::abs(i - currentCenter);
float relValue = value - pow(float(distance) / float(size), width);
auto& p = (*buffer).at(i);
(*buffer).at(i) = HSV{p.hue, p.saturation, std::clamp(relValue, 0.f, 1.f)};
}

return buffer;
}

void KnightRiderOperation::update(const size_t width, const size_t fps) {
if (!isEnabled()) {
return;
}

auto stepWitdh = width / fps / float(duration_milliseconds / 1000);

if (direction == Direction::ASCENDING) {
if (currentCenter >= width - 1) {
direction = Direction::DESCENDING;
return;
} else {
currentCenter += stepWitdh;
}
}

if (direction == Direction::DESCENDING) {
if (currentCenter <= 0) {
direction = Direction::ASCENDING;
return;
} else {
currentCenter -= stepWitdh;
}
}

currentCenter = std::clamp(currentCenter, 0, int(width - 1));
}
36 changes: 36 additions & 0 deletions src/operations/KnightRider.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once

#include "Operation.h"

class KnightRiderOperation : public Operation {
enum State {
IDLE,
RUNNING,
} state;

enum Direction {
ASCENDING,
DESCENDING,
} direction;

int currentCenter = 0;

BoundConcreteValue<float> value;

BoundConcreteValue<int> duration_milliseconds;
BoundConcreteValue<float> width;

float getShade() const;

public:
KnightRiderOperation(const std::string& name,
std::shared_ptr<VariableStore> store,
YAML::const_iterator begin,
YAML::const_iterator end);

virtual ~KnightRiderOperation() {}

virtual Operation::BufferType operator()(Operation::BufferType& buffer);

virtual void update(const size_t width, const size_t fps);
};