Skip to content

Commit 4069b6a

Browse files
committed
use index instead of id based approach
1 parent 2cb9f9f commit 4069b6a

File tree

14 files changed

+62
-78
lines changed

14 files changed

+62
-78
lines changed

examples/Show/Show.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ void setup() {
4545

4646
// Trigger the show loop mode
4747
show.loop();
48-
48+
4949
// There are also other playback options
5050
// show.play(); // Plays all animations once in the order they have been added
5151
// show.playRandom(); // Randomly plays animations in a loop

src/animation/Animation.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,7 @@ using namespace BlenderServoAnimation;
77
Animation::Animation() {
88
}
99

10-
Animation::Animation(byte fps, int frames)
11-
: Animation::Animation(0, fps, frames) {
12-
}
13-
14-
Animation::Animation(byte id, byte fps, int frames) {
15-
this->id = id;
10+
Animation::Animation(byte fps, int frames) {
1611
this->fps = fps;
1712
this->frames = frames;
1813
this->frameMicros = round((float)Animation::SECOND_IN_MICROS / (float)fps);
@@ -182,8 +177,8 @@ void Animation::live(Stream &serial) {
182177
this->changeMode(MODE_LIVE);
183178
}
184179

185-
byte Animation::getID() {
186-
return this->id;
180+
byte Animation::getFPS() {
181+
return this->fps;
187182
}
188183

189184
byte Animation::getMode() {
@@ -194,6 +189,10 @@ int Animation::getFrame() {
194189
return this->frame;
195190
}
196191

192+
int Animation::getFrames() {
193+
return this->frames;
194+
}
195+
197196
bool Animation::modeIsIn(byte modeAmount, ...) {
198197
bool match = false;
199198

src/animation/Animation.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ class Animation {
1616
static const int MAX_SERVO_COUNT = 256;
1717
static const long SECOND_IN_MICROS = 1000000;
1818

19-
byte id = 0;
2019
byte fps = 0;
2120
byte stopStepDelay = 20;
2221
byte mode = MODE_DEFAULT;
@@ -49,7 +48,6 @@ class Animation {
4948

5049
Animation();
5150
Animation(byte fps, int frames);
52-
Animation(byte id, byte fps, int frames);
5351

5452
void addServo(Servo &servo);
5553
void addServos(Servo servos[], byte servoAmount);
@@ -61,10 +59,11 @@ class Animation {
6159
void stop(byte stepDelay = 20);
6260
void live(Stream &serial);
6361

64-
byte getID();
62+
byte getFPS();
6563
byte getMode();
6664

6765
int getFrame();
66+
int getFrames();
6867

6968
bool modeIsIn(byte modeAmount, ...);
7069
};

src/show/Show.cpp

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ bool Show::hasAnimations() {
1212
return this->countAnimations() > 0;
1313
}
1414

15-
bool Show::hasAnimation(byte id) {
16-
return this->getAnimationIndex(id) > -1;
15+
bool Show::hasAnimation(byte index) {
16+
return this->animations[index] != nullptr;
1717
}
1818

1919
void Show::addAnimation(Animation &animation) {
@@ -27,18 +27,6 @@ void Show::addAnimations(Animation animations[], byte animationAmount) {
2727
}
2828
}
2929

30-
int Show::getAnimationIndex(byte id) {
31-
for (int i = 0; i < this->addIndex; i++) {
32-
Animation *animation = this->animations[i];
33-
34-
if (animation && animation->getID() == id) {
35-
return i;
36-
}
37-
}
38-
39-
return -1;
40-
}
41-
4230
void Show::setRandomAnimation() {
4331
byte randomIndex = 0;
4432

@@ -63,16 +51,16 @@ void Show::play(unsigned long currentMicros) {
6351
this->changeMode(MODE_PLAY);
6452
}
6553

66-
void Show::playSingle(byte id, unsigned long currentMicros) {
67-
int animationIndex = this->getAnimationIndex(id);
54+
void Show::playSingle(byte index, unsigned long currentMicros) {
55+
Animation *animation = this->animations[index];
6856

69-
if (animationIndex < 0 || !this->modeIsIn(2, MODE_DEFAULT, MODE_PAUSE)) {
57+
if (animation == nullptr || !this->modeIsIn(2, MODE_DEFAULT, MODE_PAUSE)) {
7058
return;
7159
}
7260

7361
if (!this->animation || this->animation->getFrame() == 0) {
74-
this->playIndex = animationIndex;
75-
this->animation = this->animations[this->playIndex];
62+
this->playIndex = index;
63+
this->animation = animation;
7664
}
7765

7866
this->animation->play(currentMicros);

src/show/Show.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ class Show {
2828
void handleStopMode(unsigned long currentMicros);
2929
void setRandomAnimation();
3030

31-
int getAnimationIndex(byte id);
32-
3331
public:
3432
static const byte MODE_DEFAULT = 0;
3533
static const byte MODE_PLAY = 1;
@@ -48,7 +46,7 @@ class Show {
4846
void onModeChange(mcb modeCallback);
4947
void run(unsigned long currentMicros = micros());
5048
void play(unsigned long currentMicros = micros());
51-
void playSingle(byte id, unsigned long currentMicros = micros());
49+
void playSingle(byte index, unsigned long currentMicros = micros());
5250
void playRandom(unsigned long currentMicros = micros());
5351
void loop(unsigned long currentMicros = micros());
5452
void pause();
@@ -57,7 +55,7 @@ class Show {
5755
void reset();
5856

5957
bool hasAnimations();
60-
bool hasAnimation(byte id);
58+
bool hasAnimation(byte index);
6159
bool modeIsIn(byte modeAmount, ...);
6260

6361
Animation *getCurrentAnimation();

test/animation/test_loop/test_loop.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ void test_loop(void) {
2626
int expA[9] = {340, 330, 340, 330, 350, 340, 330, 340, 330};
2727
int expB[9] = {240, 230, 240, 230, 250, 240, 230, 240, 230};
2828

29-
for (long i = 0; i < FRAME_MICROS * (long)9; i++) {
29+
for (long i = 0; i < FRAME_MICROS * (long)10; i += FRAME_MICROS) {
3030
animation.run(i);
3131
}
3232

test/animation/test_play/test_play.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ void test_play(void) {
2626
int expA[5] = {340, 330, 340, 330, 350};
2727
int expB[5] = {240, 230, 240, 230, 250};
2828

29-
for (long i = 0; i < FRAME_MICROS * (long)9; i++) {
29+
for (long i = 0; i < FRAME_MICROS * (long)9; i += FRAME_MICROS) {
3030
animation.run(i);
3131
}
3232

test/show/test_live/test_live.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ void test_without_animations(void) {
2020
}
2121

2222
void test_prevented(void) {
23-
Animation animation(0, FPS, FRAMES);
23+
Animation animation(FPS, FRAMES);
2424
Serial_ mock;
2525
Show show;
2626
show.addAnimation(animation);

test/show/test_loop/test_loop.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ void setUp(void) {
1313
}
1414

1515
void test_loop(void) {
16-
Animation animationA(0, FPS, FRAMES);
17-
Animation animationB(1, FPS, FRAMES);
16+
Animation animationA(FPS, 4);
17+
Animation animationB(FPS, 5);
1818

1919
Show show;
20-
show.addAnimation(animationA);
2120
show.addAnimation(animationB);
21+
show.addAnimation(animationA);
2222

2323
TEST_ASSERT_TRUE(show.hasAnimations());
2424
TEST_ASSERT_EQUAL(2, show.countAnimations());
@@ -28,21 +28,21 @@ void test_loop(void) {
2828
show.loop(0);
2929

3030
TEST_ASSERT_EQUAL(Show::MODE_LOOP, show.getMode());
31-
TEST_ASSERT_EQUAL(0, show.getCurrentAnimation()->getID());
31+
TEST_ASSERT_EQUAL(5, show.getCurrentAnimation()->getFrames());
3232

33-
for (long i = 0; i < FRAME_MICROS * (long)6; i++) {
33+
for (long i = 0; i < FRAME_MICROS * (long)6; i += FRAME_MICROS) {
3434
show.run(i);
3535
}
3636

3737
TEST_ASSERT_EQUAL(Show::MODE_LOOP, show.getMode());
38-
TEST_ASSERT_EQUAL(1, show.getCurrentAnimation()->getID());
38+
TEST_ASSERT_EQUAL(4, show.getCurrentAnimation()->getFrames());
3939

40-
for (long i = 0; i < FRAME_MICROS * (long)5; i++) {
40+
for (long i = 0; i < FRAME_MICROS * (long)4; i += FRAME_MICROS) {
4141
show.run(i);
4242
}
4343

4444
TEST_ASSERT_EQUAL(Show::MODE_LOOP, show.getMode());
45-
TEST_ASSERT_EQUAL(0, show.getCurrentAnimation()->getID());
45+
TEST_ASSERT_EQUAL(5, show.getCurrentAnimation()->getFrames());
4646
}
4747

4848
void test_without_animations(void) {
@@ -52,7 +52,7 @@ void test_without_animations(void) {
5252
}
5353

5454
void test_prevented(void) {
55-
Animation animation(0, FPS, FRAMES);
55+
Animation animation(FPS, FRAMES);
5656
Serial_ mock;
5757
Show show;
5858
show.addAnimation(animation);

test/show/test_pause/test_pause.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ void setUp(void) {
1313
}
1414

1515
void test_pause(byte mode) {
16-
Animation animationA(0, FPS, FRAMES);
17-
Animation animationB(1, FPS, FRAMES);
16+
Animation animationA(FPS, FRAMES);
17+
Animation animationB(FPS, FRAMES);
1818

1919
Show show;
2020
show.addAnimation(animationA);
@@ -40,15 +40,15 @@ void test_pause(byte mode) {
4040
TEST_ASSERT_EQUAL(mode, show.getMode());
4141
TEST_ASSERT_EQUAL(0, show.getCurrentAnimation()->getFrame());
4242

43-
for (long i = 0; i < FRAME_MICROS * (long)3; i++) {
43+
for (long i = 0; i < FRAME_MICROS * (long)3; i += FRAME_MICROS) {
4444
show.run(i);
4545
}
4646

4747
TEST_ASSERT_EQUAL(2, show.getCurrentAnimation()->getFrame());
4848

4949
show.pause();
5050

51-
for (long i = 0; i < FRAME_MICROS * (long)3; i++) {
51+
for (long i = 0; i < FRAME_MICROS * (long)3; i += FRAME_MICROS) {
5252
show.run(i);
5353
}
5454

0 commit comments

Comments
 (0)