Skip to content

Commit 8f4af2d

Browse files
committed
add knightrider
1 parent efac4f1 commit 8f4af2d

File tree

4 files changed

+134
-0
lines changed

4 files changed

+134
-0
lines changed

config.yml

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

src/operations/KnightRider.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
int currentCenter = 0;
17+
18+
BoundConcreteValue<float> hue;
19+
BoundConcreteValue<float> saturation;
20+
BoundConcreteValue<float> value;
21+
22+
BoundConcreteValue<int> duration_milliseconds;
23+
BoundConcreteValue<float> width;
24+
25+
float getShade() const;
26+
27+
public:
28+
KnightRiderOperation(const std::string& name,
29+
std::shared_ptr<VariableStore> store,
30+
YAML::const_iterator begin,
31+
YAML::const_iterator end);
32+
33+
virtual ~KnightRiderOperation() {}
34+
35+
virtual Operation::BufferType operator()(Operation::BufferType& buffer);
36+
37+
virtual void update(const size_t width, const size_t fps);
38+
};

0 commit comments

Comments
 (0)