41 lines
859 B
C++
41 lines
859 B
C++
#include <Math.h>
|
|
#include "stairs.h"
|
|
#include "config.h"
|
|
|
|
|
|
static const float brightnessFactor = ((IStairs::On - IStairs::Off) + 1) * log10(2) / log10(PCA9685::On);
|
|
|
|
|
|
void Stairs::init(PCA9685* pwmDriver)
|
|
{
|
|
this->pwmDriver = pwmDriver;
|
|
}
|
|
|
|
|
|
uint8_t Stairs::getCount()
|
|
{
|
|
return StepCount;
|
|
}
|
|
|
|
|
|
void Stairs::set(uint8_t step, uint16_t brightness)
|
|
{
|
|
pwmDriver->setPWM(step, this->getPWMValue(brightness));
|
|
}
|
|
|
|
|
|
void Stairs::setAll(uint16_t brightness)
|
|
{
|
|
pwmDriver->setAll(this->getPWMValue(brightness));
|
|
}
|
|
|
|
|
|
uint16_t Stairs::getPWMValue(uint16_t brightness)
|
|
{
|
|
if (brightness == IStairs::Off || brightness == IStairs::On)
|
|
return brightness;
|
|
|
|
// Correct for the exponential perception of brightness
|
|
// Source: https://diarmuid.ie/blog/pwm-exponential-led-fading-on-arduino-or-other-platforms/
|
|
return pow(2, (brightness / brightnessFactor)) - 1;
|
|
} |