Skip to content

Commit

Permalink
add live mode test
Browse files Browse the repository at this point in the history
  • Loading branch information
timhendriks93 committed Jan 14, 2024
1 parent 19a691b commit d9ca634
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 14 deletions.
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 = test_scene
debug_test = test_servo_manager
lib_deps =
ArduinoFake
2 changes: 1 addition & 1 deletion src/internal/Animation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ void Animation::handleStopMode(unsigned long currentMicros) {
}

void Animation::handleLiveMode() {
this->servoManager.parseStream(this->liveStream);
this->servoManager.parseStream(this->liveStream, false);
}

Scene *Animation::getCurrentScene() {
Expand Down
4 changes: 2 additions & 2 deletions src/internal/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void Scene::stop(unsigned long currentMicros) {
}

this->lastMicros = currentMicros;
this->servoManager->moveAllServosToNeutral();
this->servoManager->moveAllTowardsNeutral();

if (this->servoManager->servosAreAllNeutral()) {
this->frame = 0;
Expand Down Expand Up @@ -97,7 +97,7 @@ int Scene::getFrames() {
return this->frames;
}

Stream* Scene::getAnimationData() {
Stream *Scene::getAnimationData() {
if (this->progmemData) {
return this->progmemData;
}
Expand Down
4 changes: 2 additions & 2 deletions src/internal/Scene.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "ServoManager.h"
#include "ProgmemStream.h"
#include "ServoManager.h"
#include <Arduino.h>

#ifndef BlenderServoAnimation_Scene_H
Expand Down Expand Up @@ -50,7 +50,7 @@ class Scene {

unsigned int getMicrosDiff(unsigned long currentMicros);

Stream* getAnimationData();
Stream *getAnimationData();
};

} // namespace BlenderServoAnimation
Expand Down
2 changes: 1 addition & 1 deletion src/internal/ServoManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void ServoManager::handleCommand(Command command) {
this->servos[id]->move(position);
}

void ServoManager::moveAllServosToNeutral() {
void ServoManager::moveAllTowardsNeutral() {
for (int i = 0; i < MAX_SERVO_COUNT; i++) {
Servo *servo = this->servos[i];

Expand Down
2 changes: 1 addition & 1 deletion src/internal/ServoManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ServoManager {
void setDefaultThreshold(byte value);
void setThreshold(byte servoId, byte value);
void parseStream(Stream *stream, bool considerLineBreaks = true);
void moveAllServosToNeutral();
void moveAllTowardsNeutral();

bool hasPositionCallback();
bool servosAreAllNeutral();
Expand Down
63 changes: 63 additions & 0 deletions test/animation/test_live/test_live.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include "../test/helper.h"
#include "internal/Animation.h"
#include <unity.h>

using namespace BlenderServoAnimation;
using namespace fakeit;

void setUp(void) {
ArduinoFakeReset();
}

void test_prevented(void) {
Animation animation;
Serial_ mock;
animation.addScene(mock);

When(OverloadedMethod(ArduinoFake(), random, long(long))).Return(0);

animation.loop();
TEST_ASSERT_EQUAL(Animation::MODE_LOOP, animation.getMode());
animation.live(mock);
TEST_ASSERT_EQUAL(Animation::MODE_LOOP, animation.getMode());
animation.pause();
animation.play();
TEST_ASSERT_EQUAL(Animation::MODE_PLAY, animation.getMode());
animation.live(mock);
TEST_ASSERT_EQUAL(Animation::MODE_PLAY, animation.getMode());
animation.pause();
animation.playSingle(0);
TEST_ASSERT_EQUAL(Animation::MODE_PLAY_SINGLE, animation.getMode());
animation.live(mock);
TEST_ASSERT_EQUAL(Animation::MODE_PLAY_SINGLE, animation.getMode());
animation.pause();
animation.playRandom();
TEST_ASSERT_EQUAL(Animation::MODE_PLAY_RANDOM, animation.getMode());
animation.live(mock);
TEST_ASSERT_EQUAL(Animation::MODE_PLAY_RANDOM, animation.getMode());
animation.pause();
TEST_ASSERT_EQUAL(Animation::MODE_PAUSE, animation.getMode());
animation.live(mock);
TEST_ASSERT_EQUAL(Animation::MODE_PAUSE, animation.getMode());
animation.stop();
TEST_ASSERT_EQUAL(Animation::MODE_STOP, animation.getMode());
animation.live(mock);
TEST_ASSERT_EQUAL(Animation::MODE_STOP, animation.getMode());
}

void test_allowed(void) {
Serial_ mock;
Animation animation;
animation.addScene(mock);

TEST_ASSERT_EQUAL(Animation::MODE_DEFAULT, animation.getMode());
animation.live(mock);
TEST_ASSERT_EQUAL(Animation::MODE_LIVE, animation.getMode());
}

int main(int argc, char **argv) {
UNITY_BEGIN();
RUN_TEST(test_prevented);
RUN_TEST(test_allowed);
UNITY_END();
}
10 changes: 5 additions & 5 deletions test/animation/test_stop/test_stop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ void test_allowed(void) {
int main(int argc, char **argv) {
UNITY_BEGIN();
RUN_TEST(test_stop_play);
// RUN_TEST(test_stop_play_single);
// RUN_TEST(test_stop_play_random);
// RUN_TEST(test_stop_loop);
// RUN_TEST(test_prevented);
// RUN_TEST(test_allowed);
RUN_TEST(test_stop_play_single);
RUN_TEST(test_stop_play_random);
RUN_TEST(test_stop_loop);
RUN_TEST(test_prevented);
RUN_TEST(test_allowed);
UNITY_END();
}
30 changes: 29 additions & 1 deletion test/test_servo_manager/test_servo_manager.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "../test/helper.h"
#include "internal/ServoManager.h"
#include "internal/ProgmemStream.h"
#include "internal/ServoManager.h"
#include <unity.h>

using namespace BlenderServoAnimation;
Expand Down Expand Up @@ -37,9 +37,37 @@ void test_parse_stream_without_line_breaks(void) {
TEST_ASSERT_EQUAL(10, logIndex);
}

void test_move_all_towards_neutral(void) {
ProgmemStream stream(PROGMEM_DATA, DATA_SIZE);
ServoManager servoManager;
servoManager.setPositionCallback(move);
servoManager.setDefaultThreshold(20);
servoManager.setThreshold(0, 10);

TEST_ASSERT_EQUAL(0, logIndex);

servoManager.parseStream(&stream);
servoManager.parseStream(&stream);
servoManager.parseStream(&stream);

TEST_ASSERT_EQUAL(6, logIndex);
TEST_ASSERT_FALSE(servoManager.servosAreAllNeutral());

servoManager.moveAllTowardsNeutral();

TEST_ASSERT_EQUAL(8, logIndex);
TEST_ASSERT_FALSE(servoManager.servosAreAllNeutral());

servoManager.moveAllTowardsNeutral();

TEST_ASSERT_EQUAL(10, logIndex);
TEST_ASSERT_TRUE(servoManager.servosAreAllNeutral());
}

int main(int argc, char **argv) {
UNITY_BEGIN();
RUN_TEST(test_parse_stream);
RUN_TEST(test_parse_stream_without_line_breaks);
RUN_TEST(test_move_all_towards_neutral);
UNITY_END();
}

0 comments on commit d9ca634

Please sign in to comment.