Skip to content

Commit

Permalink
Implement ftdi led device
Browse files Browse the repository at this point in the history
  • Loading branch information
nurikk-sa committed Mar 25, 2023
1 parent 2f09f9a commit 78462a5
Show file tree
Hide file tree
Showing 18 changed files with 962 additions and 4 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Expand Up @@ -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 )
Expand Down
6 changes: 6 additions & 0 deletions assets/webconfig/i18n/en.json
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
9 changes: 7 additions & 2 deletions assets/webconfig/js/content_leds.js
Expand Up @@ -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)
Expand All @@ -1659,18 +1661,21 @@ $(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')));
$("#leddevices").append(createSel(optArr[1], $.i18n('conf_leds_optgroup_RPiPWM')));
$("#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);
Expand Down
7 changes: 6 additions & 1 deletion include/utils/RgbToRgbw.h
Expand Up @@ -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);
Expand Down
14 changes: 14 additions & 0 deletions libsrc/leddevice/CMakeLists.txt
Expand Up @@ -19,6 +19,7 @@ include_directories(
dev_spi
dev_rpi_pwm
dev_tinker
dev_ftdi
)

FILE ( GLOB Leddevice_SOURCES
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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()

3 changes: 3 additions & 0 deletions libsrc/leddevice/LedDeviceSchemas.qrc
Expand Up @@ -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>
61 changes: 61 additions & 0 deletions libsrc/leddevice/dev_ftdi/LedDeviceAPA102_ftdi.cpp
@@ -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());
}
50 changes: 50 additions & 0 deletions libsrc/leddevice/dev_ftdi/LedDeviceAPA102_ftdi.h
@@ -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
93 changes: 93 additions & 0 deletions libsrc/leddevice/dev_ftdi/LedDeviceSk6812_ftdi.cpp
@@ -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());
}
56 changes: 56 additions & 0 deletions libsrc/leddevice/dev_ftdi/LedDeviceSk6812_ftdi.h
@@ -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

0 comments on commit 78462a5

Please sign in to comment.