Skip to content

Commit

Permalink
Check if LEDs are disabled when returning the average color and other…
Browse files Browse the repository at this point in the history
… improvements
  • Loading branch information
awawa-dev committed Jul 30, 2023
1 parent 03b096f commit de2b4ba
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 14 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
- Use optional system libs for compiling #541 (v20 beta 🆕)
- Update mbedtls to 3.4.0 #589 (v20 beta 🆕)
- Add Ubuntu support to HyperHDR and Github Pages APT repository #522 (v20 beta 🆕)
- Add Ubuntu support to HyperHDR and Github Pages APT repository #522 (v20 beta 🆕)
- New JsonAPI method to calculate average color of selected instance #611 (v20 beta 🆕)
- Workaround for critical Rpi udev bug affecting serial ports #583 (v20 beta 🆕)
- Add Arch Linux support #520 (v20 beta 🆕)
- Fix chrome/edge fullscreen detection #519 (v20 beta 🆕)
Expand Down
1 change: 1 addition & 0 deletions include/utils/Logger.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <utils/InternalClock.h>
#include <utils/Macros.h>

// QT includes
#include <QObject>
Expand Down
123 changes: 123 additions & 0 deletions include/utils/Macros.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
#pragma once

/* Macros.h
*
* MIT License
*
* Copyright (c) 2023 awawa-dev
*
* Project homesite: https://github.com/awawa-dev/HyperHDR
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

inline void SAFE_CALL_TEST_FUN() {};

#define SAFE_CALL_0_RET(target, method, returnType, result, ...) \
{ \
SAFE_CALL_TEST_FUN(__VA_ARGS__); \
if (target->thread() != this->thread()) \
QMetaObject::invokeMethod(target, #method, Qt::BlockingQueuedConnection, Q_RETURN_ARG(returnType, result)); \
else \
result = target->method(); \
}

#define SAFE_CALL_1_RET(target, method, returnType, result, p1type, p1value, ...) \
{ \
SAFE_CALL_TEST_FUN(__VA_ARGS__); \
if (target->thread() != this->thread()) \
QMetaObject::invokeMethod(target, #method, Qt::BlockingQueuedConnection, Q_RETURN_ARG(returnType, result), Q_ARG(p1type, p1value)); \
else \
result = target->method(p1value); \
}

#define SAFE_CALL_2_RET(target, method, returnType, result, p1type, p1value, p2type, p2value, ...) \
{ \
SAFE_CALL_TEST_FUN(__VA_ARGS__); \
if (target->thread() != this->thread()) \
QMetaObject::invokeMethod(target, #method, Qt::BlockingQueuedConnection, Q_RETURN_ARG(returnType, result), Q_ARG(p1type, p1value), Q_ARG(p2type, p2value)); \
else \
result = target->method(p1value, p2value); \
}

#define SAFE_CALL_3_RET(target, method, returnType, result, p1type, p1value, p2type, p2value, p3type, p3value, ...) \
{ \
SAFE_CALL_TEST_FUN(__VA_ARGS__); \
if (target->thread() != this->thread()) \
QMetaObject::invokeMethod(target, #method, Qt::BlockingQueuedConnection, Q_RETURN_ARG(returnType, result), Q_ARG(p1type, p1value), Q_ARG(p2type, p2value), Q_ARG(p3type, p3value)); \
else \
result = target->method(p1value, p2value, p3value); \
}

#define SAFE_CALL_4_RET(target, method, returnType, result, p1type, p1value, p2type, p2value, p3type, p3value, p4type, p4value , ...) \
{ \
SAFE_CALL_TEST_FUN(__VA_ARGS__); \
if (target->thread() != this->thread()) \
QMetaObject::invokeMethod(target, #method, Qt::BlockingQueuedConnection, Q_RETURN_ARG(returnType, result), Q_ARG(p1type, p1value), Q_ARG(p2type, p2value), Q_ARG(p3type, p3value), Q_ARG(p4type, p4value)); \
else \
result = target->method(p1value, p2value, p3value, p4value); \
}

#define SAFE_CALL_0(target, method, ...) \
{ \
SAFE_CALL_TEST_FUN(__VA_ARGS__); \
if (true) \
QMetaObject::invokeMethod(target, #method, Qt::QueuedConnection); \
else \
target->method(); \
}

#define SAFE_CALL_1(target, method, p1type, p1value, ...) \
{ \
SAFE_CALL_TEST_FUN(__VA_ARGS__); \
if (true) \
QMetaObject::invokeMethod(target, #method, Qt::QueuedConnection, Q_ARG(p1type, p1value)); \
else \
target->method(p1value); \
}

#define SAFE_CALL_2(target, method, p1type, p1value, p2type, p2value, ...) \
{ \
SAFE_CALL_TEST_FUN(__VA_ARGS__); \
if (true) \
QMetaObject::invokeMethod(target, #method, Qt::QueuedConnection, Q_ARG(p1type, p1value), Q_ARG(p2type, p2value)); \
else \
target->method(p1value, p2value); \
}

#define SAFE_CALL_3(target, method, p1type, p1value, p2type, p2value, p3type, p3value, ...) \
{ \
SAFE_CALL_TEST_FUN(__VA_ARGS__); \
if (true) \
QMetaObject::invokeMethod(target, #method, Qt::QueuedConnection, Q_ARG(p1type, p1value), Q_ARG(p2type, p2value), Q_ARG(p3type, p3value)); \
else \
target->method(p1value, p2value, p3value); \
}

#define SAFE_CALL_4(target, method, p1type, p1value, p2type, p2value, p3type, p3value, p4type, p4value , ...) \
{ \
SAFE_CALL_TEST_FUN(__VA_ARGS__); \
if (true) \
QMetaObject::invokeMethod(target, #method, Qt::QueuedConnection, Q_ARG(p1type, p1value), Q_ARG(p2type, p2value), Q_ARG(p3type, p3value), Q_ARG(p4type, p4value)); \
else \
target->method(p1value, p2value, p3value, p4value); \
}



12 changes: 4 additions & 8 deletions sources/api/API.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,26 +337,22 @@ QVector<QVariantMap> API::getAllInstanceData()
bool API::startInstance(quint8 index, int tan)
{
bool res;
(_instanceManager->thread() != this->thread())
? QMetaObject::invokeMethod(_instanceManager, "startInstance", Qt::BlockingQueuedConnection, Q_RETURN_ARG(bool, res), Q_ARG(quint8, index), Q_ARG(bool, false), Q_ARG(QObject*, this), Q_ARG(int, tan))
: res = _instanceManager->startInstance(index, false, this, tan);

SAFE_CALL_4_RET(_instanceManager, startInstance, bool, res, quint8, index, bool, false, QObject*, this, int, tan);

return res;
}

void API::stopInstance(quint8 index)
{
QMetaObject::invokeMethod(_instanceManager, "stopInstance", Qt::QueuedConnection, Q_ARG(quint8, index));
SAFE_CALL_1(_instanceManager, stopInstance, quint8, index);
}

QJsonObject API::getAverageColor(quint8 index)
{
QJsonObject res;

if (_instanceManager->thread() != this->thread())
QMetaObject::invokeMethod(_instanceManager, "getAverageColor", Qt::BlockingQueuedConnection, Q_RETURN_ARG(QJsonObject, res), Q_ARG(quint8, index));
else
res = _instanceManager->getAverageColor(index);
SAFE_CALL_1_RET(_instanceManager, getAverageColor, QJsonObject, res, quint8, index);

return res;
}
Expand Down
5 changes: 1 addition & 4 deletions sources/base/HyperHdrIManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,7 @@ QJsonObject HyperHdrIManager::getAverageColor(quint8 index)
HyperHdrInstance* instance = HyperHdrIManager::getHyperHdrInstance(index);
QJsonObject res;

if (instance->thread() != this->thread())
QMetaObject::invokeMethod(instance, "getAverageColor", Qt::BlockingQueuedConnection, Q_RETURN_ARG(QJsonObject, res));
else
res = instance->getAverageColor();
SAFE_CALL_0_RET(instance, getAverageColor, QJsonObject, res);

return res;
}
Expand Down
5 changes: 5 additions & 0 deletions sources/base/HyperHdrInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,11 @@ QJsonObject HyperHdrInstance::getAverageColor()
count++;
}

if (!_ledDeviceWrapper->enabled())
{
red = green = blue = 0;
}

if (count > 0)
{
ret["red"] = static_cast<int>(red / count);
Expand Down
3 changes: 3 additions & 0 deletions sources/leddevice/dev_rpi_pwm/LedDeviceWS281x.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ bool LedDeviceWS281x::init(const QJsonObject& deviceConfig)

Debug(_log, "ws281x strip type : %d", _led_string.channel[_channel].strip_type);

if (_refreshTimerInterval_ms > 0)
Error(_log, "The refresh timer is enabled ('Refresh time' > 0) and may limit the performance of the LED driver. Ignore this error if you set it on purpose for some reason (but you almost never need it).");

isInitOK = true;
}
}
Expand Down
3 changes: 3 additions & 0 deletions sources/leddevice/dev_serial/ProviderRs232.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ bool ProviderRs232::init(const QJsonObject& deviceConfig)
Debug(_log, "Delayed open : %d", _delayAfterConnect_ms);
Debug(_log, "Retry limit : %d", _maxRetry);

if (_refreshTimerInterval_ms > 0)
Error(_log, "The refresh timer is enabled ('Refresh time' > 0) and may limit the performance of the LED driver. Ignore this error if you set it on purpose for some reason (but you almost never need it).");

isInitOK = true;
}
return isInitOK;
Expand Down
3 changes: 3 additions & 0 deletions sources/leddevice/dev_spi/ProviderSpi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ bool ProviderSpi::init(const QJsonObject& deviceConfig)
Debug(_log, "_baudRate_Hz [%d], _spiType: %s", _baudRate_Hz, QSTRING_CSTR(_spiType));
Debug(_log, "_spiDataInvert [%d], _spiMode [%d]", _spiDataInvert, _spiMode);

if (_refreshTimerInterval_ms > 0)
Error(_log, "The refresh timer is enabled ('Refresh time' > 0) and may limit the performance of the LED driver. Ignore this error if you set it on purpose for some reason (but you almost never need it).");

isInitOK = true;
}
return isInitOK;
Expand Down
2 changes: 1 addition & 1 deletion sources/webserver/WebSocketClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ qint64 WebSocketClient::sendMessage(QJsonObject obj)

if (obj.contains("isImage"))
{
QTimer::singleShot(0, _jsonAPI, &JsonAPI::releaseLock);
SAFE_CALL_0(_jsonAPI, releaseLock);
}

return payloadWritten;
Expand Down

0 comments on commit de2b4ba

Please sign in to comment.