Skip to content

Commit

Permalink
add scene reset logic
Browse files Browse the repository at this point in the history
  • Loading branch information
timhendriks93 committed Feb 1, 2024
1 parent 559d2c7 commit eba49d1
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 41 deletions.
22 changes: 12 additions & 10 deletions examples/MultiplePCA9685/MultiplePCA9685.ino
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,20 @@ const byte servoAmount = sizeof(servoMappings) / sizeof(servoMappings[0]);
void setPWM(byte servoID, int position) {
// Iterate through the available servos
for (int i = 0; i < servoAmount; i++) {
// Check if the current servo ID matches the target servo ID
if (servoMappings[i].id == servoID) {
// Get the PWM driver instance and channel from the mapping
Adafruit_PWMServoDriver pwm = servoMappings[i].pwm;
byte channel = servoMappings[i].channel;
// Continue if the current servo ID doesn't match the target servo ID
if (servoMappings[i].id != servoID) {
continue;
}

// Set the current position as PWM output
pwm.setPWM(channel, 0, position);
// Get the PWM driver instance and channel from the mapping
Adafruit_PWMServoDriver pwm = servoMappings[i].pwm;
byte channel = servoMappings[i].channel;

// Break the for loop as we already handled the servo movement
break;
}
// Set the current position as PWM output
pwm.setPWM(channel, 0, position);

// Break the for loop as we finsihed handling the servo movement
break;
}
}

