Implement ftdi led device

This commit is contained in:
LordGrey 2023-03-25 23:32:14 +00:00
parent 2f09f9a0b8
commit f0259172fd
18 changed files with 962 additions and 4 deletions

View File

@ -88,6 +88,7 @@ SET ( DEFAULT_DEV_SPI OFF )
SET ( DEFAULT_DEV_TINKERFORGE OFF )
SET ( DEFAULT_DEV_USB_HID OFF )
SET ( DEFAULT_DEV_WS281XPWM OFF )
SET ( DEFAULT_ENABLE_FTDIDEV OFF )
# Services
SET ( DEFAULT_EFFECTENGINE ON )

View File

@ -158,6 +158,7 @@
"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",
@ -567,6 +568,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_hyperserial_cold_white": "Hyperserial, cold white",
"edt_dev_enum_hyperserial_neutral_white": "Hyperserial, neutral white",
"edt_dev_enum_wled_auto": "Wled auto",
"edt_dev_enum_wled_auto_max": "Wled auto max",
"edt_dev_enum_wled_auto_accurate": "Wled 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

@ -1645,8 +1645,10 @@ $(document).ready(function () {
optArr[3] = [];
optArr[4] = [];
optArr[5] = [];
optArr[6] = [];
for (var idx = 0; idx < ledDevices.length; idx++) {
var isFtdi = ledDevices[idx].endsWith("_ftdi");
if ($.inArray(ledDevices[idx], devRPiSPI) != -1)
optArr[0].push(ledDevices[idx]);
else if ($.inArray(ledDevices[idx], devRPiPWM) != -1)
@ -1659,8 +1661,10 @@ $(document).ready(function () {
optArr[4].push(ledDevices[idx]);
else if ($.inArray(ledDevices[idx], devHID) != -1)
optArr[4].push(ledDevices[idx]);
else
else if (isFtdi)
optArr[5].push(ledDevices[idx]);
else
optArr[6].push(ledDevices[idx]);
}
$("#leddevices").append(createSel(optArr[0], $.i18n('conf_leds_optgroup_RPiSPI')));
@ -1668,9 +1672,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')));
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);

View File

@ -11,7 +11,12 @@ namespace RGBW {
SUBTRACT_MINIMUM,
SUB_MIN_WARM_ADJUST,
SUB_MIN_COOL_ADJUST,
WHITE_OFF
WHITE_OFF,
HYPERSERIAL_COLD_WHITE,
HYPERSERIAL_NEUTRAL_WHITE,
WLED_AUTO,
WLED_AUTO_MAX,
WLED_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
@ -64,6 +65,10 @@ if ( ENABLE_DEV_WS281XPWM )
FILE ( GLOB Leddevice_PWM_SOURCES "${CURRENT_SOURCE_DIR}/dev_rpi_pwm/*.h" "${CURRENT_SOURCE_DIR}/dev_rpi_pwm/*.cpp")
endif()
if (ENABLE_FTDIDEV)
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
@ -75,6 +80,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
@ -164,3 +170,11 @@ if(ENABLE_MDNS)
target_link_libraries(leddevice mdns)
endif()
if( ENABLE_FTDIDEV )
FIND_PACKAGE(PkgConfig REQUIRED)
pkg_check_modules(LIB_FTDI REQUIRED libftdi1)
add_library(libftdi1 SHARED IMPORTED)
target_include_directories(leddevice PUBLIC ${LIB_FTDI_INCLUDE_DIRS})
target_link_libraries(leddevice ${LIB_FTDI_LINK_LIBRARIES})
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,61 @@
#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;
_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);
// Initialise sub-class
if (ProviderFtdi::init(deviceConfig))
{
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. Led's number: %d", _ledCount);
}
int LedDeviceAPA102_ftdi::write(const std::vector<ColorRgb> &ledValues)
{
if (_ledCount != ledValues.size())
{
Warning(_log, "APA102 led's number has changed (old: %d, new: %d). Rebuilding buffer.", _ledCount, ledValues.size());
_ledCount = ledValues.size();
CreateHeader();
}
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,93 @@
#include "LedDeviceSk6812_ftdi.h"
LedDeviceSk6812_ftdi::LedDeviceSk6812_ftdi(const QJsonObject &deviceConfig)
: ProviderFtdi(deviceConfig),
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);
QString whiteAlgorithm = deviceConfig["whiteAlgorithm"].toString("white_off");
_whiteAlgorithm = RGBW::stringToWhiteAlgorithm(whiteAlgorithm);
Debug(_log, "whiteAlgorithm : %s", QSTRING_CSTR(whiteAlgorithm));
WarningIf((_baudRate_Hz < 2050000 || _baudRate_Hz > 4000000), _log, "SPI rate %d outside recommended range (2050000 -> 4000000)", _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;
if (_ledCount != ledValues.size())
{
Warning(_log, "Sk6812SPI led's number has changed (old: %d, new: %d). Rebuilding buffer.", _ledCount, ledValues.size());
_ledCount = ledValues.size();
const int SPI_FRAME_END_LATCH_BYTES = 3;
_ledBuffer.resize(0, 0x00);
_ledBuffer.resize(_ledRGBWCount * SPI_BYTES_PER_COLOUR + SPI_FRAME_END_LATCH_BYTES, 0x00);
}
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,56 @@
#ifndef LEDEVICESK6812ftdi_H
#define LEDEVICESK6812ftdi_H
// HyperHDR includes
#include "ProviderFtdi.h"
///
/// Implementation of the LedDevice interface for writing to Sk6801 LED-device via SPI.
///
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,102 @@
#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, "SPI 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;
if (_ledCount != ledValues.size())
{
Warning(_log, "Led's number has changed (old: %d, new: %d). Rebuilding buffer.", _ledCount, ledValues.size());
_ledCount = ledValues.size();
_ledBuffer.resize(0, 0x00);
_ledBuffer.resize(_ledRGBCount * SPI_BYTES_PER_COLOUR + SPI_FRAME_END_LATCH_BYTES, 0x00);
}
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,248 @@
// LedDevice includes
#include <leddevice/LedDevice.h>
#include "ProviderFtdi.h"
#include <ftdi.h>
#include <libusb.h>
#include <thread>
#define ANY_FTDI_VENDOR 0x0
#define ANY_FTDI_PRODUCT 0x0
namespace Pin
{
// enumerate the AD bus for conveniance.
enum bus_t
{
SK = 0x01, // ADBUS0, SPI data clock
DO = 0x02, // ADBUS1, SPI data out
CS = 0x08, // ADBUS3, SPI chip select, active low
L0 = 0x10, // ADBUS4, SPI chip select, active high
};
}
// Use these pins as outputs
const unsigned char pinDirection = Pin::SK | Pin::DO | Pin::CS | Pin::L0;
const QString ProviderFtdi::AUTO_SETTING = QString("auto");
ProviderFtdi::ProviderFtdi(const QJsonObject &deviceConfig)
: LedDevice(deviceConfig),
_ftdic(NULL),
_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::openDevice()
{
_ftdic = ftdi_new();
bool autoDiscovery = (QString::compare(_deviceName, ProviderFtdi::AUTO_SETTING, Qt::CaseInsensitive) == 0);
Debug(_log, "Opening FTDI device=%s autoDiscovery=%s", QSTRING_CSTR(_deviceName), autoDiscovery ? "true" : "false");
if (autoDiscovery)
{
struct ftdi_device_list *devlist;
int devicesDetected = 0;
if ((devicesDetected = ftdi_usb_find_all(_ftdic, &devlist, ANY_FTDI_VENDOR, ANY_FTDI_PRODUCT)) < 0)
{
setInError(ftdi_get_error_string(_ftdic));
return -1;
}
if (devicesDetected == 0)
{
setInError("No ftdi devices detected");
return 0;
}
if (ftdi_usb_open_dev(_ftdic, devlist[0].dev) < 0)
{
setInError(ftdi_get_error_string(_ftdic));
ftdi_list_free(&devlist);
return -1;
}
ftdi_list_free(&devlist);
return 1;
}
else
{
if (ftdi_usb_open_string(_ftdic, QSTRING_CSTR(_deviceName)) < 0)
{
setInError(ftdi_get_error_string(_ftdic));
return -1;
}
return 1;
}
}
int ProviderFtdi::open()
{
int rc = 0;
if ((rc = openDevice()) != 1)
{
return -1;
}
/* doing this disable resets things if they were in a bad state */
if ((rc = ftdi_disable_bitbang(_ftdic)) < 0)
{
setInError(ftdi_get_error_string(_ftdic));
return rc;
}
if ((rc = ftdi_setflowctrl(_ftdic, SIO_DISABLE_FLOW_CTRL)) < 0)
{
setInError(ftdi_get_error_string(_ftdic));
return rc;
}
if ((rc = ftdi_set_bitmode(_ftdic, 0x00, BITMODE_RESET)) < 0)
{
setInError(ftdi_get_error_string(_ftdic));
return rc;
}
if ((rc = ftdi_set_bitmode(_ftdic, 0xff, BITMODE_MPSSE)) < 0)
{
setInError(ftdi_get_error_string(_ftdic));
return rc;
}
double reference_clock = 60e6;
int divisor = (reference_clock / 2 / _baudRate_Hz) - 1;
uint8_t buf[10] = {0};
unsigned int icmd = 0;
buf[icmd++] = DIS_DIV_5;
buf[icmd++] = TCK_DIVISOR;
buf[icmd++] = divisor;
buf[icmd++] = divisor >> 8;
buf[icmd++] = SET_BITS_LOW; // opcode: set low bits (ADBUS[0-7])
buf[icmd++] = Pin::CS & ~Pin::L0; // argument: inital pin states
buf[icmd++] = pinDirection;
if ((rc = ftdi_write_data(_ftdic, buf, icmd)) != icmd)
{
setInError(ftdi_get_error_string(_ftdic));
return rc;
}
_isDeviceReady = true;
return rc;
}
int ProviderFtdi::close()
{
// allow to clock out remaining data from powerOff()->writeBlack()
if (_ftdic != NULL)
{
std::this_thread::sleep_for(std::chrono::milliseconds(15));
Debug(_log, "Closing FTDI device");
ftdi_set_bitmode(_ftdic, 0x00, BITMODE_RESET);
ftdi_usb_close(_ftdic);
_ftdic = NULL;
}
return LedDevice::close();
}
void ProviderFtdi::setInError(const QString &errorMsg, bool isRecoverable)
{
close();
LedDevice::setInError(errorMsg);
}
int ProviderFtdi::writeBytes(const qint64 size, const uint8_t *data)
{
uint8_t buf[10] = {0};
unsigned int icmd = 0;
int rc = 0;
int count_arg = size - 1;
buf[icmd++] = SET_BITS_LOW;
buf[icmd++] = Pin::L0 & ~Pin::CS;
buf[icmd++] = pinDirection;
buf[icmd++] = MPSSE_DO_WRITE | MPSSE_WRITE_NEG;
buf[icmd++] = count_arg;
buf[icmd++] = count_arg >> 8;
if ((rc = ftdi_write_data(_ftdic, buf, icmd)) != icmd)
{
setInError(ftdi_get_error_string(_ftdic));
return rc;
}
if ((rc = ftdi_write_data(_ftdic, data, size)) != size)
{
setInError(ftdi_get_error_string(_ftdic));
return rc;
}
icmd = 0;
buf[icmd++] = SET_BITS_LOW;
buf[icmd++] = Pin::CS & ~Pin::L0;
buf[icmd++] = pinDirection;
if ((rc = ftdi_write_data(_ftdic, buf, icmd)) != icmd)
{
setInError(ftdi_get_error_string(_ftdic));
return rc;
}
return rc;
}
QJsonObject ProviderFtdi::discover(const QJsonObject & /*params*/)
{
QJsonObject devicesDiscovered;
QJsonArray deviceList;
struct ftdi_device_list *devlist;
struct ftdi_context *ftdic;
QJsonObject autoDevice = QJsonObject{{"value", AUTO_SETTING}, {"name", "Auto"}};
deviceList.push_back(autoDevice);
ftdic = ftdi_new();
if (ftdi_usb_find_all(ftdic, &devlist, ANY_FTDI_VENDOR, ANY_FTDI_PRODUCT) > 0)
{
struct ftdi_device_list *curdev = devlist;
while (curdev)
{
char manufacturer[128], description[128];
ftdi_usb_get_strings(ftdic, curdev->dev, manufacturer, 128, description, 128, NULL, 0);
libusb_device_descriptor desc;
libusb_get_device_descriptor(curdev->dev, &desc);
QString value = QString("i:0x%1:0x%2")
.arg(desc.idVendor, 4, 16, QChar{'0'})
.arg(desc.idProduct, 4, 16, QChar{'0'});
QString name = QString("%1 (%2)").arg(manufacturer, description);
deviceList.push_back(QJsonObject{
{"value", value},
{"name", name}});
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,79 @@
#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;
private:
int openDevice();
};
#endif // PROVIDERFtdi_H

View File

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

View File

@ -0,0 +1,60 @@
{
"type": "object",
"required": true,
"properties": {
"output": {
"type": "string",
"title": "edt_dev_spec_outputPath_title",
"default": "auto",
"required": true,
"propertyOrder": 1
},
"rate": {
"type": "integer",
"format": "stepper",
"step": 100000,
"title": "edt_dev_spec_baudrate_title",
"default": 3200000,
"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",
"hyperserial_cold_white",
"hyperserial_neutral_white",
"wled_auto",
"wled_auto_max",
"wled_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_hyperserial_cold_white",
"edt_dev_enum_hyperserial_neutral_white",
"edt_dev_enum_wled_auto",
"edt_dev_enum_wled_auto_max",
"edt_dev_enum_wled_auto_accurate",
"edt_dev_enum_white_off"
]
},
"propertyOrder": 5
}
},
"additionalProperties": true
}

View File

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

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,30 @@ WhiteAlgorithm stringToWhiteAlgorithm(const QString& str)
{
return WhiteAlgorithm::SUB_MIN_COOL_ADJUST;
}
if (str.isEmpty() || str == "white_off")
if (str == "hyperserial_cold_white")
{
return WhiteAlgorithm::HYPERSERIAL_COLD_WHITE;
}
if (str == "hyperserial_neutral_white")
{
return WhiteAlgorithm::HYPERSERIAL_NEUTRAL_WHITE;
}
if (str == "wled_auto")
{
return WhiteAlgorithm::WLED_AUTO;
}
if (str == "wled_auto_max")
{
return WhiteAlgorithm::WLED_AUTO_MAX;
}
if (str == "wled_auto_accurate")
{
return WhiteAlgorithm::WLED_AUTO_ACCURATE;
}
if (str.isEmpty() || str == "white_off")
{
return WhiteAlgorithm::WHITE_OFF;
}
@ -77,6 +102,63 @@ void Rgb_to_Rgbw(ColorRgb input, ColorRgbw * output, WhiteAlgorithm algorithm)
output->white = 0;
break;
}
case WhiteAlgorithm::WLED_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::WLED_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::WLED_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::HYPERSERIAL_NEUTRAL_WHITE:
case WhiteAlgorithm::HYPERSERIAL_COLD_WHITE:
{
//cold white config
uint8_t gain = 0xFF;
uint8_t red = 0xA0;
uint8_t green = 0xA0;
uint8_t blue = 0xA0;
if (algorithm == WhiteAlgorithm::HYPERSERIAL_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;
}