Theoretical implementation of tick

Completely untested
This commit is contained in:
Mark van Renswoude 2018-01-05 16:43:14 +01:00
parent fea7b1dbb5
commit 84ddcb3a1d
4 changed files with 88 additions and 56 deletions

View File

@ -14,6 +14,5 @@ board = esp12e
framework = arduino framework = arduino
upload_speed = 115200 upload_speed = 115200
lib_deps = lib_deps =
Hash
ArduinoJson ArduinoJson
ESP Async WebServer ESP Async WebServer

View File

@ -96,11 +96,14 @@ void setup()
WiFi.mode(WIFI_OFF); WiFi.mode(WIFI_OFF);
#ifdef SerialDebug #ifdef SerialDebug
// onEvent is already deprecated, but since I'm only using it // onEvent is already deprecated, but since I'm only using it
// for debug purposes we'll see how long it lasts... // for debug purposes we'll see how long it lasts...
WiFi.onEvent(wifiEvent); #pragma GCC diagnostic push
_d("WiFi :: MAC address: "); #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
_dln(WiFi.macAddress()); WiFi.onEvent(wifiEvent);
_d("WiFi :: MAC address: ");
_dln(WiFi.macAddress());
#pragma GCC diagnostic pop
#endif #endif
initWiFi(); initWiFi();

View File

@ -37,61 +37,95 @@ void Stairs::init(PCA9685* pwmDriver)
} }
/* uint8_t Stairs::ease(uint8_t startValue, uint8_t targetValue, uint16_t transitionTime, uint16_t elapsedTime)
struct Step
{ {
uint16_t currentValue; bool up = targetValue > startValue;
uint16_t startValue;
uint16_t targetValue;
uint16_t startTime;
uint16_t endTime;
};
*/
uint16_t diff = up ? targetValue - startValue : startValue - targetValue;
uint16_t delta = (diff * elapsedTime) / transitionTime;
int16_t currentValue = up ? startValue + delta : startValue - delta;
if (currentValue < 0) currentValue = 0;
if (currentValue > 255) currentValue = 255;
return currentValue;
}
inline void Stairs::updateCurrentValue(Step* stepState)
{
int32_t stepElapsedTime = -stepState->startTime;
stepState->currentValue = ease(stepState->startValue, stepState->targetValue, stepState->remainingTime + stepElapsedTime, stepElapsedTime);
}
inline void Stairs::applyCurrentValue(uint8_t step)
{
mPWMDriver->setPWM(step, this->getPWMValue(step, mStep[step].currentValue));
}
void Stairs::tick() void Stairs::tick()
{ {
if (!mTick) return; if (!mTick) return;
uint32_t elapsedTime = mLastTransitionTime != 0 ? currentTime - mLastTransitionTime : 0;
if (!elapsedTime) return;
mLastTransitionTime = currentTime;
mTick = false; mTick = false;
uint32_t elapsedTime = mTransitionStart != 0 ? currentTime - mTransitionStart : 0;
for (uint8_t step = 0; step < stepsSettings->count(); step++) for (uint8_t step = 0; step < stepsSettings->count(); step++)
{ {
if (mStep[step].currentValue != mStep[step].targetValue) Step* stepState = &mStep[step];
if (stepState->currentValue != stepState->targetValue)
{ {
// TODO more maths! // If there is a startup delay request, wait for it first
if (stepState->startTime > 0)
{
stepState->startTime -= elapsedTime;
if (stepState->startTime < 0)
{
if (stepState->remainingTime > -stepState->startTime)
{
// Shift the remaining time equally
stepState->remainingTime += stepState->startTime;
updateCurrentValue(stepState);
mTick = true;
}
else
{
// End of the transition
stepState->remainingTime = 0;
stepState->currentValue = stepState->targetValue;
}
/* applyCurrentValue(step);
uint32_t diff = this->easeState == Up ? this->parameters.brightness - this->easeStartBrightness : this->easeStartBrightness - this->parameters.brightness; }
uint32_t delta = (diff * elapsedTime) / this->parameters.easeTime; }
else if (elapsedTime >= stepState->remainingTime)
{
// End of the transition
stepState->remainingTime = 0;
stepState->currentValue = stepState->targetValue;
this->currentBrightness = this->easeState == Up ? this->easeStartBrightness + delta : this->easeStartBrightness - delta; applyCurrentValue(step);
}
else
{
stepState->startTime -= elapsedTime;
stepState->remainingTime -= elapsedTime;
updateCurrentValue(stepState);
applyCurrentValue(step);
if (elapsedTime >= this->parameters.easeTime) mTick = true;
{ }
this->currentBrightness = this->parameters.brightness;
this->easeState = None;
}
t /= d/2;
if (t < 1) return c/2*t*t + b;
t--;
return -c/2 * (t*(t-2) - 1) + b;
};
*/
} }
} }
// TODO
// mPWMDriver->setPWM(step, this->getPWMValue(step, brightness));
} }
@ -108,21 +142,14 @@ void Stairs::set(uint8_t step, uint8_t brightness, uint16_t transitionTime, uint
if (mStep[step].currentValue == brightness) if (mStep[step].currentValue == brightness)
return; return;
// TODO continue transition if one is already going on
mTick = true; mTick = true;
mStep[step].targetValue = brightness; mStep[step].targetValue = brightness;
if (transitionTime > 0) if (transitionTime > 0)
{ {
mTransitionStart = currentTime;
mStep[step].startValue = mStep[step].currentValue; mStep[step].startValue = mStep[step].currentValue;
mStep[step].startTime = startTime; mStep[step].startTime = startTime;
mStep[step].endTime = transitionTime; mStep[step].remainingTime = transitionTime;
}
else
{
mTransitionStart = 0;
} }
} }

View File

@ -7,11 +7,11 @@
struct Step struct Step
{ {
uint16_t currentValue; uint8_t currentValue;
uint16_t startValue; uint8_t startValue;
uint16_t targetValue; uint8_t targetValue;
uint16_t startTime; int16_t startTime;
uint16_t endTime; uint16_t remainingTime;
}; };
@ -21,10 +21,13 @@ class Stairs
PCA9685* mPWMDriver; PCA9685* mPWMDriver;
Step mStep[MaxStepCount]; Step mStep[MaxStepCount];
uint32_t mTransitionStart; uint32_t mLastTransitionTime;
bool mTick = false; bool mTick = false;
protected: protected:
uint8_t ease(uint8_t startValue, uint8_t targetValue, uint16_t transitionTime, uint16_t elapsedTime);
inline void updateCurrentValue(Step* stepState);
inline void applyCurrentValue(uint8_t step);
uint16_t getPWMValue(uint8_t step, uint8_t brightness); uint16_t getPWMValue(uint8_t step, uint8_t brightness);
public: public: