From ccf438e10b760827936bf7c61059029d4a7670eb Mon Sep 17 00:00:00 2001 From: Jiri Praus Date: Wed, 23 Dec 2020 22:44:44 +0100 Subject: [PATCH] controlling animation from mobile app --- src/floower-esp32/automaton.cpp | 2 -- src/floower-esp32/remote.cpp | 41 ++++++++++++++++++++++----------- 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/src/floower-esp32/automaton.cpp b/src/floower-esp32/automaton.cpp index 345e175..aec4f52 100644 --- a/src/floower-esp32/automaton.cpp +++ b/src/floower-esp32/automaton.cpp @@ -22,8 +22,6 @@ void Automaton::init() { changeState(STATE_STANDBY); floower->onLeafTouch([=](FloowerTouchEvent event){ onLeafTouch(event); }); remote->onTakeOver([=]() { onRemoteTookOver(); }); // remote controller took over - - floower->startAnimation(FloowerColorAnimation::CANDLE); } void Automaton::update() { diff --git a/src/floower-esp32/remote.cpp b/src/floower-esp32/remote.cpp index ebdbfb1..0ed0cbc 100644 --- a/src/floower-esp32/remote.cpp +++ b/src/floower-esp32/remote.cpp @@ -40,35 +40,37 @@ static const char* LOG_TAG = "Remote"; #define BATTERY_POWER_STATE_DISCHARGING B00101111 // BLE data packets -#define STATE_PACKET_SIZE 4 - typedef struct StatePacketData { - byte petalsOpenLevel; // 0-100%, read-write + byte petalsOpenLevel; // normally petals open level 0-100%, read-write byte R; // 0-255, read-write byte G; // 0-255, read-write byte B; // 0-255, read-write - - RgbColor getColor() { - return RgbColor(R, G, B); - } }; +#define STATE_PACKET_SIZE 4 typedef union StatePacket { StatePacketData data; uint8_t bytes[STATE_PACKET_SIZE]; }; -#define STATE_CHANGE_PACKET_SIZE 6 - -// TODO: define #define STATE_TRANSITION_MODE_BIT_COLOR 0 -#define STATE_TRANSITION_MODE_BIT_PETALS 1 +#define STATE_TRANSITION_MODE_BIT_PETALS 1 // when this bit is set, the VALUE parameter means open level of petals (0-100%) +#define STATE_TRANSITION_MODE_BIT_ANIMATION 2 // when this bit is set, the VALUE parameter means ID of animation -typedef struct StateChangePacketData : StatePacketData { +typedef struct StateChangePacketData { + byte value; + byte R; // 0-255, read-write + byte G; // 0-255, read-write + byte B; // 0-255, read-write byte duration; // 100 of milliseconds - byte mode; // transition mode + byte mode; // 8 flags, see defines above + + RgbColor getColor() { + return RgbColor(R, G, B); + } }; +#define STATE_CHANGE_PACKET_SIZE 6 typedef union StateChangePacket { StateChangePacketData data; uint8_t bytes[STATE_CHANGE_PACKET_SIZE]; @@ -222,7 +224,18 @@ void Remote::StateChangeCharacteristicsCallbacks::onWrite(BLECharacteristic *cha } if (CHECK_BIT(statePacket.data.mode, STATE_TRANSITION_MODE_BIT_PETALS)) { // petals open/close - remote->floower->setPetalsOpenLevel(statePacket.data.petalsOpenLevel, statePacket.data.duration * 100); + remote->floower->setPetalsOpenLevel(statePacket.data.value, statePacket.data.duration * 100); + } + else if (CHECK_BIT(statePacket.data.mode, STATE_TRANSITION_MODE_BIT_ANIMATION)) { + // play animation (according to value) + switch (statePacket.data.value) { + case 1: + remote->floower->startAnimation(FloowerColorAnimation::RAINBOW); + break; + case 2: + remote->floower->startAnimation(FloowerColorAnimation::CANDLE); + break; + } } if (remote->takeOverCallback != nullptr) {