From 5a902add81732a630d41344f6f02ca11a7a2d37e Mon Sep 17 00:00:00 2001 From: penfold42 Date: Sun, 14 Aug 2016 03:54:08 +1000 Subject: [PATCH] Added "invert" and "spimode" options to ws2801, ws2812spi and sk6812spi devices (#172) * Added "invert" and "spimode" options to ws2801, ws2812spi and sk6812spi devices * fixed test/TestSpi.cpp * some coding style wish --- libsrc/leddevice/LedDeviceFactory.cpp | 12 +++++++++--- libsrc/leddevice/LedDeviceSk6812SPI.cpp | 11 ++++++----- libsrc/leddevice/LedDeviceSk6812SPI.h | 3 ++- libsrc/leddevice/LedDeviceWs2801.cpp | 7 ++++--- libsrc/leddevice/LedDeviceWs2801.h | 4 +++- libsrc/leddevice/LedDeviceWs2812SPI.cpp | 9 +++++---- libsrc/leddevice/LedDeviceWs2812SPI.h | 4 ++-- libsrc/leddevice/LedSpiDevice.cpp | 25 ++++++++++++++++++------- libsrc/leddevice/LedSpiDevice.h | 11 ++++++++++- test/TestSpi.cpp | 4 ++-- 10 files changed, 61 insertions(+), 29 deletions(-) diff --git a/libsrc/leddevice/LedDeviceFactory.cpp b/libsrc/leddevice/LedDeviceFactory.cpp index 02ede739..e7550884 100755 --- a/libsrc/leddevice/LedDeviceFactory.cpp +++ b/libsrc/leddevice/LedDeviceFactory.cpp @@ -116,14 +116,18 @@ LedDevice * LedDeviceFactory::construct(const Json::Value & deviceConfig) device = new LedDeviceWs2801( deviceConfig["output"].asString(), deviceConfig["rate"].asInt(), - deviceConfig.get("latchtime",500000).asInt() + deviceConfig.get("latchtime",500000).asInt(), + deviceConfig.get("spimode",0).asInt(), + deviceConfig.get("invert",false).asBool() ); } else if (type == "ws2812spi") { device = new LedDeviceWs2812SPI( deviceConfig["output"].asString(), - deviceConfig.get("rate",2857143).asInt() + deviceConfig.get("rate",2857143).asInt(), + deviceConfig.get("spimode",0).asInt(), + deviceConfig.get("invert",false).asBool() ); } else if (type == "sk6812rgbw-spi") @@ -131,7 +135,9 @@ LedDevice * LedDeviceFactory::construct(const Json::Value & deviceConfig) device = new LedDeviceSk6812SPI( deviceConfig["output"].asString(), deviceConfig.get("rate",2857143).asInt(), - deviceConfig.get("white_algorithm","").asString() + deviceConfig.get("white_algorithm","").asString(), + deviceConfig.get("spimode",0).asInt(), + deviceConfig.get("invert",false).asBool() ); } #endif diff --git a/libsrc/leddevice/LedDeviceSk6812SPI.cpp b/libsrc/leddevice/LedDeviceSk6812SPI.cpp index 0e53e860..fd8a9dcd 100644 --- a/libsrc/leddevice/LedDeviceSk6812SPI.cpp +++ b/libsrc/leddevice/LedDeviceSk6812SPI.cpp @@ -11,11 +11,12 @@ // hyperion local includes #include "LedDeviceSk6812SPI.h" -LedDeviceSk6812SPI::LedDeviceSk6812SPI(const std::string& outputDevice, const unsigned baudrate, const std::string& whiteAlgorithm) : - LedSpiDevice(outputDevice, baudrate, 0), - mLedCount(0), - _whiteAlgorithm(whiteAlgorithm), - bitpair_to_byte { +LedDeviceSk6812SPI::LedDeviceSk6812SPI(const std::string& outputDevice, const unsigned baudrate, const std::string& whiteAlgorithm, + const int spiMode, const bool spiDataInvert) + : LedSpiDevice(outputDevice, baudrate, 0, spiMode, spiDataInvert) + , mLedCount(0) + , _whiteAlgorithm(whiteAlgorithm) + , bitpair_to_byte { 0b10001000, 0b10001100, 0b11001000, diff --git a/libsrc/leddevice/LedDeviceSk6812SPI.h b/libsrc/leddevice/LedDeviceSk6812SPI.h index 1b7df8b7..619c8dbc 100644 --- a/libsrc/leddevice/LedDeviceSk6812SPI.h +++ b/libsrc/leddevice/LedDeviceSk6812SPI.h @@ -20,7 +20,8 @@ public: /// LedDeviceSk6812SPI(const std::string& outputDevice, const unsigned baudrate, - const std::string& whiteAlgorithm); + const std::string& whiteAlgorithm, + const int spiMode, const bool spiDataInvert); /// /// Writes the led color values to the led-device diff --git a/libsrc/leddevice/LedDeviceWs2801.cpp b/libsrc/leddevice/LedDeviceWs2801.cpp index 2bc65b3d..e6082a70 100644 --- a/libsrc/leddevice/LedDeviceWs2801.cpp +++ b/libsrc/leddevice/LedDeviceWs2801.cpp @@ -11,9 +11,10 @@ // hyperion local includes #include "LedDeviceWs2801.h" -LedDeviceWs2801::LedDeviceWs2801(const std::string& outputDevice, const unsigned baudrate, const unsigned latchTime) : - LedSpiDevice(outputDevice, baudrate, latchTime), - mLedCount(0) +LedDeviceWs2801::LedDeviceWs2801(const std::string& outputDevice, const unsigned baudrate, const unsigned latchTime, + const int spiMode, const bool spiDataInvert) + : LedSpiDevice(outputDevice, baudrate, latchTime, spiMode, spiDataInvert) + , mLedCount(0) { // empty } diff --git a/libsrc/leddevice/LedDeviceWs2801.h b/libsrc/leddevice/LedDeviceWs2801.h index 2f6606a3..9973e8d9 100644 --- a/libsrc/leddevice/LedDeviceWs2801.h +++ b/libsrc/leddevice/LedDeviceWs2801.h @@ -21,7 +21,9 @@ public: LedDeviceWs2801(const std::string& outputDevice, const unsigned baudrate, - const unsigned latchTime); + const unsigned latchTime, + const int spiMode, + const bool spiDataInvert); /// /// Writes the led color values to the led-device diff --git a/libsrc/leddevice/LedDeviceWs2812SPI.cpp b/libsrc/leddevice/LedDeviceWs2812SPI.cpp index b9dc8a3d..efc9eb77 100644 --- a/libsrc/leddevice/LedDeviceWs2812SPI.cpp +++ b/libsrc/leddevice/LedDeviceWs2812SPI.cpp @@ -11,10 +11,11 @@ // hyperion local includes #include "LedDeviceWs2812SPI.h" -LedDeviceWs2812SPI::LedDeviceWs2812SPI(const std::string& outputDevice, const unsigned baudrate) : - LedSpiDevice(outputDevice, baudrate, 0), - mLedCount(0), - bitpair_to_byte { +LedDeviceWs2812SPI::LedDeviceWs2812SPI(const std::string& outputDevice, const unsigned baudrate, + const int spiMode, const bool spiDataInvert) + : LedSpiDevice(outputDevice, baudrate, 0, spiMode, spiDataInvert) + , mLedCount(0) + , bitpair_to_byte { 0b10001000, 0b10001100, 0b11001000, diff --git a/libsrc/leddevice/LedDeviceWs2812SPI.h b/libsrc/leddevice/LedDeviceWs2812SPI.h index 2451c772..d354ebf0 100644 --- a/libsrc/leddevice/LedDeviceWs2812SPI.h +++ b/libsrc/leddevice/LedDeviceWs2812SPI.h @@ -18,8 +18,8 @@ public: /// @param outputDevice The name of the output device (eg '/etc/SpiDev.0.0') /// @param baudrate The used baudrate for writing to the output device /// - LedDeviceWs2812SPI(const std::string& outputDevice, - const unsigned baudrate); + LedDeviceWs2812SPI(const std::string& outputDevice, const unsigned baudrate, + const int spiMode, const bool spiDataInvert); /// /// Writes the led color values to the led-device diff --git a/libsrc/leddevice/LedSpiDevice.cpp b/libsrc/leddevice/LedSpiDevice.cpp index fb258385..511d84ff 100644 --- a/libsrc/leddevice/LedSpiDevice.cpp +++ b/libsrc/leddevice/LedSpiDevice.cpp @@ -14,11 +14,14 @@ #include -LedSpiDevice::LedSpiDevice(const std::string& outputDevice, const unsigned baudrate, const int latchTime_ns) : - mDeviceName(outputDevice), - mBaudRate_Hz(baudrate), - mLatchTime_ns(latchTime_ns), - mFid(-1) +LedSpiDevice::LedSpiDevice(const std::string& outputDevice, const unsigned baudrate, const int latchTime_ns, + const int spiMode, const bool spiDataInvert) + : mDeviceName(outputDevice) + , mBaudRate_Hz(baudrate) + , mLatchTime_ns(latchTime_ns) + , mFid(-1) + , mSpiMode(spiMode) + , mSpiDataInvert(spiDataInvert) { memset(&spi, 0, sizeof(spi)); } @@ -30,6 +33,7 @@ LedSpiDevice::~LedSpiDevice() int LedSpiDevice::open() { +//printf ("mSpiDataInvert %d mSpiMode %d\n",mSpiDataInvert, mSpiMode); const int bitsPerWord = 8; mFid = ::open(mDeviceName.c_str(), O_RDWR); @@ -40,8 +44,7 @@ int LedSpiDevice::open() return -1; } - int mode = SPI_MODE_0; - if (ioctl(mFid, SPI_IOC_WR_MODE, &mode) == -1 || ioctl(mFid, SPI_IOC_RD_MODE, &mode) == -1) + if (ioctl(mFid, SPI_IOC_WR_MODE, &mSpiMode) == -1 || ioctl(mFid, SPI_IOC_RD_MODE, &mSpiMode) == -1) { return -2; } @@ -69,6 +72,14 @@ int LedSpiDevice::writeBytes(const unsigned size, const uint8_t * data) spi.tx_buf = __u64(data); spi.len = __u32(size); + if (mSpiDataInvert) { + uint8_t * newdata = (uint8_t *)malloc(size); + for (unsigned i = 0; i 0) diff --git a/libsrc/leddevice/LedSpiDevice.h b/libsrc/leddevice/LedSpiDevice.h index 353413cf..6f458eea 100644 --- a/libsrc/leddevice/LedSpiDevice.h +++ b/libsrc/leddevice/LedSpiDevice.h @@ -20,7 +20,9 @@ public: /// @param[in] latchTime_ns The latch-time to latch in the values across the SPI-device (negative /// means no latch required) [ns] /// - LedSpiDevice(const std::string& outputDevice, const unsigned baudrate, const int latchTime_ns = -1); + LedSpiDevice(const std::string& outputDevice, const unsigned baudrate, const int latchTime_ns = -1, + const int spiMode = SPI_MODE_0, const bool spiDataInvert = false); + /// /// Destructor of the LedDevice; closes the output device if it is open @@ -56,6 +58,13 @@ private: /// The File Identifier of the opened output device (or -1 if not opened) int mFid; + + /// which spi clock mode do we use? (0..3) + int mSpiMode; + + /// 1=>invert the data pattern + bool mSpiDataInvert; + /// The transfer structure for writing to the spi-device spi_ioc_transfer spi; }; diff --git a/test/TestSpi.cpp b/test/TestSpi.cpp index d1fbe836..c8420d41 100644 --- a/test/TestSpi.cpp +++ b/test/TestSpi.cpp @@ -54,7 +54,7 @@ void setColor(char* colorStr) unsigned ledCnt = 50; std::vector buff(ledCnt, color); - LedDeviceWs2801 ledDevice("/dev/spidev0.0", 40000, 500000); + LedDeviceWs2801 ledDevice("/dev/spidev0.0", 40000, 500000, 0, 0); ledDevice.open(); ledDevice.write(buff); } @@ -68,7 +68,7 @@ void doCircle() unsigned ledCnt = 50; std::vector data(ledCnt, ColorRgb::BLACK); - LedDeviceWs2801 ledDevice("/dev/spidev0.0", 40000, 500000); + LedDeviceWs2801 ledDevice("/dev/spidev0.0", 40000, 500000, 0, 0); ledDevice.open(); timespec loopTime;