2017-03-24 22:04:58 +00:00
|
|
|
#include <Math.h>
|
2017-03-27 20:24:05 +00:00
|
|
|
#include <FS.h>
|
2017-03-23 18:45:29 +00:00
|
|
|
#include "stairs.h"
|
|
|
|
|
2017-03-24 22:04:58 +00:00
|
|
|
|
2017-03-27 20:24:05 +00:00
|
|
|
|
|
|
|
const static float factorBase = log10(2) / log10(PCA9685::On);
|
|
|
|
|
|
|
|
|
|
|
|
struct Header
|
|
|
|
{
|
|
|
|
uint8_t version;
|
|
|
|
uint8_t rangeCount;
|
|
|
|
bool useScaling;
|
|
|
|
};
|
2017-03-24 22:04:58 +00:00
|
|
|
|
|
|
|
|
2017-03-23 18:45:29 +00:00
|
|
|
void Stairs::init(PCA9685* pwmDriver)
|
|
|
|
{
|
|
|
|
this->pwmDriver = pwmDriver;
|
2017-03-27 20:24:05 +00:00
|
|
|
|
|
|
|
SPIFFS.begin();
|
|
|
|
this->readRange();
|
2017-03-23 18:45:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t Stairs::getCount()
|
|
|
|
{
|
|
|
|
return StepCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-24 22:04:58 +00:00
|
|
|
void Stairs::set(uint8_t step, uint16_t brightness)
|
|
|
|
{
|
2017-03-27 20:24:05 +00:00
|
|
|
pwmDriver->setPWM(step, this->getPWMValue(step, brightness));
|
2017-03-24 22:04:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Stairs::setAll(uint16_t brightness)
|
2017-03-23 18:45:29 +00:00
|
|
|
{
|
2017-03-27 20:24:05 +00:00
|
|
|
//pwmDriver->setAll(this->getPWMValue(brightness));
|
|
|
|
|
|
|
|
for (uint8_t step = 0; step < StepCount; step++)
|
|
|
|
pwmDriver->setPWM(step, this->getPWMValue(step, brightness));
|
2017-03-23 18:45:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-27 20:24:05 +00:00
|
|
|
uint16_t Stairs::getPWMValue(uint8_t step, uint16_t brightness)
|
2017-03-23 18:45:29 +00:00
|
|
|
{
|
2017-03-24 22:04:58 +00:00
|
|
|
if (brightness == IStairs::Off || brightness == IStairs::On)
|
|
|
|
return brightness;
|
|
|
|
|
2017-03-27 20:24:05 +00:00
|
|
|
if (step < 0 || step >= StepCount)
|
|
|
|
return brightness;
|
|
|
|
|
|
|
|
Range* range = this->ranges[step];
|
|
|
|
|
|
|
|
if (this->useScaling)
|
|
|
|
{
|
|
|
|
float factor = ((range->end - range->start) + 1) * factorBase;
|
|
|
|
brightness = pow(2, (brightness / factor)) - 1 + range->start;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (brightness < range->start) brightness = range->start;
|
|
|
|
if (brightness > range->end) brightness = range->end;
|
|
|
|
}
|
|
|
|
|
|
|
|
return brightness;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Stairs::getRange(Stream* stream)
|
|
|
|
{
|
|
|
|
stream->write(this->useScaling ? 1 : 0);
|
|
|
|
stream->write(reinterpret_cast<uint8_t*>(&this->ranges), sizeof(this->ranges));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Stairs::setRange(uint8_t* data)
|
|
|
|
{
|
|
|
|
this->useScaling = *data;
|
|
|
|
data++;
|
|
|
|
|
|
|
|
memcpy(this->ranges, data, sizeof(this->ranges));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Stairs::readRange()
|
|
|
|
{
|
|
|
|
File f = SPIFFS.open("/range", "r");
|
|
|
|
if (!f)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!f.available())
|
|
|
|
return;
|
|
|
|
|
|
|
|
Header header;
|
|
|
|
f.readBytes(reinterpret_cast<char*>(&header), sizeof(Header));
|
|
|
|
|
|
|
|
if (header.version != 1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this->useScaling = (header.useScaling == 1);
|
|
|
|
|
|
|
|
memset(this->ranges, 0, sizeof(this->ranges));
|
|
|
|
f.readBytes(reinterpret_cast<char*>(&this->ranges), header.rangeCount * sizeof(Range));
|
|
|
|
f.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Stairs::writeRange()
|
|
|
|
{
|
|
|
|
File f = SPIFFS.open("/range", "w");
|
|
|
|
if (!f)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Header header;
|
|
|
|
header.version = 1;
|
|
|
|
header.useScaling = this->useScaling;
|
|
|
|
header.rangeCount = StepCount;
|
|
|
|
|
|
|
|
f.write(reinterpret_cast<uint8_t*>(&header), sizeof(Header));
|
|
|
|
f.write(reinterpret_cast<uint8_t*>(&this->ranges), sizeof(this->ranges));
|
|
|
|
f.close();
|
2017-03-23 18:45:29 +00:00
|
|
|
}
|