Reformat code
This commit is contained in:
parent
f28132b458
commit
91713c15b4
|
@ -7,38 +7,38 @@
|
|||
#include "ProviderSpi.h"
|
||||
|
||||
#ifdef ENABLE_DEV_SPI
|
||||
// Linux includes
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/ioctl.h>
|
||||
// qt includes
|
||||
#include <QDir>
|
||||
// Linux includes
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/ioctl.h>
|
||||
// qt includes
|
||||
#include <QDir>
|
||||
#endif
|
||||
|
||||
#ifdef ENABLE_DEV_FTDI
|
||||
#include <ftdi.h>
|
||||
#include <libusb.h>
|
||||
#include <utils/WaitTime.h>
|
||||
#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
|
||||
#include <ftdi.h>
|
||||
#include <libusb.h>
|
||||
#include <utils/WaitTime.h>
|
||||
|
||||
#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
|
||||
{
|
||||
namespace Pin {
|
||||
// enumerate the AD bus for convenience.
|
||||
enum bus_t
|
||||
{
|
||||
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 unsigned char pinInitialState = Pin::CS;
|
||||
// Use these pins as outputs
|
||||
const unsigned char pinDirection = Pin::SK | Pin::DO | Pin::CS;
|
||||
#endif
|
||||
|
||||
|
||||
|
@ -60,33 +60,27 @@ 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
|
||||
, _spiImplementation(SPI_SPIDEV)
|
||||
{
|
||||
, _spiImplementation(SPI_SPIDEV) {
|
||||
#ifdef ENABLE_DEV_SPI
|
||||
memset(&_spi, 0, sizeof(_spi));
|
||||
_latchTime_ms = 1;
|
||||
#endif
|
||||
}
|
||||
|
||||
ProviderSpi::~ProviderSpi()
|
||||
{
|
||||
ProviderSpi::~ProviderSpi() {
|
||||
}
|
||||
|
||||
bool ProviderSpi::init(const QJsonObject &deviceConfig)
|
||||
{
|
||||
bool ProviderSpi::init(const QJsonObject &deviceConfig) {
|
||||
bool isInitOK = false;
|
||||
|
||||
// Initialise sub-class
|
||||
if ( LedDevice::init(deviceConfig) )
|
||||
{
|
||||
if (LedDevice::init(deviceConfig)) {
|
||||
_deviceName = deviceConfig["output"].toString(_deviceName);
|
||||
_baudRate_Hz = deviceConfig["rate"].toInt(_baudRate_Hz);
|
||||
#ifdef ENABLE_DEV_SPI
|
||||
|
@ -94,7 +88,9 @@ bool ProviderSpi::init(const QJsonObject &deviceConfig)
|
|||
_spiDataInvert = deviceConfig["invert"].toBool(_spiDataInvert);
|
||||
Debug(_log, "_spiDataInvert [%d], _spiMode [%d]", _spiDataInvert, _spiMode);
|
||||
#endif
|
||||
bool isFtdiImplementation = (QString::compare(deviceConfig["implementation"].toString(ImplementationSPIDEV), ImplementationFTDI, Qt::CaseInsensitive) == 0);
|
||||
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);
|
||||
|
@ -105,12 +101,11 @@ bool ProviderSpi::init(const QJsonObject &deviceConfig)
|
|||
return isInitOK;
|
||||
}
|
||||
|
||||
int ProviderSpi::open()
|
||||
{
|
||||
int ProviderSpi::open() {
|
||||
int retVal = -1;
|
||||
QString errortext;
|
||||
_isDeviceReady = false;
|
||||
if (_spiImplementation == SPI_SPIDEV) {
|
||||
if (_spiImplementation == SPI_SPIDEV) {
|
||||
#ifdef ENABLE_DEV_SPI
|
||||
const int bitsPerWord = 8;
|
||||
|
||||
|
@ -158,7 +153,7 @@ if (_spiImplementation == SPI_SPIDEV) {
|
|||
this->setInError( errortext );
|
||||
}
|
||||
#endif
|
||||
} else if (_spiImplementation == SPI_FTDI) {
|
||||
} else if (_spiImplementation == SPI_FTDI) {
|
||||
#ifdef ENABLE_DEV_FTDI
|
||||
|
||||
_ftdic = ftdi_new();
|
||||
|
@ -189,12 +184,11 @@ if (_spiImplementation == SPI_SPIDEV) {
|
|||
|
||||
_isDeviceReady = true;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
return retVal;
|
||||
}
|
||||
|
||||
int ProviderSpi::close()
|
||||
{
|
||||
int ProviderSpi::close() {
|
||||
// LedDevice specific closing activities
|
||||
int retVal = 0;
|
||||
_isDeviceReady = false;
|
||||
|
@ -228,8 +222,7 @@ int ProviderSpi::close()
|
|||
return retVal;
|
||||
}
|
||||
|
||||
int ProviderSpi::writeBytes(unsigned size, const uint8_t * data)
|
||||
{
|
||||
int ProviderSpi::writeBytes(unsigned size, const uint8_t *data) {
|
||||
int retVal = 0;
|
||||
if (_spiImplementation == SPI_SPIDEV) {
|
||||
#ifdef ENABLE_DEV_SPI
|
||||
|
@ -282,10 +275,9 @@ int ProviderSpi::writeBytes(unsigned size, const uint8_t * data)
|
|||
return retVal;
|
||||
}
|
||||
|
||||
QJsonObject ProviderSpi::discover(const QJsonObject& /*params*/)
|
||||
{
|
||||
QJsonObject ProviderSpi::discover(const QJsonObject & /*params*/) {
|
||||
QJsonObject devicesDiscovered;
|
||||
devicesDiscovered.insert("ledDeviceType", _activeDeviceType );
|
||||
devicesDiscovered.insert("ledDeviceType", _activeDeviceType);
|
||||
QJsonArray deviceList;
|
||||
|
||||
#ifdef ENABLE_DEV_SPI
|
||||
|
@ -311,17 +303,14 @@ QJsonObject ProviderSpi::discover(const QJsonObject& /*params*/)
|
|||
|
||||
ftdic = ftdi_new();
|
||||
|
||||
if (ftdi_usb_find_all(ftdic, &devlist, ANY_FTDI_VENDOR, ANY_FTDI_PRODUCT) > 0)
|
||||
{
|
||||
if (ftdi_usb_find_all(ftdic, &devlist, ANY_FTDI_VENDOR, ANY_FTDI_PRODUCT) > 0) {
|
||||
struct ftdi_device_list *curdev = devlist;
|
||||
QMap<QString, uint8_t> deviceIndexes;
|
||||
|
||||
while (curdev)
|
||||
{
|
||||
while (curdev) {
|
||||
libusb_device_descriptor desc;
|
||||
int rc = libusb_get_device_descriptor(curdev->dev, &desc);
|
||||
if (rc == 0)
|
||||
{
|
||||
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")
|
||||
|
@ -332,16 +321,14 @@ QJsonObject ProviderSpi::discover(const QJsonObject& /*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);
|
||||
ftdi_usb_get_strings2(ftdic, curdev->dev, manufacturer_string, 128, description_string, 128,
|
||||
serial_string, 128);
|
||||
|
||||
QString serialNumber {serial_string};
|
||||
QString serialNumber{serial_string};
|
||||
QString ftdiOpenString;
|
||||
if(!serialNumber.isEmpty())
|
||||
{
|
||||
if (!serialNumber.isEmpty()) {
|
||||
ftdiOpenString = QString("s:%1:%2").arg(vendorAndProduct).arg(serialNumber);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
ftdiOpenString = QString("i:%1:%2").arg(vendorAndProduct).arg(deviceIndex);
|
||||
}
|
||||
|
||||
|
@ -367,7 +354,8 @@ QJsonObject ProviderSpi::discover(const QJsonObject& /*params*/)
|
|||
ftdi_free(ftdic);
|
||||
#endif
|
||||
devicesDiscovered.insert("devices", deviceList);
|
||||
DebugIf(verbose,_log, "devicesDiscovered: [%s]", QString(QJsonDocument(devicesDiscovered).toJson(QJsonDocument::Compact)).toUtf8().constData());
|
||||
DebugIf(verbose, _log, "devicesDiscovered: [%s]",
|
||||
QString(QJsonDocument(devicesDiscovered).toJson(QJsonDocument::Compact)).toUtf8().constData());
|
||||
|
||||
return devicesDiscovered;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue