Skip to content

Commit

Permalink
directly allow animation live stream write
Browse files Browse the repository at this point in the history
  • Loading branch information
timhendriks93 committed Jul 27, 2024
1 parent 95771ed commit c3df1ff
Show file tree
Hide file tree
Showing 23 changed files with 67 additions and 53 deletions.
13 changes: 8 additions & 5 deletions examples/AdafruitPCA9685/AdafruitPCA9685.ino
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
#include <Adafruit_PWMServoDriver.h>
#include <BlenderServoAnimation.h>

// 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();

Expand All @@ -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);

Expand Down
7 changes: 2 additions & 5 deletions examples/MultiplePCA9685/MultiplePCA9685.ino
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,12 @@
#include <Adafruit_PWMServoDriver.h>
#include <BlenderServoAnimation.h>

// 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 {
Expand Down
2 changes: 1 addition & 1 deletion examples/MultipleScenes/MultipleScenes.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion examples/MultipleScenesSD/MultipleScenesSD.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion examples/MultipleScenesSD/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion examples/SDAnimation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion examples/SDAnimation/SDAnimation.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
8 changes: 5 additions & 3 deletions examples/SerialLiveMode/SerialLiveMode.ino
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#include <Servo.h>
#endif

#define SERVO_PIN 12

// Servo object to send positions
Servo myServo;

Expand All @@ -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);
Expand Down
13 changes: 6 additions & 7 deletions examples/StandardServoLib/StandardServoLib.ino
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -15,6 +12,8 @@
#include <Servo.h>
#endif

#define SERVO_PIN 12

// Servo object to send positions
Servo myServo;

Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion examples/SwitchModeButton/SwitchModeButton.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 10 additions & 10 deletions examples/WebSocketLiveMode/WebSocketLiveMode.ino
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include <ESP32Servo.h>
#include <WiFi.h>

#define SERVO_PIN 12

// Change to your SSID and password
const char *ssid = "SSID";
const char *password = "PASSWORD";
Expand All @@ -30,27 +32,25 @@ 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) {
return;
}

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) {
Expand All @@ -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() {
Expand Down
Binary file added images/arduino-nano-with-sd-module.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/arduino-nano-with-servo-and-button.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/arduino-nano-with-servo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/esp32-with-servo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion src/BlenderServoAnimation.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
#include "internal/Animation.h"
#include "internal/AnimationData.h"
#include <Arduino.h>
31 changes: 22 additions & 9 deletions src/internal/Animation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
#include "AnimationData.h"
#include <Arduino.h>

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;
}

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -286,7 +300,6 @@ void Animation::handlePlayMode(unsigned long currentMicros) {

if (!this->scene) {
this->changeMode(MODE_DEFAULT);
return;
}
}

Expand Down Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/internal/Animation.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -60,7 +61,6 @@ class Animation {

AnimationData *liveStream = nullptr;

bool isOneTimeLiveStream = false;
bool *playedIndexes = nullptr;

mcb modeCallback = nullptr;
Expand Down
2 changes: 1 addition & 1 deletion src/internal/AnimationData.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "AnimationData.h"
#include <Arduino.h>

using namespace BlenderServoAnimation;
using BlenderServoAnimation::AnimationData;

AnimationData::AnimationData() {
}
Expand Down
2 changes: 1 addition & 1 deletion src/internal/Command.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "Command.h"
#include <Arduino.h>

using namespace BlenderServoAnimation;
using BlenderServoAnimation::Command;

void Command::write(byte value) {
if (value == START_MARKER) {
Expand Down
2 changes: 1 addition & 1 deletion src/internal/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "Servo.h"
#include <Arduino.h>

using namespace BlenderServoAnimation;
using BlenderServoAnimation::Scene;

Scene::Scene(ServoManager *servoManager, AnimationData *data, byte fps,
int frames) {
Expand Down
2 changes: 1 addition & 1 deletion src/internal/Servo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "typedefs.h"
#include <Arduino.h>

using namespace BlenderServoAnimation;
using BlenderServoAnimation::Servo;

Servo::Servo(byte id, pcb positionCallback, byte threshold) {
this->id = id;
Expand Down
3 changes: 2 additions & 1 deletion src/internal/ServoManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
#include "Servo.h"
#include <Arduino.h>

using namespace BlenderServoAnimation;
using BlenderServoAnimation::ServoManager;
using BlenderServoAnimation::Servo;

ServoManager::~ServoManager() {
if (this->servos) {
Expand Down

0 comments on commit c3df1ff

Please sign in to comment.