2013-11-01 23:48:39 +01:00
|
|
|
|
|
|
|
// STL includes
|
|
|
|
#include <cstring>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <iostream>
|
2016-06-25 14:44:52 +02:00
|
|
|
#include <cerrno>
|
2013-11-01 23:48:39 +01:00
|
|
|
|
|
|
|
// Linux includes
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
|
|
|
// Local Hyperion includes
|
|
|
|
#include "LedSpiDevice.h"
|
2016-06-25 14:44:52 +02:00
|
|
|
#include <utils/Logger.h>
|
2013-11-01 23:48:39 +01:00
|
|
|
|
|
|
|
|
2016-08-14 10:46:44 +02:00
|
|
|
LedSpiDevice::LedSpiDevice(const std::string& outputDevice, const unsigned baudrate, const int latchTime_ns, const int spiMode, const bool spiDataInvert)
|
|
|
|
: _deviceName(outputDevice)
|
|
|
|
, _baudRate_Hz(baudrate)
|
|
|
|
, _latchTime_ns(latchTime_ns)
|
|
|
|
, _fid(-1)
|
|
|
|
, _spiMode(spiMode)
|
|
|
|
, _spiDataInvert(spiDataInvert)
|
2013-11-01 23:48:39 +01:00
|
|
|
{
|
2016-08-14 10:46:44 +02:00
|
|
|
memset(&_spi, 0, sizeof(_spi));
|
|
|
|
Debug(_log, "_spiDataInvert %d, _spiMode %d", _spiDataInvert, _spiMode);
|
2013-11-01 23:48:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
LedSpiDevice::~LedSpiDevice()
|
|
|
|
{
|
2016-08-14 10:46:44 +02:00
|
|
|
// close(_fid);
|
2013-11-01 23:48:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int LedSpiDevice::open()
|
|
|
|
{
|
|
|
|
const int bitsPerWord = 8;
|
|
|
|
|
2016-08-14 10:46:44 +02:00
|
|
|
_fid = ::open(_deviceName.c_str(), O_RDWR);
|
2013-11-01 23:48:39 +01:00
|
|
|
|
2016-08-14 10:46:44 +02:00
|
|
|
if (_fid < 0)
|
2013-11-01 23:48:39 +01:00
|
|
|
{
|
2016-08-14 10:46:44 +02:00
|
|
|
Error( _log, "Failed to open device (%s). Error message: %s", _deviceName.c_str(), strerror(errno) );
|
2013-11-01 23:48:39 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-08-14 10:46:44 +02:00
|
|
|
if (ioctl(_fid, SPI_IOC_WR_MODE, &_spiMode) == -1 || ioctl(_fid, SPI_IOC_RD_MODE, &_spiMode) == -1)
|
2013-11-01 23:48:39 +01:00
|
|
|
{
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
2016-08-14 10:46:44 +02:00
|
|
|
if (ioctl(_fid, SPI_IOC_WR_BITS_PER_WORD, &bitsPerWord) == -1 || ioctl(_fid, SPI_IOC_RD_BITS_PER_WORD, &bitsPerWord) == -1)
|
2013-11-01 23:48:39 +01:00
|
|
|
{
|
|
|
|
return -4;
|
|
|
|
}
|
|
|
|
|
2016-08-14 10:46:44 +02:00
|
|
|
if (ioctl(_fid, SPI_IOC_WR_MAX_SPEED_HZ, &_baudRate_Hz) == -1 || ioctl(_fid, SPI_IOC_RD_MAX_SPEED_HZ, &_baudRate_Hz) == -1)
|
2013-11-01 23:48:39 +01:00
|
|
|
{
|
|
|
|
return -6;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2013-11-02 06:51:41 +01:00
|
|
|
|
2013-11-02 19:30:19 +01:00
|
|
|
int LedSpiDevice::writeBytes(const unsigned size, const uint8_t * data)
|
2013-11-02 06:51:41 +01:00
|
|
|
{
|
2016-08-14 10:46:44 +02:00
|
|
|
if (_fid < 0)
|
2013-11-02 06:51:41 +01:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-08-14 10:46:44 +02:00
|
|
|
_spi.tx_buf = __u64(data);
|
|
|
|
_spi.len = __u32(size);
|
2013-11-02 06:51:41 +01:00
|
|
|
|
2016-08-14 10:46:44 +02:00
|
|
|
if (_spiDataInvert)
|
|
|
|
{
|
2016-08-13 19:54:08 +02:00
|
|
|
uint8_t * newdata = (uint8_t *)malloc(size);
|
|
|
|
for (unsigned i = 0; i<size; i++) {
|
|
|
|
newdata[i] = data[i] ^ 0xff;
|
|
|
|
}
|
2016-08-14 10:46:44 +02:00
|
|
|
_spi.tx_buf = __u64(newdata);
|
2016-08-13 19:54:08 +02:00
|
|
|
}
|
|
|
|
|
2016-08-14 10:46:44 +02:00
|
|
|
int retVal = ioctl(_fid, SPI_IOC_MESSAGE(1), &_spi);
|
2013-11-02 06:51:41 +01:00
|
|
|
|
2016-08-14 10:46:44 +02:00
|
|
|
if (retVal == 0 && _latchTime_ns > 0)
|
2013-11-02 06:51:41 +01:00
|
|
|
{
|
|
|
|
// The 'latch' time for latching the shifted-value into the leds
|
|
|
|
timespec latchTime;
|
|
|
|
latchTime.tv_sec = 0;
|
2016-08-14 10:46:44 +02:00
|
|
|
latchTime.tv_nsec = _latchTime_ns;
|
2013-11-02 06:51:41 +01:00
|
|
|
|
|
|
|
// Sleep to latch the leds (only if write succesfull)
|
|
|
|
nanosleep(&latchTime, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return retVal;
|
|
|
|
}
|