Skip to content

Commit fc6ea83

Browse files
committed
add missing show tests
1 parent 3947634 commit fc6ea83

File tree

12 files changed

+620
-22
lines changed

12 files changed

+620
-22
lines changed

platformio.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
platform = native
1313
test_build_src = yes
1414
build_flags = -Wno-deprecated-declarations
15-
debug_test = test_show
15+
debug_test = test_servo
1616
lib_deps =
1717
ArduinoFake
1818
arduino-libraries/Servo@^1.1.8

src/animation/Animation.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,10 @@ void Animation::handlePlayMode(unsigned long currentMicros) {
8585
}
8686

8787
if (this->frame == 0) {
88-
if (this->mode == Animation::MODE_LOOP) {
89-
this->changeMode(Animation::MODE_LOOP);
88+
if (this->mode == MODE_LOOP) {
89+
this->changeMode(MODE_LOOP);
9090
} else {
91-
this->changeMode(Animation::MODE_DEFAULT);
91+
this->changeMode(MODE_DEFAULT);
9292
}
9393
}
9494
}
@@ -106,7 +106,7 @@ void Animation::handleStopMode() {
106106
}
107107

108108
if (allNeutral) {
109-
this->changeMode(Animation::MODE_DEFAULT);
109+
this->changeMode(MODE_DEFAULT);
110110
return;
111111
}
112112

@@ -143,15 +143,15 @@ void Animation::play(unsigned long currentMicros) {
143143
}
144144

145145
this->lastMicros = currentMicros;
146-
this->changeMode(Animation::MODE_PLAY);
146+
this->changeMode(MODE_PLAY);
147147
}
148148

149149
void Animation::pause() {
150150
if (!this->modeIsIn(2, MODE_PLAY, MODE_LOOP)) {
151151
return;
152152
}
153153

154-
this->changeMode(Animation::MODE_PAUSE);
154+
this->changeMode(MODE_PAUSE);
155155
}
156156

157157
void Animation::loop(unsigned long currentMicros) {
@@ -160,7 +160,7 @@ void Animation::loop(unsigned long currentMicros) {
160160
}
161161

162162
this->lastMicros = currentMicros;
163-
this->changeMode(Animation::MODE_LOOP);
163+
this->changeMode(MODE_LOOP);
164164
}
165165

