From 913d6365424d2c6f56a50ba016e43885e36bbfce Mon Sep 17 00:00:00 2001 From: Ainur Timerbaev Date: Thu, 13 Apr 2023 12:32:04 +0100 Subject: [PATCH 1/7] Implement ftdi led device --- .ci/ci_install.sh | 2 +- .github/workflows/codeql.yml | 2 +- CMakeLists.txt | 5 + HyperionConfig.h.in | 5 +- assets/webconfig/i18n/en.json | 7 +- assets/webconfig/js/content_leds.js | 16 +- doc/development/CompileHowto.md | 8 +- include/utils/RgbToRgbw.h | 7 +- libsrc/leddevice/CMakeLists.txt | 9 +- libsrc/leddevice/dev_spi/ProviderSpi.cpp | 486 +++++++++++++++-------- libsrc/leddevice/dev_spi/ProviderSpi.h | 25 +- libsrc/utils/RgbToRgbw.cpp | 81 +++- 12 files changed, 474 insertions(+), 179 deletions(-) diff --git a/.ci/ci_install.sh b/.ci/ci_install.sh index 86c6ffd4b..69e34dd8e 100755 --- a/.ci/ci_install.sh +++ b/.ci/ci_install.sh @@ -35,6 +35,6 @@ function installAndUpgrade() if [[ $CI_NAME == 'osx' || $CI_NAME == 'darwin' ]]; then echo "Install dependencies" brew update - dependencies=("qt5" "python" "libusb" "cmake" "doxygen") + dependencies=("qt5" "python" "libusb" "cmake" "doxygen", "libftdi") installAndUpgrade "${dependencies[@]}" fi diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index 1565d699c..bf41f3aec 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -32,7 +32,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@v2 diff --git a/CMakeLists.txt b/CMakeLists.txt index e0c458828..aba68c497 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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_DEV_FTDI ON ) # Services SET ( DEFAULT_EFFECTENGINE ON ) @@ -117,6 +118,7 @@ IF ( ${CMAKE_SYSTEM} MATCHES "Linux" ) ELSEIF ( WIN32 ) SET ( DEFAULT_DX ON ) SET ( DEFAULT_MF ON ) + SET ( DEFAULT_DEV_FTDI OFF ) ELSE() SET ( DEFAULT_FB OFF ) SET ( DEFAULT_V4L2 OFF ) @@ -341,6 +343,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:") diff --git a/HyperionConfig.h.in b/HyperionConfig.h.in index 324b703b4..d71952470 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 6845685d1..6885f27cd 100644 --- a/assets/webconfig/i18n/en.json +++ b/assets/webconfig/i18n/en.json @@ -153,7 +153,7 @@ "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_RPiSPI": "SPI", "conf_leds_optgroup_debug": "Debug", "conf_leds_optgroup_network": "Network", "conf_leds_optgroup_other": "Other", @@ -567,6 +567,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", diff --git a/assets/webconfig/js/content_leds.js b/assets/webconfig/js/content_leds.js index 5cff7fd32..4374ddc88 100755 --- a/assets/webconfig/js/content_leds.js +++ b/assets/webconfig/js/content_leds.js @@ -18,7 +18,7 @@ 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 devRPiPWM = ['ws281x']; var devRPiGPIO = ['piblaster']; var devNET = ['atmoorb', 'cololight', 'fadecandy', 'philipshue', 'nanoleaf', 'razer', 'tinkerforge', 'tpm2net', 'udpe131', 'udpartnet', 'udpddp', 'udph801', 'udpraw', 'wled', 'yeelight']; @@ -1139,7 +1139,15 @@ $(document).ready(function () { }); hwLedCountDefault = 1; - colorOrderDefault = "rgb"; + + switch (ledType) { + case "sk6812spi": + colorOrderDefault = "grb"; + break; + default: + colorOrderDefault = "rgb"; + } + break; case "philipshue": @@ -1647,7 +1655,7 @@ $(document).ready(function () { optArr[5] = []; 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]); @@ -1899,7 +1907,7 @@ var updateOutputSelectList = function (ledType, discoveryInfo) { ledTypeGroup = "devNET"; } else if ($.inArray(ledType, devSerial) != -1) { ledTypeGroup = "devSerial"; - } else if ($.inArray(ledType, devRPiSPI) != -1) { + } else if ($.inArray(ledType, devSPI) != -1) { ledTypeGroup = "devRPiSPI"; } else if ($.inArray(ledType, devRPiGPIO) != -1) { ledTypeGroup = "devRPiGPIO"; diff --git a/doc/development/CompileHowto.md b/doc/development/CompileHowto.md index f3b5502ce..25e28e826 100644 --- a/doc/development/CompileHowto.md +++ b/doc/development/CompileHowto.md @@ -87,14 +87,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 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 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** @@ -136,7 +136,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..). @@ -145,7 +145,7 @@ To install on OS X you either need Homebrew or Macport but Homebrew is the recom First you need to install the dependencies: ```console -brew install qt5 python3 cmake libusb doxygen +brew install qt5 python3 cmake libusb doxygen libftdi ``` ## Windows diff --git a/include/utils/RgbToRgbw.h b/include/utils/RgbToRgbw.h index 0bcd6f463..4fb4e34b0 100644 --- a/include/utils/RgbToRgbw.h +++ b/include/utils/RgbToRgbw.h @@ -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); diff --git a/libsrc/leddevice/CMakeLists.txt b/libsrc/leddevice/CMakeLists.txt index 233bb9fd2..303045d08 100644 --- a/libsrc/leddevice/CMakeLists.txt +++ b/libsrc/leddevice/CMakeLists.txt @@ -42,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() @@ -164,3 +164,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() + diff --git a/libsrc/leddevice/dev_spi/ProviderSpi.cpp b/libsrc/leddevice/dev_spi/ProviderSpi.cpp index f57c54f9e..7044cc0d5 100644 --- a/libsrc/leddevice/dev_spi/ProviderSpi.cpp +++ b/libsrc/leddevice/dev_spi/ProviderSpi.cpp @@ -1,195 +1,359 @@ - +// Local Hyperion includes +#include "ProviderSpi.h" + +#ifdef ENABLE_DEV_SPI // STL includes #include #include #include #include - // Linux includes #include #include #include - -// Local Hyperion includes -#include "ProviderSpi.h" -#include - // qt includes #include +#endif -// Constants -namespace { - const bool verbose = false; - - // SPI discovery service - const char DISCOVERY_DIRECTORY[] = "/dev/"; - const char DISCOVERY_FILEPATTERN[] = "spidev*"; +#ifdef ENABLE_DEV_FTDI -} //End of constants +#include +#include +#include -ProviderSpi::ProviderSpi(const QJsonObject &deviceConfig) - : LedDevice(deviceConfig) - , _deviceName("/dev/spidev0.0") - , _baudRate_Hz(1000000) - , _fid(-1) - , _spiMode(SPI_MODE_0) - , _spiDataInvert(false) -{ - memset(&_spi, 0, sizeof(_spi)); - _latchTime_ms = 1; -} - -ProviderSpi::~ProviderSpi() -{ -} +#define FTDI_CHECK_RESULT(statement) if (statement) {setInError(ftdi_get_error_string(_ftdic)); return retVal;} -bool ProviderSpi::init(const QJsonObject &deviceConfig) -{ - bool isInitOK = false; +#define ANY_FTDI_VENDOR 0x0 +#define ANY_FTDI_PRODUCT 0x0 - // Initialise sub-class - if ( LedDevice::init(deviceConfig) ) - { - _deviceName = deviceConfig["output"].toString(_deviceName); - _baudRate_Hz = deviceConfig["rate"].toInt(_baudRate_Hz); - _spiMode = deviceConfig["spimode"].toInt(_spiMode); - _spiDataInvert = deviceConfig["invert"].toBool(_spiDataInvert); - Debug(_log, "_baudRate_Hz [%d], _latchTime_ms [%d]", _baudRate_Hz, _latchTime_ms); - Debug(_log, "_spiDataInvert [%d], _spiMode [%d]", _spiDataInvert, _spiMode); - - isInitOK = true; - } - return isInitOK; -} - -int ProviderSpi::open() -{ - 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; +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 + }; } -int ProviderSpi::close() -{ - // LedDevice specific closing activities - 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; -} +const unsigned char pinInitialState = Pin::CS; +// Use these pins as outputs +const unsigned char pinDirection = Pin::SK | Pin::DO | Pin::CS; +#endif -int ProviderSpi::writeBytes(unsigned size, const uint8_t * data) -{ - if (_fid < 0) - { - return -1; - } - uint8_t * newdata {nullptr}; +#include - _spi.tx_buf = __u64(data); - _spi.len = __u32(size); - if (_spiDataInvert) - { - newdata = static_cast(malloc(size)); - for (unsigned i = 0; i 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 == 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; +} - QFileInfoList::const_iterator deviceFileIterator; - for (deviceFileIterator = deviceFiles.constBegin(); deviceFileIterator != deviceFiles.constEnd(); ++deviceFileIterator) - { - QJsonObject deviceInfo; - deviceInfo.insert("deviceName", (*deviceFileIterator).fileName().remove(0,6)); - deviceInfo.insert("systemLocation", (*deviceFileIterator).absoluteFilePath()); - deviceList.append(deviceInfo); - } - devicesDiscovered.insert("devices", deviceList); +int ProviderSpi::close() { + // LedDevice specific closing activities + int retVal = 0; + _isDeviceReady = false; + if (_spiImplementation == 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 == 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; +} - DebugIf(verbose,_log, "devicesDiscovered: [%s]", QString(QJsonDocument(devicesDiscovered).toJson(QJsonDocument::Compact)).toUtf8().constData()); +int ProviderSpi::writeBytes(unsigned size, const uint8_t *data) { + int retVal = 0; + if (_spiImplementation == SPIDEV) { +#ifdef ENABLE_DEV_SPI + 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 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; +} - return devicesDiscovered; +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); + deviceDirectory.setSorting(QDir::Name); + QFileInfoList deviceFiles = deviceDirectory.entryInfoList(QDir::System); + + QFileInfoList::const_iterator deviceFileIterator; + for (deviceFileIterator = deviceFiles.constBegin(); deviceFileIterator != deviceFiles.constEnd(); ++deviceFileIterator) + { + QJsonObject deviceInfo; + deviceInfo.insert("deviceName", (*deviceFileIterator).fileName().remove(0,6)); + deviceInfo.insert("systemLocation", (*deviceFileIterator).absoluteFilePath()); + deviceList.append(deviceInfo); + } +#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} + }); + 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 63dbb20f7..65912cccb 100644 --- a/libsrc/leddevice/dev_spi/ProviderSpi.h +++ b/libsrc/leddevice/dev_spi/ProviderSpi.h @@ -1,11 +1,22 @@ #pragma once +#include "HyperionConfig.h" -// Linux-SPI includes -#include +#ifdef ENABLE_DEV_SPI + // Linux-SPI includes + #include +#endif + +#ifdef ENABLE_DEV_FTDI + #include +#endif // Hyperion includes #include + + +enum SpiImplementation { SPIDEV, FTDI }; + /// /// The ProviderSpi implements an abstract base-class for LedDevices using the SPI-device. /// @@ -66,7 +77,7 @@ public slots: /// The used baudrate of the output device int _baudRate_Hz; - +#ifdef ENABLE_DEV_SPI /// The File Identifier of the opened output device (or -1 if not opened) int _fid; @@ -78,4 +89,12 @@ public slots: /// 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 + + SpiImplementation _spiImplementation; }; diff --git a/libsrc/utils/RgbToRgbw.cpp b/libsrc/utils/RgbToRgbw.cpp index f82fadf58..7e96a1b6f 100644 --- a/libsrc/utils/RgbToRgbw.cpp +++ b/libsrc/utils/RgbToRgbw.cpp @@ -3,6 +3,8 @@ #include #include +#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; } From a119b24d6c6aaa2aa928555bf1c42373934d49f6 Mon Sep 17 00:00:00 2001 From: Ainur Timerbaev Date: Thu, 13 Apr 2023 12:34:38 +0100 Subject: [PATCH 2/7] Refactor to IS_FTDI_DEVICE_NAME --- libsrc/leddevice/dev_spi/ProviderSpi.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/libsrc/leddevice/dev_spi/ProviderSpi.cpp b/libsrc/leddevice/dev_spi/ProviderSpi.cpp index 7044cc0d5..1b02b7d46 100644 --- a/libsrc/leddevice/dev_spi/ProviderSpi.cpp +++ b/libsrc/leddevice/dev_spi/ProviderSpi.cpp @@ -22,7 +22,7 @@ #include #define FTDI_CHECK_RESULT(statement) if (statement) {setInError(ftdi_get_error_string(_ftdic)); return retVal;} - +#define IS_FTDI_DEVICE_NAME(deviceName) (deviceName.startsWith("d:") || deviceName.startsWith("i:") || deviceName.startsWith("s:")) #define ANY_FTDI_VENDOR 0x0 #define ANY_FTDI_PRODUCT 0x0 @@ -88,9 +88,7 @@ bool ProviderSpi::init(const QJsonObject &deviceConfig) { _spiDataInvert = deviceConfig["invert"].toBool(_spiDataInvert); Debug(_log, "_spiDataInvert [%d], _spiMode [%d]", _spiDataInvert, _spiMode); #endif - bool isFtdiImplementation = _deviceName.startsWith("d:") - or _deviceName.startsWith("i:") - or _deviceName.startsWith("s:"); + bool isFtdiImplementation = IS_FTDI_DEVICE_NAME(_deviceName); _spiImplementation = isFtdiImplementation ? FTDI : SPIDEV; Debug(_log, "_baudRate_Hz [%d], _latchTime_ms [%d]", _baudRate_Hz, _latchTime_ms); From 33769ce31e345022a8c349e4ada5f117e59badb0 Mon Sep 17 00:00:00 2001 From: Ainur Timerbaev Date: Thu, 13 Apr 2023 12:35:43 +0100 Subject: [PATCH 3/7] Remove extra fields from discovery response --- libsrc/leddevice/dev_spi/ProviderSpi.cpp | 7 ------- 1 file changed, 7 deletions(-) diff --git a/libsrc/leddevice/dev_spi/ProviderSpi.cpp b/libsrc/leddevice/dev_spi/ProviderSpi.cpp index 1b02b7d46..b6b034202 100644 --- a/libsrc/leddevice/dev_spi/ProviderSpi.cpp +++ b/libsrc/leddevice/dev_spi/ProviderSpi.cpp @@ -330,13 +330,6 @@ QJsonObject ProviderSpi::discover(const QJsonObject & /*params*/) { } 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} }); From e40c2833d1de66ef9c0bbdfd7c3dc3cff44290c5 Mon Sep 17 00:00:00 2001 From: Ainur Timerbaev Date: Thu, 13 Apr 2023 12:42:44 +0100 Subject: [PATCH 4/7] Add _ftdic(nullptr) --- libsrc/leddevice/dev_spi/ProviderSpi.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/libsrc/leddevice/dev_spi/ProviderSpi.cpp b/libsrc/leddevice/dev_spi/ProviderSpi.cpp index b6b034202..c541c0d3c 100644 --- a/libsrc/leddevice/dev_spi/ProviderSpi.cpp +++ b/libsrc/leddevice/dev_spi/ProviderSpi.cpp @@ -60,11 +60,16 @@ namespace { } //End of constants ProviderSpi::ProviderSpi(const QJsonObject &deviceConfig) - : LedDevice(deviceConfig), _deviceName("/dev/spidev0.0"), _baudRate_Hz(1000000) + : LedDevice(deviceConfig), + _deviceName("/dev/spidev0.0"), + _baudRate_Hz(1000000) #ifdef ENABLE_DEV_SPI , _fid(-1) , _spiMode(SPI_MODE_0) , _spiDataInvert(false) +#endif +#ifdef ENABLE_DEV_FTDI + , _ftdic(nullptr) #endif , _spiImplementation(SPIDEV) { #ifdef ENABLE_DEV_SPI From 97538fb1efac90a63da2e1d3260b2498acd02950 Mon Sep 17 00:00:00 2001 From: Ainur Timerbaev Date: Thu, 13 Apr 2023 13:36:46 +0100 Subject: [PATCH 5/7] Rm junk --- libsrc/leddevice/dev_spi/ProviderSpi.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/libsrc/leddevice/dev_spi/ProviderSpi.cpp b/libsrc/leddevice/dev_spi/ProviderSpi.cpp index c541c0d3c..c2d47e029 100644 --- a/libsrc/leddevice/dev_spi/ProviderSpi.cpp +++ b/libsrc/leddevice/dev_spi/ProviderSpi.cpp @@ -53,10 +53,6 @@ namespace { const char DISCOVERY_DIRECTORY[] = "/dev/"; const char DISCOVERY_FILEPATTERN[] = "spidev*"; #endif - - const QString ImplementationSPIDEV = QString("spidev"); - const QString ImplementationFTDI = QString("ftdi"); - } //End of constants ProviderSpi::ProviderSpi(const QJsonObject &deviceConfig) From 43b864b92ee44ea3ef5f59bc172dff2adc94f1ae Mon Sep 17 00:00:00 2001 From: Ainur Timerbaev Date: Mon, 17 Apr 2023 14:51:18 +0100 Subject: [PATCH 6/7] Refactor spidev and ftdi spi providers into separate classes --- libsrc/leddevice/CMakeLists.txt | 13 + libsrc/leddevice/dev_spi/LedDeviceAPA104.cpp | 4 +- .../leddevice/dev_spi/LedDeviceSk6812SPI.cpp | 4 +- .../leddevice/dev_spi/LedDeviceSk6822SPI.cpp | 4 +- .../leddevice/dev_spi/LedDeviceWs2812SPI.cpp | 4 +- libsrc/leddevice/dev_spi/ProviderSpi.cpp | 358 ++++-------------- libsrc/leddevice/dev_spi/ProviderSpi.h | 50 +-- .../dev_spi/providers/BaseProvider.cpp | 40 ++ .../dev_spi/providers/BaseProvider.h | 45 +++ .../dev_spi/providers/ProviderFtdi.cpp | 174 +++++++++ .../dev_spi/providers/ProviderFtdi.h | 30 ++ .../dev_spi/providers/ProviderSpidev.cpp | 141 +++++++ .../dev_spi/providers/ProviderSpidev.h | 45 +++ 13 files changed, 569 insertions(+), 343 deletions(-) create mode 100644 libsrc/leddevice/dev_spi/providers/BaseProvider.cpp create mode 100644 libsrc/leddevice/dev_spi/providers/BaseProvider.h create mode 100644 libsrc/leddevice/dev_spi/providers/ProviderFtdi.cpp create mode 100644 libsrc/leddevice/dev_spi/providers/ProviderFtdi.h create mode 100644 libsrc/leddevice/dev_spi/providers/ProviderSpidev.cpp create mode 100644 libsrc/leddevice/dev_spi/providers/ProviderSpidev.h diff --git a/libsrc/leddevice/CMakeLists.txt b/libsrc/leddevice/CMakeLists.txt index 303045d08..77d4ef03a 100644 --- a/libsrc/leddevice/CMakeLists.txt +++ b/libsrc/leddevice/CMakeLists.txt @@ -44,6 +44,18 @@ endif() 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") + + FILE ( GLOB Leddevice_SPI_PROVIDERS_SOURCES "${CURRENT_SOURCE_DIR}/dev_spi/providers/BaseProvider.*") + + if ( ENABLE_DEV_FTDI ) + file (GLOB FTDI_PROVIDER_SOURCES "${CURRENT_SOURCE_DIR}/dev_spi/providers/ProviderFtdi.*") + list (APPEND Leddevice_SPI_PROVIDERS_SOURCES ${FTDI_PROVIDER_SOURCES}) + endif() + + if ( ENABLE_DEV_SPI ) + file (GLOB SPIDEV_PROVIDER_SOURCES "${CURRENT_SOURCE_DIR}/dev_spi/providers/ProviderSpidev.*") + list (APPEND Leddevice_SPI_PROVIDERS_SOURCES ${SPIDEV_PROVIDER_SOURCES}) + endif() endif() if ( ENABLE_DEV_TINKERFORGE ) @@ -73,6 +85,7 @@ SET( Leddevice_SOURCES ${Leddevice_PWM_SOURCES} ${Leddevice_SERIAL_SOURCES} ${Leddevice_SPI_SOURCES} + ${Leddevice_SPI_PROVIDERS_SOURCES} ${Leddevice_TINKER_SOURCES} ${Leddevice_USB_HID_SOURCES} ) diff --git a/libsrc/leddevice/dev_spi/LedDeviceAPA104.cpp b/libsrc/leddevice/dev_spi/LedDeviceAPA104.cpp index 19f3db2f0..8af54c756 100644 --- a/libsrc/leddevice/dev_spi/LedDeviceAPA104.cpp +++ b/libsrc/leddevice/dev_spi/LedDeviceAPA104.cpp @@ -55,14 +55,14 @@ LedDevice* LedDeviceAPA104::construct(const QJsonObject &deviceConfig) bool LedDeviceAPA104::init(const QJsonObject &deviceConfig) { - _baudRate_Hz = 2235000; +// _baudRate_Hz = 2235000; bool isInitOK = false; // Initialise sub-class if ( ProviderSpi::init(deviceConfig) ) { - WarningIf(( _baudRate_Hz < 2000000 || _baudRate_Hz > 2470000 ), _log, "SPI rate %d outside recommended range (2000000 -> 2470000)", _baudRate_Hz); +// WarningIf(( _baudRate_Hz < 2000000 || _baudRate_Hz > 2470000 ), _log, "SPI rate %d outside recommended range (2000000 -> 2470000)", _baudRate_Hz); _ledBuffer.resize(_ledRGBCount * SPI_BYTES_PER_COLOUR + SPI_FRAME_END_LATCH_BYTES, 0x00); diff --git a/libsrc/leddevice/dev_spi/LedDeviceSk6812SPI.cpp b/libsrc/leddevice/dev_spi/LedDeviceSk6812SPI.cpp index 866e340c1..c70314cd6 100644 --- a/libsrc/leddevice/dev_spi/LedDeviceSk6812SPI.cpp +++ b/libsrc/leddevice/dev_spi/LedDeviceSk6812SPI.cpp @@ -20,7 +20,7 @@ LedDevice* LedDeviceSk6812SPI::construct(const QJsonObject &deviceConfig) bool LedDeviceSk6812SPI::init(const QJsonObject &deviceConfig) { - _baudRate_Hz = 3000000; +// _baudRate_Hz = 3000000; bool isInitOK = false; @@ -40,7 +40,7 @@ bool LedDeviceSk6812SPI::init(const QJsonObject &deviceConfig) { 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); +// 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); diff --git a/libsrc/leddevice/dev_spi/LedDeviceSk6822SPI.cpp b/libsrc/leddevice/dev_spi/LedDeviceSk6822SPI.cpp index 12d050dc7..37bdcc903 100644 --- a/libsrc/leddevice/dev_spi/LedDeviceSk6822SPI.cpp +++ b/libsrc/leddevice/dev_spi/LedDeviceSk6822SPI.cpp @@ -57,14 +57,14 @@ LedDevice* LedDeviceSk6822SPI::construct(const QJsonObject &deviceConfig) bool LedDeviceSk6822SPI::init(const QJsonObject &deviceConfig) { - _baudRate_Hz = 2230000; +// _baudRate_Hz = 2230000; bool isInitOK = false; // Initialise sub-class if ( ProviderSpi::init(deviceConfig) ) { - WarningIf(( _baudRate_Hz < 2000000 || _baudRate_Hz > 2460000 ), _log, "SPI rate %d outside recommended range (2000000 -> 2460000)", _baudRate_Hz); +// WarningIf(( _baudRate_Hz < 2000000 || _baudRate_Hz > 2460000 ), _log, "SPI rate %d outside recommended range (2000000 -> 2460000)", _baudRate_Hz); _ledBuffer.resize( (_ledRGBCount * SPI_BYTES_PER_COLOUR) + (_ledCount * SPI_BYTES_WAIT_TIME ) + SPI_FRAME_END_LATCH_BYTES, 0x00); isInitOK = true; diff --git a/libsrc/leddevice/dev_spi/LedDeviceWs2812SPI.cpp b/libsrc/leddevice/dev_spi/LedDeviceWs2812SPI.cpp index f3d4bcdb6..9a5cdab52 100644 --- a/libsrc/leddevice/dev_spi/LedDeviceWs2812SPI.cpp +++ b/libsrc/leddevice/dev_spi/LedDeviceWs2812SPI.cpp @@ -54,14 +54,14 @@ LedDevice* LedDeviceWs2812SPI::construct(const QJsonObject &deviceConfig) bool LedDeviceWs2812SPI::init(const QJsonObject &deviceConfig) { - _baudRate_Hz = 2600000; +// _baudRate_Hz = 2600000; bool isInitOK = false; // Initialise sub-class if ( ProviderSpi::init(deviceConfig) ) { - WarningIf(( _baudRate_Hz < 2106000 || _baudRate_Hz > 3075000 ), _log, "SPI rate %d outside recommended range (2106000 -> 3075000)", _baudRate_Hz); +// 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); diff --git a/libsrc/leddevice/dev_spi/ProviderSpi.cpp b/libsrc/leddevice/dev_spi/ProviderSpi.cpp index c2d47e029..e5377c1e7 100644 --- a/libsrc/leddevice/dev_spi/ProviderSpi.cpp +++ b/libsrc/leddevice/dev_spi/ProviderSpi.cpp @@ -1,77 +1,69 @@ -// Local Hyperion includes +#include "HyperionConfig.h" #include "ProviderSpi.h" -#ifdef ENABLE_DEV_SPI -// STL includes -#include -#include -#include -#include -// Linux includes -#include -#include -#include -// qt includes -#include +#ifdef ENABLE_SPIDEV +#include "providers/ProviderSpidev.h" #endif -#ifdef ENABLE_DEV_FTDI -#include -#include -#include +#ifdef ENABLE_FTDIDEV +#include "providers/ProviderFtdi.h" +#endif -#define FTDI_CHECK_RESULT(statement) if (statement) {setInError(ftdi_get_error_string(_ftdic)); return retVal;} -#define IS_FTDI_DEVICE_NAME(deviceName) (deviceName.startsWith("d:") || deviceName.startsWith("i:") || deviceName.startsWith("s:")) -#define ANY_FTDI_VENDOR 0x0 -#define ANY_FTDI_PRODUCT 0x0 +// Local HyperHDR includes +#include -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 - }; -} +namespace SPIProvider { + SpiImplementation deviceNameToSpiImplementation(const QString &deviceName) { + if ((deviceName.startsWith("d:") || deviceName.startsWith("i:") || deviceName.startsWith("s:"))) { + return SpiImplementation::FTDI; + } else { + return SpiImplementation::SPIDEV; + } + } -const unsigned char pinInitialState = Pin::CS; -// Use these pins as outputs -const unsigned char pinDirection = Pin::SK | Pin::DO | Pin::CS; + BaseProvider *construct(const QJsonObject &deviceConfig) { + QString deviceName = deviceConfig["output"].toString("unknown"); + switch (SPIProvider::deviceNameToSpiImplementation(deviceName)) { +#ifdef ENABLE_SPIDEV + case SPIDEV: + return dynamic_cast(new ProviderSpidev(deviceConfig)); + break; #endif +#ifdef ENABLE_FTDIDEV + case FTDI: + return dynamic_cast(new ProviderFtdi(deviceConfig)); + break; +#endif + default: + break; + } + return new BaseProvider(deviceConfig); + } + QJsonArray discover(const QJsonObject ¶ms) { + QJsonArray deviceList; +#ifdef ENABLE_SPIDEV + QJsonArray spidevDevices = (new ProviderSpidev(params))->discover(params); + for (const auto &item: spidevDevices){ + deviceList += item; + } +#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*"; +#ifdef ENABLE_FTDIDEV + QJsonArray ftdiDevices = (new ProviderFtdi(params))->discover(params); + for (const auto &item: ftdiDevices){ + deviceList += item; + } #endif -} //End of constants + return deviceList; + } +} ProviderSpi::ProviderSpi(const QJsonObject &deviceConfig) : LedDevice(deviceConfig), - _deviceName("/dev/spidev0.0"), - _baudRate_Hz(1000000) -#ifdef ENABLE_DEV_SPI - , _fid(-1) - , _spiMode(SPI_MODE_0) - , _spiDataInvert(false) -#endif -#ifdef ENABLE_DEV_FTDI - , _ftdic(nullptr) -#endif - , _spiImplementation(SPIDEV) { -#ifdef ENABLE_DEV_SPI - memset(&_spi, 0, sizeof(_spi)); - _latchTime_ms = 1; -#endif + _spiProvider(nullptr) { } ProviderSpi::~ProviderSpi() { @@ -83,18 +75,8 @@ bool ProviderSpi::init(const QJsonObject &deviceConfig) { // Initialise sub-class if (LedDevice::init(deviceConfig)) { _deviceName = deviceConfig["output"].toString(_deviceName); - _baudRate_Hz = deviceConfig["rate"].toInt(_baudRate_Hz); -#ifdef ENABLE_DEV_SPI - _spiMode = deviceConfig["spimode"].toInt(_spiMode); - _spiDataInvert = deviceConfig["invert"].toBool(_spiDataInvert); - Debug(_log, "_spiDataInvert [%d], _spiMode [%d]", _spiDataInvert, _spiMode); -#endif - bool isFtdiImplementation = IS_FTDI_DEVICE_NAME(_deviceName); - _spiImplementation = isFtdiImplementation ? FTDI : SPIDEV; - - Debug(_log, "_baudRate_Hz [%d], _latchTime_ms [%d]", _baudRate_Hz, _latchTime_ms); - - + Debug(_log, "_deviceName %s", QSTRING_CSTR(_deviceName)); + _spiProvider = SPIProvider::construct(deviceConfig); isInitOK = true; } return isInitOK; @@ -102,250 +84,42 @@ bool ProviderSpi::init(const QJsonObject &deviceConfig) { int ProviderSpi::open() { int retVal = -1; - QString errortext; - _isDeviceReady = false; - if (_spiImplementation == 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 == 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; + if ((retVal = _spiProvider->open())) { _isDeviceReady = true; -#endif } return retVal; } + int ProviderSpi::close() { // LedDevice specific closing activities int retVal = 0; _isDeviceReady = false; - if (_spiImplementation == 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 == 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 + if (_spiProvider != nullptr) { + retVal = _spiProvider->close(); } + return retVal; } int ProviderSpi::writeBytes(unsigned size, const uint8_t *data) { int retVal = 0; - if (_spiImplementation == SPIDEV) { -#ifdef ENABLE_DEV_SPI - 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 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 + if ((retVal = _spiProvider->writeBytes(size, data)) < 0) { + retVal = -1; } return retVal; } -QJsonObject ProviderSpi::discover(const QJsonObject & /*params*/) { +QJsonObject ProviderSpi::discover(const QJsonObject ¶ms) { QJsonObject devicesDiscovered; - devicesDiscovered.insert("ledDeviceType", _activeDeviceType); - QJsonArray deviceList; - -#ifdef ENABLE_DEV_SPI - QDir deviceDirectory (DISCOVERY_DIRECTORY); - QStringList deviceFilter(DISCOVERY_FILEPATTERN); - deviceDirectory.setNameFilters(deviceFilter); - deviceDirectory.setSorting(QDir::Name); - QFileInfoList deviceFiles = deviceDirectory.entryInfoList(QDir::System); - - QFileInfoList::const_iterator deviceFileIterator; - for (deviceFileIterator = deviceFiles.constBegin(); deviceFileIterator != deviceFiles.constEnd(); ++deviceFileIterator) - { - QJsonObject deviceInfo; - deviceInfo.insert("deviceName", (*deviceFileIterator).fileName().remove(0,6)); - deviceInfo.insert("systemLocation", (*deviceFileIterator).absoluteFilePath()); - deviceList.append(deviceInfo); - } -#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); + QJsonArray deviceList = SPIProvider::discover(params); - 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{ - {"deviceName", description_string}, - {"systemLocation", ftdiOpenString} - }); - deviceIndexes.insert(vendorAndProduct, deviceIndex + 1); - } - curdev = curdev->next; - } - } - - ftdi_list_free(&devlist); - ftdi_free(ftdic); -#endif + devicesDiscovered.insert("ledDeviceType", _activeDeviceType); devicesDiscovered.insert("devices", deviceList); - DebugIf(verbose, _log, "devicesDiscovered: [%s]", - QString(QJsonDocument(devicesDiscovered).toJson(QJsonDocument::Compact)).toUtf8().constData()); + + Debug(_log, "SPI devices discovered: [%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 65912cccb..6bcca178b 100644 --- a/libsrc/leddevice/dev_spi/ProviderSpi.h +++ b/libsrc/leddevice/dev_spi/ProviderSpi.h @@ -1,19 +1,8 @@ #pragma once -#include "HyperionConfig.h" -#ifdef ENABLE_DEV_SPI - // Linux-SPI includes - #include -#endif - -#ifdef ENABLE_DEV_FTDI - #include -#endif - -// Hyperion includes +// HyperHDR includes #include - - +#include enum SpiImplementation { SPIDEV, FTDI }; @@ -26,14 +15,14 @@ class ProviderSpi : public LedDevice /// /// Constructs specific LedDevice /// - ProviderSpi(const QJsonObject &deviceConfig); + ProviderSpi(const QJsonObject& deviceConfig); /// /// Sets configuration /// /// @param deviceConfig the json device config /// @return true if success - bool init(const QJsonObject &deviceConfig) override; + bool init(const QJsonObject& deviceConfig) override; /// /// Destructor of the LedDevice; closes the output device if it is open @@ -43,14 +32,10 @@ class ProviderSpi : public LedDevice /// /// Opens and configures the output device /// - /// @return Zero on succes else negative + /// @return Zero on success else negative /// int open() override; - /// @param[in] params Parameters used to overwrite discovery default behaviour - /// - /// @return A JSON structure holding a list of devices found - /// QJsonObject discover(const QJsonObject& params) override; public slots: @@ -70,31 +55,10 @@ public slots: /// /// @return Zero on success, else negative /// - int writeBytes(unsigned size, const uint8_t *data); + int writeBytes(unsigned size, const uint8_t* data); /// The name of the output device QString _deviceName; - /// The used baudrate of the output device - int _baudRate_Hz; -#ifdef ENABLE_DEV_SPI - /// The File Identifier of the opened output device (or -1 if not opened) - int _fid; - - /// which spi clock mode do we use? (0..3) - int _spiMode; - - /// 1=>invert the data pattern - bool _spiDataInvert; - - /// 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 - - SpiImplementation _spiImplementation; + BaseProvider * _spiProvider; }; diff --git a/libsrc/leddevice/dev_spi/providers/BaseProvider.cpp b/libsrc/leddevice/dev_spi/providers/BaseProvider.cpp new file mode 100644 index 000000000..309c4b7d1 --- /dev/null +++ b/libsrc/leddevice/dev_spi/providers/BaseProvider.cpp @@ -0,0 +1,40 @@ +#include "BaseProvider.h" + +#include +#include +#include + +BaseProvider::BaseProvider(const QJsonObject& deviceConfig): + _deviceName("/dev/spidev0.0"), + _baudRate_Hz(1000000), + _activeDeviceType("UNSPECIFIED") +{ + _activeDeviceType = deviceConfig["type"].toString(_activeDeviceType).toLower(); + _log = Logger::getInstance("PROVIDER_" + _activeDeviceType.toUpper()); + + _deviceName = deviceConfig["output"].toString(_deviceName); + _baudRate_Hz = deviceConfig["rate"].toInt(_baudRate_Hz); + + Debug(_log, "_deviceName [%s], _baudRate_Hz [%d]", QSTRING_CSTR(_deviceName), _baudRate_Hz); +} + +BaseProvider::~BaseProvider(){} + +int BaseProvider::open() { + Debug(_log, "Open"); + return 0; +} +int BaseProvider::writeBytes(unsigned size, const uint8_t *data) { + return 0; +} + +int BaseProvider::close() { + Debug(_log, "Close"); + return 0; +} + +QJsonArray BaseProvider::discover(const QJsonObject& /*params*/) +{ + QJsonArray deviceList; + return deviceList; +} \ No newline at end of file diff --git a/libsrc/leddevice/dev_spi/providers/BaseProvider.h b/libsrc/leddevice/dev_spi/providers/BaseProvider.h new file mode 100644 index 000000000..54ec00960 --- /dev/null +++ b/libsrc/leddevice/dev_spi/providers/BaseProvider.h @@ -0,0 +1,45 @@ +#pragma once +#include "QObject" +#include "utils/Logger.h" + +/// +/// The ProviderSpi implements an abstract base-class for LedDevices using the SPI-device. +/// +class BaseProvider +{ +public: + /// + /// Constructs specific LedDevice + /// + BaseProvider(const QJsonObject& deviceConfig); + /// + /// Destructor of the LedDevice; closes the output device if it is open + /// + ~BaseProvider(); + + /// + /// Opens and configures the output device + /// + /// @return Zero on success else negative + /// + virtual int open(); + + virtual QJsonArray discover(const QJsonObject& params); + + virtual int close(); + + virtual int writeBytes(unsigned size, const uint8_t *data); +protected: + + /// The common Logger instance for all LED-devices + Logger* _log; + + /// Current device's type + QString _activeDeviceType; + + /// The name of the output device + QString _deviceName; + + /// The used baudrate of the output device + int _baudRate_Hz; +}; diff --git a/libsrc/leddevice/dev_spi/providers/ProviderFtdi.cpp b/libsrc/leddevice/dev_spi/providers/ProviderFtdi.cpp new file mode 100644 index 000000000..638c71c55 --- /dev/null +++ b/libsrc/leddevice/dev_spi/providers/ProviderFtdi.cpp @@ -0,0 +1,174 @@ +#include "ProviderFtdi.h" + +#include + +#include +#include +#include + +#define ANY_FTDI_VENDOR 0x0 +#define ANY_FTDI_PRODUCT 0x0 + +#include +#include +#include +#include + +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 + }; +} + +#define FTDI_CHECK_RESULT(statement) if (statement) {Debug(_log, "FTDI ERROR: %s", ftdi_get_error_string(_ftdic)); return retVal;} + +const unsigned char pinInitialState = Pin::CS; +// Use these pins as outputs +const unsigned char pinDirection = Pin::SK | Pin::DO | Pin::CS; + +// Local HyperHDR includes +#include "utils/Logger.h" + + +ProviderFtdi::ProviderFtdi(const QJsonObject &deviceConfig): BaseProvider(deviceConfig), + _ftdic(nullptr) { + Debug(_log, "Initialise ProviderFtdi"); +} + +ProviderFtdi::~ProviderFtdi() {} + + + +int ProviderFtdi::open() { + int retVal = -1; + _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()); + Debug(_log, "Opened ftdi device=%s retVal=%d", QSTRING_CSTR(_deviceName), retVal); + return retVal; +} + + +int ProviderFtdi::close() { + int retVal = 0; + 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 retVal; +} + +int ProviderFtdi::writeBytes(unsigned size, const uint8_t *data) { + int retVal = 0; + + 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((retVal = (ftdi_write_data(_ftdic, buf.data(), buf.size())) != buf.size())); + return retVal; +} + +QJsonArray ProviderFtdi::discover(const QJsonObject & /*params*/) { + 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) + { + QMap deviceIndexes; + struct ftdi_device_list *curdev = devlist; + while (curdev) + { + char manufacturer[128] = {0}, serial_string[128] = {0}; + ftdi_usb_get_strings(ftdic, curdev->dev, manufacturer, 128, NULL, 0, serial_string, 128); + + libusb_device_descriptor desc; + libusb_get_device_descriptor(curdev->dev, &desc); + + QString vendorAndProduct = QString("0x%1:0x%2") + .arg(desc.idVendor, 4, 16, QChar{'0'}) + .arg(desc.idProduct, 4, 16, QChar{'0'}); + + QString serialNumber {serial_string}; + QString ftdiOpenString; + if(!serialNumber.isEmpty()) + { + ftdiOpenString = QString("s:%1:%2").arg(vendorAndProduct).arg(serialNumber); + } + else + { + uint8_t deviceIndex = deviceIndexes.value(vendorAndProduct, 0); + ftdiOpenString = QString("i:%1:%2").arg(vendorAndProduct).arg(deviceIndex); + deviceIndexes.insert(vendorAndProduct, deviceIndex + 1); + } + + QString displayLabel = QString("%1 (%2)") + .arg(ftdiOpenString) + .arg(manufacturer); + + deviceList.push_back(QJsonObject{ + {"value", ftdiOpenString}, + {"name", displayLabel} + }); + + curdev = curdev->next; + } + } + + ftdi_list_free(&devlist); + ftdi_free(ftdic); + + + Debug(_log, "SPI devices discovered: [%s]", + QString(QJsonDocument(deviceList).toJson(QJsonDocument::Compact)).toUtf8().constData()); + + return deviceList; +} diff --git a/libsrc/leddevice/dev_spi/providers/ProviderFtdi.h b/libsrc/leddevice/dev_spi/providers/ProviderFtdi.h new file mode 100644 index 000000000..ade473e5b --- /dev/null +++ b/libsrc/leddevice/dev_spi/providers/ProviderFtdi.h @@ -0,0 +1,30 @@ +#pragma once +#include +#include "providers/BaseProvider.h" + +class ProviderFtdi : public BaseProvider +{ +public: + /// + /// Constructs specific LedDevice + /// + ProviderFtdi(const QJsonObject& deviceConfig); + ~ProviderFtdi(); + + /// + /// Opens and configures the output device + /// + /// @return Zero on success else negative + /// + int open() override ; + + QJsonArray discover(const QJsonObject& params) override; + + int writeBytes(unsigned size, const uint8_t *data) override ; + + int close() override ; + +protected: + /// The Ftdi serial-device + struct ftdi_context *_ftdic; +}; diff --git a/libsrc/leddevice/dev_spi/providers/ProviderSpidev.cpp b/libsrc/leddevice/dev_spi/providers/ProviderSpidev.cpp new file mode 100644 index 000000000..cd63e8135 --- /dev/null +++ b/libsrc/leddevice/dev_spi/providers/ProviderSpidev.cpp @@ -0,0 +1,141 @@ +#include "ProviderSpidev.h" + +// STL includes +#include +#include +#include +#include + +// Linux includes +#include +#include +#include +#include + +#include +#include +#include + + +// Local HyperHDR includes +#include "utils/Logger.h" + + +ProviderSpidev::ProviderSpidev(const QJsonObject &deviceConfig) +: BaseProvider(deviceConfig), + _fid(-1), + _spiMode(SPI_MODE_0), + _spiDataInvert(false) { + memset(&_spi, 0, sizeof(_spi)); + + _spiMode = deviceConfig["spimode"].toInt(_spiMode); + _spiDataInvert = deviceConfig["invert"].toBool(_spiDataInvert); +} + +ProviderSpidev::~ProviderSpidev() { +} + +int ProviderSpidev::open() { + int retVal = -1; + QString errortext; + 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 + 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; +} + + +int ProviderSpidev::close() { + // LedDevice specific closing activities + int retVal = 0; + + // 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; +} + +int ProviderSpidev::writeBytes(unsigned size, const uint8_t *data) { + int retVal = 0; + + uint8_t *newdata = nullptr; + + if (_fid < 0) { + return -1; + } + + _spi.tx_buf = __u64(data); + _spi.len = __u32(size); + + if (_spiDataInvert) { + newdata = (uint8_t *) malloc(size); + for (unsigned i = 0; i < size; i++) { + newdata[i] = data[i] ^ 0xff; + } + _spi.tx_buf = __u64(newdata); + } + + retVal = ioctl(_fid, SPI_IOC_MESSAGE(1), &_spi); + ErrorIf((retVal < 0), _log, "SPI failed to write. errno: %d, %s", errno, strerror(errno)); + + if (newdata != nullptr) + free(newdata); + + return retVal; +} + +QJsonArray ProviderSpidev::discover(const QJsonObject& /*params*/) { + QJsonArray deviceList; + QStringList files; + QDirIterator it("/dev", QStringList() << "spidev*", QDir::System); + + while (it.hasNext()) + files << it.next(); + files.sort(); + + for (const auto &path: files) + deviceList.push_back(QJsonObject{ + {"value", path}, + {"name", path}}); + + Debug(_log, "SPI devices discovered: [%s]", + QString(QJsonDocument(deviceList).toJson(QJsonDocument::Compact)).toUtf8().constData()); + + return deviceList; +} diff --git a/libsrc/leddevice/dev_spi/providers/ProviderSpidev.h b/libsrc/leddevice/dev_spi/providers/ProviderSpidev.h new file mode 100644 index 000000000..8cd744be8 --- /dev/null +++ b/libsrc/leddevice/dev_spi/providers/ProviderSpidev.h @@ -0,0 +1,45 @@ +#pragma once +#include "providers/BaseProvider.h" +// Linux-SPI includes +#include +#include "QObject" + +/// +/// The ProviderSpi implements an abstract base-class for LedDevices using the SPI-device. +/// +class ProviderSpidev : public BaseProvider +{ +public: + /// + /// Constructs specific LedDevice + /// + ProviderSpidev(const QJsonObject& deviceConfig); + + /// + /// Destructor of the LedDevice; closes the output device if it is open + /// + ~ProviderSpidev(); + + /// + /// Opens and configures the output device + /// + /// @return Zero on succes else negative + /// + int open() override; + QJsonArray discover(const QJsonObject& params) override; + int close() override; + int writeBytes(unsigned size, const uint8_t* data) override; + +protected: + /// The File Identifier of the opened output device (or -1 if not opened) + int _fid; + + /// which spi clock mode do we use? (0..3) + int _spiMode; + + /// 1=>invert the data pattern + bool _spiDataInvert; + + /// The transfer structure for writing to the spi-device + spi_ioc_transfer _spi; +}; From f650d07d6ad5a77f390315b7a8f8e97904ec9723 Mon Sep 17 00:00:00 2001 From: Ainur Timerbaev Date: Wed, 19 Apr 2023 12:35:04 +0100 Subject: [PATCH 7/7] Add missing white algo options --- .../leddevice/schemas/schema-sk6812spi.json | 24 +++++++++++++++++-- libsrc/leddevice/schemas/schema-ws281x.json | 24 +++++++++++++++++-- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/libsrc/leddevice/schemas/schema-sk6812spi.json b/libsrc/leddevice/schemas/schema-sk6812spi.json index 49b7fef75..41cd5bf11 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-ws281x.json b/libsrc/leddevice/schemas/schema-ws281x.json index 2ccfb16d0..1af09eee4 100644 --- a/libsrc/leddevice/schemas/schema-ws281x.json +++ b/libsrc/leddevice/schemas/schema-ws281x.json @@ -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 },