Implement ftdi led device - 2 (#1746)

This commit is contained in:
LordGrey 2024-05-31 23:08:13 +02:00 committed by GitHub
parent 897e4aac8a
commit 76fff98f5c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
24 changed files with 1037 additions and 25 deletions

View File

@ -36,7 +36,7 @@ jobs:
if: ${{ matrix.language == 'cpp' }}
run: |
sudo apt-get update
sudo apt-get install --yes git cmake build-essential qtbase5-dev libqt5serialport5-dev libqt5sql5-sqlite libqt5svg5-dev libqt5x11extras5-dev libusb-1.0-0-dev python3-dev libcec-dev libxcb-image0-dev libxcb-util0-dev libxcb-shm0-dev libxcb-render0-dev libxcb-randr0-dev libxrandr-dev libxrender-dev libavahi-core-dev libavahi-compat-libdnssd-dev libasound2-dev libturbojpeg0-dev libjpeg-dev libssl-dev
sudo apt-get install --yes git cmake build-essential qtbase5-dev libqt5serialport5-dev libqt5sql5-sqlite libqt5svg5-dev libqt5x11extras5-dev libusb-1.0-0-dev python3-dev libcec-dev libxcb-image0-dev libxcb-util0-dev libxcb-shm0-dev libxcb-render0-dev libxcb-randr0-dev libxrandr-dev libxrender-dev libavahi-core-dev libavahi-compat-libdnssd-dev libasound2-dev libturbojpeg0-dev libjpeg-dev libssl-dev libftdi1-dev
- name: 🔁 Initialize CodeQL
uses: github/codeql-action/init@v3

View File

@ -117,7 +117,7 @@ jobs:
echo '::group::Update/Install dependencies'
brew untap --force homebrew/core homebrew/cask
brew update || true
brew install qt@${{ inputs.qt_version }} vulkan-headers ninja || true
brew install qt@${{ inputs.qt_version }} vulkan-headers ninja libftdi || true
echo '::endgroup::'
- name: 👷 Build

View File

@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Support for ftdi chip based LED-devices with ws2812, sk6812 apa102 LED types (Many thanks to @nurikk) (#1746)
- Support gaps on Matrix Layout (#1696)
- Windows: Added a new grabber that uses the DXGI DDA (Desktop Duplication API). This has much better performance than the DX grabber as it does more of its work on the GPU.

View File

@ -94,6 +94,7 @@ set(DEFAULT_DEV_SPI OFF)
set(DEFAULT_DEV_TINKERFORGE OFF)
set(DEFAULT_DEV_USB_HID OFF)
set(DEFAULT_DEV_WS281XPWM OFF)
set(DEFAULT_DEV_FTDI ON )
# Services
set(DEFAULT_EFFECTENGINE ON )
@ -121,9 +122,10 @@ if(${CMAKE_SYSTEM} MATCHES "Linux")
set(DEFAULT_DEV_USB_HID ON)
set(DEFAULT_CEC ON)
elseif (WIN32)
set(DEFAULT_DX ON)
set(DEFAULT_DDA ON)
set(DEFAULT_MF ON)
set(DEFAULT_DX ON )
set(DEFAULT_DDA ON )
set(DEFAULT_MF ON )
set(DEFAULT_DEV_FTDI OFF)
else()
set(DEFAULT_FB OFF)
set(DEFAULT_V4L2 OFF)
@ -353,6 +355,9 @@ message(STATUS "ENABLE_DEV_USB_HID = ${ENABLE_DEV_USB_HID}")
option(ENABLE_DEV_WS281XPWM "Enable the WS281x-PWM device" ${DEFAULT_DEV_WS281XPWM})
message(STATUS "ENABLE_DEV_WS281XPWM = ${ENABLE_DEV_WS281XPWM}")
option(ENABLE_DEV_FTDI "Enable the FTDI devices" ${DEFAULT_DEV_FTDI} )
message(STATUS "ENABLE_DEV_FTDI = ${ENABLE_DEV_FTDI}")
removeIndent()
message(STATUS "Services options:")

View File

@ -161,11 +161,12 @@
"conf_leds_note_layout_overwrite": "Note: Overwrite creates a default layout for {{plural:$1| one LED| all $1 LEDs}} given by the hardware LED count",
"conf_leds_optgroup_RPiGPIO": "RPi GPIO",
"conf_leds_optgroup_RPiPWM": "RPi PWM",
"conf_leds_optgroup_RPiSPI": "RPi SPI",
"conf_leds_optgroup_SPI": "SPI",
"conf_leds_optgroup_debug": "Debug",
"conf_leds_optgroup_network": "Network",
"conf_leds_optgroup_other": "Other",
"conf_leds_optgroup_usb": "USB/Serial",
"conf_leds_optgroup_ftdi": "USB/Ftdi",
"conf_logging_btn_autoscroll": "Auto scrolling",
"conf_logging_btn_clipboard": "Copy Log to Clipboard",
"conf_logging_btn_pbupload": "Upload a report for support requests",
@ -619,6 +620,11 @@
"edt_dev_enum_sub_min_cool_adjust": "Subtract cool white",
"edt_dev_enum_sub_min_warm_adjust": "Subtract warm white",
"edt_dev_enum_subtract_minimum": "Subtract minimum",
"edt_dev_enum_cold_white": "Cold white",
"edt_dev_enum_neutral_white": "Neutral white",
"edt_dev_enum_auto": "Auto",
"edt_dev_enum_auto_max": "Auto max",
"edt_dev_enum_auto_accurate": "Auto accurate",
"edt_dev_enum_white_off": "White off",
"edt_dev_general_autostart_title": "Autostart",
"edt_dev_general_autostart_title_info": "The LED device is switched-on during startup or not",

View File

@ -18,7 +18,8 @@ var bottomRight2bottomLeft = null;
var bottomLeft2topLeft = null;
var toggleKeystoneCorrectionArea = false;
var devRPiSPI = ['apa102', 'apa104', 'ws2801', 'lpd6803', 'lpd8806', 'p9813', 'sk6812spi', 'sk6822spi', 'sk9822', 'ws2812spi'];
var devSPI = ['apa102', 'apa104', 'ws2801', 'lpd6803', 'lpd8806', 'p9813', 'sk6812spi', 'sk6822spi', 'sk9822', 'ws2812spi'];
var devFTDI = ['apa102_ftdi', 'sk6812_ftdi', 'ws2812_ftdi'];
var devRPiPWM = ['ws281x'];
var devRPiGPIO = ['piblaster'];
var devNET = ['atmoorb', 'cololight', 'fadecandy', 'philipshue', 'nanoleaf', 'razer', 'tinkerforge', 'tpm2net', 'udpe131', 'udpartnet', 'udpddp', 'udph801', 'udpraw', 'wled', 'yeelight'];
@ -1121,6 +1122,12 @@ $(document).ready(function () {
case "karate":
case "sedu":
case "tpm2":
//FTDI devices
case "apa102_ftdi":
case "sk6812_ftdi":
case "ws2812_ftdi":
if (storedAccess === 'expert') {
filter.discoverAll = true;
}
@ -1139,6 +1146,7 @@ $(document).ready(function () {
.catch(error => {
showNotification('danger', "Device discovery for " + ledType + " failed with error:" + error);
});
break;
case "philipshue": {
@ -1441,6 +1449,9 @@ $(document).ready(function () {
case "sk9822":
case "ws2812spi":
case "piblaster":
case "apa102_ftdi":
case "sk6812_ftdi":
case "ws2812_ftdi":
default:
}
@ -1657,9 +1668,10 @@ $(document).ready(function () {
optArr[3] = [];
optArr[4] = [];
optArr[5] = [];
optArr[6] = [];
for (var idx = 0; idx < ledDevices.length; idx++) {
if ($.inArray(ledDevices[idx], devRPiSPI) != -1)
if ($.inArray(ledDevices[idx], devSPI) != -1)
optArr[0].push(ledDevices[idx]);
else if ($.inArray(ledDevices[idx], devRPiPWM) != -1)
optArr[1].push(ledDevices[idx]);
@ -1671,8 +1683,12 @@ $(document).ready(function () {
optArr[4].push(ledDevices[idx]);
else if ($.inArray(ledDevices[idx], devHID) != -1)
optArr[4].push(ledDevices[idx]);
else if (ledDevices[idx].endsWith("_ftdi")) {
var title = ledDevices[idx].replace('_ftdi','');
optArr[5].push(ledDevices[idx] + ":" + title);
}
else
optArr[5].push(ledDevices[idx]);
optArr[6].push(ledDevices[idx]);
}
$("#leddevices").append(createSel(optArr[0], $.i18n('conf_leds_optgroup_RPiSPI')));
@ -1680,9 +1696,10 @@ $(document).ready(function () {
$("#leddevices").append(createSel(optArr[2], $.i18n('conf_leds_optgroup_RPiGPIO')));
$("#leddevices").append(createSel(optArr[3], $.i18n('conf_leds_optgroup_network')));
$("#leddevices").append(createSel(optArr[4], $.i18n('conf_leds_optgroup_usb')));
$("#leddevices").append(createSel(optArr[5], $.i18n('conf_leds_optgroup_ftdi'), true));
if (storedAccess === 'expert' || window.serverConfig.device.type === "file") {
$("#leddevices").append(createSel(optArr[5], $.i18n('conf_leds_optgroup_other')));
$("#leddevices").append(createSel(optArr[6], $.i18n('conf_leds_optgroup_other')));
}
$("#leddevices").val(window.serverConfig.device.type);
@ -1886,6 +1903,9 @@ function saveLedConfig(genDefLayout = false) {
case "sk9822":
case "ws2812spi":
case "piblaster":
case "apa102_ftdi":
case "sk6812_ftdi":
case "ws2812_ftdi":
default:
if (genDefLayout === true) {
ledConfig = {
@ -1938,8 +1958,10 @@ var updateOutputSelectList = function (ledType, discoveryInfo) {
ledTypeGroup = "devNET";
} else if ($.inArray(ledType, devSerial) != -1) {
ledTypeGroup = "devSerial";
} else if ($.inArray(ledType, devRPiSPI) != -1) {
ledTypeGroup = "devRPiSPI";
} else if ($.inArray(ledType, devSPI) != -1) {
ledTypeGroup = "devSPI";
} else if ($.inArray(ledType, devFTDI) != -1) {
ledTypeGroup = "devFTDI";
} else if ($.inArray(ledType, devRPiGPIO) != -1) {
ledTypeGroup = "devRPiGPIO";
} else if ($.inArray(ledType, devRPiPWM) != -1) {
@ -2062,7 +2084,63 @@ var updateOutputSelectList = function (ledType, discoveryInfo) {
}
}
break;
case "devRPiSPI":
case "devFTDI":
key = "output";
if (discoveryInfo.devices.length == 0) {
enumVals.push("NONE");
enumTitleVals.push($.i18n('edt_dev_spec_devices_discovered_none'));
$('#btn_submit_controller').prop('disabled', true);
showAllDeviceInputOptions(key, false);
}
else {
switch (ledType) {
case "ws2812_ftdi":
case "sk6812_ftdi":
case "apa102_ftdi":
for (const device of discoveryInfo.devices) {
enumVals.push(device.ftdiOpenString);
var title = "FTDI";
if (device.manufacturer) {
title = device.manufacturer;
}
if (device.serialNumber) {
title += " - " + device.serialNumber;
}
title += " (" + device.vendorIdentifier + "|" + device.productIdentifier + ")";
if (device.description) {
title += " " + device.description;
}
enumTitleVals.push(title);
}
// Select configured device
var configuredDeviceType = window.serverConfig.device.type;
var configuredOutput = window.serverConfig.device.output;
if (ledType === configuredDeviceType) {
if ($.inArray(configuredOutput, enumVals) != -1) {
enumDefaultVal = configuredOutput;
} else {
enumVals.push(window.serverConfig.device.output);
enumDefaultVal = configuredOutput;
}
}
else {
addSelect = true;
}
break;
default:
}
}
break;
case "devSPI":
case "devRPiGPIO":
key = "output";
@ -2128,7 +2206,6 @@ var updateOutputSelectList = function (ledType, discoveryInfo) {
async function discover_device(ledType, params) {
const result = await requestLedDeviceDiscovery(ledType, params);
var discoveryResult = {};
if (result) {
if (result.error) {

View File

@ -61,14 +61,14 @@ cd $HYPERION_HOME
```console
sudo apt-get update
sudo apt-get install git cmake build-essential qtbase5-dev libqt5serialport5-dev libqt5sql5-sqlite libqt5svg5-dev libqt5x11extras5-dev libusb-1.0-0-dev python3-dev libasound2-dev libturbojpeg0-dev libjpeg-dev libssl-dev
sudo apt-get install git cmake build-essential qtbase5-dev libqt5serialport5-dev libqt5sql5-sqlite libqt5svg5-dev libqt5x11extras5-dev libusb-1.0-0-dev python3-dev libasound2-dev libturbojpeg0-dev libjpeg-dev libssl-dev libftdi1-dev
```
**Ubuntu (22.04+) - Qt6 based**
```console
sudo apt-get update
sudo apt-get install git cmake build-essential qt6-base-dev libqt6serialport6-dev libxkbcommon-dev libvulkan-dev libgl1-mesa-dev libusb-1.0-0-dev python3-dev libasound2-dev libturbojpeg0-dev libjpeg-dev libssl-dev pkg-config
sudo apt-get install git cmake build-essential qt6-base-dev libqt6serialport6-dev libxkbcommon-dev libvulkan-dev libgl1-mesa-dev libusb-1.0-0-dev python3-dev libasound2-dev libturbojpeg0-dev libjpeg-dev libssl-dev pkg-config libftdi1-dev
```
**For Linux X11/XCB grabber support**
@ -110,7 +110,7 @@ See [AUR](https://aur.archlinux.org/packages/?O=0&SeB=nd&K=hyperion&outdated=&SB
The following dependencies are needed to build hyperion.ng on fedora.
```console
sudo dnf -y groupinstall "Development Tools"
sudo dnf install python3-devel qt-devel qt5-qtbase-devel qt5-qtserialport-devel xrandr xcb-util-image-devel qt5-qtx11extras-devel alsa-lib-devel turbojpeg-devel libusb-devel xcb-util-devel dbus-devel openssl-devel fedora-packager rpmdevtools gcc libcec-devel
sudo dnf install python3-devel qt-devel qt5-qtbase-devel qt5-qtserialport-devel xrandr xcb-util-image-devel qt5-qtx11extras-devel alsa-lib-devel turbojpeg-devel libusb-devel xcb-util-devel dbus-devel openssl-devel fedora-packager rpmdevtools gcc libcec-devel libftdi1-dev
```
After installing the dependencies, you can continue with the compile instructions later on this page (the more detailed way..).
@ -119,7 +119,7 @@ To install on OS X you either need [Homebrew](https://brew.sh/) or [Macport](htt
First you need to install the dependencies:
```console
brew install git qt@5 python3 cmake libusb openssl@1.1
brew install git qt@5 python3 cmake libusb openssl@1.1 libftdi
```
## Windows

View File

@ -11,7 +11,12 @@ namespace RGBW {
SUBTRACT_MINIMUM,
SUB_MIN_WARM_ADJUST,
SUB_MIN_COOL_ADJUST,
WHITE_OFF
WHITE_OFF,
COLD_WHITE,
NEUTRAL_WHITE,
AUTO,
AUTO_MAX,
AUTO_ACCURATE
};
WhiteAlgorithm stringToWhiteAlgorithm(const QString& str);

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

@ -38,5 +38,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,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,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

@ -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;
}