Skip to content

Commit

Permalink
animations are now interruptible
Browse files Browse the repository at this point in the history
  • Loading branch information
jiripraus committed Dec 23, 2020
1 parent ccf438e commit b97ae23
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/floower-esp32/automaton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void Automaton::onLeafTouch(FloowerTouchEvent event) {
if (disabledTouchUp) {
disabledTouchUp = false;
}
else if (floower->isIdle()) {
else if (!floower->arePetalsMoving() && !floower->isChangingColor()) {
if (state == STATE_STANDBY) {
// open + set color
if (!floower->isLit()) {
Expand Down
8 changes: 4 additions & 4 deletions src/floower-esp32/floower-esp32.ino
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ void loop() {
periodicOperationsTime = now + PERIODIC_OPERATIONS_INTERVAL;
periodicOperation();
}
if (initRemoteTime != 0 && initRemoteTime < now && floower.isIdle()) {
if (initRemoteTime != 0 && initRemoteTime < now && !floower.arePetalsMoving()) {
initRemoteTime = 0;
remote.init();
remote.startAdvertising();
Expand All @@ -141,7 +141,7 @@ void loop() {
// plan to enter deep sleep in inactivity
if (deepSleepEnabled && !batteryDead) {
// plan to enter deep sleep to save power if floower is in open/dark position & remote is not connected
if (!batteryCharging && !floower.isLit() && floower.isIdle() && floower.getPetalsOpenLevel() == 0 && !remote.isConnected()) {
if (!batteryCharging && !floower.isLit() && !floower.arePetalsMoving() && floower.getPetalsOpenLevel() == 0 && !remote.isConnected()) {
if (deepSleepTime == 0) {
planDeepSleep(DEEP_SLEEP_INACTIVITY_TIMEOUT);
}
Expand All @@ -158,8 +158,8 @@ void loop() {
}
#endif

// save some power when flower is idle
if (floower.isIdle()) {
// save some power when flower is not animating
if (!floower.isAnimating()) {
delay(10);
}
}
Expand Down
11 changes: 9 additions & 2 deletions src/floower-esp32/floower.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ void Floower::setColor(RgbColor color, FloowerColorMode colorMode, int transitio

pixelsColorMode = colorMode;
pixelsTargetColor = color;
interruptiblePixelsAnimation = false;

ESP_LOGI(LOG_TAG, "Color %d,%d,%d", color.R, color.G, color.B);

Expand Down Expand Up @@ -291,6 +292,8 @@ RgbColor Floower::getCurrentColor() {
}

void Floower::startAnimation(FloowerColorAnimation animation) {
interruptiblePixelsAnimation = true;

if (animation == RAINBOW) {
pixelsOriginColor = pixelsColor;
animations.StartAnimation(1, 10000, [=](const AnimationParam& param){ pixelsRainbowAnimationUpdate(param); });
Expand Down Expand Up @@ -359,12 +362,16 @@ bool Floower::isLit() {
return pixelsPowerOn;
}

bool Floower::isAnimating() {
return !animations.IsAnimating();
}

bool Floower::arePetalsMoving() {
return animations.IsAnimationActive(0);
}

bool Floower::isIdle() {
return !animations.IsAnimating();
bool Floower::isChangingColor() {
return (!interruptiblePixelsAnimation && animations.IsAnimationActive(1));
}

void Floower::acty() {
Expand Down
14 changes: 8 additions & 6 deletions src/floower-esp32/floower.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ class Floower {
RgbColor getCurrentColor();
void startAnimation(FloowerColorAnimation animation);
void stopAnimation(bool retainColor);
bool arePetalsMoving();
bool isLit();
bool isIdle();
bool isAnimating();
bool arePetalsMoving();
bool isChangingColor();

void acty();
Battery readBatteryState();
Expand Down Expand Up @@ -107,6 +108,11 @@ class Floower {
FloowerColorMode pixelsColorMode;
bool pixelsPowerOn;

// leds animations
bool interruptiblePixelsAnimation = false;
RgbColor candleOriginColors[6];
RgbColor candleTargetColors[6];

// touch
FloowerOnLeafTouchCallback touchCallback;
static unsigned long touchStartedTime;
Expand All @@ -122,10 +128,6 @@ class Floower {

// acty
unsigned long actyOffTime;

// animations
RgbColor candleOriginColors[6];
RgbColor candleTargetColors[6];
};

#endif
2 changes: 1 addition & 1 deletion src/floower-esp32/remote.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ void Remote::onTakeOver(RemoteTakeOverCallback callback) {
}

void Remote::setBatteryLevel(uint8_t level, bool charging) {
if (deviceConnected && batteryService != nullptr && floower->isIdle()) {
if (deviceConnected && batteryService != nullptr && !floower->arePetalsMoving()) {
ESP_LOGD(LOG_TAG, "level: %d, charging: %d", level, charging);

BLECharacteristic* characteristic = batteryService->getCharacteristic(BATTERY_LEVEL_UUID);
Expand Down

0 comments on commit b97ae23

Please sign in to comment.