Skip to content

Commit

Permalink
add first scene callback tests
Browse files Browse the repository at this point in the history
  • Loading branch information
timhendriks93 committed Jan 16, 2024
1 parent d7394e8 commit c42b9bc
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 17 deletions.
3 changes: 0 additions & 3 deletions examples/SwitchModeButton/SwitchModeButton.ino
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,6 @@ void setup() {
// Add a scene based on PROGMEM data
animation.addScene(ANIMATION_DATA, LENGTH, FPS, FRAMES);

// Add a scene based on (live) Serial data
animation.addScene(Serial);

// Register the mode change callback function
animation.onModeChange(modeChanged);
}
Expand Down
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ platform = native
test_build_src = yes
build_flags =
-Wno-deprecated-declarations
debug_test = animation/test_stop
debug_test = animation/test_scene_callback
lib_deps =
ArduinoFake
50 changes: 37 additions & 13 deletions src/internal/Animation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,42 @@ void Animation::setServoThreshold(byte id, byte value) {
this->servoManager.setThreshold(id, value);
}

void Animation::setScene(byte index) {
Scene *scene = this->scenes[index];

if (!scene) {
return;
}

byte prevIndex = this->playIndex;

this->playIndex = index;
this->scene = scene;

if (this->sceneCallback) {
this->sceneCallback(prevIndex, index);
}
}

void Animation::setRandomScene() {
byte randomIndex = 0;

if (this->countScenes() > 1) {
randomIndex = random(this->addIndex);
}

this->playIndex = randomIndex;
this->scene = this->scenes[this->playIndex];
this->setScene(randomIndex);
}

void Animation::resetScene() {
byte prevIndex = this->playIndex;

this->scene = nullptr;
this->playIndex = 0;

if (this->sceneCallback) {
this->sceneCallback(prevIndex, 0);
}
}

void Animation::play() {
Expand All @@ -74,7 +101,7 @@ void Animation::play() {
}

if (!this->scene) {
this->scene = this->scenes[this->playIndex];
this->setScene(this->playIndex);
}

this->changeMode(MODE_PLAY);
Expand All @@ -89,8 +116,7 @@ void Animation::playSingle(byte index) {
}

if (!this->scene || this->scene->getFrame() == 0) {
this->playIndex = index;
this->scene = scene;
this->setScene(index);
}

this->changeMode(MODE_PLAY_SINGLE);
Expand All @@ -114,7 +140,7 @@ void Animation::loop() {
}

if (!this->scene) {
this->scene = this->scenes[this->playIndex];
this->setScene(this->playIndex);
}

this->changeMode(MODE_LOOP);
Expand Down Expand Up @@ -196,26 +222,24 @@ void Animation::handlePlayMode(unsigned long currentMicros) {
switch (this->mode) {
case MODE_PLAY:
if (this->hasFinished()) {
this->scene = nullptr;
this->resetScene();
} else {
this->playIndex++;
this->scene = this->scenes[this->playIndex];
this->setScene(this->playIndex + 1);
}
break;
case MODE_PLAY_SINGLE:
this->scene = nullptr;
this->resetScene();
break;
case MODE_PLAY_RANDOM:
this->setRandomScene();
this->changeMode(MODE_PLAY_RANDOM);
break;
case MODE_LOOP:
if (this->hasFinished()) {
this->playIndex = 0;
this->setScene(0);
} else {
this->playIndex++;
this->setScene(this->playIndex + 1);
}
this->scene = this->scenes[this->playIndex];
this->changeMode(MODE_LOOP);
break;
}
Expand Down
2 changes: 2 additions & 0 deletions src/internal/Animation.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ class Animation {
void handlePlayMode(unsigned long currentMicros);
void handleStopMode(unsigned long currentMicros);
void handleLiveMode();
void setScene(byte index);
void setRandomScene();
void resetScene();

bool modeIsIn(byte modeAmount, ...);
};
Expand Down
1 change: 1 addition & 0 deletions test/animation/test_play/test_play.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ void test_play(void) {
}

TEST_ASSERT_EQUAL(Animation::MODE_DEFAULT, animation.getMode());
TEST_ASSERT_EQUAL(nullptr, animation.getCurrentScene());
TEST_ASSERT_EQUAL(10, logIndex);
}

Expand Down
70 changes: 70 additions & 0 deletions test/animation/test_scene_callback/test_scene_callback.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include "../test/helper.h"
#include "internal/Animation.h"

#include <unity.h>

using namespace BlenderServoAnimation;

int prevSceneIndex = -1;
int nextSceneIndex = -1;

void setUp(void) {
prevSceneIndex = -1;
nextSceneIndex = -1;
}

void onSceneChange(byte prevArg, byte newArg) {
prevSceneIndex = prevArg;
nextSceneIndex = newArg;
}

void test_play(void) {
Animation animation;
animation.onSceneChange(onSceneChange);
animation.addScene(PROGMEM_DATA, DATA_SIZE, FPS, FRAMES);
animation.addScene(PROGMEM_DATA, DATA_SIZE, FPS, FRAMES);

TEST_ASSERT_EQUAL(-1, prevSceneIndex);
TEST_ASSERT_EQUAL(-1, nextSceneIndex);

animation.play();

TEST_ASSERT_EQUAL(0, prevSceneIndex);
TEST_ASSERT_EQUAL(0, nextSceneIndex);

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

TEST_ASSERT_EQUAL(0, prevSceneIndex);
TEST_ASSERT_EQUAL(1, nextSceneIndex);
}

void test_play_single(void) {
Animation animation;
animation.onSceneChange(onSceneChange);
animation.addScene(PROGMEM_DATA, DATA_SIZE, FPS, FRAMES);
animation.addScene(PROGMEM_DATA, DATA_SIZE, FPS, FRAMES);

TEST_ASSERT_EQUAL(-1, prevSceneIndex);
TEST_ASSERT_EQUAL(-1, nextSceneIndex);

animation.playSingle(1);

TEST_ASSERT_EQUAL(0, prevSceneIndex);
TEST_ASSERT_EQUAL(1, nextSceneIndex);

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

TEST_ASSERT_EQUAL(1, prevSceneIndex);
TEST_ASSERT_EQUAL(0, nextSceneIndex);
}

int main(int argc, char **argv) {
UNITY_BEGIN();
RUN_TEST(test_play);
RUN_TEST(test_play_single);
UNITY_END();
}

0 comments on commit c42b9bc

Please sign in to comment.