Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
nurikk-sa committed Mar 27, 2023
1 parent ae324ed commit 083d622
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 96 deletions.
16 changes: 4 additions & 12 deletions sources/leddevice/dev_ftdi/LedDeviceAPA102_ftdi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ bool LedDeviceAPA102_ftdi::init(const QJsonObject &deviceConfig)
{
bool isInitOK = false;

_brightnessControlMaxLevel = deviceConfig["brightnessControlMaxLevel"].toInt(LED_BRIGHTNESS_FULL);
Info(_log, "[%s] Setting maximum brightness to [%d] = %d%%", QSTRING_CSTR(_activeDeviceType), _brightnessControlMaxLevel, _brightnessControlMaxLevel * 100 / LED_BRIGHTNESS_FULL);

// Initialise sub-class
if (ProviderFtdi::init(deviceConfig))
{
CreateHeader();
_brightnessControlMaxLevel = deviceConfig["brightnessControlMaxLevel"].toInt(LED_BRIGHTNESS_FULL);
Info(_log, "[%s] Setting maximum brightness to [%d] = %d%%", QSTRING_CSTR(_activeDeviceType), _brightnessControlMaxLevel, _brightnessControlMaxLevel * 100 / LED_BRIGHTNESS_FULL);

CreateHeader();
isInitOK = true;
}
return isInitOK;
Expand All @@ -40,14 +40,6 @@ void LedDeviceAPA102_ftdi::CreateHeader()

int LedDeviceAPA102_ftdi::write(const std::vector<ColorRgb> &ledValues)
{
if (_ledCount != ledValues.size())
{
Warning(_log, "APA102 led's number has changed (old: %d, new: %d). Rebuilding buffer.", _ledCount, ledValues.size());
_ledCount = ledValues.size();

CreateHeader();
}

for (signed iLed = 0; iLed < static_cast<int>(_ledCount); ++iLed)
{
const ColorRgb &rgb = ledValues[iLed];
Expand Down
9 changes: 0 additions & 9 deletions sources/leddevice/dev_ftdi/LedDeviceSk6812_ftdi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,6 @@ int LedDeviceSk6812_ftdi::write(const std::vector<ColorRgb> &ledValues)
unsigned spi_ptr = 0;
const int SPI_BYTES_PER_LED = sizeof(ColorRgbw) * SPI_BYTES_PER_COLOUR;

if (_ledCount != ledValues.size())
{
Warning(_log, "Sk6812SPI led's number has changed (old: %d, new: %d). Rebuilding buffer.", _ledCount, ledValues.size());
_ledCount = ledValues.size();

const int SPI_FRAME_END_LATCH_BYTES = 3;
_ledBuffer.resize(0, 0x00);
_ledBuffer.resize(_ledRGBWCount * SPI_BYTES_PER_COLOUR + SPI_FRAME_END_LATCH_BYTES, 0x00);
}
ColorRgbw temp_rgbw;
ColorRgb scaled_color;
for (const ColorRgb &color : ledValues)
Expand Down
2 changes: 1 addition & 1 deletion sources/leddevice/dev_ftdi/LedDeviceWs2812_ftdi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ bool LedDeviceWs2812_ftdi::init(const QJsonObject &deviceConfig)
// Initialise sub-class
if (ProviderFtdi::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, "Baud rate %d outside recommended range (2106000 -> 3075000)", _baudRate_Hz);
_ledBuffer.resize(_ledRGBCount * SPI_BYTES_PER_COLOUR + SPI_FRAME_END_LATCH_BYTES, 0x00);
isInitOK = true;
}
Expand Down
85 changes: 25 additions & 60 deletions sources/leddevice/dev_ftdi/ProviderFtdi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ namespace Pin
// Use these pins as outputs
const unsigned char pinDirection = Pin::SK | Pin::DO | Pin::CS | Pin::L0;

const QString ProviderFtdi::AUTO_SETTING = QString("auto");

ProviderFtdi::ProviderFtdi(const QJsonObject &deviceConfig)
: LedDevice(deviceConfig),
_ftdic(NULL),
Expand All @@ -40,7 +38,7 @@ bool ProviderFtdi::init(const QJsonObject &deviceConfig)
if (LedDevice::init(deviceConfig))
{
_baudRate_Hz = deviceConfig["rate"].toInt(_baudRate_Hz);
_deviceName = deviceConfig["output"].toString(AUTO_SETTING);
_deviceName = deviceConfig["output"].toString("");

Debug(_log, "_baudRate_Hz [%d]", _baudRate_Hz);
Debug(_log, "_deviceName [%s]", QSTRING_CSTR(_deviceName));
Expand All @@ -50,55 +48,19 @@ bool ProviderFtdi::init(const QJsonObject &deviceConfig)
return isInitOK;
}

int ProviderFtdi::openDevice()
{
_ftdic = ftdi_new();

bool autoDiscovery = (QString::compare(_deviceName, ProviderFtdi::AUTO_SETTING, Qt::CaseInsensitive) == 0);
Debug(_log, "Opening FTDI device=%s autoDiscovery=%s", QSTRING_CSTR(_deviceName), autoDiscovery ? "true" : "false");
if (autoDiscovery)
{
struct ftdi_device_list *devlist;
int devicesDetected = 0;
if ((devicesDetected = ftdi_usb_find_all(_ftdic, &devlist, ANY_FTDI_VENDOR, ANY_FTDI_PRODUCT)) < 0)
{
setInError(ftdi_get_error_string(_ftdic));
return -1;
}
if (devicesDetected == 0)
{
setInError("No ftdi devices detected");
return 0;
}

if (ftdi_usb_open_dev(_ftdic, devlist[0].dev) < 0)
{
setInError(ftdi_get_error_string(_ftdic));
ftdi_list_free(&devlist);
return -1;
}

ftdi_list_free(&devlist);
return 1;
}
else
{
if (ftdi_usb_open_string(_ftdic, QSTRING_CSTR(_deviceName)) < 0)
{
setInError(ftdi_get_error_string(_ftdic));
return -1;
}
return 1;
}
}
int ProviderFtdi::open()
{
int rc = 0;

if ((rc = openDevice()) != 1)
{
return -1;
}
_ftdic = ftdi_new();

Debug(_log, "Opening FTDI device=%s", QSTRING_CSTR(_deviceName));

if ((rc = ftdi_usb_open_string(_ftdic, QSTRING_CSTR(_deviceName))) < 0)
{
setInError(ftdi_get_error_string(_ftdic));
return rc;
}

/* doing this disable resets things if they were in a bad state */
if ((rc = ftdi_disable_bitbang(_ftdic)) < 0)
Expand Down Expand Up @@ -208,29 +170,32 @@ QJsonObject ProviderFtdi::discover(const QJsonObject & /*params*/)
struct ftdi_device_list *devlist;
struct ftdi_context *ftdic;

QJsonObject autoDevice = QJsonObject{{"value", AUTO_SETTING}, {"name", "Auto"}};
deviceList.push_back(autoDevice);

ftdic = ftdi_new();

if (ftdi_usb_find_all(ftdic, &devlist, ANY_FTDI_VENDOR, ANY_FTDI_PRODUCT) > 0)
{
struct ftdi_device_list *curdev = devlist;
while (curdev)
{
char manufacturer[128], description[128];
ftdi_usb_get_strings(ftdic, curdev->dev, manufacturer, 128, description, 128, NULL, 0);
char manufacturer[128];
ftdi_usb_get_strings(ftdic, curdev->dev, manufacturer, 128, NULL, 0, NULL, 0);

uint8_t bus_number = libusb_get_bus_number(curdev->dev);
uint8_t device_address = libusb_get_device_address(curdev->dev);

QString value = QString("d:%1/%2")
.arg(bus_number, 3, 10, QChar{'0'})
.arg(device_address, 3, 10, QChar{'0'});


libusb_device_descriptor desc;
libusb_get_device_descriptor(curdev->dev, &desc);
QString value = QString("i:0x%1:0x%2")
.arg(desc.idVendor, 4, 16, QChar{'0'})
.arg(desc.idProduct, 4, 16, QChar{'0'});
QString displayLabel = QString("%1 (%2)")
.arg(value)
.arg(manufacturer);

QString name = QString("%1 (%2)").arg(manufacturer, description);
deviceList.push_back(QJsonObject{
{"value", value},
{"name", name}});
{"name", displayLabel}
});

curdev = curdev->next;
}
Expand Down
3 changes: 0 additions & 3 deletions sources/leddevice/dev_ftdi/ProviderFtdi.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,6 @@ protected slots:
/// @param errorMsg The error message to be logged
///
void setInError(const QString& errorMsg) override;

private:
int openDevice();
};

#endif // PROVIDERFtdi_H
8 changes: 0 additions & 8 deletions sources/leddevice/dev_spi/LedDeviceAPA102.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,6 @@ void LedDeviceAPA102::CreateHeader()

int LedDeviceAPA102::write(const std::vector<ColorRgb>& ledValues)
{
if (_ledCount != ledValues.size())
{
Warning(_log, "APA102 led's number has changed (old: %d, new: %d). Rebuilding buffer.", _ledCount, ledValues.size());
_ledCount = ledValues.size();

CreateHeader();
}

for (signed iLed = 0; iLed < static_cast<int>(_ledCount); ++iLed) {
const ColorRgb& rgb = ledValues[iLed];
_ledBuffer[4 + iLed * 4] = 0xFF;
Expand Down
2 changes: 1 addition & 1 deletion sources/leddevice/schemas/schema-apa102_ftdi.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"output": {
"type": "string",
"title":"edt_dev_spec_outputPath_title",
"default":"auto"
"required" : true
},
"rate": {
"type": "integer",
Expand Down
1 change: 0 additions & 1 deletion sources/leddevice/schemas/schema-sk6812_ftdi.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"output": {
"type": "string",
"title": "edt_dev_spec_outputPath_title",
"default": "auto",
"required": true,
"propertyOrder": 1
},
Expand Down
2 changes: 1 addition & 1 deletion sources/leddevice/schemas/schema-ws2812_ftdi.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"output": {
"type": "string",
"title": "edt_dev_spec_outputPath_title",
"default": "auto"
"required" : true
},
"rate": {
"type": "integer",
Expand Down

0 comments on commit 083d622

Please sign in to comment.