Skip to content

Commit

Permalink
merge live and progmem stream into animation data
Browse files Browse the repository at this point in the history
  • Loading branch information
timhendriks93 committed Jan 15, 2024
1 parent d9ca634 commit 83cc85d
Show file tree
Hide file tree
Showing 30 changed files with 321 additions and 354 deletions.
7 changes: 2 additions & 5 deletions examples/SerialLiveMode/SerialLiveMode.ino
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,8 @@ void setup() {
// Set the position callback
animation.onPositionChange(move);

// Add a new scene with the Serial stream - we can omit fps and frames
animation.addScene(Serial);

// Trigger the animation play mode
animation.play();
// Trigger the animation live mode
animation.live(Serial);
}

void loop() {
Expand Down
15 changes: 6 additions & 9 deletions examples/WebSocketLiveMode/WebSocketLiveMode.ino
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ AsyncWebSocket ws("/");
// Servo object to send positions
Servo myServo;

// LiveStream instance acting as a middleware between web socket and animation
// instance
BlenderServoAnimation::LiveStream liveStream;

// Callback function which is called whenever a servo needs to be moved
void move(byte servoID, int position) {
// Ignore the servoID (there is only one servo) and write the current position
Expand All @@ -37,6 +33,10 @@ void move(byte servoID, int position) {
// Animation object to represent the original Blender 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
void onWebSocketEvent(AsyncWebSocket *server, AsyncWebSocketClient *client,
AwsEventType type, void *arg, uint8_t *data, size_t len) {
Expand Down Expand Up @@ -71,11 +71,8 @@ void setup() {
// Set the position callback
animation.onPositionChange(move);

// Add a new scene with the LiveStream - we can omit fps and frames
animation.addScene(liveStream);

// Trigger the animation play mode
animation.play();
// Trigger the animation live mode
animation.live(liveStream);
}

void loop() {
Expand Down
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ platform = native
test_build_src = yes
build_flags =
-Wno-deprecated-declarations
debug_test = test_servo_manager
debug_test = animation/test_pause
lib_deps =
ArduinoFake
3 changes: 1 addition & 2 deletions src/BlenderServoAnimation.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include "internal/Animation.h"
#include "internal/LiveStream.h"
#include "internal/Scene.h"
#include "internal/AnimationData.h"
#include <Arduino.h>
24 changes: 14 additions & 10 deletions src/internal/Animation.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "Animation.h"
#include "ProgmemStream.h"
#include "AnimationData.h"
#include <Arduino.h>

using namespace BlenderServoAnimation;
Expand All @@ -10,6 +10,10 @@ Animation::~Animation() {
delete this->scenes[i];
}
}

if (this->liveStream) {
delete this->liveStream;
}
}

int Animation::countScenes() {
Expand All @@ -29,15 +33,14 @@ bool Animation::hasScene(byte index) {
}

void Animation::addScene(const byte *data, int size, byte fps, int frames) {
ProgmemStream *stream = new ProgmemStream(data, size);
Scene *scene = new Scene(this->servoManager, fps, frames);
scene->setProgmemData(stream);
AnimationData *animationData = new AnimationData(data, size);
Scene *scene = new Scene(&this->servoManager, animationData, fps, frames);
this->registerScene(scene);
}

void Animation::addScene(Stream &data, byte fps, int frames) {
Scene *scene = new Scene(this->servoManager, fps, frames);
scene->setData(&data);
AnimationData *animationData = new AnimationData(&data);
Scene *scene = new Scene(&this->servoManager, animationData, fps, frames);
this->registerScene(scene);
}

Expand Down Expand Up @@ -118,15 +121,16 @@ void Animation::loop() {
}

void Animation::pause() {
if (!this->scene || this->modeIsIn(4, MODE_DEFAULT, MODE_PAUSE, MODE_STOP)) {
if (!this->scene ||
this->modeIsIn(4, MODE_DEFAULT, MODE_PAUSE, MODE_STOP, MODE_LIVE)) {
return;
}

this->changeMode(MODE_PAUSE);
}

void Animation::stop() {
if (!this->scene || this->modeIsIn(2, MODE_DEFAULT, MODE_STOP)) {
if (this->modeIsIn(2, MODE_DEFAULT, MODE_STOP)) {
return;
}

Expand All @@ -138,7 +142,7 @@ void Animation::live(Stream &stream) {
return;
}

this->liveStream = &stream;
this->liveStream = new AnimationData(&stream);
this->changeMode(MODE_LIVE);
}

Expand Down Expand Up @@ -226,7 +230,7 @@ void Animation::handleStopMode(unsigned long currentMicros) {
}

void Animation::handleLiveMode() {
this->servoManager.parseStream(this->liveStream, false);
this->servoManager.parseData(this->liveStream, false);
}

Scene *Animation::getCurrentScene() {
Expand Down
3 changes: 2 additions & 1 deletion src/internal/Animation.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "AnimationData.h"
#include "Scene.h"
#include "ServoManager.h"
#include "typedefs.h"
Expand Down Expand Up @@ -58,7 +59,7 @@ class Animation {
Scene *scenes[MAX_SCENE_COUNT] = {nullptr};
Scene *scene = nullptr;

Stream *liveStream;
AnimationData *liveStream = nullptr;

mcb modeCallback = nullptr;
scb sceneCallback = nullptr;
Expand Down
62 changes: 62 additions & 0 deletions src/internal/AnimationData.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include "AnimationData.h"
#include <Arduino.h>

using namespace BlenderServoAnimation;

AnimationData::AnimationData() {
}

AnimationData::AnimationData(const byte *data, int dataSize) {
this->data = data;
this->dataSize = dataSize;
}

AnimationData::AnimationData(Stream *stream) {
this->stream = stream;
}

bool AnimationData::isAvailable() {
if (this->stream) {
return this->stream->available() > 0;
} else if (this->data) {
return this->dataSize - this->dataPosition > 0;
} else {
return this->readIndex != this->writeIndex;
}
}

byte AnimationData::getNextByte() {
if (this->stream) {
return this->stream->read();
} else if (this->data) {
return this->readProgmem();
} else {
return this->readBuffer();
}
}

byte AnimationData::readProgmem() {
if (this->dataPosition < this->dataSize) {
return pgm_read_byte(this->data + this->dataPosition++);
} else {
return -1;
}
}

byte AnimationData::readBuffer() {
byte value = this->buffer[this->readIndex++];

if (this->readIndex >= BUFFER_SIZE) {
this->readIndex = 0;
}

return value;
}

void AnimationData::writeByte(byte value) {
this->buffer[this->writeIndex++] = value;

if (this->writeIndex >= BUFFER_SIZE) {
this->writeIndex = 0;
}
}
43 changes: 43 additions & 0 deletions src/internal/AnimationData.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <Arduino.h>

#ifndef BlenderServoAnimation_AnimationData_H
#define BlenderServoAnimation_AnimationData_H

namespace BlenderServoAnimation {

class AnimationData {

public:
static const byte LINE_BREAK = 0xA;

AnimationData();
AnimationData(const byte *data, int dataSize);
AnimationData(Stream *stream);

bool isAvailable();

byte getNextByte();

void writeByte(byte value);

private:
static const byte BUFFER_SIZE = 64;

int dataSize = 0;
int dataPosition = 0;

const byte *data = nullptr;

Stream *stream = nullptr;

byte buffer[BUFFER_SIZE] = {0};
byte writeIndex = 0;
byte readIndex = 0;

byte readProgmem();
byte readBuffer();
};

} // namespace BlenderServoAnimation

#endif
49 changes: 0 additions & 49 deletions src/internal/LiveStream.cpp

This file was deleted.

29 changes: 0 additions & 29 deletions src/internal/LiveStream.h

This file was deleted.

37 changes: 0 additions & 37 deletions src/internal/ProgmemStream.cpp

This file was deleted.

Loading

0 comments on commit 83cc85d

Please sign in to comment.