From c42b9bcb059ab73bf42c8e87ba816d004260989e Mon Sep 17 00:00:00 2001 From: Tim Hendriks Date: Tue, 16 Jan 2024 21:52:46 +0100 Subject: [PATCH] add first scene callback tests --- .../SwitchModeButton/SwitchModeButton.ino | 3 - platformio.ini | 2 +- src/internal/Animation.cpp | 50 +++++++++---- src/internal/Animation.h | 2 + test/animation/test_play/test_play.cpp | 1 + .../test_scene_callback.cpp | 70 +++++++++++++++++++ 6 files changed, 111 insertions(+), 17 deletions(-) create mode 100644 test/animation/test_scene_callback/test_scene_callback.cpp diff --git a/examples/SwitchModeButton/SwitchModeButton.ino b/examples/SwitchModeButton/SwitchModeButton.ino index c4e1a69..3d7de7a 100644 --- a/examples/SwitchModeButton/SwitchModeButton.ino +++ b/examples/SwitchModeButton/SwitchModeButton.ino @@ -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); } diff --git a/platformio.ini b/platformio.ini index a340cf3..22dff0d 100644 --- a/platformio.ini +++ b/platformio.ini @@ -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 diff --git a/src/internal/Animation.cpp b/src/internal/Animation.cpp index 5d9314c..7ec7571 100644 --- a/src/internal/Animation.cpp +++ b/src/internal/Animation.cpp @@ -57,6 +57,23 @@ 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; @@ -64,8 +81,18 @@ void Animation::setRandomScene() { 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() { @@ -74,7 +101,7 @@ void Animation::play() { } if (!this->scene) { - this->scene = this->scenes[this->playIndex]; + this->setScene(this->playIndex); } this->changeMode(MODE_PLAY); @@ -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); @@ -114,7 +140,7 @@ void Animation::loop() { } if (!this->scene) { - this->scene = this->scenes[this->playIndex]; + this->setScene(this->playIndex); } this->changeMode(MODE_LOOP); @@ -196,14 +222,13 @@ 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(); @@ -211,11 +236,10 @@ void Animation::handlePlayMode(unsigned long currentMicros) { 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; } diff --git a/src/internal/Animation.h b/src/internal/Animation.h index 9d4bdc1..ef27f76 100644 --- a/src/internal/Animation.h +++ b/src/internal/Animation.h @@ -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, ...); }; diff --git a/test/animation/test_play/test_play.cpp b/test/animation/test_play/test_play.cpp index 7622fa9..315f5a2 100644 --- a/test/animation/test_play/test_play.cpp +++ b/test/animation/test_play/test_play.cpp @@ -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); } diff --git a/test/animation/test_scene_callback/test_scene_callback.cpp b/test/animation/test_scene_callback/test_scene_callback.cpp new file mode 100644 index 0000000..bc6baea --- /dev/null +++ b/test/animation/test_scene_callback/test_scene_callback.cpp @@ -0,0 +1,70 @@ +#include "../test/helper.h" +#include "internal/Animation.h" + +#include + +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(); +}