Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

brightness/power limit #352

Merged
merged 8 commits into from
Jun 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 51 additions & 11 deletions Software/src/AbstractLedDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,24 @@ void AbstractLedDevice::setBrightness(int value, bool updateColors) {
setColors(m_colorsSaved);
}

void AbstractLedDevice::setBrightnessCap(int value, bool updateColors) {
m_brightnessCap = value;
if (updateColors)
setColors(m_colorsSaved);
}

void AbstractLedDevice::setLedMilliAmps(const int value, const bool updateColors) {
m_ledMilliAmps = value;
if (updateColors)
setColors(m_colorsSaved);
}

void AbstractLedDevice::setPowerSupplyAmps(const double value, const bool updateColors) {
m_powerSupplyAmps = value;
if (updateColors)
setColors(m_colorsSaved);
}

void AbstractLedDevice::setLuminosityThreshold(int value, bool updateColors) {
m_luminosityThreshold = value;
if (updateColors)
Expand Down Expand Up @@ -69,6 +87,7 @@ void AbstractLedDevice::updateDeviceSettings()
using namespace SettingsScope;
setGamma(Settings::getDeviceGamma(), false);
setBrightness(Settings::getDeviceBrightness(), false);
setBrightnessCap(Settings::getDeviceBrightnessCap(), false);
setLuminosityThreshold(Settings::getLuminosityThreshold(), false);
setMinimumLuminosityThresholdEnabled(Settings::isMinimumLuminosityEnabled(), false);
updateWBAdjustments(Settings::getLedCoefs(), false);
Expand All @@ -83,35 +102,38 @@ void AbstractLedDevice::updateDeviceSettings()
*/
void AbstractLedDevice::applyColorModifications(const QList<QRgb> &inColors, QList<StructRgb> &outColors) {

bool isApplyWBAdjustments = m_wbAdjustments.count() == inColors.count();
const bool isApplyWBAdjustments = m_wbAdjustments.count() == inColors.count();

for(int i = 0; i < inColors.count(); i++) {

//renormalize to 12bit
double k = 4095/255.0;
outColors[i].r = qRed(inColors[i]) * k;
const constexpr double k = 4095/255.0;
outColors[i].r = qRed(inColors[i]) * k;
outColors[i].g = qGreen(inColors[i]) * k;
outColors[i].b = qBlue(inColors[i]) * k;
outColors[i].b = qBlue(inColors[i]) * k;

PrismatikMath::gammaCorrection(m_gamma, outColors[i]);
}

StructLab avgColor = PrismatikMath::toLab(PrismatikMath::avgColor(outColors));
const StructLab avgColor = PrismatikMath::toLab(PrismatikMath::avgColor(outColors));

const double ampCoef = m_ledMilliAmps / (4095.0 * 3.0) / 1000.0;
double estimatedTotalAmps = 0.0;

for (int i = 0; i < outColors.count(); ++i) {
StructLab lab = PrismatikMath::toLab(outColors[i]);
int dl = m_luminosityThreshold - lab.l;
const int dl = m_luminosityThreshold - lab.l;
if (dl > 0) {
if (m_isMinimumLuminosityEnabled) { // apply minimum luminosity or dead-zone
// Cross-fade a and b channels to avarage value within kFadingRange, fadingFactor = (dL - fadingRange)^2 / (fadingRange^2)
const int kFadingRange = 5;
double fadingCoeff = dl < kFadingRange ? (dl - kFadingRange)*(dl - kFadingRange)/(kFadingRange*kFadingRange): 1;
char da = avgColor.a - lab.a;
char db = avgColor.b - lab.b;
constexpr int kFadingRange = 5;
const double fadingCoeff = dl < kFadingRange ? (dl - kFadingRange)*(dl - kFadingRange)/(kFadingRange*kFadingRange): 1;
const char da = avgColor.a - lab.a;
const char db = avgColor.b - lab.b;
lab.l = m_luminosityThreshold;
lab.a += PrismatikMath::round(da * fadingCoeff);
lab.b += PrismatikMath::round(db * fadingCoeff);
StructRgb rgb = PrismatikMath::toRgb(lab);
const StructRgb rgb = PrismatikMath::toRgb(lab);
outColors[i] = rgb;
} else {
outColors[i].r = 0;
Expand All @@ -127,6 +149,24 @@ void AbstractLedDevice::applyColorModifications(const QList<QRgb> &inColors, QLi
outColors[i].g *= m_wbAdjustments[i].green;
outColors[i].b *= m_wbAdjustments[i].blue;
}
if (m_brightnessCap < SettingsScope::Profile::Device::BrightnessCapMax) {
const double bcapFactor = (m_brightnessCap / 100.0 * 4095 * 3) / (outColors[i].r + outColors[i].g + outColors[i].b);
if (bcapFactor < 1.0) {
outColors[i].r *= bcapFactor;
outColors[i].g *= bcapFactor;
outColors[i].b *= bcapFactor;
}
}

estimatedTotalAmps += ((double)outColors[i].r + (double)outColors[i].g + (double)outColors[i].b) * ampCoef;
}

if (m_powerSupplyAmps > 0.0 && m_powerSupplyAmps < estimatedTotalAmps) {
const double powerRatio = m_powerSupplyAmps / estimatedTotalAmps;
for (StructRgb& color : outColors) {
color.r *= powerRatio;
color.g *= powerRatio;
color.b *= powerRatio;
}
}
}
6 changes: 6 additions & 0 deletions Software/src/AbstractLedDevice.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ public slots:
virtual void setSmoothSlowdown(int value) = 0;
virtual void setGamma(double value, bool updateColors = true);
virtual void setBrightness(int value, bool updateColors = true);
virtual void setBrightnessCap(int value, bool updateColors = true);
virtual void setLedMilliAmps(const int value, const bool updateColors = true);
virtual void setPowerSupplyAmps(const double value, const bool updateColors = true);
virtual void setColorSequence(QString value) = 0;
virtual void setLuminosityThreshold(int value, bool updateColors = true);
virtual void setMinimumLuminosityThresholdEnabled(bool value, bool updateColors = true);
Expand Down Expand Up @@ -98,6 +101,9 @@ public slots:
QString m_colorSequence;
double m_gamma;
int m_brightness;
int m_brightnessCap;
int m_ledMilliAmps{0};
double m_powerSupplyAmps{0.0};
int m_luminosityThreshold;
bool m_isMinimumLuminosityEnabled;

Expand Down
59 changes: 47 additions & 12 deletions Software/src/LedDeviceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ LedDeviceManager::LedDeviceManager(QObject *parent)

m_failedCreationAttempts = 0;

m_savedBrightness = SettingsScope::Profile::Device::BrightnessDefault;

m_savedBrightnessCap = SettingsScope::Profile::Device::BrightnessCapDefault;

for (int i = 0; i < SupportedDevices::DeviceTypesCount; i++)
m_ledDevices.append(NULL);
}
Expand Down Expand Up @@ -245,6 +249,22 @@ void LedDeviceManager::setBrightness(int value)
}
}

