Skip to content

Commit c3df1ff

Browse files
committed
directly allow animation live stream write
1 parent 95771ed commit c3df1ff

23 files changed

+67
-53
lines changed

examples/AdafruitPCA9685/AdafruitPCA9685.ino

+8-5
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010
#include <Adafruit_PWMServoDriver.h>
1111
#include <BlenderServoAnimation.h>
1212

13-
// Using the namespace to have short class references (Animation and Servo)
14-
using namespace BlenderServoAnimation;
15-
1613
// PWM driver instance to set PWM output
1714
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
1815

@@ -22,10 +19,16 @@ void move(byte servoID, int position) {
2219
pwm.setPWM(servoID, 0, position);
2320
}
2421

25-
// Animation object to represent the original Blender animation
26-
Animation animation;
22+
// Animation object to control the animation
23+
BlenderServoAnimation::Animation animation;
24+
25+
#define POWER_PIN 19
2726

2827
void setup() {
28+
// Set power pin
29+
pinMode(POWER_PIN, OUTPUT);
30+
digitalWrite(POWER_PIN, HIGH);
31+
2932
// Set the position callback
3033
animation.onPositionChange(move);
3134

examples/MultiplePCA9685/MultiplePCA9685.ino

+2-5
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,12 @@
1010
#include <Adafruit_PWMServoDriver.h>
1111
#include <BlenderServoAnimation.h>
1212

13-
// Using the namespace to have short class references (Animation and Servo)
14-
using namespace BlenderServoAnimation;
15-
1613
// PWM driver instances to set PWM output
1714
Adafruit_PWMServoDriver pwmA(0x40);
1815
Adafruit_PWMServoDriver pwmB(0x41);
1916

20-
// Animation object to represent the original Blender animation
21-
Animation animation;
17+
// Animation object to control the animation
18+
BlenderServoAnimation::Animation animation;
2219

2320
// We use a struct to map a servo to a PCA9685 board and channel
2421
struct servoMapping {

examples/MultipleScenes/MultipleScenes.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ void move(byte servoID, int position) {
2727
myServo.writeMicroseconds(position);
2828
}
2929

30-
// Animation object to represent the original Blender animation
30+
// Animation object to control the animation
3131
BlenderServoAnimation::Animation animation;
3232

3333
void setup() {

examples/MultipleScenesSD/MultipleScenesSD.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ void changeSceneFile(byte prevSceneIndex, byte nextSceneIndex) {
6060
}
6161
}
6262

63-
// Animation object to represent the original Blender animation
63+
// Animation object to control the animation
6464
BlenderServoAnimation::Animation animation;
6565

6666
void setup() {

examples/MultipleScenesSD/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ Setting up a show consisting of 2 animations.
44

55
By default, the 2 animations will be played synchronously in a loop.
66

7-
![Arduino Nano with servo](../../images/arduino-nano-with-servo.png)
7+
![Arduino Nano with servo](../../images/arduino-nano-with-sd-module.png)

examples/SDAnimation/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ Using the standard Arduino servo library to send servo positions.
44

55
The setup requires nothing but a micro controller and a single servo.
66

7-
![Arduino Nano with servo](../../images/arduino-nano-with-servo.png)
7+
![Arduino Nano with servo](../../images/arduino-nano-with-sd-module.png)

examples/SDAnimation/SDAnimation.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ void resetFile(byte prevSceneIndex, byte nextSceneIndex) {
3636
animationFile.seek(0);
3737
}
3838

39-
// Animation object to represent the original Blender animation
39+
// Animation object to control the animation
4040
BlenderServoAnimation::Animation animation;
4141

4242
void setup() {

examples/SerialLiveMode/SerialLiveMode.ino

+5-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#include <Servo.h>
1515
#endif
1616

17+
#define SERVO_PIN 12
18+
1719
// Servo object to send positions
1820
Servo myServo;
1921

@@ -23,15 +25,15 @@ void move(byte servoID, int position) {
2325
myServo.writeMicroseconds(position);
2426
}
2527

26-
// Animation object to represent the original Blender animation
28+
// Animation object to control the animation
2729
BlenderServoAnimation::Animation animation;
2830

2931
void setup() {
3032
// Initialize serial communication
3133
Serial.begin(115200);
3234

33-
// Attach the servo to pin 12
34-
myServo.attach(12);
35+
// Attach the servo to the defined servo pin
36+
myServo.attach(SERVO_PIN);
3537

3638
// Set the position callback
3739
animation.onPositionChange(move);

examples/StandardServoLib/StandardServoLib.ino

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
/*
22
Using the standard Arduino servo library to send servo positions.
3-
4-
Note the namespace BlenderServoAnimation which helps to distinguish
5-
between the standard library servo class (Servo) and the servo
6-
class of this library (BlenderServoAnimation::Servo).
3+
The animation is played in a loop.
74
*/
85

96
#include "simple.h"
@@ -15,6 +12,8 @@
1512
#include <Servo.h>
1613
#endif
1714

15+
#define SERVO_PIN 12
16+
1817
// Servo object to send positions
1918
Servo myServo;
2019

@@ -24,12 +23,12 @@ void move(byte servoID, int position) {
2423
myServo.writeMicroseconds(position);
2524
}
2625

27-
// Animation object to represent the original Blender animation
26+
// Animation object to control the animation
2827
BlenderServoAnimation::Animation animation;
2928

3029
void setup() {
31-
// Attach the servo to pin 12
32-
myServo.attach(12);
30+
// Attach the servo to the defined servo pin
31+
myServo.attach(SERVO_PIN);
3332

3433
// Set the position callback
3534
animation.onPositionChange(move);

examples/SwitchModeButton/SwitchModeButton.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ void modeChanged(byte prevMode, byte newMode) {
6262
}
6363
}
6464

65-
// Animation object to represent the original Blender animation
65+
// Animation object to control the animation
6666
BlenderServoAnimation::Animation animation;
6767

6868
// Callback to be triggered on a short button press

examples/WebSocketLiveMode/WebSocketLiveMode.ino

+10-10
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#include <ESP32Servo.h>
1414
#include <WiFi.h>
1515

16+
#define SERVO_PIN 12
17+
1618
// Change to your SSID and password
1719
const char *ssid = "SSID";
1820
const char *password = "PASSWORD";
@@ -30,27 +32,25 @@ void move(byte servoID, int position) {
3032
myServo.writeMicroseconds(position);
3133
}
3234

33-
// Animation object to represent the original Blender animation
35+
// Animation object to control the animation
3436
BlenderServoAnimation::Animation animation;
3537

36-
// AnimationData instance acting as a middleware between web socket and
37-
// animation instance
38-
BlenderServoAnimation::AnimationData liveStream;
39-
40-
// Handler function writing data to the live stream instance when receiving data
38+
// Callback function writing live stream data to the animation
4139
void onWebSocketEvent(AsyncWebSocket *server, AsyncWebSocketClient *client,
4240
AwsEventType type, void *arg, uint8_t *data, size_t len) {
4341
if (type != WS_EVT_DATA) {
4442
return;
4543
}
4644

4745
for (size_t i = 0; i < len; i++) {
48-
liveStream.writeByte(data[i]);
46+
animation.writeLiveStream(data[i]);
4947
}
5048
}
5149

5250
void setup() {
5351
Serial.begin(9600);
52+
while (!Serial) {
53+
};
5454

5555
WiFi.begin(ssid, password);
5656
while (WiFi.status() != WL_CONNECTED) {
@@ -65,14 +65,14 @@ void setup() {
6565
server.addHandler(&ws);
6666
server.begin();
6767

68-
// Attach the servo to pin 12
69-
myServo.attach(12);
68+
// Attach the servo to the defined servo pin
69+
myServo.attach(SERVO_PIN);
7070

7171
// Set the position callback
7272
animation.onPositionChange(move);
7373

7474
// Trigger the animation live mode
75-
animation.live(liveStream);
75+
animation.live();
7676
}
7777

7878
void loop() {
91.8 KB
Loading
15.8 KB
Loading

images/arduino-nano-with-servo.png

13.8 KB
Loading

images/esp32-with-servo.png

14.3 KB
Loading

src/BlenderServoAnimation.h

-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
#include "internal/Animation.h"
2-
#include "internal/AnimationData.h"
32
#include <Arduino.h>

src/internal/Animation.cpp

+22-9
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
#include "AnimationData.h"
33
#include <Arduino.h>
44

5-
using namespace BlenderServoAnimation;
5+
using BlenderServoAnimation::Animation;
6+
using BlenderServoAnimation::Scene;
67

78
Animation::~Animation() {
89
if (this->scenes) {
910
delete[] this->scenes;
1011
}
1112

12-
if (this->liveStream != nullptr && this->isOneTimeLiveStream) {
13+
if (this->liveStream != nullptr) {
1314
delete this->liveStream;
1415
}
1516

@@ -189,25 +190,38 @@ void Animation::stop() {
189190
this->changeMode(MODE_STOP);
190191
}
191192

192-
void Animation::live(Stream &stream) {
193+
void Animation::live() {
193194
if (this->mode != MODE_DEFAULT) {
194195
return;
195196
}
196197

197-
this->liveStream = new AnimationData(&stream);
198-
this->isOneTimeLiveStream = true;
198+
if (this->liveStream != nullptr) {
199+
delete this->liveStream;
200+
}
201+
202+
this->liveStream = new AnimationData();
199203
this->changeMode(MODE_LIVE);
200204
}
201205

202-
void Animation::live(AnimationData &data) {
206+
void Animation::live(Stream &stream) {
203207
if (this->mode != MODE_DEFAULT) {
204208
return;
205209
}
206210

207-
this->liveStream = &data;
211+
if (this->liveStream != nullptr) {
212+
delete this->liveStream;
213+
}
214+
215+
this->liveStream = new AnimationData(&stream);
208216
this->changeMode(MODE_LIVE);
209217
}
210218

219+
void Animation::writeLiveStream(byte value) {
220+
if (this->liveStream != nullptr) {
221+
this->liveStream->writeByte(value);
222+
}
223+
}
224+
211225
byte Animation::getMode() {
212226
return this->mode;
213227
}
@@ -286,7 +300,6 @@ void Animation::handlePlayMode(unsigned long currentMicros) {
286300

287301
if (!this->scene) {
288302
this->changeMode(MODE_DEFAULT);
289-
return;
290303
}
291304
}
292305

@@ -327,7 +340,7 @@ void Animation::changeMode(byte mode) {
327340
byte prevMode = this->mode;
328341
this->mode = mode;
329342

330-
if (prevMode == MODE_LIVE && this->isOneTimeLiveStream) {
343+
if (prevMode == MODE_LIVE) {
331344
delete this->liveStream;
332345
this->liveStream = nullptr;
333346
}

src/internal/Animation.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ class Animation {
3939
void loop();
4040
void pause();
4141
void stop();
42+
void live();
4243
void live(Stream &stream);
43-
void live(AnimationData &data);
44+
void writeLiveStream(byte value);
4445
void setDefaultServoThreshold(byte value);
4546
void setServoThreshold(byte id, byte value);
4647
void setServoOffset(byte id, int offset);
@@ -60,7 +61,6 @@ class Animation {
6061

6162
AnimationData *liveStream = nullptr;
6263

63-
bool isOneTimeLiveStream = false;
6464
bool *playedIndexes = nullptr;
6565

6666
mcb modeCallback = nullptr;

src/internal/AnimationData.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "AnimationData.h"
22
#include <Arduino.h>
33

4-
using namespace BlenderServoAnimation;
4+
using BlenderServoAnimation::AnimationData;
55

66
AnimationData::AnimationData() {
77
}

src/internal/Command.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "Command.h"
22
#include <Arduino.h>
33

4-
using namespace BlenderServoAnimation;
4+
using BlenderServoAnimation::Command;
55

66
void Command::write(byte value) {
77
if (value == START_MARKER) {

src/internal/Scene.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include "Servo.h"
44
#include <Arduino.h>
55

6-
using namespace BlenderServoAnimation;
6+
using BlenderServoAnimation::Scene;
77

88
Scene::Scene(ServoManager *servoManager, AnimationData *data, byte fps,
99
int frames) {

src/internal/Servo.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include "typedefs.h"
33
#include <Arduino.h>
44

5-
using namespace BlenderServoAnimation;
5+
using BlenderServoAnimation::Servo;
66

77
Servo::Servo(byte id, pcb positionCallback, byte threshold) {
88
this->id = id;

src/internal/ServoManager.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
#include "Servo.h"
33
#include <Arduino.h>
44

5-
using namespace BlenderServoAnimation;
5+
using BlenderServoAnimation::ServoManager;
6+
using BlenderServoAnimation::Servo;
67

78
ServoManager::~ServoManager() {
89
if (this->servos) {

0 commit comments

Comments
 (0)