Skip to content

Commit

Permalink
re-add live mode
Browse files Browse the repository at this point in the history
  • Loading branch information
timhendriks93 committed Jan 13, 2024
1 parent 0d9a228 commit 19a691b
Show file tree
Hide file tree
Showing 11 changed files with 128 additions and 106 deletions.
7 changes: 6 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
"__locale": "cpp",
"iterator": "cpp",
"locale": "cpp",
"thread": "cpp"
"thread": "cpp",
"memory": "cpp",
"random": "cpp",
"mutex": "cpp",
"optional": "cpp",
"__tree": "cpp"
}
}
2 changes: 1 addition & 1 deletion examples/MultiplePCA9685/MultiplePCA9685.ino
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ servoMapping servoMappings[] = {
};

// Calculate the amount of servos so that we can easily extend the array
const byte servoAmount = sizeof(servoMaps) / sizeof(servoMaps[0]);
const byte servoAmount = sizeof(servoMappings) / sizeof(servoMappings[0]);

// Callback function which is called whenever a servo needs to be moved
void setPWM(byte servoID, int position) {
Expand Down
3 changes: 3 additions & 0 deletions examples/SwitchModeButton/SwitchModeButton.ino
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ void setup() {
// Add a scene based on PROGMEM data
animation.addScene(ANIMATION_DATA, LENGTH, FPS, FRAMES);

// Add a scene based on (live) Serial data
animation.addScene(Serial);

// Register the mode change callback function
animation.onModeChange(modeChanged);
}
Expand Down
40 changes: 26 additions & 14 deletions src/internal/Animation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,21 @@ bool Animation::hasScene(byte index) {
return this->scenes[index] != nullptr;
}

void Animation::addScene(const byte *data, int dataLength, byte fps,
int frames) {
ProgmemStream *stream = new ProgmemStream(data, dataLength);
this->addScene(*stream, fps, frames);
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);
this->registerScene(scene);
}

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

void Animation::registerScene(Scene *scene) {
this->scenes[this->addIndex] = scene;
this->addIndex++;
}

Expand Down Expand Up @@ -112,8 +118,7 @@ void Animation::loop() {
}

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

Expand All @@ -128,13 +133,13 @@ void Animation::stop() {
this->changeMode(MODE_STOP);
}