void LedDeviceManager::setBrightnessCap(int value)
{
DEBUG_MID_LEVEL << Q_FUNC_INFO << value << "Is last command completed:" << m_isLastCommandCompleted;

if (m_isLastCommandCompleted)
{
m_isLastCommandCompleted = false;
m_cmdTimeoutTimer->start();
emit ledDeviceSetBrightnessCap(value);
}
else {
m_savedBrightnessCap = value;
cmdQueueAppend(LedDeviceCommands::SetBrightnessCap);
}
}

void LedDeviceManager::setLuminosityThreshold(int value)
{
DEBUG_MID_LEVEL << Q_FUNC_INFO << value << "Is last command completed:" << m_isLastCommandCompleted;
Expand Down Expand Up @@ -419,7 +439,7 @@ void LedDeviceManager::initLedDevice()
}

AbstractLedDevice * LedDeviceManager::createLedDevice(SupportedDevices::DeviceType deviceType)
{
{

if (deviceType == SupportedDevices::DeviceTypeAlienFx){
# if !defined(Q_OS_WIN)
Expand All @@ -430,54 +450,62 @@ AbstractLedDevice * LedDeviceManager::createLedDevice(SupportedDevices::DeviceTy
# endif /* Q_OS_WIN */
}

AbstractLedDevice* device = nullptr;

switch (deviceType){

case SupportedDevices::DeviceTypeLightpack:
DEBUG_LOW_LEVEL << Q_FUNC_INFO << "SupportedDevices::LightpackDevice";
return (AbstractLedDevice *)new LedDeviceLightpack();
device = (AbstractLedDevice *)new LedDeviceLightpack();

case SupportedDevices::DeviceTypeAlienFx:
DEBUG_LOW_LEVEL << Q_FUNC_INFO << "SupportedDevices::AlienFxDevice";

# ifdef Q_OS_WIN
return (AbstractLedDevice *)new LedDeviceAlienFx();
device = (AbstractLedDevice *)new LedDeviceAlienFx();
# else
break;
# endif /* Q_OS_WIN */

case SupportedDevices::DeviceTypeAdalight:
DEBUG_LOW_LEVEL << Q_FUNC_INFO << "SupportedDevices::AdalightDevice";
return (AbstractLedDevice *)new LedDeviceAdalight(Settings::getAdalightSerialPortName(), Settings::getAdalightSerialPortBaudRate());
device = (AbstractLedDevice *)new LedDeviceAdalight(Settings::getAdalightSerialPortName(), Settings::getAdalightSerialPortBaudRate());

case SupportedDevices::DeviceTypeArdulight:
DEBUG_LOW_LEVEL << Q_FUNC_INFO << "SupportedDevices::ArdulightDevice";
return (AbstractLedDevice *)new LedDeviceArdulight(Settings::getArdulightSerialPortName(), Settings::getArdulightSerialPortBaudRate());
device = (AbstractLedDevice *)new LedDeviceArdulight(Settings::getArdulightSerialPortName(), Settings::getArdulightSerialPortBaudRate());

case SupportedDevices::DeviceTypeDrgb:
DEBUG_LOW_LEVEL << Q_FUNC_INFO << "SupportedDevices::DrgbDevice";
return (AbstractLedDevice*)new LedDeviceDrgb(Settings::getDrgbAddress(), Settings::getDrgbPort(), Settings::getDrgbTimeout());
device = (AbstractLedDevice*)new LedDeviceDrgb(Settings::getDrgbAddress(), Settings::getDrgbPort(), Settings::getDrgbTimeout());

case SupportedDevices::DeviceTypeDnrgb:
DEBUG_LOW_LEVEL << Q_FUNC_INFO << "SupportedDevices::DnrgbDevice";
return (AbstractLedDevice*)new LedDeviceDnrgb(Settings::getDnrgbAddress(), Settings::getDnrgbPort(), Settings::getDnrgbTimeout());
device = (AbstractLedDevice*)new LedDeviceDnrgb(Settings::getDnrgbAddress(), Settings::getDnrgbPort(), Settings::getDnrgbTimeout());

case SupportedDevices::DeviceTypeWarls:
DEBUG_LOW_LEVEL << Q_FUNC_INFO << "SupportedDevices::WarlsDevice";
return (AbstractLedDevice*)new LedDeviceWarls(Settings::getWarlsAddress(), Settings::getWarlsPort(), Settings::getWarlsTimeout());
device = (AbstractLedDevice*)new LedDeviceWarls(Settings::getWarlsAddress(), Settings::getWarlsPort(), Settings::getWarlsTimeout());

case SupportedDevices::DeviceTypeVirtual:
DEBUG_LOW_LEVEL << Q_FUNC_INFO << "SupportedDevices::VirtualDevice";
return (AbstractLedDevice *)new LedDeviceVirtual();
device = (AbstractLedDevice *)new LedDeviceVirtual();

default:
break;
}

if (device) {
device->setLedMilliAmps(Settings::getDeviceLedMilliAmps(deviceType));
device->setPowerSupplyAmps(Settings::getDevicePowerSupplyAmps(deviceType));
return device;
}

qFatal("%s %s%d%s", Q_FUNC_INFO,
"Create LedDevice fail. deviceType = '", deviceType,
"'. Application exit.");

return NULL; // Avoid compiler warning
return nullptr; // Avoid compiler warning
}

void LedDeviceManager::connectSignalSlotsLedDevice()
Expand All @@ -487,10 +515,10 @@ void LedDeviceManager::connectSignalSlotsLedDevice()
qWarning() << Q_FUNC_INFO << "m_ledDevice == NULL";
return;
}

connect(m_ledDevice, SIGNAL(commandCompleted(bool)), this, SLOT(ledDeviceCommandCompleted(bool)), Qt::QueuedConnection);
connect(m_ledDevice, SIGNAL(ioDeviceSuccess(bool)), this, SLOT(ledDeviceIoDeviceSuccess(bool)), Qt::QueuedConnection);
connect(m_ledDevice, SIGNAL(openDeviceSuccess(bool)), this, SLOT(ledDeviceOpenDeviceSuccess(bool)), Qt::QueuedConnection);
connect(m_ledDevice, SIGNAL(openDeviceSuccess(bool)), this, SLOT(ledDeviceOpenDeviceSuccess(bool)), Qt::QueuedConnection);

connect(m_ledDevice, SIGNAL(firmwareVersion(QString)), this, SIGNAL(firmwareVersion(QString)), Qt::QueuedConnection);
connect(m_ledDevice, SIGNAL(firmwareVersionUnofficial(int)), this, SIGNAL(firmwareVersionUnofficial(int)), Qt::QueuedConnection);
Expand All @@ -505,6 +533,7 @@ void LedDeviceManager::connectSignalSlotsLedDevice()
connect(this, SIGNAL(ledDeviceSetSmoothSlowdown(int)), m_ledDevice, SLOT(setSmoothSlowdown(int)), Qt::QueuedConnection);
connect(this, SIGNAL(ledDeviceSetGamma(double)), m_ledDevice, SLOT(setGamma(double)), Qt::QueuedConnection);
connect(this, SIGNAL(ledDeviceSetBrightness(int)), m_ledDevice, SLOT(setBrightness(int)), Qt::QueuedConnection);
connect(this, SIGNAL(ledDeviceSetBrightnessCap(int)), m_ledDevice, SLOT(setBrightnessCap(int)), Qt::QueuedConnection);
connect(this, SIGNAL(ledDeviceSetColorSequence(QString)), m_ledDevice, SLOT(setColorSequence(QString)), Qt::QueuedConnection);
connect(this, SIGNAL(ledDeviceSetLuminosityThreshold(int)), m_ledDevice, SLOT(setLuminosityThreshold(int)), Qt::QueuedConnection);
connect(this, SIGNAL(ledDeviceSetMinimumLuminosityEnabled(bool)), m_ledDevice, SLOT(setMinimumLuminosityThresholdEnabled(bool)), Qt::QueuedConnection);
Expand Down Expand Up @@ -538,6 +567,7 @@ void LedDeviceManager::disconnectSignalSlotsLedDevice()
disconnect(this, SIGNAL(ledDeviceSetSmoothSlowdown(int)), m_ledDevice, SLOT(setSmoothSlowdown(int)));
disconnect(this, SIGNAL(ledDeviceSetGamma(double)), m_ledDevice, SLOT(setGamma(double)));
disconnect(this, SIGNAL(ledDeviceSetBrightness(int)), m_ledDevice, SLOT(setBrightness(int)));
disconnect(this, SIGNAL(ledDeviceSetBrightnessCap(int)), m_ledDevice, SLOT(setBrightnessCap(int)));
disconnect(this, SIGNAL(ledDeviceSetColorSequence(QString)), m_ledDevice, SLOT(setColorSequence(QString)));
disconnect(this, SIGNAL(ledDeviceSetLuminosityThreshold(int)), m_ledDevice, SLOT(setLuminosityThreshold(int)));
disconnect(this, SIGNAL(ledDeviceSetMinimumLuminosityEnabled(bool)), m_ledDevice, SLOT(setMinimumLuminosityThresholdEnabled(bool)));
Expand Down Expand Up @@ -610,6 +640,11 @@ void LedDeviceManager::cmdQueueProcessNext()
emit ledDeviceSetBrightness(m_savedBrightness);
break;

case LedDeviceCommands::SetBrightnessCap:
m_cmdTimeoutTimer->start();
emit ledDeviceSetBrightnessCap(m_savedBrightnessCap);
break;

case LedDeviceCommands::SetColorSequence:
m_cmdTimeoutTimer->start();
emit ledDeviceSetColorSequence(m_savedColorSequence);
Expand Down
3 changes: 3 additions & 0 deletions Software/src/LedDeviceManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class LedDeviceManager : public QObject
void ledDeviceSetSmoothSlowdown(int value);
void ledDeviceSetGamma(double value);
void ledDeviceSetBrightness(int value);
void ledDeviceSetBrightnessCap(int value);
void ledDeviceSetLuminosityThreshold(int value);
void ledDeviceSetMinimumLuminosityEnabled(bool);
void ledDeviceSetColorSequence(QString value);
Expand All @@ -82,6 +83,7 @@ public slots:
void setSmoothSlowdown(int value);
void setGamma(double value);
void setBrightness(int value);
void setBrightnessCap(int value);
void setLuminosityThreshold(int value);
void setMinimumLuminosityEnabled(bool value);
void setColorSequence(QString value);
Expand Down Expand Up @@ -119,6 +121,7 @@ private slots:
int m_savedSmoothSlowdown;
double m_savedGamma;
int m_savedBrightness;
int m_savedBrightnessCap;
int m_savedLuminosityThreshold;
bool m_savedIsMinimumLuminosityEnabled;
QString m_savedColorSequence;
Expand Down
3 changes: 2 additions & 1 deletion Software/src/LightpackApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,7 @@ void LightpackApplication::startLedDeviceManager()
connect(settings(), SIGNAL(deviceUsbPowerLedDisabledChanged(bool)), m_ledDeviceManager, SLOT(setUsbPowerLedDisabled(bool)), Qt::QueuedConnection);
connect(settings(), SIGNAL(deviceGammaChanged(double)), m_ledDeviceManager, SLOT(setGamma(double)), Qt::QueuedConnection);
connect(settings(), SIGNAL(deviceBrightnessChanged(int)), m_ledDeviceManager, SLOT(setBrightness(int)), Qt::QueuedConnection);
connect(settings(), SIGNAL(deviceBrightnessCapChanged(int)), m_ledDeviceManager, SLOT(setBrightnessCap(int)), Qt::QueuedConnection);
connect(settings(), SIGNAL(luminosityThresholdChanged(int)), m_ledDeviceManager, SLOT(setLuminosityThreshold(int)), Qt::QueuedConnection);
connect(settings(), SIGNAL(minimumLuminosityEnabledChanged(bool)), m_ledDeviceManager, SLOT(setMinimumLuminosityEnabled(bool)), Qt::QueuedConnection);
connect(settings(), SIGNAL(ledCoefBlueChanged(int,double)), m_ledDeviceManager, SLOT(updateWBAdjustments()), Qt::QueuedConnection);
Expand Down Expand Up @@ -709,7 +710,7 @@ void LightpackApplication::initGrabManager()
else
qWarning() << Q_FUNC_INFO << "No compatible Sound Manager";
#endif

connect(settings(), SIGNAL(grabberTypeChanged(const Grab::GrabberType &)), m_grabManager, SLOT(onGrabberTypeChanged(const Grab::GrabberType &)), Qt::QueuedConnection);
connect(settings(), SIGNAL(grabSlowdownChanged(int)), m_grabManager, SLOT(onGrabSlowdownChanged(int)), Qt::QueuedConnection);
connect(settings(), SIGNAL(grabAvgColorsEnabledChanged(bool)), m_grabManager, SLOT(onGrabAvgColorsEnabledChanged(bool)), Qt::QueuedConnection);
Expand Down
Loading