Skip to content

Commit 04d5879

Browse files
committed
avoid redundancy when playing randomly
1 parent 5b632e6 commit 04d5879

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

src/internal/Animation.cpp

+37-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ Animation::~Animation() {
1212
if (this->liveStream != nullptr && this->isOneTimeLiveStream) {
1313
delete this->liveStream;
1414
}
15+
16+
if (this->playedIndexes != nullptr) {
17+
delete[] this->playedIndexes;
18+
}
1519
}
1620

1721
bool Animation::hasFinished() {
@@ -36,16 +40,21 @@ void Animation::addScene(Stream &stream, byte fps, int frames) {
3640

3741
void Animation::registerScene(Scene *scene) {
3842
Scene **newScenes = new Scene *[this->addIndex + 1];
43+
bool *newPlayedIndexes = new bool[this->addIndex + 1];
3944

4045
for (int i = 0; i < this->addIndex; i++) {
4146
newScenes[i] = this->scenes[i];
47+
newPlayedIndexes[i] = this->playedIndexes[i];
4248
}
4349

4450
newScenes[this->addIndex] = scene;
51+
newPlayedIndexes[this->addIndex] = false;
4552

4653
delete[] this->scenes;
54+
delete[] this->playedIndexes;
4755

4856
this->scenes = newScenes;
57+
this->playedIndexes = newPlayedIndexes;
4958
this->addIndex++;
5059
}
5160

@@ -65,9 +74,14 @@ void Animation::setScene(byte index) {
6574
byte prevIndex = this->playIndex;
6675

6776
this->playIndex = index;
77+
this->playedIndexes[index] = true;
6878
this->scene = this->scenes[index];
6979
this->scene->reset();
7080

81+
if (this->allScenesPlayed()) {
82+
this->resetPlayedScenes();
83+
}
84+
7185
if (this->sceneCallback) {
7286
this->sceneCallback(prevIndex, index);
7387
}
@@ -77,7 +91,9 @@ void Animation::setRandomScene() {
7791
byte randomIndex = 0;
7892

7993
if (this->addIndex > 1) {
80-
randomIndex = random(this->addIndex);
94+
do {
95+
randomIndex = random(this->addIndex);
96+
} while (this->playedIndexes[randomIndex]);
8197
}
8298

8399
this->setScene(randomIndex);
@@ -94,6 +110,12 @@ void Animation::resetScene() {
94110
}
95111
}
96112

113+
void Animation::resetPlayedScenes() {
114+
for (int i = 0; i < this->addIndex; i++) {
115+
this->playedIndexes[i] = false;
116+
}
117+
}
118+
97119
void Animation::play() {
98120
if (!this->hasScenes() || !this->modeIsIn(2, MODE_DEFAULT, MODE_PAUSE)) {
99121
return;
@@ -190,6 +212,20 @@ byte Animation::getPlayIndex() {
190212
return this->playIndex;
191213
}
192214

215+
bool Animation::scenePlayed(int index) {
216+
return this->playedIndexes[index];
217+
}
218+
219+
bool Animation::allScenesPlayed() {
220+
for (int i = 0; i < this->addIndex; i++) {
221+
if (!this->playedIndexes[i]) {
222+
return false;
223+
}
224+
}
225+
226+
return true;
227+
}
228+
193229
void Animation::run(unsigned long currentMicros) {
194230
switch (this->mode) {
195231
case MODE_PLAY:

src/internal/Animation.h

+4
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ class Animation {
4646

4747
bool hasFinished();
4848
bool hasScenes();
49+
bool scenePlayed(int index);
50+
bool allScenesPlayed();
4951

5052
Scene *getCurrentScene();
5153

@@ -58,6 +60,7 @@ class Animation {
5860
AnimationData *liveStream = nullptr;
5961

6062
bool isOneTimeLiveStream = false;
63+
bool *playedIndexes = nullptr;
6164

6265
mcb modeCallback = nullptr;
6366
scb sceneCallback = nullptr;
@@ -75,6 +78,7 @@ class Animation {
7578
void setScene(byte index);
7679
void setRandomScene();
7780
void resetScene();
81+
void resetPlayedScenes();
7882

7983
bool modeIsIn(byte modeAmount, ...);
8084
};

test/animation/test_play_random/test_play_random.cpp

+13-1
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,30 @@ void test_play_random(void) {
1818
animation.addScene(PROGMEM_DATA, DATA_SIZE, FPS, FRAMES);
1919

2020
TEST_ASSERT_EQUAL(Animation::MODE_DEFAULT, animation.getMode());
21+
TEST_ASSERT_FALSE(animation.scenePlayed(0));
22+
TEST_ASSERT_FALSE(animation.scenePlayed(1));
23+
TEST_ASSERT_FALSE(animation.scenePlayed(2));
2124

22-
When(OverloadedMethod(ArduinoFake(), random, long(long))).Return(1, 0, 2);
25+
When(OverloadedMethod(ArduinoFake(), random, long(long))).Return(1, 0, 0, 2);
2326

2427
animation.playRandom();
2528

2629
TEST_ASSERT_EQUAL(Animation::MODE_PLAY_RANDOM, animation.getMode());
2730
TEST_ASSERT_NOT_EQUAL(nullptr, animation.getCurrentScene());
2831
TEST_ASSERT_EQUAL(1, animation.getPlayIndex());
32+
TEST_ASSERT_FALSE(animation.scenePlayed(0));
33+
TEST_ASSERT_TRUE(animation.scenePlayed(1));
34+
TEST_ASSERT_FALSE(animation.scenePlayed(2));
2935

3036
for (int i = 0; i < ANIMATION_MICROS; i += FRAME_MICROS) {
3137
animation.run(i);
3238
}
3339

3440
TEST_ASSERT_EQUAL(Animation::MODE_PLAY_RANDOM, animation.getMode());
3541
TEST_ASSERT_EQUAL(0, animation.getPlayIndex());
42+
TEST_ASSERT_TRUE(animation.scenePlayed(0));
43+
TEST_ASSERT_TRUE(animation.scenePlayed(1));
44+
TEST_ASSERT_FALSE(animation.scenePlayed(2));
3645

3746
for (long i = 0; i < ANIMATION_MICROS; i += FRAME_MICROS) {
3847
animation.run(i);
@@ -41,6 +50,9 @@ void test_play_random(void) {
4150
TEST_ASSERT_EQUAL(Animation::MODE_PLAY_RANDOM, animation.getMode());
4251
TEST_ASSERT_EQUAL(2, animation.getPlayIndex());
4352
TEST_ASSERT_EQUAL(20, logIndex);
53+
TEST_ASSERT_FALSE(animation.scenePlayed(0));
54+
TEST_ASSERT_FALSE(animation.scenePlayed(1));
55+
TEST_ASSERT_FALSE(animation.scenePlayed(2));
4456
}
4557

4658
void test_without_scenes(void) {

0 commit comments

Comments
 (0)