154 lines
2.9 KiB
C++
154 lines
2.9 KiB
C++
#include "stairs.h"
|
|
#include <Math.h>
|
|
#include <FS.h>
|
|
#include "debug.h"
|
|
|
|
|
|
|
|
const static float factorBase = log10(2) / log10(PCA9685::On);
|
|
|
|
|
|
struct Header
|
|
{
|
|
uint8_t version;
|
|
uint8_t rangeCount;
|
|
bool useScaling;
|
|
};
|
|
|
|
|
|
void Stairs::init(PCA9685* pwmDriver)
|
|
{
|
|
this->useScaling = false;
|
|
|
|
/*
|
|
for (uint8_t i = 0; i < StepCount; i++)
|
|
{
|
|
this->ranges[i].start = IStairs::Off;
|
|
this->ranges[i].end = IStairs::On;
|
|
}
|
|
*/
|
|
|
|
this->pwmDriver = pwmDriver;
|
|
|
|
_dln("Loading range configuration");
|
|
SPIFFS.begin();
|
|
this->readRange();
|
|
}
|
|
|
|
|
|
uint8_t Stairs::getCount()
|
|
{
|
|
return 0;//StepCount;
|
|
}
|
|
|
|
|
|
void Stairs::set(uint8_t step, uint16_t brightness)
|
|
{
|
|
pwmDriver->setPWM(step, this->getPWMValue(step, brightness));
|
|
}
|
|
|
|
|
|
void Stairs::setAll(uint16_t brightness)
|
|
{
|
|
//pwmDriver->setAll(this->getPWMValue(brightness));
|
|
|
|
/*
|
|
for (uint8_t step = 0; step < StepCount; step++)
|
|
pwmDriver->setPWM(step, this->getPWMValue(step, brightness));
|
|
*/
|
|
}
|
|
|
|
|
|
uint16_t Stairs::getPWMValue(uint8_t step, uint16_t brightness)
|
|
{
|
|
_d("Getting PWM value for step "); _d(step); _d(", brightness "); _dln(brightness);
|
|
if (brightness == IStairs::Off || brightness == IStairs::On)
|
|
{
|
|
_dln("Full on/off, returning input");
|
|
return brightness;
|
|
}
|
|
|
|
if (step < 0 || step >= getCount())
|
|
{
|
|
_dln("Step out of bounds, returning input");
|
|
return brightness;
|
|
}
|
|
|
|
Range* range = &this->ranges[step];
|
|
_d("Start: "); _dln(range->start);
|
|
_d("End: "); _dln(range->end);
|
|
|
|
if (this->useScaling)
|
|
{
|
|
_dln("Using scaling");
|
|
float factor = ((range->end - range->start) + 1) * factorBase;
|
|
brightness = pow(2, (brightness / factor)) - 1 + range->start;
|
|
}
|
|
else
|
|
{
|
|
_dln("Not using scaling");
|
|
if (brightness < range->start) brightness = range->start;
|
|
if (brightness > range->end) brightness = range->end;
|
|
}
|
|
|
|
_d("Output: "); _dln(brightness);
|
|
return brightness;
|
|
}
|
|
|
|
|
|
void Stairs::getRange(Stream* stream)
|
|
{
|
|
stream->write(this->useScaling ? 1 : 0);
|
|
stream->write((uint8_t*)&this->ranges, sizeof(this->ranges));
|
|
}
|
|
|
|
|
|
void Stairs::setRange(uint8_t* data)
|
|
{
|
|
this->useScaling = *data;
|
|
data++;
|
|
|
|
memcpy(this->ranges, data, sizeof(this->ranges));
|
|
this->writeRange();
|
|
}
|
|
|
|
|
|
void Stairs::readRange()
|
|
{
|
|
File f = SPIFFS.open("/range", "r");
|
|
if (!f)
|
|
return;
|
|
|
|
if (!f.available())
|
|
return;
|
|
|
|
Header header;
|
|
f.readBytes((char*)&header, sizeof(Header));
|
|
|
|
if (header.version != 1)
|
|
return;
|
|
|
|
this->useScaling = (header.useScaling == 1);
|
|
f.readBytes((char*)&this->ranges, header.rangeCount * sizeof(Range));
|
|
f.close();
|
|
|
|
_d("- useScaling: ");
|
|
_dln(this->useScaling);
|
|
}
|
|
|
|
|
|
void Stairs::writeRange()
|
|
{
|
|
File f = SPIFFS.open("/range", "w");
|
|
if (!f)
|
|
return;
|
|
|
|
Header header;
|
|
header.version = 1;
|
|
header.useScaling = this->useScaling;
|
|
header.rangeCount = getCount();
|
|
|
|
f.write((uint8_t*)&header, sizeof(Header));
|
|
f.write((uint8_t*)&this->ranges, sizeof(this->ranges));
|
|
f.close();
|
|
} |