Merge branch 'hyperion-project:master' into temperture

This commit is contained in:
LordGrey
2024-09-01 17:45:44 +02:00
committed by GitHub
57 changed files with 2267 additions and 203 deletions

View File

@@ -745,7 +745,7 @@ void JsonAPI::handleConfigSetCommand(const QJsonObject &message, const JsonApiCo
}
else
{
sendErrorReply("Saving configuration while Hyperion is disabled isn't possible", cmd);
sendErrorReply("It is not possible saving a configuration while Hyperion is disabled", cmd);
}
}
}

View File

@@ -174,7 +174,7 @@ int DDAGrabber::grabFrame(Image<ColorRgb> &image)
// Acquire the next frame.
CComPtr<IDXGIResource> desktopResource;
DXGI_OUTDUPL_FRAME_INFO frameInfo;
hr = d->desktopDuplication->AcquireNextFrame(INFINITE, &frameInfo, &desktopResource);
hr = d->desktopDuplication->AcquireNextFrame(500, &frameInfo, &desktopResource);
if (hr == DXGI_ERROR_ACCESS_LOST || hr == DXGI_ERROR_INVALID_CALL)
{
if (!restartCapture())
@@ -185,7 +185,7 @@ int DDAGrabber::grabFrame(Image<ColorRgb> &image)
}
if (hr == DXGI_ERROR_WAIT_TIMEOUT)
{
// This shouldn't happen since we specified an INFINITE timeout.
// Nothing changed on the screen in the 500ms we waited.
return 0;
}
RETURN_IF_ERROR(hr, "Failed to acquire next frame", 0);

View File

@@ -131,7 +131,7 @@ void EncoderThread::process()
#if defined(ENABLE_V4L2)
_pixelFormat,
#else
PixelFormat::BGR24,
PixelFormat::BGR24, // MF-Grabber always sends RGB24, but memory layout is RGBTRIPLE (b,g,r) -> process as BGR24
#endif
image
);

View File

@@ -363,6 +363,18 @@ done:
_height = props.height;
_frameByteSize = _width * _height * 3;
_lineLength = _width * 3;
// adjust flipMode for bottom-up images
if (props.defstride < 0)
{
if (_flipMode == FlipMode::NO_CHANGE)
_flipMode = FlipMode::HORIZONTAL;
else if (_flipMode == FlipMode::HORIZONTAL)
_flipMode = FlipMode::NO_CHANGE;
else if (_flipMode == FlipMode::VERTICAL)
_flipMode = FlipMode::BOTH;
else if (_flipMode == FlipMode::BOTH)
_flipMode = FlipMode::VERTICAL;
}
}
// Cleanup
@@ -436,6 +448,14 @@ void MFGrabber::enumVideoCaptureDevices()
properties.denominator = denominator;
properties.pf = pixelformat;
properties.guid = format;
HRESULT hr = pType->GetUINT32(MF_MT_DEFAULT_STRIDE, (UINT32*)&properties.defstride);
if (FAILED(hr))
{
hr = MFGetStrideForBitmapInfoHeader(format.Data1, width, &properties.defstride);
if (FAILED(hr))
DebugIf (verbose, _log, "failed to get default stride");
}
devicePropertyList.append(properties);
DebugIf (verbose, _log, "%s %d x %d @ %d fps (%s)", QSTRING_CSTR(dev), properties.width, properties.height, properties.fps, QSTRING_CSTR(pixelFormatToString(properties.pf)));
@@ -797,7 +817,7 @@ QJsonArray MFGrabber::discover(const QJsonObject& params)
resolution_default["width"] = 640;
resolution_default["height"] = 480;
resolution_default["fps"] = 25;
format_default["format"] = "bgr24";
format_default["format"] = "rgb24";
format_default["resolution"] = resolution_default;
video_inputs_default["inputIdx"] = 0;
video_inputs_default["standards"] = "PAL";

View File

@@ -27,7 +27,7 @@
static PixelFormat GetPixelFormatForGuid(const GUID guid)
{
if (IsEqualGUID(guid, MFVideoFormat_RGB32)) return PixelFormat::RGB32;
if (IsEqualGUID(guid, MFVideoFormat_RGB24)) return PixelFormat::BGR24;
if (IsEqualGUID(guid, MFVideoFormat_RGB24)) return PixelFormat::RGB24;
if (IsEqualGUID(guid, MFVideoFormat_YUY2)) return PixelFormat::YUYV;
if (IsEqualGUID(guid, MFVideoFormat_UYVY)) return PixelFormat::UYVY;
#ifdef HAVE_TURBO_JPEG
@@ -145,11 +145,11 @@ public:
}
#ifdef HAVE_TURBO_JPEG
if (_pixelformat != PixelFormat::MJPEG && _pixelformat != PixelFormat::BGR24 && _pixelformat != PixelFormat::NO_CHANGE)
if (_pixelformat != PixelFormat::MJPEG && _pixelformat != PixelFormat::RGB24 && _pixelformat != PixelFormat::NO_CHANGE)
#else
if (_pixelformat != PixelFormat::BGR24 && _pixelformat != PixelFormat::NO_CHANGE)
if (_pixelformat != PixelFormat::RGB24 && _pixelformat != PixelFormat::NO_CHANGE)
#endif
pSample = TransformSample(_transform, pSample);
pSample = TransformSample(_transform, pSample); // forced conversion to RGB24, but memory layout is RGBTRIPLE (b,g,r) -> process as BGR24
_hrStatus = pSample->ConvertToContiguousBuffer(&buffer);
if (FAILED(_hrStatus))
@@ -181,9 +181,9 @@ public:
_bEOS = TRUE; // Reached the end of the stream.
#ifdef HAVE_TURBO_JPEG
if (_pixelformat != PixelFormat::MJPEG && _pixelformat != PixelFormat::BGR24 && _pixelformat != PixelFormat::NO_CHANGE)
if (_pixelformat != PixelFormat::MJPEG && _pixelformat != PixelFormat::RGB24 && _pixelformat != PixelFormat::NO_CHANGE)
#else
if (_pixelformat != PixelFormat::BGR24 && _pixelformat != PixelFormat::NO_CHANGE)
if (_pixelformat != PixelFormat::RGB24 && _pixelformat != PixelFormat::NO_CHANGE)
#endif
SAFE_RELEASE(pSample);
@@ -196,9 +196,9 @@ public:
{
_pixelformat = format;
#ifdef HAVE_TURBO_JPEG
if (format == PixelFormat::MJPEG || format == PixelFormat::BGR24 || format == PixelFormat::NO_CHANGE)
if (format == PixelFormat::MJPEG || format == PixelFormat::RGB24 || format == PixelFormat::NO_CHANGE)
#else
if (format == PixelFormat::BGR24 || format == PixelFormat::NO_CHANGE)
if (format == PixelFormat::RGB24 || format == PixelFormat::NO_CHANGE)
#endif
return S_OK;
@@ -392,10 +392,10 @@ private:
private:
long _nRefCount;
CRITICAL_SECTION _critsec;
MFGrabber* _grabber;
MFGrabber* _grabber;
BOOL _bEOS;
HRESULT _hrStatus;
IMFTransform* _transform;
IMFTransform* _transform;
PixelFormat _pixelformat;
std::atomic<bool> _isBusy;
};

View File