166166
void Animation::stop(byte stepDelay) {
@@ -170,7 +170,7 @@ void Animation::stop(byte stepDelay) {
170170

171171
this->stopStepDelay = stepDelay;
172172
this->frame = 0;
173-
this->changeMode(Animation::MODE_STOP);
173+
this->changeMode(MODE_STOP);
174174
}
175175

176176
void Animation::live(Stream &serial) {
@@ -179,7 +179,7 @@ void Animation::live(Stream &serial) {
179179
}
180180

181181
this->serial = &serial;
182-
this->changeMode(Animation::MODE_LIVE);
182+
this->changeMode(MODE_LIVE);
183183
}
184184

185185
byte Animation::getID() {

src/show/Show.cpp

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,18 @@ int Show::getAnimationIndex(byte id) {
4040
}
4141

4242
void Show::setRandomAnimation() {
43-
byte randomIndex = random(this->addIndex - 1);
43+
byte randomIndex = 0;
44+
45+
if (this->countAnimations() > 1) {
46+
randomIndex = random(this->addIndex - 1);
47+
}
48+
4449
this->playIndex = randomIndex;
4550
this->animation = this->animations[this->playIndex];
4651
}
4752

4853
void Show::play(unsigned long currentMicros) {
49-
if (!this->hasAnimations()) {
54+
if (!this->hasAnimations() || !this->modeIsIn(2, MODE_DEFAULT, MODE_PAUSE)) {
5055
return;
5156
}
5257

@@ -61,7 +66,7 @@ void Show::play(unsigned long currentMicros) {
6166
void Show::playSingle(byte id, unsigned long currentMicros) {
6267
int animationIndex = this->getAnimationIndex(id);
6368

64-
if (animationIndex < 0) {
69+
if (animationIndex < 0 || !this->modeIsIn(2, MODE_DEFAULT, MODE_PAUSE)) {
6570
return;
6671
}
6772

@@ -75,7 +80,7 @@ void Show::playSingle(byte id, unsigned long currentMicros) {
7580
}
7681

7782
void Show::playRandom(unsigned long currentMicros) {
78-
if (!this->hasAnimations()) {
83+
if (!this->hasAnimations() || !this->modeIsIn(2, MODE_DEFAULT, MODE_PAUSE)) {
7984
return;
8085
}
8186

@@ -88,7 +93,7 @@ void Show::playRandom(unsigned long currentMicros) {
8893
}
8994

9095
void Show::loop(unsigned long currentMicros) {
91-
if (!this->hasAnimations()) {
96+
if (!this->hasAnimations() || !this->modeIsIn(2, MODE_DEFAULT, MODE_PAUSE)) {
9297
return;
9398
}
9499

@@ -101,23 +106,37 @@ void Show::loop(unsigned long currentMicros) {
101106
}
102107

103108
void Show::pause() {
104-
if (!this->animation) {
109+
if (!this->animation || !this->modeIsIn(4, MODE_PLAY, MODE_PLAY_SINGLE,
110+
MODE_PLAY_RANDOM, MODE_LOOP)) {
105111
return;
106112
}
107113

108114
this->animation->pause();
109-
this->changeMode(Show::MODE_PAUSE);
115+
this->changeMode(MODE_PAUSE);
110116
}
111117

112118
void Show::stop(byte stepDelay) {
113-
if (!this->animation) {
119+
if (!this->animation || this->modeIsIn(2, MODE_DEFAULT, MODE_STOP)) {
114120
return;
115121
}
116122

117123
this->animation->stop(stepDelay);
118124
this->changeMode(MODE_STOP);
119125
}
120126

127+
void Show::live(Stream &serial) {
128+
if (!this->hasAnimations() || this->mode != MODE_DEFAULT) {
129+
return;
130+
}
131+
132+
if (!this->animation) {
133+
this->animation = this->animations[this->playIndex];
134+
}
135+
136+
this->animation->live(serial);
137+
this->changeMode(MODE_LIVE);
138+
}
139+
121140
void Show::reset() {
122141
this->addIndex = 0;
123142
this->playIndex = 0;
@@ -140,13 +159,14 @@ void Show::run(unsigned long currentMicros) {
140159
this->handlePlayMode(currentMicros);
141160
break;
142161
case MODE_STOP:
143-
this->handleStopMode();
162+
this->handleStopMode(currentMicros);
144163
break;
145164
}
146165
}
147166

148167
void Show::handlePlayMode(unsigned long currentMicros) {
149168
if (!this->animation) {
169+
this->changeMode(MODE_DEFAULT);
150170
return;
151171
}
152172

@@ -193,7 +213,17 @@ void Show::handlePlayMode(unsigned long currentMicros) {
193213
this->animation->play(currentMicros);
194214
}
195215

196-
void Show::handleStopMode() {
216+
void Show::handleStopMode(unsigned long currentMicros) {
217+
if (!this->animation) {
218+
this->changeMode(MODE_DEFAULT);
219+
return;
220+
}
221+
222+
this->animation->run(currentMicros);
223+
224+
if (this->animation->getMode() == Animation::MODE_DEFAULT) {
225+
this->changeMode(MODE_DEFAULT);
226+
}
197227
}
198228

199229
Animation *Show::getCurrentAnimation() {
@@ -212,3 +242,20 @@ void Show::changeMode(byte mode) {
212242
this->modeCallback(prevMode, mode);
213243
}
214244
}
245+
246+
bool Show::modeIsIn(byte modeAmount, ...) {
247+
bool match = false;
248+
249+
va_list modes;
250+
va_start(modes, modeAmount);
251+
252+
for (int i = 0; i < modeAmount; i++) {
253+
if (this->mode == va_arg(modes, int)) {
254+
match = true;
255+
}
256+
}
257+
258+
va_end(modes);
259+
260+
return match;
261+
}

src/show/Show.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "animation/Animation.h"
22
#include <Arduino.h>
3+
#include <stdarg.h>
34

45
#ifndef BlenderServoAnimation_Show_H
56
#define BlenderServoAnimation_Show_H
@@ -24,7 +25,7 @@ class Show {
2425

2526
void changeMode(byte mode);
2627
void handlePlayMode(unsigned long currentMicros);
27-
void handleStopMode();
28+
void handleStopMode(unsigned long currentMicros);
2829
void setRandomAnimation();
2930

3031
int getAnimationIndex(byte id);
@@ -37,8 +38,11 @@ class Show {
3738
static const byte MODE_PAUSE = 4;
3839
static const byte MODE_LOOP = 5;
3940
static const byte MODE_STOP = 6;
41+
static const byte MODE_LIVE = 7;
4042

43+
byte getMode();
4144
byte countAnimations();
45+
4246
void addAnimation(Animation &animation);
4347
void addAnimations(Animation animations[], byte animationAmount);
4448
void onModeChange(mcb modeCallback);
@@ -49,10 +53,13 @@ class Show {
4953
void loop(unsigned long currentMicros = micros());
5054
void pause();
5155
void stop(byte stepDelay = 20);
56+
void live(Stream &serial);
5257
void reset();
53-
byte getMode();
58+
5459
bool hasAnimations();
5560
bool hasAnimation(byte id);
61+
bool modeIsIn(byte modeAmount, ...);
62+
5663
Animation *getCurrentAnimation();
5764
};
5865

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#include "../helper.h"
2+
#include "BlenderServoAnimation.h"
3+
#include <unity.h>
4+
5+
using namespace BlenderServoAnimation;
6+
7+
void setUp(void) {
8+
setUpHelper();
9+
}
10+
11+
const int positionsA[5] PROGMEM = {350, 340, 330, 340, 330};
12+
const int positionsB[5] PROGMEM = {250, 240, 230, 240, 230};
13+
14+
void test_loop(void) {
15+
Animation animation(FPS, FRAMES);
16+
Servo servos[] = {
17+
Servo(1, positionsA, move),
18+
Servo(2, positionsB, move),
19+
Servo(3, move),
20+
};
21+
animation.addServos(servos, 3);
22+
TEST_ASSERT_EQUAL(Animation::MODE_DEFAULT, animation.getMode());
23+
animation.loop(0);
24+
TEST_ASSERT_EQUAL(Animation::MODE_LOOP, animation.getMode());
25+
26+
int expA[9] = {340, 330, 340, 330, 350, 340, 330, 340, 330};
27+
int expB[9] = {240, 230, 240, 230, 250, 240, 230, 240, 230};
28+
29+
for (long i = 0; i < FRAME_MICROS * (long)9; i++) {
30+
animation.run(i);
31+
}
32+
33+
for (int i = 0; i < 9; i++) {
34+
TEST_ASSERT_EQUAL(expA[i], lastPositions[1].positions[i]);
35+
TEST_ASSERT_EQUAL(expB[i], lastPositions[2].positions[i]);
36+
TEST_ASSERT_EQUAL(0, lastPositions[3].positions[i]);
37+
}
38+
39+
TEST_ASSERT_EQUAL(Animation::MODE_LOOP, animation.getMode());
40+
}
41+
42+
void test_call_twice(void) {
43+
Animation animation(FPS, FRAMES);
44+
animation.onModeChange(onModeChange);
45+
46+
TEST_ASSERT_EQUAL(Animation::MODE_DEFAULT, animation.getMode());
47+
animation.loop(0);
48+
TEST_ASSERT_EQUAL(Animation::MODE_LOOP, animation.getMode());
49+
animation.loop(0);
50+
TEST_ASSERT_EQUAL(Animation::MODE_LOOP, animation.getMode());
51+
52+
TEST_ASSERT_EQUAL(1, modeChangeCount);
53+
}
54+
55+
void test_prevented(void) {
56+
Serial_ mock;
57+
Animation animation(FPS, FRAMES);
58+
59+
animation.play(0);
60+
TEST_ASSERT_EQUAL(Animation::MODE_PLAY, animation.getMode());
61+
animation.loop(0);
62+
TEST_ASSERT_EQUAL(Animation::MODE_PLAY, animation.getMode());
63+
animation.stop();
64+
TEST_ASSERT_EQUAL(Animation::MODE_STOP, animation.getMode());
65+
animation.loop(0);
66+
TEST_ASSERT_EQUAL(Animation::MODE_STOP, animation.getMode());
67+
animation.run(0);
68+
animation.live(mock);
69+
TEST_ASSERT_EQUAL(Animation::MODE_LIVE, animation.getMode());
70+
animation.loop(0);
71+
TEST_ASSERT_EQUAL(Animation::MODE_LIVE, animation.getMode());
72+
}
73+
74+
void test_allowed(void) {
75+
Animation animation(FPS, FRAMES);
76+
77+
TEST_ASSERT_EQUAL(Animation::MODE_DEFAULT, animation.getMode());
78+
animation.loop(0);
79+
TEST_ASSERT_EQUAL(Animation::MODE_LOOP, animation.getMode());
80+
animation.pause();
81+
TEST_ASSERT_EQUAL(Animation::MODE_PAUSE, animation.getMode());
82+
animation.loop(0);
83+
TEST_ASSERT_EQUAL(Animation::MODE_LOOP, animation.getMode());
84+
}
85+
86+
int main(int argc, char **argv) {
87+
UNITY_BEGIN();
88+
RUN_TEST(test_loop);
89+
RUN_TEST(test_call_twice);
90+
RUN_TEST(test_prevented);
91+
RUN_TEST(test_allowed);
92+
UNITY_END();
93+
}

0 commit comments

Comments
 (0)