forked from profezzorn/ProffieOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconcat.h
193 lines (172 loc) · 5.19 KB
/
concat.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#ifndef TRANSITIONS_CONCAT_H
#define TRANSITIONS_CONCAT_H
// Usage: TrConcat<TRANSITION, INTERMEDIATE, TRANSITION, ...>
// OR: TrConcat<TRANSITION, TRANSITION, ...>
// TRANSITION: TRANSITION
// INTERMEDIATE: COLOR
// return value: TRANSITION
// Concatenates any number of transitions.
// If an intermediate color is provided, we first transition to that color, then
// we transition away from it in the next transition.
// If no intermediate color is provided, the first and second transition will both
// transition from the same input colors. If for instance both the first and second
// transitions are TrFades, then there will be a jump in the middle as the transition
// will go back and start from the beginning. Using TimeReverseX on the second transition
// will avoid this, as the second transition will then run backwards.
#ifdef OPTIMIZE_TRCONCAT
// this TRCONCAT can save some RAM, but it's not compatible with
// IntArg/RgbArg
template<class A, class INTERMEDIATE, class B>
class TrConcat3<A, INTERMEDIATE, B...> {
public:
TrConcat3() {
new (&b_) B;
}
~TrConcat() {
if (run_a_) {
a_.~A();
} else {
b_.~B();
}
}
void begin() {
if (!run_a_) {
b_.~B();
new (&a_) A;
run_a_ = true;
}
a_.begin();
}
bool done() { return !run_a_ && b_.done(); }
void run(BladeBase* blade) {
intermediate_.run(blade);
if (run_a_) {
a_.run(blade);
if (!a_.done()) return;
a_.~A();
new (&b_) B;
run_a_ = false;
b_.begin();
}
b_.run(blade);
}
private:
bool run_a_ = false;
union {
A a_;
B b_;
};
PONUA INTERMEDIATE intermediate_;
public:
template<class X, class Y>
auto getColor(const X& a, const Y& b, int led) -> decltype(MixColors(a_.getColor(a, intermediate_.getColor(led), led),
b_.getColor(intermediate_.getColor(led), b, led), 1,1)) {
if (done()) return b;
auto intermediate = intermediate_.getColor(led);
if (run_a_) {
return a_.getColor(a, intermediate, led);
} else {
return b_.getColor(intermediate, b, led);
}
}
};
#else // OPTIMIZE_TRCONCAT
template<class A, class INTERMEDIATE, class B>
class TrConcat3 {
public:
void begin() { a_.begin(); run_a_ = true; }
bool done() { return !run_a_ && b_.done(); }
void run(BladeBase* blade) {
intermediate_.run(blade);
if (run_a_) {
a_.run(blade);
if (!a_.done()) return;
run_a_ = false;
b_.begin();
}
b_.run(blade);
}
private:
bool run_a_ = false;
PONUA A a_;
PONUA B b_;
PONUA INTERMEDIATE intermediate_;
public:
template<class X, class Y>
auto getColor(const X& a, const Y& b, int led) -> decltype(
MixColors(b, MixColors(a_.getColor(a, intermediate_.getColor(led), led),
b_.getColor(intermediate_.getColor(led), b, led), 1,1), 1, 1)) {
if (done()) return b;
auto intermediate = intermediate_.getColor(led);
if (run_a_) {
return a_.getColor(a, intermediate, led);
} else {
return b_.getColor(intermediate, b, led);
}
}
};
#endif
template<class A, class B>
class TrConcat2 {
public:
void begin() { a_.begin(); run_a_ = true; }
bool done() { return !run_a_ && b_.done(); }
void run(BladeBase* blade) {
if (run_a_) {
a_.run(blade);
if (!a_.done()) return;
run_a_ = false;
b_.begin();
}
b_.run(blade);
}
private:
bool run_a_ = false;
PONUA A a_;
PONUA B b_;
public:
template<class X, class Y>
auto getColor(const X& a, const Y& b, int led) -> decltype(MixColors(a_.getColor(a, b, 1), b_.getColor(a, b, 1), 1, 1)) {
if (run_a_) {
return a_.getColor(a, b, led);
} else {
return b_.getColor(a, b, led);
}
}
};
template <typename R, bool result = std::is_same<decltype(((R*)nullptr)->getColor(0).c.r), uint16_t>::value>
constexpr bool hasGetColorHelper(int) {
return result;
}
template <typename R> constexpr bool hasGetColorHelper(...) { return false; }
template <typename R> constexpr bool hasGetColor() { return hasGetColorHelper<R>(0); }
static_assert(std::is_same<decltype(((Black*)nullptr)->getColor(0).c.r), uint16_t>::value, "getcolor has changed...");
static_assert(hasGetColor<Black>(), "hasGetColor is not working");
static_assert(!hasGetColor<Int<1>>(), "hasGetColor is not working");
template<class ... REST> struct TrConcatSelector {};
template<class A, class B, typename Enable, class ... REST> struct TrConcat3Selector {
private:
typedef void type;
};
template<class ... REST> using TrConcat = typename TrConcatSelector<REST...>::type;
template<class A, class B, class ... REST>
struct TrConcat3Selector<A, B, typename std::enable_if<hasGetColor<B>()>::type, REST...> {
typedef TrConcat3<A, B, TrConcat<REST...>> type;
};
template<class A, class B, class ... REST>
struct TrConcat3Selector<A, B, typename std::enable_if<!hasGetColor<B>()>::type, REST...> {
typedef TrConcat2<A, TrConcat<B, REST...>> type;
};
template<class A, class B, class ... REST>
struct TrConcatSelector<A, B, REST...> {
typedef typename TrConcat3Selector<A, B, void, REST...>::type type;
};
template<class A, class B>
struct TrConcatSelector<A, B> {
typedef TrConcat2<A, B> type;
};
template<class A>
struct TrConcatSelector<A> {
typedef A type;
};
#endif