Expand Down
4 changes: 2 additions & 2 deletions examples/MultipleScenes/MultipleScenes.ino
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Setting up a show consisting of 2 animations.
Setting up an animation consisting of 2 scenes.
Note the namespaces which are used to distinguish the positions
of one scene / animation from another. It's even possible to
Expand Down Expand Up @@ -41,7 +41,7 @@ void setup() {
animation.addScene(SceneB::ANIMATION_DATA, SceneB::LENGTH, SceneB::FPS,
SceneB::FRAMES);

// Trigger the show loop mode
// Trigger the animation loop mode
animation.loop();

// There are also other playback options
Expand Down
6 changes: 3 additions & 3 deletions examples/MultipleScenes/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Show
# Multiple scenes

Setting up a show consisting of 2 animations.
Setting up an animation consisting of 2 scenes.

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

![Arduino Nano with servo](../../images/arduino-nano-with-servo.png)
1 change: 1 addition & 0 deletions examples/SerialLiveMode/SerialLiveMode.ino
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ void move(byte servoID, int position) {
BlenderServoAnimation::Animation animation;

void setup() {
// Initialize serial communication
Serial.begin(115200);

// Attach the servo to pin 12
Expand Down
17 changes: 8 additions & 9 deletions examples/SwitchModeButton/SwitchModeButton.ino
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
based on the current mode.
Starting the animation will only play it once, so another button press is
required to play it again. Alternatively, you can also use MODE_LOOP instead
required to play it again. Alternatively, you can use MODE_LOOP instead
of MODE_PLAY and loop() instead of play(). This will keep the animation
running until the button is short or long pressed again, thus pausing or
stopping the animation.
Expand Down Expand Up @@ -53,6 +53,9 @@ void modeChanged(byte prevMode, byte newMode) {
case BlenderServoAnimation::Animation::MODE_PAUSE:
// E.g. pause audio
break;
case BlenderServoAnimation::Animation::MODE_STOP:
// E.g. stop audio
break;
}
}

Expand Down Expand Up @@ -80,15 +83,8 @@ void onPressed() {
void onLongPressed() {
// Get the current mode, act accordingly and trigger another mode
switch (animation.getMode()) {
// On long press in default mode, we want to trigger the live mode
case BlenderServoAnimation::Animation::MODE_DEFAULT:
animation.live(Serial);
break;
// On long press in any other mode, we want to stop the animation
// On long press in play, pause or live mode, we want to stop the animation
case BlenderServoAnimation::Animation::MODE_PLAY:
case BlenderServoAnimation::Animation::MODE_PLAY_SINGLE:
case BlenderServoAnimation::Animation::MODE_PLAY_RANDOM:
case BlenderServoAnimation::Animation::MODE_LOOP:
case BlenderServoAnimation::Animation::MODE_PAUSE:
animation.stop();
break;
Expand All @@ -103,6 +99,9 @@ void setup() {
modeButton.attachClick(onPressed);
modeButton.attachLongPressStart(onLongPressed);

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

// Add a scene based on PROGMEM data
animation.addScene(ANIMATION_DATA, LENGTH, FPS, FRAMES);

Expand Down
1 change: 1 addition & 0 deletions src/internal/Animation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ void Animation::setScene(byte index) {

this->playIndex = index;
this->scene = scene;
this->scene->reset();

if (this->sceneCallback) {
this->sceneCallback(prevIndex, index);
Expand Down
9 changes: 9 additions & 0 deletions src/internal/AnimationData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,12 @@ void AnimationData::writeByte(byte value) {
this->writeIndex = 0;
}
}

void AnimationData::reset() {
if (this->data) {
this->dataPosition = 0;
} else {
this->readIndex = 0;
this->writeIndex = 0;
}
}
1 change: 1 addition & 0 deletions src/internal/AnimationData.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class AnimationData {
byte getNextByte();

void writeByte(byte value);
void reset();

private:
static const byte BUFFER_SIZE = 64;
Expand Down
16 changes: 8 additions & 8 deletions src/internal/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,7 @@ void Scene::play(unsigned long currentMicros) {
}

this->lastMicros = currentMicros;

if (this->frame >= this->frames) {
this->frame = 0;
} else {
this->frame++;
}
this->frame++;

if (this->frame % this->fps == 0) {
this->lastMicros += this->diffPerSecond;
Expand All @@ -55,10 +50,15 @@ void Scene::stop(unsigned long currentMicros) {
this->servoManager->moveAllTowardsNeutral();

if (this->servoManager->servosAreAllNeutral()) {
this->frame = 0;
this->reset();
}
}

void Scene::reset() {
this->frame = 0;
this->data->reset();
}

unsigned int Scene::getMicrosDiff(unsigned long currentMicros) {
if (currentMicros >= this->lastMicros) {
return currentMicros - this->lastMicros;
Expand All @@ -73,7 +73,7 @@ bool Scene::isNewFrame(unsigned long currentMicros) {
}

bool Scene::hasFinished() {
return this->frame == this->frames;
return this->frame + 1 == this->frames;
}

byte Scene::getFPS() {
Expand Down
1 change: 1 addition & 0 deletions src/internal/Scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Scene {

void play(unsigned long currentMicros);
void stop(unsigned long currentMicros);
void reset();

bool hasFinished();

Expand Down
14 changes: 6 additions & 8 deletions src/internal/ServoManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,26 @@ void ServoManager::parseData(AnimationData *data, bool considerLineBreaks) {
return;
}

Command command;

while (data->isAvailable()) {
byte value = data->getNextByte();

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

command.write(value);
this->command.write(value);

this->handleCommand(command);
this->handleCommand();
}
}

void ServoManager::handleCommand(Command command) {
if (!command.isValid()) {
void ServoManager::handleCommand() {
if (!this->command.isValid()) {
return;
}

byte id = command.getServoID();
int position = command.getServoPosition();
byte id = this->command.getServoID();
int position = this->command.getServoPosition();

if (!this->servos[id]) {
this->addServo(id);
Expand Down
4 changes: 3 additions & 1 deletion src/internal/ServoManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ class ServoManager {

Servo *servos[MAX_SERVO_COUNT] = {nullptr};

Command command;

pcb positionCallback = nullptr;

byte defaultThreshold = 0;
byte thresholds[MAX_SERVO_COUNT] = {0};

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

} // namespace BlenderServoAnimation
Expand Down

0 comments on commit eba49d1

Please sign in to comment.