diff --git a/examples/AdafruitPCA9685/AdafruitPCA9685.ino b/examples/AdafruitPCA9685/AdafruitPCA9685.ino index f754b8a..5a0b2e7 100644 --- a/examples/AdafruitPCA9685/AdafruitPCA9685.ino +++ b/examples/AdafruitPCA9685/AdafruitPCA9685.ino @@ -10,9 +10,6 @@ #include #include -// Using the namespace to have short class references (Animation and Servo) -using namespace BlenderServoAnimation; - // PWM driver instance to set PWM output Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(); @@ -22,10 +19,16 @@ void move(byte servoID, int position) { pwm.setPWM(servoID, 0, position); } -// Animation object to represent the original Blender animation -Animation animation; +// Animation object to control the animation +BlenderServoAnimation::Animation animation; + +#define POWER_PIN 19 void setup() { + // Set power pin + pinMode(POWER_PIN, OUTPUT); + digitalWrite(POWER_PIN, HIGH); + // Set the position callback animation.onPositionChange(move); diff --git a/examples/MultiplePCA9685/MultiplePCA9685.ino b/examples/MultiplePCA9685/MultiplePCA9685.ino index 0c5c209..e512b9c 100644 --- a/examples/MultiplePCA9685/MultiplePCA9685.ino +++ b/examples/MultiplePCA9685/MultiplePCA9685.ino @@ -10,15 +10,12 @@ #include #include -// Using the namespace to have short class references (Animation and Servo) -using namespace BlenderServoAnimation; - // PWM driver instances to set PWM output Adafruit_PWMServoDriver pwmA(0x40); Adafruit_PWMServoDriver pwmB(0x41); -// Animation object to represent the original Blender animation -Animation animation; +// Animation object to control the animation +BlenderServoAnimation::Animation animation; // We use a struct to map a servo to a PCA9685 board and channel struct servoMapping { diff --git a/examples/MultipleScenes/MultipleScenes.ino b/examples/MultipleScenes/MultipleScenes.ino index 87a93c7..1d6d608 100644 --- a/examples/MultipleScenes/MultipleScenes.ino +++ b/examples/MultipleScenes/MultipleScenes.ino @@ -27,7 +27,7 @@ void move(byte servoID, int position) { myServo.writeMicroseconds(position); } -// Animation object to represent the original Blender animation +// Animation object to control the animation BlenderServoAnimation::Animation animation; void setup() { diff --git a/examples/MultipleScenesSD/MultipleScenesSD.ino b/examples/MultipleScenesSD/MultipleScenesSD.ino index 4ca93b7..01f2bde 100644 --- a/examples/MultipleScenesSD/MultipleScenesSD.ino +++ b/examples/MultipleScenesSD/MultipleScenesSD.ino @@ -60,7 +60,7 @@ void changeSceneFile(byte prevSceneIndex, byte nextSceneIndex) { } } -// Animation object to represent the original Blender animation +// Animation object to control the animation BlenderServoAnimation::Animation animation; void setup() { diff --git a/examples/MultipleScenesSD/README.md b/examples/MultipleScenesSD/README.md index 2619f9f..278bfa3 100644 --- a/examples/MultipleScenesSD/README.md +++ b/examples/MultipleScenesSD/README.md @@ -4,4 +4,4 @@ Setting up a show consisting of 2 animations. By default, the 2 animations will be played synchronously in a loop. -![Arduino Nano with servo](../../images/arduino-nano-with-servo.png) +![Arduino Nano with servo](../../images/arduino-nano-with-sd-module.png) diff --git a/examples/SDAnimation/README.md b/examples/SDAnimation/README.md index bb47cc7..9f91039 100644 --- a/examples/SDAnimation/README.md +++ b/examples/SDAnimation/README.md @@ -4,4 +4,4 @@ Using the standard Arduino servo library to send servo positions. The setup requires nothing but a micro controller and a single servo. -![Arduino Nano with servo](../../images/arduino-nano-with-servo.png) +![Arduino Nano with servo](../../images/arduino-nano-with-sd-module.png) diff --git a/examples/SDAnimation/SDAnimation.ino b/examples/SDAnimation/SDAnimation.ino index c550cc4..4f4eafb 100644 --- a/examples/SDAnimation/SDAnimation.ino +++ b/examples/SDAnimation/SDAnimation.ino @@ -36,7 +36,7 @@ void resetFile(byte prevSceneIndex, byte nextSceneIndex) { animationFile.seek(0); } -// Animation object to represent the original Blender animation +// Animation object to control the animation BlenderServoAnimation::Animation animation; void setup() { diff --git a/examples/SerialLiveMode/SerialLiveMode.ino b/examples/SerialLiveMode/SerialLiveMode.ino index e128845..8d04bf1 100644 --- a/examples/SerialLiveMode/SerialLiveMode.ino +++ b/examples/SerialLiveMode/SerialLiveMode.ino @@ -14,6 +14,8 @@ #include #endif +#define SERVO_PIN 12 + // Servo object to send positions Servo myServo; @@ -23,15 +25,15 @@ void move(byte servoID, int position) { myServo.writeMicroseconds(position); } -// Animation object to represent the original Blender animation +// Animation object to control the animation BlenderServoAnimation::Animation animation; void setup() { // Initialize serial communication Serial.begin(115200); - // Attach the servo to pin 12 - myServo.attach(12); + // Attach the servo to the defined servo pin + myServo.attach(SERVO_PIN); // Set the position callback animation.onPositionChange(move); diff --git a/examples/StandardServoLib/StandardServoLib.ino b/examples/StandardServoLib/StandardServoLib.ino index 70b6b82..a439788 100644 --- a/examples/StandardServoLib/StandardServoLib.ino +++ b/examples/StandardServoLib/StandardServoLib.ino @@ -1,9 +1,6 @@ /* Using the standard Arduino servo library to send servo positions. - - Note the namespace BlenderServoAnimation which helps to distinguish - between the standard library servo class (Servo) and the servo - class of this library (BlenderServoAnimation::Servo). + The animation is played in a loop. */ #include "simple.h" @@ -15,6 +12,8 @@ #include #endif +#define SERVO_PIN 12 + // Servo object to send positions Servo myServo; @@ -24,12 +23,12 @@ void move(byte servoID, int position) { myServo.writeMicroseconds(position); } -// Animation object to represent the original Blender animation +// Animation object to control the animation BlenderServoAnimation::Animation animation; void setup() { - // Attach the servo to pin 12 - myServo.attach(12); + // Attach the servo to the defined servo pin + myServo.attach(SERVO_PIN); // Set the position callback animation.onPositionChange(move); diff --git a/examples/SwitchModeButton/SwitchModeButton.ino b/examples/SwitchModeButton/SwitchModeButton.ino index 96b7548..751d394 100644 --- a/examples/SwitchModeButton/SwitchModeButton.ino +++ b/examples/SwitchModeButton/SwitchModeButton.ino @@ -62,7 +62,7 @@ void modeChanged(byte prevMode, byte newMode) { } } -// Animation object to represent the original Blender animation +// Animation object to control the animation BlenderServoAnimation::Animation animation; // Callback to be triggered on a short button press diff --git a/examples/WebSocketLiveMode/WebSocketLiveMode.ino b/examples/WebSocketLiveMode/WebSocketLiveMode.ino index 0fc7240..3741f33 100644 --- a/examples/WebSocketLiveMode/WebSocketLiveMode.ino +++ b/examples/WebSocketLiveMode/WebSocketLiveMode.ino @@ -13,6 +13,8 @@ #include #include +#define SERVO_PIN 12 + // Change to your SSID and password const char *ssid = "SSID"; const char *password = "PASSWORD"; @@ -30,14 +32,10 @@ void move(byte servoID, int position) { myServo.writeMicroseconds(position); } -// Animation object to represent the original Blender animation +// Animation object to control the animation BlenderServoAnimation::Animation animation; -// AnimationData instance acting as a middleware between web socket and -// animation instance -BlenderServoAnimation::AnimationData liveStream; - -// Handler function writing data to the live stream instance when receiving data +// Callback function writing live stream data to the animation void onWebSocketEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len) { if (type != WS_EVT_DATA) { @@ -45,12 +43,14 @@ void onWebSocketEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, } for (size_t i = 0; i < len; i++) { - liveStream.writeByte(data[i]); + animation.writeLiveStream(data[i]); } } void setup() { Serial.begin(9600); + while (!Serial) { + }; WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { @@ -65,14 +65,14 @@ void setup() { server.addHandler(&ws); server.begin(); - // Attach the servo to pin 12 - myServo.attach(12); + // Attach the servo to the defined servo pin + myServo.attach(SERVO_PIN); // Set the position callback animation.onPositionChange(move); // Trigger the animation live mode - animation.live(liveStream); + animation.live(); } void loop() { diff --git a/images/arduino-nano-with-sd-module.png b/images/arduino-nano-with-sd-module.png new file mode 100644 index 0000000..aff5b2e Binary files /dev/null and b/images/arduino-nano-with-sd-module.png differ diff --git a/images/arduino-nano-with-servo-and-button.png b/images/arduino-nano-with-servo-and-button.png index 61eafcf..d755ada 100644 Binary files a/images/arduino-nano-with-servo-and-button.png and b/images/arduino-nano-with-servo-and-button.png differ diff --git a/images/arduino-nano-with-servo.png b/images/arduino-nano-with-servo.png index 952ae04..5c2190a 100644 Binary files a/images/arduino-nano-with-servo.png and b/images/arduino-nano-with-servo.png differ diff --git a/images/esp32-with-servo.png b/images/esp32-with-servo.png index 4013fd5..5411c1c 100644 Binary files a/images/esp32-with-servo.png and b/images/esp32-with-servo.png differ diff --git a/src/BlenderServoAnimation.h b/src/BlenderServoAnimation.h index 3f0f66f..222198f 100644 --- a/src/BlenderServoAnimation.h +++ b/src/BlenderServoAnimation.h @@ -1,3 +1,2 @@ #include "internal/Animation.h" -#include "internal/AnimationData.h" #include diff --git a/src/internal/Animation.cpp b/src/internal/Animation.cpp index a3f2184..0b8bd3a 100644 --- a/src/internal/Animation.cpp +++ b/src/internal/Animation.cpp @@ -2,14 +2,15 @@ #include "AnimationData.h" #include -using namespace BlenderServoAnimation; +using BlenderServoAnimation::Animation; +using BlenderServoAnimation::Scene; Animation::~Animation() { if (this->scenes) { delete[] this->scenes; } - if (this->liveStream != nullptr && this->isOneTimeLiveStream) { + if (this->liveStream != nullptr) { delete this->liveStream; } @@ -189,25 +190,38 @@ void Animation::stop() { this->changeMode(MODE_STOP); } -void Animation::live(Stream &stream) { +void Animation::live() { if (this->mode != MODE_DEFAULT) { return; } - this->liveStream = new AnimationData(&stream); - this->isOneTimeLiveStream = true; + if (this->liveStream != nullptr) { + delete this->liveStream; + } + + this->liveStream = new AnimationData(); this->changeMode(MODE_LIVE); } -void Animation::live(AnimationData &data) { +void Animation::live(Stream &stream) { if (this->mode != MODE_DEFAULT) { return; } - this->liveStream = &data; + if (this->liveStream != nullptr) { + delete this->liveStream; + } + + this->liveStream = new AnimationData(&stream); this->changeMode(MODE_LIVE); } +void Animation::writeLiveStream(byte value) { + if (this->liveStream != nullptr) { + this->liveStream->writeByte(value); + } +} + byte Animation::getMode() { return this->mode; } @@ -286,7 +300,6 @@ void Animation::handlePlayMode(unsigned long currentMicros) { if (!this->scene) { this->changeMode(MODE_DEFAULT); - return; } } @@ -327,7 +340,7 @@ void Animation::changeMode(byte mode) { byte prevMode = this->mode; this->mode = mode; - if (prevMode == MODE_LIVE && this->isOneTimeLiveStream) { + if (prevMode == MODE_LIVE) { delete this->liveStream; this->liveStream = nullptr; } diff --git a/src/internal/Animation.h b/src/internal/Animation.h index 42da630..908eaa4 100644 --- a/src/internal/Animation.h +++ b/src/internal/Animation.h @@ -39,8 +39,9 @@ class Animation { void loop(); void pause(); void stop(); + void live(); void live(Stream &stream); - void live(AnimationData &data); + void writeLiveStream(byte value); void setDefaultServoThreshold(byte value); void setServoThreshold(byte id, byte value); void setServoOffset(byte id, int offset); @@ -60,7 +61,6 @@ class Animation { AnimationData *liveStream = nullptr; - bool isOneTimeLiveStream = false; bool *playedIndexes = nullptr; mcb modeCallback = nullptr; diff --git a/src/internal/AnimationData.cpp b/src/internal/AnimationData.cpp index 0836dc1..9805f6d 100644 --- a/src/internal/AnimationData.cpp +++ b/src/internal/AnimationData.cpp @@ -1,7 +1,7 @@ #include "AnimationData.h" #include -using namespace BlenderServoAnimation; +using BlenderServoAnimation::AnimationData; AnimationData::AnimationData() { } diff --git a/src/internal/Command.cpp b/src/internal/Command.cpp index e0b3d38..b2c84e6 100644 --- a/src/internal/Command.cpp +++ b/src/internal/Command.cpp @@ -1,7 +1,7 @@ #include "Command.h" #include -using namespace BlenderServoAnimation; +using BlenderServoAnimation::Command; void Command::write(byte value) { if (value == START_MARKER) { diff --git a/src/internal/Scene.cpp b/src/internal/Scene.cpp index 09f54b4..c41a3e1 100644 --- a/src/internal/Scene.cpp +++ b/src/internal/Scene.cpp @@ -3,7 +3,7 @@ #include "Servo.h" #include -using namespace BlenderServoAnimation; +using BlenderServoAnimation::Scene; Scene::Scene(ServoManager *servoManager, AnimationData *data, byte fps, int frames) { diff --git a/src/internal/Servo.cpp b/src/internal/Servo.cpp index dabddeb..f2627ff 100644 --- a/src/internal/Servo.cpp +++ b/src/internal/Servo.cpp @@ -2,7 +2,7 @@ #include "typedefs.h" #include -using namespace BlenderServoAnimation; +using BlenderServoAnimation::Servo; Servo::Servo(byte id, pcb positionCallback, byte threshold) { this->id = id; diff --git a/src/internal/ServoManager.cpp b/src/internal/ServoManager.cpp index b414466..3f63b27 100644 --- a/src/internal/ServoManager.cpp +++ b/src/internal/ServoManager.cpp @@ -2,7 +2,8 @@ #include "Servo.h" #include -using namespace BlenderServoAnimation; +using BlenderServoAnimation::ServoManager; +using BlenderServoAnimation::Servo; ServoManager::~ServoManager() { if (this->servos) {