From d7394e87d0088971590303cd6cc4de7ce69ba832 Mon Sep 17 00:00:00 2001 From: Tim Hendriks Date: Tue, 16 Jan 2024 17:20:10 +0100 Subject: [PATCH] support live mode change with AnimationData instance --- .../WebSocketLiveMode/WebSocketLiveMode.ino | 2 +- platformio.ini | 2 +- src/internal/Animation.cpp | 17 ++++++++++++++++- src/internal/Animation.h | 5 ++++- test/animation/test_live/test_live.cpp | 4 ++-- 5 files changed, 24 insertions(+), 6 deletions(-) diff --git a/examples/WebSocketLiveMode/WebSocketLiveMode.ino b/examples/WebSocketLiveMode/WebSocketLiveMode.ino index 29766ad..0fc7240 100644 --- a/examples/WebSocketLiveMode/WebSocketLiveMode.ino +++ b/examples/WebSocketLiveMode/WebSocketLiveMode.ino @@ -45,7 +45,7 @@ void onWebSocketEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, } for (size_t i = 0; i < len; i++) { - liveStream.write(data[i]); + liveStream.writeByte(data[i]); } } diff --git a/platformio.ini b/platformio.ini index ca0300d..a340cf3 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_pause +debug_test = animation/test_stop lib_deps = ArduinoFake diff --git a/src/internal/Animation.cpp b/src/internal/Animation.cpp index 8ae1398..5d9314c 100644 --- a/src/internal/Animation.cpp +++ b/src/internal/Animation.cpp @@ -11,7 +11,7 @@ Animation::~Animation() { } } - if (this->liveStream) { + if (this->liveStream != nullptr && this->isOneTimeLiveStream) { delete this->liveStream; } } @@ -143,6 +143,16 @@ void Animation::live(Stream &stream) { } this->liveStream = new AnimationData(&stream); + this->isOneTimeLiveStream = true; + this->changeMode(MODE_LIVE); +} + +void Animation::live(AnimationData &data) { + if (this->mode != MODE_DEFAULT) { + return; + } + + this->liveStream = &data; this->changeMode(MODE_LIVE); } @@ -253,6 +263,11 @@ void Animation::changeMode(byte mode) { byte prevMode = this->mode; this->mode = mode; + if (prevMode == MODE_LIVE && this->isOneTimeLiveStream) { + delete this->liveStream; + this->liveStream = nullptr; + } + if (this->modeCallback) { this->modeCallback(prevMode, mode); } diff --git a/src/internal/Animation.h b/src/internal/Animation.h index b8af1b5..9d4bdc1 100644 --- a/src/internal/Animation.h +++ b/src/internal/Animation.h @@ -30,7 +30,7 @@ class Animation { int countScenes(); void addScene(const byte *data, int dataLength, byte fps, int frames); - void addScene(Stream &data, byte fps = 0, int frames = 0); + void addScene(Stream &data, byte fps, int frame); void onPositionChange(pcb positionCallback); void onModeChange(mcb modeCallback); void onSceneChange(scb sceneCallback); @@ -42,6 +42,7 @@ class Animation { void pause(); void stop(); void live(Stream &stream); + void live(AnimationData &data); void setDefaultServoThreshold(byte value); void setServoThreshold(byte id, byte value); @@ -61,6 +62,8 @@ class Animation { AnimationData *liveStream = nullptr; + bool isOneTimeLiveStream = false; + mcb modeCallback = nullptr; scb sceneCallback = nullptr; diff --git a/test/animation/test_live/test_live.cpp b/test/animation/test_live/test_live.cpp index 5d17be3..3d7d4a9 100644 --- a/test/animation/test_live/test_live.cpp +++ b/test/animation/test_live/test_live.cpp @@ -12,7 +12,7 @@ void setUp(void) { void test_prevented(void) { Animation animation; StreamMock mock; - animation.addScene(mock); + animation.addScene(PROGMEM_DATA, DATA_SIZE, FPS, FRAMES); When(OverloadedMethod(ArduinoFake(), random, long(long))).Return(0); @@ -48,7 +48,7 @@ void test_prevented(void) { void test_allowed(void) { StreamMock mock; Animation animation; - animation.addScene(mock); + animation.addScene(PROGMEM_DATA, DATA_SIZE, FPS, FRAMES); TEST_ASSERT_EQUAL(Animation::MODE_DEFAULT, animation.getMode()); animation.live(mock);