Skip to content

Commit

Permalink
support live mode change with AnimationData instance
Browse files Browse the repository at this point in the history
  • Loading branch information
timhendriks93 committed Jan 16, 2024
1 parent 83cc85d commit d7394e8
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 6 deletions.
2 changes: 1 addition & 1 deletion examples/WebSocketLiveMode/WebSocketLiveMode.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}

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_pause
debug_test = animation/test_stop
lib_deps =
ArduinoFake
17 changes: 16 additions & 1 deletion src/internal/Animation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Animation::~Animation() {
}
}

if (this->liveStream) {
if (this->liveStream != nullptr && this->isOneTimeLiveStream) {
delete this->liveStream;
}
}
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}
Expand Down
5 changes: 4 additions & 1 deletion src/internal/Animation.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);

Expand All @@ -61,6 +62,8 @@ class Animation {

AnimationData *liveStream = nullptr;

bool isOneTimeLiveStream = false;

mcb modeCallback = nullptr;
scb sceneCallback = nullptr;

Expand Down
4 changes: 2 additions & 2 deletions test/animation/test_live/test_live.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit d7394e8

Please sign in to comment.