Skip to content

Commit c5cf26c

Browse files
committed
add knightrider
1 parent efac4f1 commit c5cf26c

File tree

4 files changed

+140
-0
lines changed

4 files changed

+140
-0
lines changed

config.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ operations:
5555
type: rotate
5656
params:
5757
step_width: 50
58+
knightrider:
59+
type: knightrider
60+
params:
61+
duration: 33
62+
width: 0.3
5863
example-lua:
5964
type: lua
6065
params:

src/config_sequence.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "operations/FlashOperation.h"
99
#include "operations/GameOfLifeOperation.h"
1010
#include "operations/HSVUDPInputOperation.h"
11+
#include "operations/KnightRider.h"
1112
#include "operations/RainbowOperation.h"
1213
#include "operations/RaindropOperation.h"
1314
#include "operations/RotateOperation.h"
@@ -45,6 +46,7 @@ std::unique_ptr<Operation> generateSequenceStep(
4546
{"gameoflife", &generator<GameOfLifeOperation>},
4647
{"hsvudpinput", &generator<HSVUDPInputOperation>},
4748
{"lua", &generator<LuaOperation>},
49+
{"knightrider", &generator<KnightRiderOperation>},
4850
{"rainbow", &generator<RainbowOperation>},
4951
{"raindrop", &generator<RaindropOperation>},
5052
{"rotate", &generator<RotateOperation>},

src/operations/KnightRider.cpp

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#include "KnightRider.h"
2+
3+
#include <algorithm>
4+
#include <chrono>
5+
#include <cmath>
6+
7+
8+
KnightRiderOperation::KnightRiderOperation(const std::string& name,
9+
std::shared_ptr<VariableStore> store,
10+
YAML::const_iterator begin,
11+
YAML::const_iterator end)
12+
: Operation(name, store, begin, end),
13+
state(State::IDLE),
14+
time_passed(0),
15+
hue(name + "/hue",
16+
Operation::HSV_HUE,
17+
store,
18+
getValueByKey<float>("hue", begin, end, 0.0f)),
19+
saturation(name + "/saturation",
20+
Operation::HSV_SATURATION,
21+
store,
22+
getValueByKey<float>("saturation", begin, end, 1.0f)),
23+
value(name + "/value",
24+
Operation::HSV_VALUE,
25+
store,
26+
getValueByKey<float>("value", begin, end, 1.0f)),
27+
duration_milliseconds(name + "/duration",
28+
Operation::INT,
29+
store,
30+
getValueByKey<int>("duration", begin, end, 33)),
31+
width(name + "/width",
32+
Operation::FLOAT,
33+
store,
34+
getValueByKey<float>("width", begin, end, 0.1f)) {}
35+
36+
Operation::BufferType KnightRiderOperation::operator()(
37+
Operation::BufferType& buffer) {
38+
if (state == State::IDLE)
39+
return buffer;
40+
41+
int size = (*buffer).size();
42+
43+
for (int i = 0; i < size; i++) {
44+
int distance = std::abs(i - currentCenter);
45+
float relValue = value - pow(float(distance) / float(size), width);
46+
(*buffer).at(i) = HSV{hue.getValue(), saturation, std::clamp(relValue,0.f, 1.f)};
47+
}
48+
49+
return buffer;
50+
}
51+
52+
void KnightRiderOperation::update(const size_t width, const size_t) {
53+
if (!isEnabled()) {
54+
state = State::IDLE;
55+
return;
56+
}
57+
58+
if (time_measurement.measure() < duration_milliseconds)
59+
return;
60+
61+
state = State::RUNNING;
62+
time_measurement.reset();
63+
64+
if (direction == Direction::ASCENDING) {
65+
if (currentCenter >= width) {
66+
direction = Direction::DESCENDING;
67+
return;
68+
} else {
69+
currentCenter++;
70+
}
71+
}
72+
73+
if (direction == Direction::DESCENDING) {
74+
if (currentCenter <= 0) {
75+
direction = Direction::ASCENDING;
76+
return;
77+
} else {
78+
currentCenter--;
79+
}
80+
}
81+
}

src/operations/KnightRider.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#pragma once
2+
3+
#include "Operation.h"
4+
5+
class KnightRiderOperation : public Operation {
6+
enum State {
7+
IDLE,
8+
RUNNING,
9+
} state;
10+
11+
enum Direction {
12+
ASCENDING,
13+
DESCENDING,
14+
} direction;
15+
16+
inline const char* toString(State s) {
17+
switch (s) {
18+
case State::IDLE:
19+
return "IDLE";
20+
case State::RUNNING:
21+
return "RUNNING";
22+
default:
23+
return "UNKNOWN";
24+
}
25+
}
26+
27+
int64_t time_passed;
28+
int currentCenter;
29+
30+
MeasureTime<> time_measurement;
31+
32+
BoundConcreteValue<float> hue;
33+
BoundConcreteValue<float> saturation;
34+
BoundConcreteValue<float> value;
35+
36+
BoundConcreteValue<int> duration_milliseconds;
37+
BoundConcreteValue<float> width;
38+
39+
float getShade() const;
40+
41+
public:
42+
KnightRiderOperation(const std::string& name,
43+
std::shared_ptr<VariableStore> store,
44+
YAML::const_iterator begin,
45+
YAML::const_iterator end);
46+
47+
virtual ~KnightRiderOperation() {}
48+
49+
virtual Operation::BufferType operator()(Operation::BufferType& buffer);
50+
51+
virtual void update(const size_t width, const size_t fps);
52+
};

0 commit comments

Comments
 (0)