@@ -54,7 +54,9 @@ Q_GLOBAL_STATIC_WITH_ARGS(ControlIDPropertyMap, _controlIDPropertyMap, (initCont
static PixelFormat GetPixelFormat(const unsigned int format)
{
if (format == V4L2_PIX_FMT_RGB32) return PixelFormat::RGB32;
if (format == V4L2_PIX_FMT_RGB24) return PixelFormat::BGR24;
if (format == V4L2_PIX_FMT_BGR32) return PixelFormat::BGR32;
if (format == V4L2_PIX_FMT_RGB24) return PixelFormat::RGB24;
if (format == V4L2_PIX_FMT_BGR24) return PixelFormat::BGR24;
if (format == V4L2_PIX_FMT_YUYV) return PixelFormat::YUYV;
if (format == V4L2_PIX_FMT_UYVY) return PixelFormat::UYVY;
if (format == V4L2_PIX_FMT_NV12) return PixelFormat::NV12;
@@ -557,10 +559,18 @@ void V4L2Grabber::init_device(VideoStandard videoStandard)
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_RGB32;
break;
case PixelFormat::BGR24:
case PixelFormat::BGR32:
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_BGR32;
break;
case PixelFormat::RGB24:
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_RGB24;
break;
case PixelFormat::BGR24:
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_BGR24;
break;
case PixelFormat::YUYV:
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
break;
@@ -691,7 +701,23 @@ void V4L2Grabber::init_device(VideoStandard videoStandard)
}
break;
case V4L2_PIX_FMT_BGR32:
{
_pixelFormat = PixelFormat::BGR32;
_frameByteSize = _width * _height * 4;
Debug(_log, "Pixel format=BGR32");
}
break;
case V4L2_PIX_FMT_RGB24:
{
_pixelFormat = PixelFormat::RGB24;
_frameByteSize = _width * _height * 3;
Debug(_log, "Pixel format=RGB24");
}
break;
case V4L2_PIX_FMT_BGR24:
{
_pixelFormat = PixelFormat::BGR24;
_frameByteSize = _width * _height * 3;
@@ -699,7 +725,6 @@ void V4L2Grabber::init_device(VideoStandard videoStandard)
}
break;
case V4L2_PIX_FMT_YUYV:
{
_pixelFormat = PixelFormat::YUYV;
@@ -743,9 +768,9 @@ void V4L2Grabber::init_device(VideoStandard videoStandard)
default:
#ifdef HAVE_TURBO_JPEG
throw_exception("Only pixel formats RGB32, BGR24, YUYV, UYVY, NV12, I420 and MJPEG are supported");
throw_exception("Only pixel formats RGB32, BGR32, RGB24, BGR24, YUYV, UYVY, NV12, I420 and MJPEG are supported");
#else
throw_exception("Only pixel formats RGB32, BGR24, YUYV, UYVY, NV12 and I420 are supported");
throw_exception("Only pixel formats RGB32, BGR32, RGB24, BGR24, YUYV, UYVY, NV12 and I420 are supported");
#endif
return;
}

View File

@@ -19,6 +19,7 @@ include_directories(
dev_spi
dev_rpi_pwm
dev_tinker
dev_ftdi
)
file (GLOB Leddevice_SOURCES
@@ -63,7 +64,11 @@ if(ENABLE_DEV_WS281XPWM)
file (GLOB Leddevice_PWM_SOURCES "${CURRENT_SOURCE_DIR}/dev_rpi_pwm/*.h" "${CURRENT_SOURCE_DIR}/dev_rpi_pwm/*.cpp")
endif()
set(LedDevice_RESOURCES ${CURRENT_SOURCE_DIR}/LedDeviceSchemas.qrc)
if (ENABLE_DEV_FTDI)
FILE ( GLOB Leddevice_FTDI_SOURCES "${CURRENT_SOURCE_DIR}/dev_ftdi/*.h" "${CURRENT_SOURCE_DIR}/dev_ftdi/*.cpp")
endif()
set(LedDevice_RESOURCES ${CURRENT_SOURCE_DIR}/LedDeviceSchemas.qrc )
set(Leddevice_SOURCES
${Leddevice_SOURCES}
@@ -74,6 +79,7 @@ set(Leddevice_SOURCES
${Leddevice_SPI_SOURCES}
${Leddevice_TINKER_SOURCES}
${Leddevice_USB_HID_SOURCES}
${Leddevice_FTDI_SOURCES}
)
# auto generate header file that include all available leddevice headers
@@ -165,3 +171,10 @@ if(ENABLE_MDNS)
target_link_libraries(leddevice mdns)
endif()
if( ENABLE_DEV_FTDI )
find_package(PkgConfig REQUIRED)
pkg_check_modules(LIB_FTDI REQUIRED IMPORTED_TARGET libftdi1 )
target_include_directories(leddevice PRIVATE PkgConfig::LIB_FTDI)
target_link_libraries(leddevice PkgConfig::LIB_FTDI)
endif()

View File

@@ -7,6 +7,7 @@
<file alias="schema-dmx">schemas/schema-dmx.json</file>
<file alias="schema-fadecandy">schemas/schema-fadecandy.json</file>
<file alias="schema-file">schemas/schema-file.json</file>
<file alias="schema-homeassistant">schemas/schema-homeassistant.json</file>
<file alias="schema-hyperionusbasp">schemas/schema-hyperionusbasp.json</file>
<file alias="schema-lightpack">schemas/schema-lightpack.json</file>
<file alias="schema-lpd6803">schemas/schema-lpd6803.json</file>
@@ -38,5 +39,8 @@
<file alias="schema-yeelight">schemas/schema-yeelight.json</file>
<file alias="schema-razer">schemas/schema-razer.json</file>
<file alias="schema-cololight">schemas/schema-cololight.json</file>
<file alias="schema-ws2812_ftdi">schemas/schema-ws2812_ftdi.json</file>
<file alias="schema-apa102_ftdi">schemas/schema-apa102_ftdi.json</file>
<file alias="schema-sk6812_ftdi">schemas/schema-sk6812_ftdi.json</file>
</qresource>
</RCC>

View File

@@ -0,0 +1,52 @@
#include "LedDeviceAPA102_ftdi.h"
#define LED_HEADER 0b11100000
#define LED_BRIGHTNESS_FULL 31
LedDeviceAPA102_ftdi::LedDeviceAPA102_ftdi(const QJsonObject &deviceConfig) : ProviderFtdi(deviceConfig)
{
}
LedDevice *LedDeviceAPA102_ftdi::construct(const QJsonObject &deviceConfig)
{
return new LedDeviceAPA102_ftdi(deviceConfig);
}
bool LedDeviceAPA102_ftdi::init(const QJsonObject &deviceConfig)
{
bool isInitOK = false;
// Initialise sub-class
if (ProviderFtdi::init(deviceConfig))
{
_brightnessControlMaxLevel = deviceConfig["brightnessControlMaxLevel"].toInt(LED_BRIGHTNESS_FULL);
Info(_log, "[%s] Setting maximum brightness to [%d] = %d%%", QSTRING_CSTR(_activeDeviceType), _brightnessControlMaxLevel, _brightnessControlMaxLevel * 100 / LED_BRIGHTNESS_FULL);
CreateHeader();
isInitOK = true;
}
return isInitOK;
}
void LedDeviceAPA102_ftdi::CreateHeader()
{
const unsigned int startFrameSize = 4;
// Endframe, add additional 4 bytes to cover SK9922 Reset frame (in case SK9922 were sold as AP102) - has no effect on APA102
const unsigned int endFrameSize = (_ledCount / 32) * 4 + 4;
const unsigned int APAbufferSize = (_ledCount * 4) + startFrameSize + endFrameSize;
_ledBuffer.resize(APAbufferSize, 0);
Debug(_log, "APA102 buffer created for %d LEDs", _ledCount);
}
int LedDeviceAPA102_ftdi::write(const std::vector<ColorRgb> &ledValues)
{
for (signed iLed = 0; iLed < static_cast<int>(_ledCount); ++iLed)
{
const ColorRgb &rgb = ledValues[iLed];
_ledBuffer[4 + iLed * 4 + 0] = LED_HEADER | _brightnessControlMaxLevel;
_ledBuffer[4 + iLed * 4 + 1] = rgb.red;
_ledBuffer[4 + iLed * 4 + 2] = rgb.green;
_ledBuffer[4 + iLed * 4 + 3] = rgb.blue;
}
return writeBytes(_ledBuffer.size(), _ledBuffer.data());
}

View File

@@ -0,0 +1,50 @@
#ifndef LEDEVICET_APA102_H
#define LEDEVICET_APA102_H
#include "ProviderFtdi.h"
class LedDeviceAPA102_ftdi : public ProviderFtdi
{
Q_OBJECT
public:
///
/// @brief Constructs an APA102 LED-device
///
/// @param deviceConfig Device's configuration as JSON-Object
///
explicit LedDeviceAPA102_ftdi(const QJsonObject& deviceConfig);
///
/// @brief Constructs the LED-device
///
/// @param[in] deviceConfig Device's configuration as JSON-Object
/// @return LedDevice constructed
static LedDevice* construct(const QJsonObject& deviceConfig);
private:
///
/// @brief Initialise the device's configuration
///
/// @param[in] deviceConfig the JSON device configuration
/// @return True, if success
///
bool init(const QJsonObject& deviceConfig) override;
void CreateHeader();
///
/// @brief Writes the RGB-Color values to the LEDs.
///
/// @param[in] ledValues The RGB-color per LED
/// @return Zero on success, else negative
///
int write(const std::vector<ColorRgb>& ledValues) override;
/// The brighness level. Possibile values 1 .. 31.
int _brightnessControlMaxLevel;
};
#endif // LEDEVICET_APA102_H

View File

@@ -0,0 +1,96 @@
#include "LedDeviceSk6812_ftdi.h"
LedDeviceSk6812_ftdi::LedDeviceSk6812_ftdi(const QJsonObject &deviceConfig)
: ProviderFtdi(deviceConfig),
_whiteAlgorithm(RGBW::WhiteAlgorithm::INVALID),
SPI_BYTES_PER_COLOUR(4),
bitpair_to_byte{
0b10001000,
0b10001100,
0b11001000,
0b11001100}
{
}
LedDevice *LedDeviceSk6812_ftdi::construct(const QJsonObject &deviceConfig)
{
return new LedDeviceSk6812_ftdi(deviceConfig);
}
bool LedDeviceSk6812_ftdi::init(const QJsonObject &deviceConfig)
{
bool isInitOK = false;
// Initialise sub-class
if (ProviderFtdi::init(deviceConfig))
{
_brightnessControlMaxLevel = deviceConfig["brightnessControlMaxLevel"].toInt(255);
Info(_log, "[%s] Setting maximum brightness to [%d]", QSTRING_CSTR(_activeDeviceType), _brightnessControlMaxLevel);
QString whiteAlgorithm = deviceConfig["whiteAlgorithm"].toString("white_off");
_whiteAlgorithm = RGBW::stringToWhiteAlgorithm(whiteAlgorithm);
if (_whiteAlgorithm == RGBW::WhiteAlgorithm::INVALID)
{
QString errortext = QString ("unknown whiteAlgorithm: %1").arg(whiteAlgorithm);
this->setInError(errortext);
isInitOK = false;
}
else
{
Debug(_log, "whiteAlgorithm : %s", QSTRING_CSTR(whiteAlgorithm));
WarningIf((_baudRate_Hz < 2050000 || _baudRate_Hz > 3750000), _log, "Baud rate %d outside recommended range (2050000 -> 3750000)", _baudRate_Hz);
const int SPI_FRAME_END_LATCH_BYTES = 3;
_ledBuffer.resize(_ledRGBWCount * SPI_BYTES_PER_COLOUR + SPI_FRAME_END_LATCH_BYTES, 0x00);
isInitOK = true;
}
}
return isInitOK;
}
inline __attribute__((always_inline)) uint8_t LedDeviceSk6812_ftdi::scale(uint8_t i, uint8_t scale) {
return (((uint16_t)i) * (1+(uint16_t)(scale))) >> 8;
}
int LedDeviceSk6812_ftdi::write(const std::vector<ColorRgb> &ledValues)
{
unsigned spi_ptr = 0;
const int SPI_BYTES_PER_LED = sizeof(ColorRgbw) * SPI_BYTES_PER_COLOUR;
ColorRgbw temp_rgbw;
ColorRgb scaled_color;
for (const ColorRgb &color : ledValues)
{
scaled_color.red = scale(color.red, _brightnessControlMaxLevel);
scaled_color.green = scale(color.green, _brightnessControlMaxLevel);
scaled_color.blue = scale(color.blue, _brightnessControlMaxLevel);
RGBW::Rgb_to_Rgbw(scaled_color, &temp_rgbw, _whiteAlgorithm);
uint32_t colorBits =
((uint32_t)temp_rgbw.red << 24) +
((uint32_t)temp_rgbw.green << 16) +
((uint32_t)temp_rgbw.blue << 8) +
temp_rgbw.white;
for (int j = SPI_BYTES_PER_LED - 1; j >= 0; j--)
{
_ledBuffer[spi_ptr + j] = bitpair_to_byte[colorBits & 0x3];
colorBits >>= 2;
}
spi_ptr += SPI_BYTES_PER_LED;
}
_ledBuffer[spi_ptr++] = 0;
_ledBuffer[spi_ptr++] = 0;
_ledBuffer[spi_ptr++] = 0;
return writeBytes(_ledBuffer.size(), _ledBuffer.data());
}

View File

@@ -0,0 +1,52 @@
#ifndef LEDEVICESK6812ftdi_H
#define LEDEVICESK6812ftdi_H
#include "ProviderFtdi.h"
class LedDeviceSk6812_ftdi : public ProviderFtdi
{
public:
///
/// @brief Constructs a Sk6801 LED-device
///
/// @param deviceConfig Device's configuration as JSON-Object
///
explicit LedDeviceSk6812_ftdi(const QJsonObject& deviceConfig);
///
/// @brief Constructs the LED-device
///
/// @param[in] deviceConfig Device's configuration as JSON-Object
/// @return LedDevice constructed
static LedDevice* construct(const QJsonObject& deviceConfig);
private:
///
/// @brief Initialise the device's configuration
///
/// @param[in] deviceConfig the JSON device configuration
/// @return True, if success
///
bool init(const QJsonObject& deviceConfig) override;
///
/// @brief Writes the RGB-Color values to the LEDs.
///
/// @param[in] ledValues The RGB-color per LED
/// @return Zero on success, else negative
///
int write(const std::vector<ColorRgb>& ledValues) override;
inline __attribute__((always_inline)) uint8_t scale(uint8_t i, uint8_t scale);
RGBW::WhiteAlgorithm _whiteAlgorithm;
const int SPI_BYTES_PER_COLOUR;
uint8_t bitpair_to_byte[4];
int _brightnessControlMaxLevel;
};
#endif // LEDEVICESK6812ftdi_H

View File

@@ -0,0 +1,93 @@
#include "LedDeviceWs2812_ftdi.h"
/*
From the data sheet:
(TH+TL=1.25μs±600ns)
T0H, 0 code, high level time, 0.40µs ±0.150ns
T0L, 0 code, low level time, 0.85µs ±0.150ns
T1H, 1 code, high level time, 0.80µs ±0.150ns
T1L, 1 code, low level time, 0.45µs ±0.150ns
WT, Wait for the processing time, NA
Trst, Reset code,low level time, 50µs (not anymore... need 300uS for latest revision)
To normalise the pulse times so they fit in 4 SPI bits:
On the assumption that the "low" time doesnt matter much
A SPI bit time of 0.40uS = 2.5 Mbit/sec
T0 is sent as 1000
T1 is sent as 1100
With a bit of excel testing, we can work out the maximum and minimum speeds:
2106000 MIN
2590500 AVG
3075000 MAX
Wait time:
Not Applicable for WS2812
Reset time:
using the max of 3075000, the bit time is 0.325
Reset time is 300uS = 923 bits = 116 bytes
*/
LedDeviceWs2812_ftdi::LedDeviceWs2812_ftdi(const QJsonObject &deviceConfig)
: ProviderFtdi(deviceConfig),
SPI_BYTES_PER_COLOUR(4),
SPI_FRAME_END_LATCH_BYTES(116),
bitpair_to_byte{
0b10001000,
0b10001100,
0b11001000,
0b11001100,
}
{
}
LedDevice *LedDeviceWs2812_ftdi::construct(const QJsonObject &deviceConfig)
{
return new LedDeviceWs2812_ftdi(deviceConfig);
}
bool LedDeviceWs2812_ftdi::init(const QJsonObject &deviceConfig)
{
bool isInitOK = false;
// Initialise sub-class
if (ProviderFtdi::init(deviceConfig))
{
WarningIf((_baudRate_Hz < 2106000 || _baudRate_Hz > 3075000), _log, "Baud rate %d outside recommended range (2106000 -> 3075000)", _baudRate_Hz);
_ledBuffer.resize(_ledRGBCount * SPI_BYTES_PER_COLOUR + SPI_FRAME_END_LATCH_BYTES, 0x00);
isInitOK = true;
}
return isInitOK;
}
int LedDeviceWs2812_ftdi::write(const std::vector<ColorRgb> &ledValues)
{
unsigned spi_ptr = 0;
const int SPI_BYTES_PER_LED = sizeof(ColorRgb) * SPI_BYTES_PER_COLOUR;
for (const ColorRgb &color : ledValues)
{
uint32_t colorBits = ((unsigned int)color.red << 16) | ((unsigned int)color.green << 8) | color.blue;
for (int j = SPI_BYTES_PER_LED - 1; j >= 0; j--)
{
_ledBuffer[spi_ptr + j] = bitpair_to_byte[colorBits & 0x3];
colorBits >>= 2;
}
spi_ptr += SPI_BYTES_PER_LED;
}
for (int j = 0; j < SPI_FRAME_END_LATCH_BYTES; j++)
{
_ledBuffer[spi_ptr++] = 0;
}
return writeBytes(_ledBuffer.size(), _ledBuffer.data());
}

View File

@@ -0,0 +1,49 @@
#ifndef LEDEVICEWS2812_ftdi_H
#define LEDEVICEWS2812_ftdi_H
#include "ProviderFtdi.h"
class LedDeviceWs2812_ftdi : public ProviderFtdi
{
public:
///
/// @brief Constructs a Ws2812 LED-device
///
/// @param deviceConfig Device's configuration as JSON-Object
///
explicit LedDeviceWs2812_ftdi(const QJsonObject& deviceConfig);
///
/// @brief Constructs the LED-device
///
/// @param[in] deviceConfig Device's configuration as JSON-Object
/// @return LedDevice constructed
static LedDevice* construct(const QJsonObject& deviceConfig);
private:
///
/// @brief Initialise the device's configuration
///
/// @param[in] deviceConfig the JSON device configuration
/// @return True, if success
///
bool init(const QJsonObject& deviceConfig) override;
///
/// @brief Writes the RGB-Color values to the LEDs.
///
/// @param[in] ledValues The RGB-color per LED
/// @return Zero on success, else negative
///
int write(const std::vector<ColorRgb>& ledValues) override;
const int SPI_BYTES_PER_COLOUR;
const int SPI_FRAME_END_LATCH_BYTES;
uint8_t bitpair_to_byte[4];
};
#endif // LEDEVICEWS2812_ftdi_H

View File

@@ -0,0 +1,208 @@
// LedDevice includes
#include <leddevice/LedDevice.h>
#include "ProviderFtdi.h"
#include <utils/WaitTime.h>
#include <ftdi.h>
#include <libusb.h>
#define ANY_FTDI_VENDOR 0x0
#define ANY_FTDI_PRODUCT 0x0
#define FTDI_CHECK_RESULT(statement) if (statement) {setInError(ftdi_get_error_string(_ftdic)); return rc;}
namespace Pin
{
// enumerate the AD bus for convenience.
enum bus_t
{
SK = 0x01, // ADBUS0, SPI data clock
DO = 0x02, // ADBUS1, SPI data out
CS = 0x08, // ADBUS3, SPI chip select, active low
};
}
const uint8_t pinInitialState = Pin::CS;
// Use these pins as outputs
const uint8_t pinDirection = Pin::SK | Pin::DO | Pin::CS;
const QString ProviderFtdi::AUTO_SETTING = QString("auto");
ProviderFtdi::ProviderFtdi(const QJsonObject &deviceConfig)
: LedDevice(deviceConfig),
_ftdic(nullptr),
_baudRate_Hz(1000000)
{
}
bool ProviderFtdi::init(const QJsonObject &deviceConfig)
{
bool isInitOK = false;
if (LedDevice::init(deviceConfig))
{
_baudRate_Hz = deviceConfig["rate"].toInt(_baudRate_Hz);
_deviceName = deviceConfig["output"].toString(AUTO_SETTING);
Debug(_log, "_baudRate_Hz [%d]", _baudRate_Hz);
Debug(_log, "_deviceName [%s]", QSTRING_CSTR(_deviceName));
isInitOK = true;
}
return isInitOK;
}
int ProviderFtdi::open()
{
int rc = 0;
_ftdic = ftdi_new();
if (ftdi_init(_ftdic) < 0)
{
_ftdic = nullptr;
setInError("Could not initialize the ftdi library");
return -1;
}
Debug(_log, "Opening FTDI device=%s", QSTRING_CSTR(_deviceName));
FTDI_CHECK_RESULT((rc = ftdi_usb_open_string(_ftdic, QSTRING_CSTR(_deviceName))) < 0);
/* doing this disable resets things if they were in a bad state */
FTDI_CHECK_RESULT((rc = ftdi_disable_bitbang(_ftdic)) < 0);
FTDI_CHECK_RESULT((rc = ftdi_setflowctrl(_ftdic, SIO_DISABLE_FLOW_CTRL)) < 0);
FTDI_CHECK_RESULT((rc = ftdi_set_bitmode(_ftdic, 0x00, BITMODE_RESET)) < 0);
FTDI_CHECK_RESULT((rc = ftdi_set_bitmode(_ftdic, 0xff, BITMODE_MPSSE)) < 0);
double reference_clock = 60e6;
int divisor = (reference_clock / 2 / _baudRate_Hz) - 1;
std::vector<uint8_t> buf = {
DIS_DIV_5,
TCK_DIVISOR,
static_cast<unsigned char>(divisor),
static_cast<unsigned char>(divisor >> 8),
SET_BITS_LOW, // opcode: set low bits (ADBUS[0-7]
pinInitialState, // argument: inital pin state
pinDirection
};
FTDI_CHECK_RESULT((rc = ftdi_write_data(_ftdic, buf.data(), buf.size())) != buf.size());
_isDeviceReady = true;
return rc;
}
int ProviderFtdi::close()
{
LedDevice::close();
if (_ftdic != nullptr) {
Debug(_log, "Closing FTDI device");
// Delay to give time to push color black from writeBlack() into the led,
// otherwise frame transmission will be terminated half way through
wait(30);
ftdi_set_bitmode(_ftdic, 0x00, BITMODE_RESET);
ftdi_usb_close(_ftdic);
ftdi_free(_ftdic);
_ftdic = nullptr;
}
return 0;
}
void ProviderFtdi::setInError(const QString &errorMsg, bool isRecoverable)
{
close();
LedDevice::setInError(errorMsg, isRecoverable);
}
int ProviderFtdi::writeBytes(const qint64 size, const uint8_t *data)
{
int rc;
int count_arg = size - 1;
std::vector<uint8_t> buf = {
SET_BITS_LOW,
pinInitialState & ~Pin::CS,
pinDirection,
MPSSE_DO_WRITE | MPSSE_WRITE_NEG,
static_cast<unsigned char>(count_arg),
static_cast<unsigned char>(count_arg >> 8),
SET_BITS_LOW,
pinInitialState | Pin::CS,
pinDirection
};
// insert before last SET_BITS_LOW command
// SET_BITS_LOW takes 2 arguments, so we're inserting data in -3 position from the end
buf.insert(buf.end() - 3, &data[0], &data[size]);
FTDI_CHECK_RESULT((rc = ftdi_write_data(_ftdic, buf.data(), buf.size())) != buf.size());
return rc;
}
QJsonObject ProviderFtdi::discover(const QJsonObject & /*params*/)
{
QJsonObject devicesDiscovered;
QJsonArray deviceList;
struct ftdi_device_list *devlist;
struct ftdi_context *ftdic;
ftdic = ftdi_new();
if (ftdi_usb_find_all(ftdic, &devlist, ANY_FTDI_VENDOR, ANY_FTDI_PRODUCT) > 0)
{
struct ftdi_device_list *curdev = devlist;
QMap<QString, uint8_t> deviceIndexes;
while (curdev)
{
libusb_device_descriptor desc;
int rc = libusb_get_device_descriptor(curdev->dev, &desc);
if (rc == 0)
{
QString vendorIdentifier = QString("0x%1").arg(desc.idVendor, 4, 16, QChar{'0'});
QString productIdentifier = QString("0x%1").arg(desc.idProduct, 4, 16, QChar{'0'});
QString vendorAndProduct = QString("%1:%2")
.arg(vendorIdentifier)
.arg(productIdentifier);
uint8_t deviceIndex = deviceIndexes.value(vendorAndProduct, 0);
char serial_string[128] = {0};
char manufacturer_string[128] = {0};
char description_string[128] = {0};
ftdi_usb_get_strings2(ftdic, curdev->dev, manufacturer_string, 128, description_string, 128, serial_string, 128);
QString serialNumber {serial_string};
QString ftdiOpenString;
if(!serialNumber.isEmpty())
{
ftdiOpenString = QString("s:%1:%2").arg(vendorAndProduct).arg(serialNumber);
}
else
{
ftdiOpenString = QString("i:%1:%2").arg(vendorAndProduct).arg(deviceIndex);
}
deviceList.push_back(QJsonObject{
{"ftdiOpenString", ftdiOpenString},
{"vendorIdentifier", vendorIdentifier},
{"productIdentifier", productIdentifier},
{"deviceIndex", deviceIndex},
{"serialNumber", serialNumber},
{"manufacturer", manufacturer_string},
{"description", description_string}
});
deviceIndexes.insert(vendorAndProduct, deviceIndex + 1);
}
curdev = curdev->next;
}
}
ftdi_list_free(&devlist);
ftdi_free(ftdic);
devicesDiscovered.insert("ledDeviceType", _activeDeviceType);
devicesDiscovered.insert("devices", deviceList);
Debug(_log, "FTDI devices discovered: [%s]", QString(QJsonDocument(devicesDiscovered).toJson(QJsonDocument::Compact)).toUtf8().constData());
return devicesDiscovered;
}

View File

@@ -0,0 +1,76 @@
#ifndef PROVIDERFtdi_H
#define PROVIDERFtdi_H
// LedDevice includes
#include <leddevice/LedDevice.h>
#include <ftdi.h>
///
/// The ProviderFtdi implements an abstract base-class for LedDevices using a Ftdi-device.
///
class ProviderFtdi : public LedDevice
{
Q_OBJECT
public:
///
/// @brief Constructs a Ftdi LED-device
///
ProviderFtdi(const QJsonObject& deviceConfig);
static const QString AUTO_SETTING;
protected:
///
/// @brief Opens the output device.
///
/// @return Zero on success (i.e. device is ready), else negative
///
int open() override;
///
/// Sets configuration
///
/// @param deviceConfig the json device config
/// @return true if success
bool init(const QJsonObject& deviceConfig) override;
///
/// @brief Closes the UDP device.
///
/// @return Zero on success (i.e. device is closed), else negative
///
int close() override;
/// @brief Write the given bytes to the Ftdi-device
///
/// @param[in[ size The length of the data
/// @param[in] data The data
/// @return Zero on success, else negative
///
int writeBytes(const qint64 size, const uint8_t* data);
QJsonObject discover(const QJsonObject& params) override;
/// The Ftdi serial-device
struct ftdi_context *_ftdic;
/// The used baud-rate of the output device
qint32 _baudRate_Hz;
QString _deviceName;
protected slots:
///
/// @brief Set device in error state
///
/// @param errorMsg The error message to be logged
///
void setInError(const QString& errorMsg, bool isRecoverable=true) override;
};
#endif // PROVIDERFtdi_H

View File

@@ -0,0 +1,446 @@
// Local-Hyperion includes
#include "LedDeviceHomeAssistant.h"
#include <ssdp/SSDPDiscover.h>
// mDNS discover
#ifdef ENABLE_MDNS
#include <mdns/MdnsBrowser.h>
#include <mdns/MdnsServiceRegister.h>
#endif
#include <utils/NetUtils.h>
#include <utils/ColorRgb.h>
#include <algorithm>
// Constants
namespace {
const bool verbose = false;
// Configuration settings
const char CONFIG_HOST[] = "host";
const char CONFIG_PORT[] = "port";
const char CONFIG_AUTH_TOKEN[] = "token";
const char CONFIG_ENITYIDS[] = "entityIds";
const char CONFIG_BRIGHTNESS[] = "brightness";
const char CONFIG_BRIGHTNESS_OVERWRITE[] = "overwriteBrightness";
const char CONFIG_FULL_BRIGHTNESS_AT_START[] = "fullBrightnessAtStart";
const char CONFIG_ON_OFF_BLACK[] = "switchOffOnBlack";
const char CONFIG_TRANSITIONTIME[] = "transitionTime";
const bool DEFAULT_IS_BRIGHTNESS_OVERWRITE = true;
const bool DEFAULT_IS_FULL_BRIGHTNESS_AT_START = true;
const int BRI_MAX = 255;
const bool DEFAULT_IS_SWITCH_OFF_ON_BLACK = false;
// Home Assistant API
const int API_DEFAULT_PORT = 8123;
const char API_BASE_PATH[] = "/api/";
const char API_STATES[] = "states";
const char API_LIGHT_TURN_ON[] = "services/light/turn_on";
const char API_LIGHT_TURN_OFF[] = "services/light/turn_off";
const char ENTITY_ID[] = "entity_id";
const char RGB_COLOR[] = "rgb_color";
const char BRIGHTNESS[] = "brightness";
const char TRANSITION[] = "transition";
const char FLASH[] = "flash";
// // Home Assistant ssdp services
const char SSDP_ID[] = "ssdp:all";
const char SSDP_FILTER_HEADER[] = "ST";
const char SSDP_FILTER[] = "(.*)home-assistant.io(.*)";
} //End of constants
LedDeviceHomeAssistant::LedDeviceHomeAssistant(const QJsonObject& deviceConfig)
: LedDevice(deviceConfig)
, _restApi(nullptr)
, _apiPort(API_DEFAULT_PORT)
, _isBrightnessOverwrite(DEFAULT_IS_BRIGHTNESS_OVERWRITE)
, _isFullBrightnessAtStart(DEFAULT_IS_FULL_BRIGHTNESS_AT_START)
, _brightness (BRI_MAX)
{
#ifdef ENABLE_MDNS
QMetaObject::invokeMethod(MdnsBrowser::getInstance().data(), "browseForServiceType",
Qt::QueuedConnection, Q_ARG(QByteArray, MdnsServiceRegister::getServiceType(_activeDeviceType)));
#endif
}
LedDevice* LedDeviceHomeAssistant::construct(const QJsonObject& deviceConfig)
{
return new LedDeviceHomeAssistant(deviceConfig);
}
LedDeviceHomeAssistant::~LedDeviceHomeAssistant()
{
delete _restApi;
_restApi = nullptr;
}
bool LedDeviceHomeAssistant::init(const QJsonObject& deviceConfig)
{
bool isInitOK{ false };
if ( LedDevice::init(deviceConfig) )
{
// Overwrite non supported/required features
if (deviceConfig["rewriteTime"].toInt(0) > 0)
{
Info(_log, "Home Assistant lights do not require rewrites. Refresh time is ignored.");
setRewriteTime(0);
}
DebugIf(verbose, _log, "deviceConfig: [%s]", QString(QJsonDocument(_devConfig).toJson(QJsonDocument::Compact)).toUtf8().constData());
//Set hostname as per configuration and default port
_hostName = deviceConfig[CONFIG_HOST].toString();
_apiPort = deviceConfig[CONFIG_PORT].toInt(API_DEFAULT_PORT);
_bearerToken = deviceConfig[CONFIG_AUTH_TOKEN].toString();
_isBrightnessOverwrite = _devConfig[CONFIG_BRIGHTNESS_OVERWRITE].toBool(DEFAULT_IS_BRIGHTNESS_OVERWRITE);
_isFullBrightnessAtStart = _devConfig[CONFIG_FULL_BRIGHTNESS_AT_START].toBool(DEFAULT_IS_FULL_BRIGHTNESS_AT_START);
_brightness = _devConfig[CONFIG_BRIGHTNESS].toInt(BRI_MAX);
_switchOffOnBlack = _devConfig[CONFIG_ON_OFF_BLACK].toBool(DEFAULT_IS_SWITCH_OFF_ON_BLACK);
int transitionTimeMs = _devConfig[CONFIG_TRANSITIONTIME].toInt(0);
_transitionTime = transitionTimeMs / 1000.0;
Debug(_log, "Hostname/IP : %s", QSTRING_CSTR(_hostName));
Debug(_log, "Port : %d", _apiPort );
Debug(_log, "Overwrite Brightn.: %s", _isBrightnessOverwrite ? "Yes" : "No" );
Debug(_log, "Set Brightness to : %d", _brightness);
Debug(_log, "Full Bri. at start: %s", _isFullBrightnessAtStart ? "Yes" : "No" );
Debug(_log, "Off on Black : %s", _switchOffOnBlack ? "Yes" : "No" );
Debug(_log, "Transition Time : %d ms", transitionTimeMs );
_lightEntityIds = _devConfig[ CONFIG_ENITYIDS ].toVariant().toStringList();
int configuredLightsCount = _lightEntityIds.size();
if ( configuredLightsCount == 0 )
{
this->setInError( "No light entity-ids configured" );
isInitOK = false;
}
else
{
Debug(_log, "Lights configured : %d", configuredLightsCount );
isInitOK = true;
}
}
return isInitOK;
}
bool LedDeviceHomeAssistant::initLedsConfiguration()
{
bool isInitOK = false;
//Currently on one light is supported
QString lightEntityId = _lightEntityIds[0];
//Get properties for configured light entitiy to check availability
_restApi->setPath({ API_STATES, lightEntityId});
httpResponse response = _restApi->get();
if (response.error())
{
QString errorReason = QString("%1 get properties failed with error: '%2'").arg(_activeDeviceType,response.getErrorReason());
this->setInError(errorReason);
}
else
{
QJsonObject propertiesDetails = response.getBody().object();
if (propertiesDetails.isEmpty())
{
QString errorReason = QString("Light [%1] does not exist").arg(lightEntityId);
this->setInError(errorReason);
}
else
{
if (propertiesDetails.value("state").toString().compare("unavailable") == 0)
{
Warning(_log, "Light [%s] is currently unavailable", QSTRING_CSTR(lightEntityId));
}
isInitOK = true;
}
}
return isInitOK;
}
bool LedDeviceHomeAssistant::openRestAPI()
{
bool isInitOK{ true };
if (_restApi == nullptr)
{
if (_apiPort == 0)
{
_apiPort = API_DEFAULT_PORT;
}
_restApi = new ProviderRestApi(_address.toString(), _apiPort);
_restApi->setLogger(_log);
_restApi->setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
_restApi->setHeader("Authorization", QByteArrayLiteral("Bearer ") + _bearerToken.toUtf8());
//Base-path is api-path
_restApi->setBasePath(API_BASE_PATH);
}
return isInitOK;
}
int LedDeviceHomeAssistant::open()
{
int retval = -1;
_isDeviceReady = false;
if (NetUtils::resolveHostToAddress(_log, _hostName, _address, _apiPort))
{
if (openRestAPI())
{
// Read LedDevice configuration and validate against device configuration
if (initLedsConfiguration())
{
// Everything is OK, device is ready
_isDeviceReady = true;
retval = 0;
}
}
else
{
_restApi->setHost(_address.toString());
_restApi->setPort(_apiPort);
}
}
return retval;
}
QJsonArray LedDeviceHomeAssistant::discoverSsdp() const
{
QJsonArray deviceList;
SSDPDiscover ssdpDiscover;
ssdpDiscover.skipDuplicateKeys(true);
ssdpDiscover.setSearchFilter(SSDP_FILTER, SSDP_FILTER_HEADER);
QString searchTarget = SSDP_ID;
if (ssdpDiscover.discoverServices(searchTarget) > 0)
{
deviceList = ssdpDiscover.getServicesDiscoveredJson();
}
return deviceList;
}
QJsonObject LedDeviceHomeAssistant::discover(const QJsonObject& /*params*/)
{
QJsonObject devicesDiscovered;
devicesDiscovered.insert("ledDeviceType", _activeDeviceType);
QJsonArray deviceList;
#ifdef ENABLE_MDNS
QString discoveryMethod("mDNS");
deviceList = MdnsBrowser::getInstance().data()->getServicesDiscoveredJson(
MdnsServiceRegister::getServiceType(_activeDeviceType),
MdnsServiceRegister::getServiceNameFilter(_activeDeviceType),
DEFAULT_DISCOVER_TIMEOUT
);
#else
QString discoveryMethod("ssdp");
deviceList = discoverSsdp();
#endif
devicesDiscovered.insert("discoveryMethod", discoveryMethod);
devicesDiscovered.insert("devices", deviceList);
DebugIf(verbose, _log, "devicesDiscovered: [%s]", QString(QJsonDocument(devicesDiscovered).toJson(QJsonDocument::Compact)).toUtf8().constData());
return devicesDiscovered;
}
QJsonObject LedDeviceHomeAssistant::getProperties(const QJsonObject& params)
{
DebugIf(verbose, _log, "params: [%s]", QString(QJsonDocument(params).toJson(QJsonDocument::Compact)).toUtf8().constData());
QJsonObject properties;
_hostName = params[CONFIG_HOST].toString("");
_apiPort = API_DEFAULT_PORT;
_bearerToken = params[CONFIG_AUTH_TOKEN].toString("");
Info(_log, "Get properties for %s, hostname (%s)", QSTRING_CSTR(_activeDeviceType), QSTRING_CSTR(_hostName));
if (NetUtils::resolveHostToAddress(_log, _hostName, _address, _apiPort))
{
if (openRestAPI())
{
QString filter = params["filter"].toString("");
_restApi->setPath(filter);
// Perform request
httpResponse response = _restApi->get();
if (response.error())
{
Warning(_log, "%s get properties failed with error: '%s'", QSTRING_CSTR(_activeDeviceType), QSTRING_CSTR(response.getErrorReason()));
}
QJsonObject propertiesDetails;
const QJsonDocument jsonDoc = response.getBody();
if (jsonDoc.isArray()) {
const QJsonArray jsonArray = jsonDoc.array();
QVector<QJsonValue> filteredVector;
// Iterate over the array and filter objects with entity_id starting with "light."
for (const QJsonValue &value : jsonArray)
{
QJsonObject obj = value.toObject();
QString entityId = obj[ENTITY_ID].toString();
if (entityId.startsWith("light."))
{
filteredVector.append(obj);
}
}
// Sort the filtered vector by "friendly_name" in ascending order
std::sort(filteredVector.begin(), filteredVector.end(), [](const QJsonValue &a, const QJsonValue &b) {
QString nameA = a.toObject()["attributes"].toObject()["friendly_name"].toString();
QString nameB = b.toObject()["attributes"].toObject()["friendly_name"].toString();
return nameA < nameB; // Ascending order
});
// Convert the sorted vector back to a QJsonArray
QJsonArray sortedArray;
for (const QJsonValue &value : filteredVector) {
sortedArray.append(value);
}
propertiesDetails.insert("lightEntities", sortedArray);
}
if (!propertiesDetails.isEmpty())
{
propertiesDetails.insert("ledCount", 1);
}
properties.insert("properties", propertiesDetails);
}
DebugIf(verbose, _log, "properties: [%s]", QString(QJsonDocument(properties).toJson(QJsonDocument::Compact)).toUtf8().constData());
}
return properties;
}
void LedDeviceHomeAssistant::identify(const QJsonObject& params)
{
DebugIf(verbose, _log, "params: [%s]", QString(QJsonDocument(params).toJson(QJsonDocument::Compact)).toUtf8().constData());
_hostName = params[CONFIG_HOST].toString("");
_apiPort = API_DEFAULT_PORT;
_bearerToken = params[CONFIG_AUTH_TOKEN].toString("");
Info(_log, "Identify %s, hostname (%s)", QSTRING_CSTR(_activeDeviceType), QSTRING_CSTR(_hostName));
if (NetUtils::resolveHostToAddress(_log, _hostName, _address, _apiPort))
{
if (openRestAPI())
{
QJsonArray lightEntityIds = params[ ENTITY_ID ].toArray();
_restApi->setPath(API_LIGHT_TURN_ON);
QJsonObject serviceAttributes{{ENTITY_ID, lightEntityIds}};
serviceAttributes.insert(FLASH, "short");
httpResponse response = _restApi->post(serviceAttributes);
if (response.error())
{
Warning(_log, "%s identification failed with error: '%s'", QSTRING_CSTR(_activeDeviceType), QSTRING_CSTR(response.getErrorReason()));
}
}
}
}
bool LedDeviceHomeAssistant::powerOn()
{
bool isOn = false;
if (_isDeviceReady)
{
_restApi->setPath(API_LIGHT_TURN_ON);
QJsonObject serviceAttributes {{ENTITY_ID, QJsonArray::fromStringList(_lightEntityIds)}};
if (_isFullBrightnessAtStart)
{
serviceAttributes.insert(BRIGHTNESS, BRI_MAX);
}
httpResponse response = _restApi->post(serviceAttributes);
if (response.error())
{
QString errorReason = QString("Power-on request failed with error: '%1'").arg(response.getErrorReason());
this->setInError(errorReason);
isOn = false;
}
else {
isOn = true;
}
}
return isOn;
}
bool LedDeviceHomeAssistant::powerOff()
{
bool isOff = true;
if (_isDeviceReady)
{
_restApi->setPath(API_LIGHT_TURN_OFF);
QJsonObject serviceAttributes {{ENTITY_ID, QJsonArray::fromStringList(_lightEntityIds)}};
httpResponse response = _restApi->post(serviceAttributes);
if (response.error())
{
QString errorReason = QString("Power-off request failed with error: '%1'").arg(response.getErrorReason());
this->setInError(errorReason);
isOff = false;
}
}
return isOff;
}
int LedDeviceHomeAssistant::write(const std::vector<ColorRgb>& ledValues)
{
int retVal = 0;
QJsonObject serviceAttributes {{ENTITY_ID, QJsonArray::fromStringList(_lightEntityIds)}};
ColorRgb ledValue = ledValues.at(0);
if (_switchOffOnBlack && ledValue == ColorRgb::BLACK)
{
_restApi->setPath(API_LIGHT_TURN_OFF);
}
else
{
// http://hostname:port/api/services/light/turn_on
// {
// "entity_id": [ entity-IDs ],
// "rgb_color": [R,G,B]
// }
_restApi->setPath(API_LIGHT_TURN_ON);
QJsonArray rgbColor {ledValue.red, ledValue.green, ledValue.blue};
serviceAttributes.insert(RGB_COLOR, rgbColor);
if (_isBrightnessOverwrite)
{
serviceAttributes.insert(BRIGHTNESS, _brightness);
}
if (_transitionTime > 0)
{
// Transition time in seconds
serviceAttributes.insert(TRANSITION, _transitionTime);
}
}
httpResponse response = _restApi->post(serviceAttributes);
if (response.error())
{
Warning(_log,"Updating lights failed with error: '%s'", QSTRING_CSTR(response.getErrorReason()) );
retVal = -1;
}
return retVal;
}

View File

@@ -0,0 +1,181 @@
#ifndef LEDEVICEHOMEASSISTANT_H
#define LEDEVICEHOMEASSISTANT_H
// LedDevice includes
#include <leddevice/LedDevice.h>
#include "ProviderRestApi.h"
// Qt includes
#include <QString>
#include <QHostAddress>
#include <QNetworkAccessManager>
///
/// Implementation of the LedDevice interface for sending to
/// lights made available via the Home Assistant platform.
///
class LedDeviceHomeAssistant : LedDevice
{
public:
///
/// @brief Constructs LED-device for Home Assistant Lights
///
/// following code shows all configuration options
/// @code
/// "device" :
/// {
/// "type" : "homeassistant"
/// "host" : "hostname or IP",
/// "port" : port
/// "token": "bearer token",
/// },
///@endcode
///
/// @param deviceConfig Device's configuration as JSON-Object
///
explicit LedDeviceHomeAssistant(const QJsonObject& deviceConfig);
///
/// @brief Destructor of the LED-device
///
~LedDeviceHomeAssistant() override;
///
/// @brief Constructs the LED-device
///
/// @param[in] deviceConfig Device's configuration as JSON-Object
/// @return LedDevice constructed
static LedDevice* construct(const QJsonObject& deviceConfig);
///
/// @brief Discover Home Assistant lights available (for configuration).
///
/// @param[in] params Parameters used to overwrite discovery default behaviour
///
/// @return A JSON structure holding a list of devices found
///
QJsonObject discover(const QJsonObject& params) override;
///
/// @brief Get the Home Assistant light's resource properties
///
/// Following parameters are required
/// @code
/// {
/// "host" : "hostname or IP",
/// "port" : port
/// "token" : "bearer token",
/// "filter": "resource to query", root "/" is used, if empty
/// }
///@endcode
///
/// @param[in] params Parameters to query device
/// @return A JSON structure holding the device's properties
///
QJsonObject getProperties(const QJsonObject& params) override;
///
/// @brief Send an update to the Nanoleaf device to identify it.
///
/// Following parameters are required
/// @code
/// {
/// "host" : "hostname or IP",
/// "port" : port
/// "token" : "bearer token",
/// "entity_id": array of lightIds
/// }
///@endcode
///
/// @param[in] params Parameters to address device
///
void identify(const QJsonObject& params) override;
protected:
///
/// @brief Initialise the Home Assistant light's configuration and network address details
///
/// @param[in] deviceConfig the JSON device configuration
/// @return True, if success
///
bool init(const QJsonObject& deviceConfig) override;
///
/// @brief Opens the output device.
///
/// @return Zero on success (i.e. device is ready), else negative
///
int open() override;
///
/// @brief Writes the RGB-Color values to the Home Assistant light.
///
/// @param[in] ledValues The RGB-color
/// @return Zero on success, else negative
//////
int write(const std::vector<ColorRgb>& ledValues) override;
///
/// @brief Power-/turn on the Home Assistant light.
///
/// @brief Store the device's original state.
///
bool powerOn() override;
///
/// @brief Power-/turn off the Home Assistant light.
///
/// @return True if success
///
bool powerOff() override;
private:
///
/// @brief Initialise the access to the REST-API wrapper
///
/// @return True, if success
///
bool openRestAPI();
///
/// @brief Get Nanoleaf device details and configuration
///
/// @return True, if Nanoleaf device capabilities fit configuration
///
bool initLedsConfiguration();
///
/// @brief Discover Home Assistant lights available (for configuration).
///
/// @return A JSON structure holding a list of devices found
///
QJsonArray discoverSsdp() const;
// ///
// /// @brief Get number of panels that can be used as LEds.
// ///
// /// @return Number of usable LED panels
// ///
// int getHwLedCount(const QJsonObject& jsonLayout) const;
QString _hostName;
QHostAddress _address;
ProviderRestApi* _restApi;
int _apiPort;
QString _bearerToken;
/// List of the HA light entity_ids.
QStringList _lightEntityIds;
bool _isBrightnessOverwrite;
bool _isFullBrightnessAtStart;
int _brightness;
bool _switchOffOnBlack;
/// Transition time in seconds
double _transitionTime;
};
#endif // LEDEVICEHOMEASSISTANT_H

View File

@@ -31,7 +31,7 @@ const char CONFIG_TRANSITIONTIME[] = "transitiontime";
const char CONFIG_BLACK_LIGHTS_TIMEOUT[] = "blackLightsTimeout";
const char CONFIG_ON_OFF_BLACK[] = "switchOffOnBlack";
const char CONFIG_RESTORE_STATE[] = "restoreOriginalState";
const char CONFIG_lightIdS[] = "lightIds";
const char CONFIG_LIGHTIDS[] = "lightIds";
const char CONFIG_USE_HUE_API_V2[] = "useAPIv2";
const char CONFIG_USE_HUE_ENTERTAINMENT_API[] = "useEntertainmentAPI";
const char CONFIG_groupId[] = "groupId";
@@ -1849,7 +1849,7 @@ bool LedDevicePhilipsHue::setLights()
_useEntertainmentAPI = false;
Error(_log, "Group-ID [%s] is not usable - Entertainment API usage was disabled!", QSTRING_CSTR(_groupId) );
}
lights = _devConfig[ CONFIG_lightIdS ].toVariant().toStringList();
lights = _devConfig[ CONFIG_LIGHTIDS ].toVariant().toStringList();
}
_lightIds = lights;

View File

@@ -23,7 +23,7 @@ namespace {
const char CONFIG_RAZER_DEVICE_TYPE[] = "subType";
const char CONFIG_SINGLE_COLOR[] = "singleColor";
// WLED JSON-API elements
// API elements
const char API_DEFAULT_HOST[] = "localhost";
const int API_DEFAULT_PORT = 54235;

View File

@@ -58,6 +58,11 @@ bool LedDeviceAdalight::init(const QJsonObject &deviceConfig)
case Adalight::ADA:
Debug( _log, "Adalight driver uses standard Adalight protocol");
break;
case Adalight::SKYDIMO:
Debug( _log, "Adalight driver uses Skydimo protocol");
break;
default:
Error( _log, "Adalight driver - unsupported protocol");
return false;
@@ -71,10 +76,6 @@ bool LedDeviceAdalight::init(const QJsonObject &deviceConfig)
void LedDeviceAdalight::prepareHeader()
{
// create ledBuffer
uint totalLedCount = _ledCount;
_bufferLength = static_cast<qint64>(HEADER_SIZE + _ledRGBCount);
switch (_streamProtocol) {
case Adalight::LBAPA:
{
@@ -82,7 +83,6 @@ void LedDeviceAdalight::prepareHeader()
const unsigned int bytesPerRGBLed = 4;
const unsigned int endFrameSize = qMax<unsigned int>(((_ledCount + 15) / 16), bytesPerRGBLed);
_bufferLength = HEADER_SIZE + (_ledCount * bytesPerRGBLed) + startFrameSize + endFrameSize;
_ledBuffer.resize(static_cast<size_t>(_bufferLength), 0x00);
// init constant data values
@@ -91,39 +91,47 @@ void LedDeviceAdalight::prepareHeader()
_ledBuffer[iLed*4+HEADER_SIZE] = 0xFF;
}
}
break;
break;
case Adalight::SKYDIMO:
{
_bufferLength = static_cast<qint64>(HEADER_SIZE + _ledRGBCount);
_ledBuffer.resize(static_cast<size_t>(_bufferLength), 0x00);
_ledBuffer[0] = 'A';
_ledBuffer[1] = 'd';
_ledBuffer[2] = 'a';
_ledBuffer[3] = 0;
_ledBuffer[4] = 0;
_ledBuffer[5] = static_cast<quint8>(_ledCount);
}
break;
case Adalight::AWA:
_bufferLength += 8;
[[fallthrough]];
{
_bufferLength = static_cast<qint64>(HEADER_SIZE + _ledRGBCount + 8);
_ledBuffer.resize(static_cast<size_t>(_bufferLength), 0x00);
_ledBuffer[0] = 'A';
_ledBuffer[1] = 'w';
_ledBuffer[2] = _white_channel_calibration ? 'A' : 'a';
qToBigEndian<quint16>(static_cast<quint16>(_ledCount-1), &_ledBuffer[3]);
_ledBuffer[5] = _ledBuffer[3] ^ _ledBuffer[4] ^ 0x55; // Checksum
}
break;
case Adalight::ADA:
[[fallthrough]];
default:
totalLedCount -= 1;
_bufferLength = static_cast<qint64>(HEADER_SIZE + _ledRGBCount);
_ledBuffer.resize(static_cast<size_t>(_bufferLength), 0x00);
break;
}
_ledBuffer[0] = 'A';
if (_streamProtocol == Adalight::AWA )
{
_ledBuffer[1] = 'w';
_ledBuffer[2] = _white_channel_calibration ? 'A' : 'a';
}
else
{
_ledBuffer[0] = 'A';
_ledBuffer[1] = 'd';
_ledBuffer[2] = 'a';
qToBigEndian<quint16>(static_cast<quint16>(_ledCount-1), &_ledBuffer[3]);
_ledBuffer[5] = _ledBuffer[3] ^ _ledBuffer[4] ^ 0x55; // Checksum
break;
}
qToBigEndian<quint16>(static_cast<quint16>(totalLedCount), &_ledBuffer[3]);
_ledBuffer[5] = _ledBuffer[3] ^ _ledBuffer[4] ^ 0x55; // Checksum
Debug( _log, "Adalight header for %d leds (size: %d): %c%c%c 0x%02x 0x%02x 0x%02x", _ledCount, _ledBuffer.size(),
_ledBuffer[0], _ledBuffer[1], _ledBuffer[2], _ledBuffer[3], _ledBuffer[4], _ledBuffer[5] );
}
int LedDeviceAdalight::write(const std::vector<ColorRgb> & ledValues)
{
if (_ledCount != ledValues.size())

View File

@@ -10,7 +10,8 @@ typedef enum ProtocolType
{
ADA = 0,
LBAPA,
AWA
AWA,
SKYDIMO
} PROTOCOLTYPE;
}

View File

@@ -11,10 +11,10 @@
"streamProtocol": {
"type": "string",
"title": "edt_dev_spec_stream_protocol_title",
"enum": [ "0", "1", "2" ],
"enum": [ "0", "1", "2", "3" ],
"default": "0",
"options": {
"enum_titles": [ "edt_dev_spec_ada_mode_title", "edt_dev_spec_LBap102Mode_title","edt_dev_spec_awa_mode_title" ]
"enum_titles": [ "edt_dev_spec_ada_mode_title", "edt_dev_spec_LBap102Mode_title","edt_dev_spec_awa_mode_title", "edt_dev_spec_skydimo_mode_title" ]
},
"propertyOrder": 2
},

View File

@@ -0,0 +1,27 @@
{
"type": "object",
"required": true,
"properties": {
"output": {
"type": "string",
"title":"edt_dev_spec_outputPath_title",
"propertyOrder": 1
},
"rate": {
"type": "integer",
"title": "edt_dev_spec_baudrate_title",
"default": 5000000,
"propertyOrder": 2
},
"brightnessControlMaxLevel": {
"type": "integer",
"title": "edt_conf_color_brightness_title",
"default": 31,
"minimum": 1,
"maximum": 31,
"propertyOrder": 3
}
},
"additionalProperties": true
}

View File

@@ -0,0 +1,135 @@
{
"type": "object",
"required": true,
"properties": {
"hostList": {
"type": "string",
"title": "edt_dev_spec_devices_discovered_title",
"enum": [ "NONE" ],
"options": {
"enum_titles": [ "edt_dev_spec_devices_discovery_inprogress" ],
"infoText": "edt_dev_spec_devices_discovered_title_info"
},
"required": true,
"propertyOrder": 1
},
"host": {
"type": "string",
"format": "hostname_or_ip",
"title": "edt_dev_spec_targetIpHost_title",
"options": {
"infoText": "edt_dev_spec_targetIpHost_title_info"
},
"required": true,
"propertyOrder": 2
},
"port": {
"type": "integer",
"title": "edt_dev_spec_port_title",
"default": 8123,
"minimum": 0,
"maximum": 65535,
"access": "expert",
"propertyOrder": 3
},
"token": {
"type": "string",
"title": "edt_dev_auth_key_title",
"options": {
"infoText": "edt_dev_auth_key_title_info"
},
"propertyOrder": 4
},
"restoreOriginalState": {
"type": "boolean",
"format": "checkbox",
"title": "edt_dev_spec_restoreOriginalState_title",
"default": true,
"required": true,
"options": {
"hidden": true,
"infoText": "edt_dev_spec_restoreOriginalState_title_info"
},
"propertyOrder": 5
},
"overwriteBrightness": {
"type": "boolean",
"format": "checkbox",
"title": "edt_dev_spec_brightnessOverwrite_title",
"default": true,
"required": true,
"access": "advanced",
"propertyOrder": 5
},
"brightness": {
"type": "integer",
"title": "edt_dev_spec_brightness_title",
"default": 255,
"minimum": 1,
"maximum": 255,
"options": {
"dependencies": {
"overwriteBrightness": true
}
},
"access": "advanced",
"propertyOrder": 6
},
"fullBrightnessAtStart": {
"type": "boolean",
"format": "checkbox",
"title": "edt_dev_spec_fullBrightnessAtStart_title",
"default": true,
"required": true,
"access": "advanced",
"propertyOrder": 7
},
"switchOffOnBlack": {
"type": "boolean",
"format": "checkbox",
"title": "edt_dev_spec_switchOffOnBlack_title",
"default": false,
"access": "advanced",
"propertyOrder": 8
},
"transitionTime": {
"type": "integer",
"title": "edt_dev_spec_transistionTime_title",
"default": 0,
"append": "ms",
"minimum": 0,
"maximum": 2000,
"required": false,
"access": "advanced",
"propertyOrder": 9
},
"entityIds": {
"title": "edt_dev_spec_lightid_title",
"type": "array",
"required": true,
"format": "select",
"options": {
"hidden": true
},
"items": {
"type": "string",
"title": "edt_dev_spec_lights_itemtitle"
},
"propertyOrder": 10
},
"latchTime": {
"type": "integer",
"title": "edt_dev_spec_latchtime_title",
"default": 250,
"append": "edt_append_ms",
"minimum": 100,
"maximum": 2000,
"access": "expert",
"options": {
"infoText": "edt_dev_spec_latchtime_title_info"
},
"propertyOrder": 11
}
},
"additionalProperties": true
}

View File

@@ -0,0 +1,60 @@
{
"type": "object",
"required": true,
"properties": {
"output": {
"type": "string",
"title": "edt_dev_spec_outputPath_title",
"required": true,
"propertyOrder": 1
},
"rate": {
"type": "integer",
"step": 100000,
"title": "edt_dev_spec_baudrate_title",
"default": 3200000,
"minimum": 2050000,
"maximum": 3750000,
"propertyOrder": 2
},
"brightnessControlMaxLevel": {
"type": "integer",
"title": "edt_conf_color_brightness_title",
"default": 255,
"minimum": 1,
"maximum": 255,
"propertyOrder": 3
},
"whiteAlgorithm": {
"type": "string",
"title": "edt_dev_spec_whiteLedAlgor_title",
"enum": [
"subtract_minimum",
"sub_min_cool_adjust",
"sub_min_warm_adjust",
"cold_white",
"neutral_white",
"auto",
"auto_max",
"auto_accurate",
"white_off"
],
"default": "white_off",
"options": {
"enum_titles": [
"edt_dev_enum_subtract_minimum",
"edt_dev_enum_sub_min_cool_adjust",
"edt_dev_enum_sub_min_warm_adjust",
"edt_dev_enum_cold_white",
"edt_dev_enum_neutral_white",
"edt_dev_enum_auto",
"edt_dev_enum_auto_max",
"edt_dev_enum_auto_accurate",
"edt_dev_enum_white_off"
]
},
"propertyOrder": 4
}
},
"additionalProperties": true
}

View File

@@ -22,10 +22,30 @@
"whiteAlgorithm": {
"type": "string",
"title":"edt_dev_spec_whiteLedAlgor_title",
"enum" : ["subtract_minimum","sub_min_cool_adjust","sub_min_warm_adjust","white_off"],
"enum" : [
"subtract_minimum",
"sub_min_cool_adjust",
"sub_min_warm_adjust",
"cold_white",
"neutral_white",
"auto",
"auto_max",
"auto_accurate",
"white_off"
],
"default": "subtract_minimum",
"options" : {
"enum_titles" : ["edt_dev_enum_subtract_minimum", "edt_dev_enum_sub_min_cool_adjust","edt_dev_enum_sub_min_warm_adjust", "edt_dev_enum_white_off"]
"enum_titles" : [
"edt_dev_enum_subtract_minimum",
"edt_dev_enum_sub_min_cool_adjust",
"edt_dev_enum_sub_min_warm_adjust",
"edt_dev_enum_cold_white",
"edt_dev_enum_neutral_white",
"edt_dev_enum_auto",
"edt_dev_enum_auto_max",
"edt_dev_enum_auto_accurate",
"edt_dev_enum_white_off"
]
},
"propertyOrder" : 4
},

View File

@@ -0,0 +1,20 @@
{
"type": "object",
"required": true,
"properties": {
"output": {
"type": "string",
"title": "edt_dev_spec_outputPath_title",
"propertyOrder": 1
},
"rate": {
"type": "integer",
"title": "edt_dev_spec_baudrate_title",
"default": 3075000,
"minimum": 2106000,
"maximum": 3075000,
"propertyOrder": 2
}
},
"additionalProperties": true
}

View File

@@ -43,10 +43,30 @@
"whiteAlgorithm": {
"type": "string",
"title":"edt_dev_spec_whiteLedAlgor_title",
"enum" : ["subtract_minimum","sub_min_cool_adjust","sub_min_warm_adjust","white_off"],
"enum" : [
"subtract_minimum",
"sub_min_cool_adjust",
"sub_min_warm_adjust",
"cold_white",
"neutral_white",
"auto",
"auto_max",
"auto_accurate",
"white_off"
],
"default": "subtract_minimum",
"options" : {
"enum_titles" : ["edt_dev_enum_subtract_minimum", "edt_dev_enum_sub_min_cool_adjust","edt_dev_enum_sub_min_warm_adjust", "edt_dev_enum_white_off"]
"enum_titles" : [
"edt_dev_enum_subtract_minimum",
"edt_dev_enum_sub_min_cool_adjust",
"edt_dev_enum_sub_min_warm_adjust",
"edt_dev_enum_cold_white",
"edt_dev_enum_neutral_white",
"edt_dev_enum_auto",
"edt_dev_enum_auto_max",
"edt_dev_enum_auto_accurate",
"edt_dev_enum_white_off"
]
},
"propertyOrder" : 7
},

View File

@@ -133,6 +133,22 @@ void ImageResampler::processImage(const uint8_t * data, int width, int height, i
break;
}
case PixelFormat::RGB24:
{
for (int yDest = yDestStart, ySource = cropTop + (_verticalDecimation >> 1); yDest <= yDestEnd; ySource += _verticalDecimation, ++yDest)
{
for (int xDest = xDestStart, xSource = cropLeft + (_horizontalDecimation >> 1); xDest <= xDestEnd; xSource += _horizontalDecimation, ++xDest)
{
ColorRgb & rgb = outputImage(abs(xDest), abs(yDest));
int index = lineLength * ySource + (xSource << 1) + xSource;
rgb.red = data[index ];
rgb.green = data[index+1];
rgb.blue = data[index+2];
}
}
break;
}
case PixelFormat::BGR24:
{
for (int yDest = yDestStart, ySource = cropTop + (_verticalDecimation >> 1); yDest <= yDestEnd; ySource += _verticalDecimation, ++yDest)

View File

@@ -3,6 +3,8 @@
#include <utils/RgbToRgbw.h>
#include <utils/Logger.h>
#define ROUND_DIVIDE(number, denom) (((number) + (denom) / 2) / (denom))
namespace RGBW {
WhiteAlgorithm stringToWhiteAlgorithm(const QString& str)
@@ -19,7 +21,27 @@ WhiteAlgorithm stringToWhiteAlgorithm(const QString& str)
{
return WhiteAlgorithm::SUB_MIN_COOL_ADJUST;
}
if (str.isEmpty() || str == "white_off")
if (str == "cold_white")
{
return WhiteAlgorithm::COLD_WHITE;
}
if (str == "neutral_white")
{
return WhiteAlgorithm::NEUTRAL_WHITE;
}
if (str == "auto")
{
return WhiteAlgorithm::AUTO;
}
if (str == "auto_max")
{
return WhiteAlgorithm::AUTO_MAX;
}
if (str == "auto_accurate")
{
return WhiteAlgorithm::AUTO_ACCURATE;
}
if (str.isEmpty() || str == "white_off")
{
return WhiteAlgorithm::WHITE_OFF;
}
@@ -77,6 +99,63 @@ void Rgb_to_Rgbw(ColorRgb input, ColorRgbw * output, WhiteAlgorithm algorithm)
output->white = 0;
break;
}
case WhiteAlgorithm::AUTO_MAX:
{
output->red = input.red;
output->green = input.green;
output->blue = input.blue;
output->white = input.red > input.green ? (input.red > input.blue ? input.red : input.blue) : (input.green > input.blue ? input.green : input.blue);
break;
}
case WhiteAlgorithm::AUTO_ACCURATE:
{
output->white = input.red < input.green ? (input.red < input.blue ? input.red : input.blue) : (input.green < input.blue ? input.green : input.blue);
output->red = input.red - output->white;
output->green = input.green - output->white;
output->blue = input.blue - output->white;
break;
}
case WhiteAlgorithm::AUTO:
{
output->red = input.red;
output->green = input.green;
output->blue = input.blue;
output->white = input.red < input.green ? (input.red < input.blue ? input.red : input.blue) : (input.green < input.blue ? input.green : input.blue);
break;
}
case WhiteAlgorithm::NEUTRAL_WHITE:
case WhiteAlgorithm::COLD_WHITE:
{
//cold white config
uint8_t gain = 0xFF;
uint8_t red = 0xA0;
uint8_t green = 0xA0;
uint8_t blue = 0xA0;
if (algorithm == WhiteAlgorithm::NEUTRAL_WHITE) {
gain = 0xFF;
red = 0xB0;
green = 0xB0;
blue = 0x70;
}
uint8_t _r = qMin((uint32_t)(ROUND_DIVIDE(red * input.red, 0xFF)), (uint32_t)0xFF);
uint8_t _g = qMin((uint32_t)(ROUND_DIVIDE(green * input.green, 0xFF)), (uint32_t)0xFF);
uint8_t _b = qMin((uint32_t)(ROUND_DIVIDE(blue * input.blue, 0xFF)), (uint32_t)0xFF);
output->white = qMin(_r, qMin(_g, _b));
output->red = input.red - _r;
output->green = input.green - _g;
output->blue = input.blue - _b;
uint8_t _w = qMin((uint32_t)(ROUND_DIVIDE(gain * output->white, 0xFF)), (uint32_t)0xFF);
output->white = _w;
break;
}
default:
break;
}