Theoretical implementation of tick
Completely untested
This commit is contained in:
parent
fea7b1dbb5
commit
84ddcb3a1d
@ -14,6 +14,5 @@ board = esp12e
|
||||
framework = arduino
|
||||
upload_speed = 115200
|
||||
lib_deps =
|
||||
Hash
|
||||
ArduinoJson
|
||||
ESP Async WebServer
|
13
src/main.cpp
13
src/main.cpp
@ -96,11 +96,14 @@ void setup()
|
||||
WiFi.mode(WIFI_OFF);
|
||||
|
||||
#ifdef SerialDebug
|
||||
// onEvent is already deprecated, but since I'm only using it
|
||||
// for debug purposes we'll see how long it lasts...
|
||||
WiFi.onEvent(wifiEvent);
|
||||
_d("WiFi :: MAC address: ");
|
||||
_dln(WiFi.macAddress());
|
||||
// onEvent is already deprecated, but since I'm only using it
|
||||
// for debug purposes we'll see how long it lasts...
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
||||
WiFi.onEvent(wifiEvent);
|
||||
_d("WiFi :: MAC address: ");
|
||||
_dln(WiFi.macAddress());
|
||||
#pragma GCC diagnostic pop
|
||||
#endif
|
||||
|
||||
initWiFi();
|
||||
|
115
src/stairs.cpp
115
src/stairs.cpp
@ -37,61 +37,95 @@ void Stairs::init(PCA9685* pwmDriver)
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
struct Step
|
||||
uint8_t Stairs::ease(uint8_t startValue, uint8_t targetValue, uint16_t transitionTime, uint16_t elapsedTime)
|
||||
{
|
||||
uint16_t currentValue;
|
||||
uint16_t startValue;
|
||||
uint16_t targetValue;
|
||||
uint16_t startTime;
|
||||
uint16_t endTime;
|
||||
};
|
||||
*/
|
||||
bool up = targetValue > startValue;
|
||||
|
||||
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()
|
||||
{
|
||||
if (!mTick) return;
|
||||
|
||||
uint32_t elapsedTime = mLastTransitionTime != 0 ? currentTime - mLastTransitionTime : 0;
|
||||
if (!elapsedTime) return;
|
||||
|
||||
|
||||
mLastTransitionTime = currentTime;
|
||||
mTick = false;
|
||||
|
||||
uint32_t elapsedTime = mTransitionStart != 0 ? currentTime - mTransitionStart : 0;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/*
|
||||
uint32_t diff = this->easeState == Up ? this->parameters.brightness - this->easeStartBrightness : this->easeStartBrightness - this->parameters.brightness;
|
||||
uint32_t delta = (diff * elapsedTime) / this->parameters.easeTime;
|
||||
applyCurrentValue(step);
|
||||
}
|
||||
}
|
||||
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)
|
||||
{
|
||||
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;
|
||||
};
|
||||
|
||||
*/
|
||||
mTick = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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)
|
||||
return;
|
||||
|
||||
// TODO continue transition if one is already going on
|
||||
|
||||
mTick = true;
|
||||
mStep[step].targetValue = brightness;
|
||||
|
||||
if (transitionTime > 0)
|
||||
{
|
||||
mTransitionStart = currentTime;
|
||||
mStep[step].startValue = mStep[step].currentValue;
|
||||
mStep[step].startTime = startTime;
|
||||
mStep[step].endTime = transitionTime;
|
||||
}
|
||||
else
|
||||
{
|
||||
mTransitionStart = 0;
|
||||
mStep[step].remainingTime = transitionTime;
|
||||
}
|
||||
}
|
||||
|
||||
|
15
src/stairs.h
15
src/stairs.h
@ -7,11 +7,11 @@
|
||||
|
||||
struct Step
|
||||
{
|
||||
uint16_t currentValue;
|
||||
uint16_t startValue;
|
||||
uint16_t targetValue;
|
||||
uint16_t startTime;
|
||||
uint16_t endTime;
|
||||
uint8_t currentValue;
|
||||
uint8_t startValue;
|
||||
uint8_t targetValue;
|
||||
int16_t startTime;
|
||||
uint16_t remainingTime;
|
||||
};
|
||||
|
||||
|
||||
@ -21,10 +21,13 @@ class Stairs
|
||||
PCA9685* mPWMDriver;
|
||||
Step mStep[MaxStepCount];
|
||||
|
||||
uint32_t mTransitionStart;
|
||||
uint32_t mLastTransitionTime;
|
||||
bool mTick = false;
|
||||
|
||||
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);
|
||||
|
||||
public:
|
||||
|
Loading…
Reference in New Issue
Block a user