void Animation::reset() {
this->addIndex = 0;
this->playIndex = 0;

for (int i = 0; i < this->addIndex; i++) {
this->scenes[i] = nullptr;
void Animation::live(Stream &stream) {
if (this->mode != MODE_DEFAULT) {
return;
}

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

byte Animation::getMode() {
Expand All @@ -156,6 +161,9 @@ void Animation::run(unsigned long currentMicros) {
case MODE_STOP:
this->handleStopMode(currentMicros);
break;
case MODE_LIVE:
this->handleLiveMode();
break;
}
}

Expand Down Expand Up @@ -217,6 +225,10 @@ void Animation::handleStopMode(unsigned long currentMicros) {
}
}

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

Scene *Animation::getCurrentScene() {
return this->scene;
}
Expand Down
10 changes: 8 additions & 2 deletions src/internal/Animation.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Animation {
static const byte MODE_PAUSE = 4;
static const byte MODE_LOOP = 5;
static const byte MODE_STOP = 6;
static const byte MODE_LIVE = 7;

~Animation();

Expand All @@ -39,14 +40,13 @@ class Animation {
void loop();
void pause();
void stop();
void reset();
void live(Stream &stream);
void setDefaultServoThreshold(byte value);
void setServoThreshold(byte id, byte value);

bool hasFinished();
bool hasScenes();
bool hasScene(byte index);
bool modeIsIn(byte modeAmount, ...);

Scene *getCurrentScene();

Expand All @@ -58,6 +58,8 @@ class Animation {
Scene *scenes[MAX_SCENE_COUNT] = {nullptr};
Scene *scene = nullptr;

Stream *liveStream;

mcb modeCallback = nullptr;
scb sceneCallback = nullptr;

Expand All @@ -66,10 +68,14 @@ class Animation {
int addIndex = 0;
int playIndex = 0;

void registerScene(Scene *scene);
void changeMode(byte mode);
void handlePlayMode(unsigned long currentMicros);
void handleStopMode(unsigned long currentMicros);
void handleLiveMode();
void setRandomScene();

bool modeIsIn(byte modeAmount, ...);
};

} // namespace BlenderServoAnimation
Expand Down
43 changes: 19 additions & 24 deletions src/internal/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,33 @@

using namespace BlenderServoAnimation;

Scene::Scene(ServoManager &servoManager, Stream &data, byte fps, int frames,
bool hasProgmemStream) {
Scene::Scene(ServoManager &servoManager, byte fps, int frames) {
this->servoManager = &servoManager;
this->data = &data;
this->fps = fps;
this->frames = frames;
this->frameMicros = round((float)Scene::SECOND_IN_MICROS / (float)fps);
this->diffPerSecond = Scene::SECOND_IN_MICROS - (this->frameMicros * fps);
this->hasProgmemStream = hasProgmemStream;
}

Scene::~Scene() {
if (this->hasProgmemStream) {
delete this->data;
if (this->progmemData) {
delete this->progmemData;
}
}

void Scene::setData(Stream *data) {
this->data = data;
}

void Scene::setProgmemData(ProgmemStream *data) {
this->progmemData = data;
}

void Scene::play(unsigned long currentMicros) {
Stream *data = this->getAnimationData();

if (this->frames == 0) {
this->parseCommands();
this->servoManager->parseStream(data);
return;
}

Expand All @@ -45,7 +52,7 @@ void Scene::play(unsigned long currentMicros) {
this->lastMicros += this->diffPerSecond;
}

this->parseCommands();
this->servoManager->parseStream(data);
}

void Scene::stop(unsigned long currentMicros) {
Expand Down Expand Up @@ -90,22 +97,10 @@ int Scene::getFrames() {
return this->frames;
}

void Scene::parseCommands() {
if (!this->servoManager->hasPositionCallback()) {
return;
Stream* Scene::getAnimationData() {
if (this->progmemData) {
return this->progmemData;
}

Command command;

while (this->data->available() > 0) {
byte value = this->data->read();

if (this->frames > 0 && value == Command::LINE_BREAK) {
break;
}

command.write(value);

this->servoManager->handleCommand(command);
}
return this->data;
}
12 changes: 7 additions & 5 deletions src/internal/Scene.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "ServoManager.h"
#include "ProgmemStream.h"
#include <Arduino.h>

#ifndef BlenderServoAnimation_Scene_H
Expand All @@ -9,10 +10,11 @@ namespace BlenderServoAnimation {
class Scene {

public:
Scene(ServoManager &servoManager, Stream &data, byte fps = 0, int frames = 0,
bool hasProgmemStream = false);
Scene(ServoManager &servoManager, byte fps, int frames);
~Scene();

void setData(Stream *data);
void setProgmemData(ProgmemStream *data);
void play(unsigned long currentMicros);
void stop(unsigned long currentMicros);

Expand All @@ -38,17 +40,17 @@ class Scene {

unsigned long lastMicros = 0;

bool hasProgmemStream = false;

ServoManager *servoManager;

Stream *data = nullptr;

void parseCommands();
ProgmemStream *progmemData = nullptr;

bool isNewFrame(unsigned long currentMicros);

unsigned int getMicrosDiff(unsigned long currentMicros);

Stream* getAnimationData();
};

} // namespace BlenderServoAnimation
Expand Down
20 changes: 20 additions & 0 deletions src/internal/ServoManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,26 @@ void ServoManager::setThreshold(byte servoId, byte value) {
}
}

void ServoManager::parseStream(Stream *stream, bool considerLineBreaks) {
if (!stream || !this->hasPositionCallback()) {
return;
}

Command command;

while (stream->available() > 0) {
byte value = stream->read();

if (considerLineBreaks && value == Command::LINE_BREAK) {
break;
}

command.write(value);

this->handleCommand(command);
}
}

void ServoManager::handleCommand(Command command) {
if (!command.isValid()) {
return;
Expand Down
3 changes: 2 additions & 1 deletion src/internal/ServoManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ServoManager {
void setPositionCallback(pcb positionCallback);
void setDefaultThreshold(byte value);
void setThreshold(byte servoId, byte value);
void handleCommand(Command command);
void parseStream(Stream *stream, bool considerLineBreaks = true);
void moveAllServosToNeutral();

bool hasPositionCallback();
Expand All @@ -33,6 +33,7 @@ class ServoManager {
byte thresholds[MAX_SERVO_COUNT] = {0};

void addServo(byte id);
void handleCommand(Command command);
};

} // namespace BlenderServoAnimation
Expand Down
Loading

0 comments on commit 19a691b

Please sign in to comment.