Skip to content

Commit 2cb9f9f

Browse files
committed
add show example
1 parent 17f816f commit 2cb9f9f

File tree

6 files changed

+137
-1
lines changed

6 files changed

+137
-1
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ jobs:
2929
AdafruitPCA9685,
3030
LiveMode,
3131
MultiplePCA9685,
32+
Show,
3233
StandardServoLib,
3334
SwitchModeButton,
3435
]

examples/Show/Show.ino

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
Setting up a show consisting of 2 animations.
3+
4+
Note the namespaces which are used to distinguish the positions
5+
of one scene / animation from another. It's even possible to
6+
have different playback rates (fps) and frames per animation.
7+
*/
8+
9+
#include "scene-a.h"
10+
#include "scene-b.h"
11+
#include <BlenderServoAnimation.h>
12+
#include <Servo.h>
13+
14+
// Servo object to send positions
15+
Servo myServo;
16+
17+
// Callback function which is called whenever a servo needs to be moved
18+
void move(byte servoID, int angle) {
19+
// Ignore the servoID (there is only one servo) and write the current angle
20+
myServo.write(angle);
21+
}
22+
23+
// Show object to manage all Blender animations
24+
BlenderServoAnimation::Show show;
25+
26+
// Animation objects to represent the original Blender animations
27+
BlenderServoAnimation::Animation animationA(0, SceneA::FPS, SceneA::FRAMES);
28+
BlenderServoAnimation::Animation animationB(1, SceneB::FPS, SceneB::FRAMES);
29+
30+
// Servo objects to manage the positions
31+
BlenderServoAnimation::Servo myBlenderServoA(0, SceneA::Bone, move);
32+
BlenderServoAnimation::Servo myBlenderServoB(0, SceneB::Bone, move);
33+
34+
void setup() {
35+
// Attach the servo to pin 9
36+
myServo.attach(9);
37+
38+
// Add the Blender servo objects to the animations
39+
animationA.addServo(myBlenderServoA);
40+
animationB.addServo(myBlenderServoB);
41+
42+
// Add the Blender animation objects to the show
43+
show.addAnimation(animationA);
44+
show.addAnimation(animationB);
45+
46+
// Trigger the show loop mode
47+
show.loop();
48+
49+
// There are also other playback options
50+
// show.play(); // Plays all animations once in the order they have been added
51+
// show.playRandom(); // Randomly plays animations in a loop
52+
// show.playSingle(1); // Play the animation with the given ID once
53+
54+
// Further controls are similar to what we can do on animation objects
55+
// show.pause();
56+
// show.stop();
57+
// show.live(Serial);
58+
}
59+
60+
void loop() {
61+
// Update the show state on each loop
62+
show.run();
63+
}

examples/Show/platformio.ini

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[env]
2+
lib_deps =
3+
arduino-libraries/Servo@^1.1.8
4+
5+
[env:uno]
6+
board = uno
7+
platform = atmelavr
8+
framework = arduino
9+
10+
[env:ATmega2560]
11+
board = ATmega2560
12+
platform = atmelavr
13+
framework = arduino
14+
15+
[env:nanoatmega328]
16+
board = nanoatmega328
17+
platform = atmelavr
18+
framework = arduino

examples/Show/scene-a.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
Blender Servo Animation Positions
3+
4+
FPS: 30
5+
Frames: 100
6+
Seconds: 3
7+
Bones: 1
8+
Armature: Armature
9+
Scene: SceneA
10+
File: scenes.blend
11+
*/
12+
13+
#include <Arduino.h>
14+
15+
namespace SceneA {
16+
17+
const byte FPS = 30;
18+
const int FRAMES = 100;
19+
20+
// Servo ID: 0
21+
const int Bone[FRAMES] PROGMEM = {
22+
90, 90, 89, 89, 88, 87, 86, 84, 83, 81, 80, 78, 76, 74, 72, 70, 68, 65, 63, 61, 59, 57, 55, 54, 52, 51, 49, 48, 47, 46, 46, 45, 45, 45, 46, 47, 49, 51, 53, 55, 58, 61, 65, 68, 72, 76, 80, 84, 88, 92,
23+
96, 100, 104, 108, 112, 115, 119, 122, 125, 127, 129, 131, 133, 134, 135, 135, 135, 135, 134, 133, 132, 131, 130, 129, 127, 126, 124, 122, 120, 118, 116, 114, 112, 111, 109, 107, 105, 103, 101, 99, 98, 96, 95, 94, 93, 92, 91, 90, 90, 90,
24+
};
25+
26+
} // namespace SceneA

examples/Show/scene-b.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
Blender Servo Animation Positions
3+
4+
FPS: 60
5+
Frames: 200
6+
Seconds: 3
7+
Bones: 1
8+
Armature: Armature.001
9+
Scene: SceneB
10+
File: scenes.blend
11+
*/
12+
13+
#include <Arduino.h>
14+
15+
namespace SceneB {
16+
17+
const byte FPS = 60;
18+
const int FRAMES = 200;
19+
20+
// Servo ID: 0
21+
const int Bone[FRAMES] PROGMEM = {
22+
90, 90, 90, 90, 90, 90, 90, 89, 89, 89, 89, 88, 88, 88, 88, 87, 87, 86, 86, 86, 85, 85, 84, 84, 83, 83, 82, 82, 81, 81, 80, 80, 79, 78, 78, 77, 76, 76, 75, 75, 74, 73, 73, 72, 71, 71, 70, 69, 69, 68,
23+
67, 66, 66, 65, 64, 64, 63, 62, 62, 61, 60, 60, 59, 59, 58, 57, 57, 56, 55, 55, 54, 54, 53, 53, 52, 52, 51, 51, 50, 50, 49, 49, 49, 48, 48, 47, 47, 47, 47, 46, 46, 46, 46, 45, 45, 45, 45, 45, 45, 45,
24+
45, 45, 45, 45, 45, 45, 46, 46, 46, 46, 47, 47, 47, 47, 48, 48, 48, 49, 49, 50, 50, 51, 51, 52, 52, 53, 53, 54, 54, 55, 55, 56, 56, 57, 58, 58, 59, 60, 60, 61, 61, 62, 63, 63, 64, 65, 65, 66, 67, 68,
25+
68, 69, 70, 70, 71, 72, 72, 73, 74, 74, 75, 75, 76, 77, 77, 78, 79, 79, 80, 80, 81, 81, 82, 82, 83, 83, 84, 84, 85, 85, 86, 86, 87, 87, 87, 88, 88, 88, 88, 89, 89, 89, 89, 90, 90, 90, 90, 90, 90, 90,
26+
};
27+
28+
} // namespace SceneB

src/show/Show.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ void Show::setRandomAnimation() {
4343
byte randomIndex = 0;
4444

4545
if (this->countAnimations() > 1) {
46-
randomIndex = random(this->addIndex - 1);
46+
randomIndex = random(this->addIndex);
4747
}
4848

4949
this->playIndex = randomIndex;

0 commit comments

Comments
 (0)