diff --git a/HyperionConfig.h.in b/HyperionConfig.h.in index 324b703b48..d719524701 100644 --- a/HyperionConfig.h.in +++ b/HyperionConfig.h.in @@ -61,9 +61,12 @@ // Define to enable the Serial devices #cmakedefine ENABLE_DEV_SERIAL -// Define to enable the SPI devices +// Define to enable the SPI spidev devices #cmakedefine ENABLE_DEV_SPI +// Define to enable the SPI ftdi devices +#cmakedefine ENABLE_DEV_FTDI + // Define to enable the Tinkerforge devices #cmakedefine ENABLE_DEV_TINKERFORGE diff --git a/assets/webconfig/i18n/en.json b/assets/webconfig/i18n/en.json index c923ad5e26..87cd560b5d 100644 --- a/assets/webconfig/i18n/en.json +++ b/assets/webconfig/i18n/en.json @@ -671,6 +671,7 @@ "edt_dev_spec_segmentsOverlapValidation_error": "Correct the WLED setup! The segment must not overlap with {{plural:$1| segment|segments}}: \"$2\".", "edt_dev_spec_serial_title": "Serial number", "edt_dev_spec_spipath_title": "SPI Device", + "edt_dev_spec_implementation_title": "SPI Implementation", "edt_dev_spec_sslHSTimeoutMax_title": "Streamer handshake timeout maximum", "edt_dev_spec_sslHSTimeoutMin_title": "Streamer handshake timeout minimum", "edt_dev_spec_stayOnAfterStreaming_title": "Stay on after streaming", diff --git a/libsrc/leddevice/CMakeLists.txt b/libsrc/leddevice/CMakeLists.txt index e4d16bab09..303045d08e 100644 --- a/libsrc/leddevice/CMakeLists.txt +++ b/libsrc/leddevice/CMakeLists.txt @@ -19,7 +19,6 @@ include_directories( dev_spi dev_rpi_pwm dev_tinker - dev_ftdi ) FILE ( GLOB Leddevice_SOURCES @@ -43,7 +42,7 @@ if ( ENABLE_DEV_SERIAL ) FILE ( GLOB Leddevice_SERIAL_SOURCES "${CURRENT_SOURCE_DIR}/dev_serial/*.h" "${CURRENT_SOURCE_DIR}/dev_serial/*.cpp") endif() -if ( ENABLE_DEV_SPI ) +if ( ENABLE_DEV_SPI OR ENABLE_DEV_FTDI ) FILE ( GLOB Leddevice_SPI_SOURCES "${CURRENT_SOURCE_DIR}/dev_spi/*.h" "${CURRENT_SOURCE_DIR}/dev_spi/*.cpp") endif() @@ -65,10 +64,6 @@ 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_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 @@ -80,7 +75,6 @@ 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 diff --git a/libsrc/leddevice/LedDeviceSchemas.qrc b/libsrc/leddevice/LedDeviceSchemas.qrc index d2a93fb551..be976000ca 100644 --- a/libsrc/leddevice/LedDeviceSchemas.qrc +++ b/libsrc/leddevice/LedDeviceSchemas.qrc @@ -38,8 +38,5 @@ schemas/schema-yeelight.json schemas/schema-razer.json schemas/schema-cololight.json - schemas/schema-ws2812_ftdi.json - schemas/schema-apa102_ftdi.json - schemas/schema-sk6812_ftdi.json diff --git a/libsrc/leddevice/dev_ftdi/LedDeviceAPA102_ftdi.cpp b/libsrc/leddevice/dev_ftdi/LedDeviceAPA102_ftdi.cpp deleted file mode 100644 index 32ae75701c..0000000000 --- a/libsrc/leddevice/dev_ftdi/LedDeviceAPA102_ftdi.cpp +++ /dev/null @@ -1,52 +0,0 @@ -#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 &ledValues) -{ - for (signed iLed = 0; iLed < static_cast(_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()); -} diff --git a/libsrc/leddevice/dev_ftdi/LedDeviceAPA102_ftdi.h b/libsrc/leddevice/dev_ftdi/LedDeviceAPA102_ftdi.h deleted file mode 100644 index 699332d73b..0000000000 --- a/libsrc/leddevice/dev_ftdi/LedDeviceAPA102_ftdi.h +++ /dev/null @@ -1,50 +0,0 @@ -#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& ledValues) override; - - /// The brighness level. Possibile values 1 .. 31. - int _brightnessControlMaxLevel; - -}; - -#endif // LEDEVICET_APA102_H diff --git a/libsrc/leddevice/dev_ftdi/LedDeviceSk6812_ftdi.cpp b/libsrc/leddevice/dev_ftdi/LedDeviceSk6812_ftdi.cpp deleted file mode 100644 index 81e8dadc5e..0000000000 --- a/libsrc/leddevice/dev_ftdi/LedDeviceSk6812_ftdi.cpp +++ /dev/null @@ -1,86 +0,0 @@ -#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); - Info(_log, "[%s] Setting maximum brightness to [%d]", QSTRING_CSTR(_activeDeviceType), _brightnessControlMaxLevel); - - - QString whiteAlgorithm = deviceConfig["whiteAlgorithm"].toString("white_off"); - - _whiteAlgorithm = RGBW::stringToWhiteAlgorithm(whiteAlgorithm); - - 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 &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()); -} diff --git a/libsrc/leddevice/dev_ftdi/LedDeviceSk6812_ftdi.h b/libsrc/leddevice/dev_ftdi/LedDeviceSk6812_ftdi.h deleted file mode 100644 index 017b0f30e5..0000000000 --- a/libsrc/leddevice/dev_ftdi/LedDeviceSk6812_ftdi.h +++ /dev/null @@ -1,52 +0,0 @@ -#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& 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 diff --git a/libsrc/leddevice/dev_ftdi/LedDeviceWs2812_ftdi.cpp b/libsrc/leddevice/dev_ftdi/LedDeviceWs2812_ftdi.cpp deleted file mode 100644 index 57c0ac035c..0000000000 --- a/libsrc/leddevice/dev_ftdi/LedDeviceWs2812_ftdi.cpp +++ /dev/null @@ -1,93 +0,0 @@ -#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 &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()); -} diff --git a/libsrc/leddevice/dev_ftdi/LedDeviceWs2812_ftdi.h b/libsrc/leddevice/dev_ftdi/LedDeviceWs2812_ftdi.h deleted file mode 100644 index 972b935b94..0000000000 --- a/libsrc/leddevice/dev_ftdi/LedDeviceWs2812_ftdi.h +++ /dev/null @@ -1,49 +0,0 @@ -#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& 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 diff --git a/libsrc/leddevice/dev_ftdi/ProviderFtdi.cpp b/libsrc/leddevice/dev_ftdi/ProviderFtdi.cpp deleted file mode 100644 index 77afaf89b5..0000000000 --- a/libsrc/leddevice/dev_ftdi/ProviderFtdi.cpp +++ /dev/null @@ -1,202 +0,0 @@ -// LedDevice includes -#include -#include "ProviderFtdi.h" -#include - -#include -#include - -#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 unsigned char pinInitialState = Pin::CS; -// Use these pins as outputs -const unsigned char 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(); - - 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 buf = { - DIS_DIV_5, - TCK_DIVISOR, - static_cast(divisor), - static_cast(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() -{ - 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 LedDevice::close(); -} - -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 buf = { - SET_BITS_LOW, - pinInitialState & ~Pin::CS, - pinDirection, - MPSSE_DO_WRITE | MPSSE_WRITE_NEG, - static_cast(count_arg), - static_cast(count_arg >> 8), -// LED's data will be inserted here - 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 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; -} diff --git a/libsrc/leddevice/dev_ftdi/ProviderFtdi.h b/libsrc/leddevice/dev_ftdi/ProviderFtdi.h deleted file mode 100644 index 955b26727f..0000000000 --- a/libsrc/leddevice/dev_ftdi/ProviderFtdi.h +++ /dev/null @@ -1,76 +0,0 @@ -#ifndef PROVIDERFtdi_H -#define PROVIDERFtdi_H - -// LedDevice includes -#include - -#include - -/// -/// 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 diff --git a/libsrc/leddevice/dev_spi/ProviderSpi.cpp b/libsrc/leddevice/dev_spi/ProviderSpi.cpp index f57c54f9eb..cb442c48ee 100644 --- a/libsrc/leddevice/dev_spi/ProviderSpi.cpp +++ b/libsrc/leddevice/dev_spi/ProviderSpi.cpp @@ -1,29 +1,64 @@ - -// STL includes +// STL includes #include #include #include #include - -// Linux includes -#include -#include -#include - // Local Hyperion includes #include "ProviderSpi.h" -#include -// qt includes +#ifdef ENABLE_DEV_SPI + // Linux includes + #include + #include + #include + // qt includes #include +#endif + +#include +#include + +#ifdef ENABLE_DEV_FTDI + #include + #include + #include + #define FTDI_CHECK_RESULT(statement) if (statement) {setInError(ftdi_get_error_string(_ftdic)); return retVal;} + + #define ANY_FTDI_VENDOR 0x0 + #define ANY_FTDI_PRODUCT 0x0 + + + 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 unsigned char pinInitialState = Pin::CS; + // Use these pins as outputs + const unsigned char pinDirection = Pin::SK | Pin::DO | Pin::CS; +#endif + + +#include + // Constants namespace { const bool verbose = false; - +#ifdef ENABLE_DEV_SPI // SPI discovery service const char DISCOVERY_DIRECTORY[] = "/dev/"; const char DISCOVERY_FILEPATTERN[] = "spidev*"; +#endif + + const QString ImplementationSPIDEV = QString("spidev"); + const QString ImplementationFTDI = QString("ftdi"); } //End of constants @@ -34,9 +69,12 @@ ProviderSpi::ProviderSpi(const QJsonObject &deviceConfig) , _fid(-1) , _spiMode(SPI_MODE_0) , _spiDataInvert(false) + , _spiImplementation(SPI_SPIDEV) { +#ifdef ENABLE_DEV_SPI memset(&_spi, 0, sizeof(_spi)); _latchTime_ms = 1; +#endif } ProviderSpi::~ProviderSpi() @@ -55,6 +93,9 @@ bool ProviderSpi::init(const QJsonObject &deviceConfig) _spiMode = deviceConfig["spimode"].toInt(_spiMode); _spiDataInvert = deviceConfig["invert"].toBool(_spiDataInvert); + bool isFtdiImplementation = (QString::compare(deviceConfig["implementation"].toString(ImplementationSPIDEV), ImplementationFTDI, Qt::CaseInsensitive) == 0); + _spiImplementation = isFtdiImplementation ? SPI_FTDI : SPI_SPIDEV; + Debug(_log, "_baudRate_Hz [%d], _latchTime_ms [%d]", _baudRate_Hz, _latchTime_ms); Debug(_log, "_spiDataInvert [%d], _spiMode [%d]", _spiDataInvert, _spiMode); @@ -65,104 +106,178 @@ bool ProviderSpi::init(const QJsonObject &deviceConfig) int ProviderSpi::open() { - int retval = -1; + int retVal = -1; QString errortext; _isDeviceReady = false; - - const int bitsPerWord = 8; - - _fid = ::open(QSTRING_CSTR(_deviceName), O_RDWR); - - if (_fid < 0) - { - errortext = QString ("Failed to open device (%1). Error message: %2").arg(_deviceName, strerror(errno)); - retval = -1; - } - else - { - if (ioctl(_fid, SPI_IOC_WR_MODE, &_spiMode) == -1 || ioctl(_fid, SPI_IOC_RD_MODE, &_spiMode) == -1) - { - retval = -2; - } - else - { - if (ioctl(_fid, SPI_IOC_WR_BITS_PER_WORD, &bitsPerWord) == -1 || ioctl(_fid, SPI_IOC_RD_BITS_PER_WORD, &bitsPerWord) == -1) - { - retval = -4; - } - else - { - if (ioctl(_fid, SPI_IOC_WR_MAX_SPEED_HZ, &_baudRate_Hz) == -1 || ioctl(_fid, SPI_IOC_RD_MAX_SPEED_HZ, &_baudRate_Hz) == -1) - { - retval = -6; - } - else - { - // Everything OK -> enable device - _isDeviceReady = true; - retval = 0; - } - } - } - if ( retval < 0 ) - { - errortext = QString ("Failed to open device (%1). Error Code: %2").arg(_deviceName).arg(retval); - } - } - - if ( retval < 0 ) - { - this->setInError( errortext ); - } - - return retval; +if (_spiImplementation == SPI_SPIDEV) { +#ifdef ENABLE_DEV_SPI + const int bitsPerWord = 8; + + _fid = ::open(QSTRING_CSTR(_deviceName), O_RDWR); + + if (_fid < 0) + { + errortext = QString ("Failed to open device (%1). Error message: %2").arg(_deviceName, strerror(errno)); + retVal = -1; + } + else + { + if (ioctl(_fid, SPI_IOC_WR_MODE, &_spiMode) == -1 || ioctl(_fid, SPI_IOC_RD_MODE, &_spiMode) == -1) + { + retVal = -2; + } + else + { + if (ioctl(_fid, SPI_IOC_WR_BITS_PER_WORD, &bitsPerWord) == -1 || ioctl(_fid, SPI_IOC_RD_BITS_PER_WORD, &bitsPerWord) == -1) + { + retVal = -4; + } + else + { + if (ioctl(_fid, SPI_IOC_WR_MAX_SPEED_HZ, &_baudRate_Hz) == -1 || ioctl(_fid, SPI_IOC_RD_MAX_SPEED_HZ, &_baudRate_Hz) == -1) + { + retVal = -6; + } + else + { + // Everything OK -> enable device + _isDeviceReady = true; + retVal = 0; + } + } + } + if ( retVal < 0 ) + { + errortext = QString ("Failed to open device (%1). Error Code: %2").arg(_deviceName).arg(retVal); + } + } + + if ( retVal < 0 ) + { + this->setInError( errortext ); + } +#endif +} else if (_spiImplementation == SPI_FTDI) { +#ifdef ENABLE_DEV_FTDI + + _ftdic = ftdi_new(); + + Debug(_log, "Opening FTDI device=%s", QSTRING_CSTR(_deviceName)); + + FTDI_CHECK_RESULT((retVal = ftdi_usb_open_string(_ftdic, QSTRING_CSTR(_deviceName))) < 0); + /* doing this disable resets things if they were in a bad state */ + FTDI_CHECK_RESULT((retVal = ftdi_disable_bitbang(_ftdic)) < 0); + FTDI_CHECK_RESULT((retVal = ftdi_setflowctrl(_ftdic, SIO_DISABLE_FLOW_CTRL)) < 0); + FTDI_CHECK_RESULT((retVal = ftdi_set_bitmode(_ftdic, 0x00, BITMODE_RESET)) < 0); + FTDI_CHECK_RESULT((retVal = ftdi_set_bitmode(_ftdic, 0xff, BITMODE_MPSSE)) < 0); + + + double reference_clock = 60e6; + int divisor = (reference_clock / 2 / _baudRate_Hz) - 1; + std::vector buf = { + DIS_DIV_5, + TCK_DIVISOR, + static_cast(divisor), + static_cast(divisor >> 8), + SET_BITS_LOW, // opcode: set low bits (ADBUS[0-7] + pinInitialState, // argument: inital pin state + pinDirection + }; + + FTDI_CHECK_RESULT((retVal = ftdi_write_data(_ftdic, buf.data(), buf.size())) != buf.size()); + + _isDeviceReady = true; +#endif +} + return retVal; } int ProviderSpi::close() { // LedDevice specific closing activities - int retval = 0; + int retVal = 0; _isDeviceReady = false; - - // Test, if device requires closing - if ( _fid > -1 ) - { - // Close device - if ( ::close(_fid) != 0 ) - { - Error( _log, "Failed to close device (%s). Error message: %s", QSTRING_CSTR(_deviceName), strerror(errno) ); - retval = -1; - } - } - return retval; + if (_spiImplementation == SPI_SPIDEV) { +#ifdef ENABLE_DEV_SPI + // Test, if device requires closing + if ( _fid > -1 ) + { + // Close device + if ( ::close(_fid) != 0 ) + { + Error( _log, "Failed to close device (%s). Error message: %s", QSTRING_CSTR(_deviceName), strerror(errno) ); + retVal = -1; + } + } +#endif + } else if (_spiImplementation == SPI_FTDI) { +#ifdef ENABLE_DEV_FTDI + 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; + } +#endif + } + return retVal; } int ProviderSpi::writeBytes(unsigned size, const uint8_t * data) { - if (_fid < 0) - { - return -1; - } - - uint8_t * newdata {nullptr}; - - _spi.tx_buf = __u64(data); - _spi.len = __u32(size); - - if (_spiDataInvert) - { - newdata = static_cast(malloc(size)); - for (unsigned i = 0; i(malloc(size)); + for (unsigned i = 0; i buf = { + SET_BITS_LOW, + pinInitialState & ~Pin::CS, + pinDirection, + MPSSE_DO_WRITE | MPSSE_WRITE_NEG, + static_cast(count_arg), + static_cast(count_arg >> 8), +// LED's data will be inserted here + 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((retVal = ftdi_write_data(_ftdic, buf.data(), buf.size())) != buf.size()); +#endif + } return retVal; } @@ -170,9 +285,9 @@ QJsonObject ProviderSpi::discover(const QJsonObject& /*params*/) { QJsonObject devicesDiscovered; devicesDiscovered.insert("ledDeviceType", _activeDeviceType ); - QJsonArray deviceList; +#ifdef ENABLE_DEV_SPI QDir deviceDirectory (DISCOVERY_DIRECTORY); QStringList deviceFilter(DISCOVERY_FILEPATTERN); deviceDirectory.setNameFilters(deviceFilter); @@ -185,10 +300,72 @@ QJsonObject ProviderSpi::discover(const QJsonObject& /*params*/) QJsonObject deviceInfo; deviceInfo.insert("deviceName", (*deviceFileIterator).fileName().remove(0,6)); deviceInfo.insert("systemLocation", (*deviceFileIterator).absoluteFilePath()); + deviceInfo.insert("implementation", ImplementationSPIDEV); deviceList.append(deviceInfo); } - devicesDiscovered.insert("devices", deviceList); - +#endif +#ifdef ENABLE_DEV_FTDI + 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 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}, + {"deviceName", description_string}, + {"systemLocation", ftdiOpenString}, + {"implementation", ImplementationFTDI}, + }); + deviceIndexes.insert(vendorAndProduct, deviceIndex + 1); + } + curdev = curdev->next; + } + } + + ftdi_list_free(&devlist); + ftdi_free(ftdic); +#endif + devicesDiscovered.insert("devices", deviceList); DebugIf(verbose,_log, "devicesDiscovered: [%s]", QString(QJsonDocument(devicesDiscovered).toJson(QJsonDocument::Compact)).toUtf8().constData()); return devicesDiscovered; diff --git a/libsrc/leddevice/dev_spi/ProviderSpi.h b/libsrc/leddevice/dev_spi/ProviderSpi.h index 63dbb20f70..ee556fe444 100644 --- a/libsrc/leddevice/dev_spi/ProviderSpi.h +++ b/libsrc/leddevice/dev_spi/ProviderSpi.h @@ -1,11 +1,24 @@ #pragma once -// Linux-SPI includes -#include +#ifdef ENABLE_DEV_SPI + // Linux-SPI includes + #include +#endif + +#ifdef ENABLE_DEV_FTDI + #include +#endif // Hyperion includes #include + +#ifndef SPI_MODE_0 + #define SPI_MODE_0 0 +#endif + +#define SPI_SPIDEV 0 +#define SPI_FTDI 1 /// /// The ProviderSpi implements an abstract base-class for LedDevices using the SPI-device. /// @@ -76,6 +89,14 @@ public slots: /// 1=>invert the data pattern bool _spiDataInvert; + uint8_t _spiImplementation; +#ifdef ENABLE_DEV_SPI /// The transfer structure for writing to the spi-device spi_ioc_transfer _spi; +#endif + +#ifdef ENABLE_DEV_FTDI + /// The Ftdi serial-device + struct ftdi_context *_ftdic; +#endif }; diff --git a/libsrc/leddevice/schemas/schema-apa102.json b/libsrc/leddevice/schemas/schema-apa102.json index 43bc649c62..174df99d6e 100644 --- a/libsrc/leddevice/schemas/schema-apa102.json +++ b/libsrc/leddevice/schemas/schema-apa102.json @@ -2,6 +2,15 @@ "type":"object", "required":true, "properties":{ + "implementation": { + "propertyOrder" : 6, + "type": "string", + "title": "edt_dev_spec_implementation_title", + "enum": ["spidev", "ftdi"], + "options": { + "enum_titles": [ "spidev", "ftdi" ] + } + }, "output": { "type": "string", "title":"edt_dev_spec_spipath_title", diff --git a/libsrc/leddevice/schemas/schema-apa102_ftdi.json b/libsrc/leddevice/schemas/schema-apa102_ftdi.json deleted file mode 100644 index 35ace3d01c..0000000000 --- a/libsrc/leddevice/schemas/schema-apa102_ftdi.json +++ /dev/null @@ -1,27 +0,0 @@ -{ - "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 -} \ No newline at end of file diff --git a/libsrc/leddevice/schemas/schema-sk6812_ftdi.json b/libsrc/leddevice/schemas/schema-sk6812_ftdi.json deleted file mode 100644 index 5667909b4d..0000000000 --- a/libsrc/leddevice/schemas/schema-sk6812_ftdi.json +++ /dev/null @@ -1,60 +0,0 @@ -{ - "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 -} diff --git a/libsrc/leddevice/schemas/schema-sk6812spi.json b/libsrc/leddevice/schemas/schema-sk6812spi.json index 49b7fef75d..41cd5bf110 100644 --- a/libsrc/leddevice/schemas/schema-sk6812spi.json +++ b/libsrc/leddevice/schemas/schema-sk6812spi.json @@ -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 }, diff --git a/libsrc/leddevice/schemas/schema-ws2812_ftdi.json b/libsrc/leddevice/schemas/schema-ws2812_ftdi.json deleted file mode 100644 index c6e7a57535..0000000000 --- a/libsrc/leddevice/schemas/schema-ws2812_ftdi.json +++ /dev/null @@ -1,20 +0,0 @@ -{ - "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 -} \ No newline at end of file