Skip to content

Commit 189e958

Browse files
committed
use micros instead of millis
1 parent c0d9279 commit 189e958

File tree

4 files changed

+42
-34
lines changed

4 files changed

+42
-34
lines changed

examples/SwitchModeButton/SwitchModeButton.ino

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ void setup() {
8989
}
9090

9191
void loop() {
92+
// Update the mode button state on each loop
93+
modeButton.tick();
94+
9295
// Update the animation state on each loop
9396
animation.run();
9497
}

src/BlenderServoAnimation.cpp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ Animation::Animation() {
88
}
99

1010
Animation::Animation(byte fps, int frames) {
11-
this->frameMillis = Animation::SECOND_IN_MILLIS / fps;
11+
this->fps = fps;
1212
this->frames = frames;
13+
this->frameMicros = round((float)Animation::SECOND_IN_MICROS / (float)fps);
14+
this->diffPerSecond = Animation::SECOND_IN_MICROS - (this->frameMicros * fps);
1315
}
1416

1517
void Animation::addServo(Servo &servo) {
@@ -23,12 +25,10 @@ void Animation::addServos(Servo servos[], byte servoAmount) {
2325
}
2426
}
2527

26-
void Animation::run(unsigned long currentMillis) {
27-
this->currentMillis = currentMillis;
28-
28+
void Animation::run(unsigned long currentMicros) {
2929
switch (mode) {
3030
case MODE_PLAY:
31-
this->handlePlayMode();
31+
this->handlePlayMode(currentMicros);
3232
break;
3333
case MODE_STOP:
3434
this->handleStopMode();
@@ -39,20 +39,24 @@ void Animation::run(unsigned long currentMillis) {
3939
}
4040
}
4141

42-
void Animation::handlePlayMode() {
43-
bool isNewFrame = this->currentMillis - this->lastMillis >= this->frameMillis;
42+
void Animation::handlePlayMode(unsigned long currentMicros) {
43+
bool isNewFrame = currentMicros - this->lastMicros >= this->frameMicros;
4444

4545
if (!isNewFrame || this->frames == 0) {
4646
return;
4747
}
4848

49-
this->lastMillis = this->currentMillis;
49+
this->lastMicros = currentMicros;
5050
this->frame++;
5151

5252
if (this->frame >= this->frames) {
5353
this->frame = 0;
5454
}
5555

56+
if (this->frame % this->fps == 0) {
57+
this->lastMicros += this->diffPerSecond;
58+
}
59+
5660
for (int i = 0; i < MAX_SERVO_COUNT; i++) {
5761
Servo *servo = this->servos[i];
5862

@@ -76,7 +80,6 @@ void Animation::handleStopMode() {
7680

7781
if (allNeutral) {
7882
this->mode = Animation::MODE_DEFAULT;
79-
this->frame = 0;
8083
return;
8184
}
8285

@@ -109,8 +112,7 @@ void Animation::handleLiveMode() {
109112

110113
void Animation::play() {
111114
this->mode = Animation::MODE_PLAY;
112-
this->currentMillis = millis();
113-
this->lastMillis = this->currentMillis;
115+
this->lastMicros = micros();
114116
}
115117

116118
void Animation::pause() {
@@ -120,6 +122,7 @@ void Animation::pause() {
120122
void Animation::stop(byte stepDelay) {
121123
this->mode = Animation::MODE_STOP;
122124
this->stopStepDelay = stepDelay;
125+
this->frame = 0;
123126
}
124127

125128
void Animation::live(Stream &serial) {

src/BlenderServoAnimation.h

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,25 @@ namespace BlenderServoAnimation {
99
class Animation {
1010
private:
1111
static const int MAX_SERVO_COUNT = 256;
12-
static const int SECOND_IN_MILLIS = 1000;
12+
static const long SECOND_IN_MICROS = 1000000;
1313

14-
byte frameMillis = 0;
14+
byte fps = 0;
1515
byte stopStepDelay = 20;
1616
byte mode = MODE_DEFAULT;
1717

18-
int frame = 0;
19-
int frames = 0;
18+
int diffPerSecond = 0;
2019

21-
unsigned long currentMillis;
22-
unsigned long lastMillis;
20+
unsigned int frame = 0;
21+
unsigned int frames = 0;
22+
unsigned int frameMicros = 0;
23+
24+
unsigned long lastMicros;
2325

2426
Servo *servos[MAX_SERVO_COUNT] = {};
2527
Stream *serial;
2628
Command command;
2729

28-
void handlePlayMode();
30+
void handlePlayMode(unsigned long currentMicros);
2931

3032
void handleStopMode();
3133

@@ -46,7 +48,7 @@ class Animation {
4648

4749
void addServos(Servo servos[], byte servoAmount);
4850

49-
void run(unsigned long currentMillis = millis());
51+
void run(unsigned long currentMicros = micros());
5052

5153
void play();
5254

test/test_animation/test_animation.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
using namespace BlenderServoAnimation;
66

77
#define FPS 60
8-
#define FRAME_MILLIS 1000 / FPS
8+
#define FRAMES 5
9+
#define FRAME_MICROS 16667
910

1011
struct positionLog {
1112
int index;
@@ -34,7 +35,7 @@ const int positionsA[5] PROGMEM = {350, 340, 330, 340, 330};
3435
const int positionsB[5] PROGMEM = {250, 240, 230, 240, 230};
3536

3637
void test_play(void) {
37-
Animation animation(FPS, 5);
38+
Animation animation(FPS, FRAMES);
3839
Servo servos[] = {
3940
Servo(1, positionsA, move),
4041
Servo(2, positionsB, move),
@@ -48,20 +49,19 @@ void test_play(void) {
4849
int expA[9] = {340, 330, 340, 330, 350, 340, 330, 340, 330};
4950
int expB[9] = {240, 230, 240, 230, 250, 240, 230, 240, 230};
5051

52+
for (long i = 0; i < FRAME_MICROS * (long)9; i++) {
53+
animation.run(i);
54+
}
55+
5156
for (int i = 0; i < 9; i++) {
52-
animation.run(FRAME_MILLIS * (i + 1));
5357
TEST_ASSERT_EQUAL(expA[i], lastPositions[1].positions[i]);
5458
TEST_ASSERT_EQUAL(expB[i], lastPositions[2].positions[i]);
5559
TEST_ASSERT_EQUAL(0, lastPositions[3].positions[i]);
56-
animation.run(FRAME_MILLIS * (i + 1.5));
57-
TEST_ASSERT_EQUAL(0, lastPositions[1].positions[i + 1]);
58-
TEST_ASSERT_EQUAL(0, lastPositions[2].positions[i + 1]);
59-
TEST_ASSERT_EQUAL(0, lastPositions[3].positions[i + 1]);
6060
}
6161
}
6262

6363
void test_pause(void) {
64-
Animation animation(FPS, 5);
64+
Animation animation(FPS, FRAMES);
6565
Servo servo(2, positionsA, move);
6666
animation.addServo(servo);
6767
animation.play();
@@ -70,29 +70,29 @@ void test_pause(void) {
7070
int exp[9] = {340, 330, 340, 330, 350, 340, 330, 340, 330};
7171

7272
for (int i = 0; i < 4; i++) {
73-
animation.run(FRAME_MILLIS * (i + 1));
73+
animation.run(FRAME_MICROS * (long)(i + 1));
7474
TEST_ASSERT_EQUAL(exp[i], lastPositions[2].positions[i]);
7575
}
7676

7777
animation.pause();
7878
TEST_ASSERT_EQUAL(Animation::MODE_PAUSE, animation.getMode());
7979

8080
for (int i = 4; i < 8; i++) {
81-
animation.run(FRAME_MILLIS * (i + 1));
81+
animation.run(FRAME_MICROS * (long)(i + 1));
8282
TEST_ASSERT_EQUAL(0, lastPositions[2].positions[i]);
8383
}
8484

8585
animation.play();
8686
TEST_ASSERT_EQUAL(Animation::MODE_PLAY, animation.getMode());
8787

8888
for (int i = 8; i < 14; i++) {
89-
animation.run(FRAME_MILLIS * (i + 1));
89+
animation.run(FRAME_MICROS * (long)(i + 1));
9090
TEST_ASSERT_EQUAL(exp[i - 5], lastPositions[2].positions[i - 5]);
9191
}
9292
}
9393

9494
void test_stop(void) {
95-
Animation animation(FPS, 5);
95+
Animation animation(FPS, FRAMES);
9696
Servo servos[] = {
9797
Servo(0, positionsA, move),
9898
Servo(1, positionsB, move),
@@ -102,22 +102,22 @@ void test_stop(void) {
102102
TEST_ASSERT_EQUAL(Animation::MODE_DEFAULT, animation.getMode());
103103
animation.play();
104104
TEST_ASSERT_EQUAL(Animation::MODE_PLAY, animation.getMode());
105-
animation.run(FRAME_MILLIS);
105+
animation.run(FRAME_MICROS);
106106
TEST_ASSERT_EQUAL(340, lastPositions[0].positions[0]);
107107
TEST_ASSERT_EQUAL(240, lastPositions[1].positions[0]);
108108
TEST_ASSERT_EQUAL(0, lastPositions[2].positions[0]);
109109
TEST_ASSERT_EQUAL(1, animation.getFrame());
110110
animation.stop(0);
111111

112112
for (int i = 0; i < 10; i++) {
113-
animation.run(FRAME_MILLIS * (i + 2));
113+
animation.run(FRAME_MICROS * (long)(i + 2));
114114
TEST_ASSERT_EQUAL(341 + i, lastPositions[0].positions[i + 1]);
115115
TEST_ASSERT_EQUAL(241 + i, lastPositions[1].positions[i + 1]);
116116
TEST_ASSERT_EQUAL(0, lastPositions[2].positions[i + 1]);
117117
TEST_ASSERT_EQUAL(Animation::MODE_STOP, animation.getMode());
118118
}
119119

120-
animation.run(FRAME_MILLIS * 12);
120+
animation.run(FRAME_MICROS * (long)12);
121121
TEST_ASSERT_EQUAL(Animation::MODE_DEFAULT, animation.getMode());
122122
TEST_ASSERT_EQUAL(0, animation.getFrame());
123123
}

0 commit comments

Comments
 (0)