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
This commit is contained in:
penfold42 2016-08-14 03:54:08 +10:00 committed by redPanther
parent 9c27db390d
commit 5a902add81
10 changed files with 61 additions and 29 deletions

View File

@ -116,14 +116,18 @@ LedDevice * LedDeviceFactory::construct(const Json::Value & deviceConfig)
device = new LedDeviceWs2801( device = new LedDeviceWs2801(
deviceConfig["output"].asString(), deviceConfig["output"].asString(),
deviceConfig["rate"].asInt(), 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") else if (type == "ws2812spi")
{ {
device = new LedDeviceWs2812SPI( device = new LedDeviceWs2812SPI(
deviceConfig["output"].asString(), 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") else if (type == "sk6812rgbw-spi")
@ -131,7 +135,9 @@ LedDevice * LedDeviceFactory::construct(const Json::Value & deviceConfig)
device = new LedDeviceSk6812SPI( device = new LedDeviceSk6812SPI(
deviceConfig["output"].asString(), deviceConfig["output"].asString(),
deviceConfig.get("rate",2857143).asInt(), 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 #endif

View File

@ -11,11 +11,12 @@
// hyperion local includes // hyperion local includes
#include "LedDeviceSk6812SPI.h" #include "LedDeviceSk6812SPI.h"
LedDeviceSk6812SPI::LedDeviceSk6812SPI(const std::string& outputDevice, const unsigned baudrate, const std::string& whiteAlgorithm) : LedDeviceSk6812SPI::LedDeviceSk6812SPI(const std::string& outputDevice, const unsigned baudrate, const std::string& whiteAlgorithm,
LedSpiDevice(outputDevice, baudrate, 0), const int spiMode, const bool spiDataInvert)
mLedCount(0), : LedSpiDevice(outputDevice, baudrate, 0, spiMode, spiDataInvert)
_whiteAlgorithm(whiteAlgorithm), , mLedCount(0)
bitpair_to_byte { , _whiteAlgorithm(whiteAlgorithm)
, bitpair_to_byte {
0b10001000, 0b10001000,
0b10001100, 0b10001100,
0b11001000, 0b11001000,

View File

@ -20,7 +20,8 @@ public:
/// ///
LedDeviceSk6812SPI(const std::string& outputDevice, const unsigned baudrate, 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 /// Writes the led color values to the led-device

View File

@ -11,9 +11,10 @@
// hyperion local includes // hyperion local includes
#include "LedDeviceWs2801.h" #include "LedDeviceWs2801.h"
LedDeviceWs2801::LedDeviceWs2801(const std::string& outputDevice, const unsigned baudrate, const unsigned latchTime) : LedDeviceWs2801::LedDeviceWs2801(const std::string& outputDevice, const unsigned baudrate, const unsigned latchTime,
LedSpiDevice(outputDevice, baudrate, latchTime), const int spiMode, const bool spiDataInvert)
mLedCount(0) : LedSpiDevice(outputDevice, baudrate, latchTime, spiMode, spiDataInvert)
, mLedCount(0)
{ {
// empty // empty
} }

View File

@ -21,7 +21,9 @@ public:
LedDeviceWs2801(const std::string& outputDevice, LedDeviceWs2801(const std::string& outputDevice,
const unsigned baudrate, const unsigned baudrate,
const unsigned latchTime); const unsigned latchTime,
const int spiMode,
const bool spiDataInvert);
/// ///
/// Writes the led color values to the led-device /// Writes the led color values to the led-device

View File

@ -11,10 +11,11 @@
// hyperion local includes // hyperion local includes
#include "LedDeviceWs2812SPI.h" #include "LedDeviceWs2812SPI.h"
LedDeviceWs2812SPI::LedDeviceWs2812SPI(const std::string& outputDevice, const unsigned baudrate) : LedDeviceWs2812SPI::LedDeviceWs2812SPI(const std::string& outputDevice, const unsigned baudrate,
LedSpiDevice(outputDevice, baudrate, 0), const int spiMode, const bool spiDataInvert)
mLedCount(0), : LedSpiDevice(outputDevice, baudrate, 0, spiMode, spiDataInvert)
bitpair_to_byte { , mLedCount(0)
, bitpair_to_byte {
0b10001000, 0b10001000,
0b10001100, 0b10001100,
0b11001000, 0b11001000,

View File

@ -18,8 +18,8 @@ public:
/// @param outputDevice The name of the output device (eg '/etc/SpiDev.0.0') /// @param outputDevice The name of the output device (eg '/etc/SpiDev.0.0')
/// @param baudrate The used baudrate for writing to the output device /// @param baudrate The used baudrate for writing to the output device
/// ///
LedDeviceWs2812SPI(const std::string& outputDevice, LedDeviceWs2812SPI(const std::string& outputDevice, const unsigned baudrate,
const unsigned baudrate); const int spiMode, const bool spiDataInvert);
/// ///
/// Writes the led color values to the led-device /// Writes the led color values to the led-device

View File

@ -14,11 +14,14 @@
#include <utils/Logger.h> #include <utils/Logger.h>
LedSpiDevice::LedSpiDevice(const std::string& outputDevice, const unsigned baudrate, const int latchTime_ns) : LedSpiDevice::LedSpiDevice(const std::string& outputDevice, const unsigned baudrate, const int latchTime_ns,
mDeviceName(outputDevice), const int spiMode, const bool spiDataInvert)
mBaudRate_Hz(baudrate), : mDeviceName(outputDevice)
mLatchTime_ns(latchTime_ns), , mBaudRate_Hz(baudrate)
mFid(-1) , mLatchTime_ns(latchTime_ns)
, mFid(-1)
, mSpiMode(spiMode)
, mSpiDataInvert(spiDataInvert)
{ {
memset(&spi, 0, sizeof(spi)); memset(&spi, 0, sizeof(spi));
} }
@ -30,6 +33,7 @@ LedSpiDevice::~LedSpiDevice()
int LedSpiDevice::open() int LedSpiDevice::open()
{ {
//printf ("mSpiDataInvert %d mSpiMode %d\n",mSpiDataInvert, mSpiMode);
const int bitsPerWord = 8; const int bitsPerWord = 8;
mFid = ::open(mDeviceName.c_str(), O_RDWR); mFid = ::open(mDeviceName.c_str(), O_RDWR);
@ -40,8 +44,7 @@ int LedSpiDevice::open()
return -1; return -1;
} }
int mode = SPI_MODE_0; if (ioctl(mFid, SPI_IOC_WR_MODE, &mSpiMode) == -1 || ioctl(mFid, SPI_IOC_RD_MODE, &mSpiMode) == -1)
if (ioctl(mFid, SPI_IOC_WR_MODE, &mode) == -1 || ioctl(mFid, SPI_IOC_RD_MODE, &mode) == -1)
{ {
return -2; return -2;
} }
@ -69,6 +72,14 @@ int LedSpiDevice::writeBytes(const unsigned size, const uint8_t * data)
spi.tx_buf = __u64(data); spi.tx_buf = __u64(data);
spi.len = __u32(size); spi.len = __u32(size);
if (mSpiDataInvert) {
uint8_t * newdata = (uint8_t *)malloc(size);
for (unsigned i = 0; i<size; i++) {
newdata[i] = data[i] ^ 0xff;
}
spi.tx_buf = __u64(newdata);
}
int retVal = ioctl(mFid, SPI_IOC_MESSAGE(1), &spi); int retVal = ioctl(mFid, SPI_IOC_MESSAGE(1), &spi);
if (retVal == 0 && mLatchTime_ns > 0) if (retVal == 0 && mLatchTime_ns > 0)

View File

@ -20,7 +20,9 @@ public:
/// @param[in] latchTime_ns The latch-time to latch in the values across the SPI-device (negative /// @param[in] latchTime_ns The latch-time to latch in the values across the SPI-device (negative
/// means no latch required) [ns] /// 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 /// 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) /// The File Identifier of the opened output device (or -1 if not opened)
int mFid; 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 /// The transfer structure for writing to the spi-device
spi_ioc_transfer spi; spi_ioc_transfer spi;
}; };

View File

@ -54,7 +54,7 @@ void setColor(char* colorStr)
unsigned ledCnt = 50; unsigned ledCnt = 50;
std::vector<ColorRgb> buff(ledCnt, color); std::vector<ColorRgb> buff(ledCnt, color);
LedDeviceWs2801 ledDevice("/dev/spidev0.0", 40000, 500000); LedDeviceWs2801 ledDevice("/dev/spidev0.0", 40000, 500000, 0, 0);
ledDevice.open(); ledDevice.open();
ledDevice.write(buff); ledDevice.write(buff);
} }
@ -68,7 +68,7 @@ void doCircle()
unsigned ledCnt = 50; unsigned ledCnt = 50;
std::vector<ColorRgb> data(ledCnt, ColorRgb::BLACK); std::vector<ColorRgb> data(ledCnt, ColorRgb::BLACK);
LedDeviceWs2801 ledDevice("/dev/spidev0.0", 40000, 500000); LedDeviceWs2801 ledDevice("/dev/spidev0.0", 40000, 500000, 0, 0);
ledDevice.open(); ledDevice.open();
timespec loopTime; timespec loopTime;