Skip to content

Commit

Permalink
Modifiable SPI path with device auto-detection (#346)
Browse files Browse the repository at this point in the history
  • Loading branch information
awawa-dev committed Oct 14, 2022
1 parent 5358874 commit 879e6b3
Show file tree
Hide file tree
Showing 20 changed files with 106 additions and 152 deletions.
102 changes: 42 additions & 60 deletions assets/webconfig/js/light_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,33 @@ $(document).ready(function()
$('#ip_ma_' + key).val(slConfig.matrix[key]);
}

async function deviceListRefresh(ledTypeTarget, discoveryResult, targetDiscoveryEditor, targetDiscoveryFirstLabel, targetDiscoverySecondLabel = 'main_menu_update_token')
{
let receiver = $("#deviceListInstances");
receiver.off();
receiver.empty();

$("<option />", {value: "", text: $.i18n(targetDiscoveryFirstLabel)}).appendTo(receiver);

if (discoveryResult.info != null && discoveryResult.info.devices != null)
{
(discoveryResult.info.devices).forEach(function (val) {
$("<option />", {value: val.value, text: val.name}).appendTo(receiver);
});
}

$("<option />", {value: -1, text: $.i18n(targetDiscoverySecondLabel)}).appendTo(receiver);

receiver.on('change', function ()
{
let selVal = $("#deviceListInstances").val();
if (selVal == -1)
requestLedDeviceDiscovery(ledTypeTarget).then( (newResult) => deviceListRefresh(ledTypeTarget, newResult, targetDiscoveryEditor, targetDiscoveryFirstLabel, targetDiscoverySecondLabel));
else if (selVal != "")
conf_editor.getEditor(targetDiscoveryEditor).setValue(selVal);
});
}

function saveValues()
{
var ledConfig = {
Expand Down Expand Up @@ -802,14 +829,6 @@ $(document).ready(function()
});
$("input[name='root[specificOptions][useEntertainmentAPI]']").trigger("change");
}
/*
else if(ledType == "wled") {
var ledWizardType = (this.checked) ? "wled" : ledType;
var data = { type: ledWizardType };
var wled_title = 'wiz_wled_title';
changeWizard(data, wled_title, startWizardWLED);
}
*/
else if (ledType == "atmoorb")
{
var ledWizardType = (this.checked) ? "atmoorb" : ledType;
Expand All @@ -836,63 +855,26 @@ $(document).ready(function()
};
var yeelight_title = 'wiz_yeelight_title';
changeWizard(data, yeelight_title, startWizardYeelight);
}
else if (ledType == "adalight")
}
else if (["apa102", "apa104", "awa_spi", "lpd6803", "lpd8806", "p9813", "sk6812spi", "sk6822spi", "sk9822", "ws2801", "ws2812spi", "wled", "adalight", "atmo", "dmx", "karate", "sedu", "tpm2"].includes(ledType))
{
if (!(window.serverInfo.serialPorts == null || window.serverInfo.serialPorts.length == 0 || $("#selectPortSerial").length > 0))
{
let sPort = $("<select id=\"selectPortSerial\" />");
sPort.addClass("form-select bg-warning").css('width', String(40)+'%');

$("<option />", {value: "auto", text: $.i18n("edt_dev_spec_outputPath_title")}).appendTo(sPort);
(window.serverInfo.serialPorts).forEach(function (val) {
$("<option />", {value: val.port, text: val.desc}).appendTo(sPort);
});

$("input[name='root[specificOptions][output]']")[0].style.width = String(58) + "%";
$("input[name='root[specificOptions][output]']")[0].parentElement.appendChild(sPort[0]);

sPort.off().on('change', function () {
conf_editor.getEditor('root.specificOptions.output').setValue(sPort.val());
});
}
}
else if (ledType == "wled")
{
async function wledRefresh(result)
{
let receiver = $("#selectWledInstances");
receiver.off();
receiver.empty();

$("<option />", {value: "", text: $.i18n("select_wled_intro")}).appendTo(receiver);
let selectorControl = $("<select id=\"deviceListInstances\" />");
let targetControl = 'output';

if (result.info != null && result.info.devices != null)
{
(result.info.devices).forEach(function (val) {
$("<option />", {value: val.value, text: val.name}).appendTo(receiver);
});
}

$("<option />", {value: -1, text: $.i18n("select_wled_rescan")}).appendTo(receiver);
selectorControl.addClass("form-select bg-warning").css('width', String(40)+'%');

receiver.on('change', function ()
{
let selVal = $("#selectWledInstances").val();
if (selVal == -1)
requestLedDeviceDiscovery('wled').then( (result) => wledRefresh(result));
else if (selVal != "")
conf_editor.getEditor('root.specificOptions.host').setValue(selVal);
});
if (ledType == "wled")
{
requestLedDeviceDiscovery(ledType).then( (result) => deviceListRefresh(ledType, result, 'root.specificOptions.host','select_wled_intro','select_wled_rescan'));
targetControl = 'host';
}

let sPort = $("<select id=\"selectWledInstances\" />");
sPort.addClass("form-select bg-warning").css('width', String(40)+'%');

requestLedDeviceDiscovery('wled').then( (result) => wledRefresh(result));
else if (["adalight", "atmo", "dmx", "karate", "sedu", "tpm2"].includes(ledType))
requestLedDeviceDiscovery(ledType).then( (result) => deviceListRefresh(ledType, result, 'root.specificOptions.output','edt_dev_spec_outputPath_title'));
else
requestLedDeviceDiscovery(ledType).then( (result) => deviceListRefresh(ledType, result, 'root.specificOptions.output', 'edt_dev_spec_spipath_title'));

$("input[name='root[specificOptions][host]']")[0].style.width = String(58) + "%";
$("input[name='root[specificOptions][host]']")[0].parentElement.appendChild(sPort[0]);
$(`input[name='root[specificOptions][${targetControl}]']`)[0].style.width = String(58) + "%";
$(`input[name='root[specificOptions][${targetControl}]']`)[0].parentElement.appendChild(selectorControl[0]);
}

function changeWizard(data, hint, fn)
Expand Down
13 changes: 0 additions & 13 deletions sources/api/JsonAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include <QHostAddress>
#include <QHostInfo>
#include <QMultiMap>
#include <QSerialPortInfo>

#include <leddevice/LedDeviceWrapper.h>
#include <leddevice/LedDevice.h>
Expand Down Expand Up @@ -529,18 +528,6 @@ void JsonAPI::handleServerInfoCommand(const QJsonObject& message, const QString&
ledDevices["available"] = availableLedDevices;
info["ledDevices"] = ledDevices;

// serial port
QJsonArray availableSerialPorts;
const auto infoSerial = QSerialPortInfo::availablePorts();
for (const QSerialPortInfo& info : infoSerial) {
QJsonObject serialPort;
serialPort["port"] = info.portName();
serialPort["desc"] = QString("%2 (%1)").arg(info.systemLocation()).arg(info.description());
availableSerialPorts.append(serialPort);
}
info["serialPorts"] = availableSerialPorts;


#if defined(ENABLE_SOUNDCAPLINUX) || defined(ENABLE_SOUNDCAPWINDOWS) || defined(ENABLE_SOUNDCAPMACOS)
if (SoundCapture::getInstance() != NULL)
{
Expand Down
6 changes: 3 additions & 3 deletions sources/base/HyperHdrInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ void HyperHdrInstance::start()
}

// handle hwLedCount
_hwLedCount = qMax(getSetting(settings::type::DEVICE).object()["hardwareLedCount"].toInt(getLedCount()), getLedCount());
_hwLedCount = qMax(getSetting(settings::type::DEVICE).object()["hardwareLedCount"].toInt(1), getLedCount());

connect(&_muxer, &PriorityMuxer::visiblePriorityChanged, this, &HyperHdrInstance::update);
connect(&_muxer, &PriorityMuxer::visiblePriorityChanged, this, &HyperHdrInstance::handlePriorityChangedLedDevice);
Expand Down Expand Up @@ -259,7 +259,7 @@ void HyperHdrInstance::handleSettingsUpdate(settings::type type, const QJsonDocu
_globalLedBuffer = color;

// handle hwLedCount update
_hwLedCount = qMax(getSetting(settings::type::DEVICE).object()["hardwareLedCount"].toInt(getLedCount()), getLedCount());
_hwLedCount = qMax(getSetting(settings::type::DEVICE).object()["hardwareLedCount"].toInt(1), getLedCount());

// change in leds are also reflected in adjustment
delete _raw2ledAdjustment;
Expand All @@ -273,7 +273,7 @@ void HyperHdrInstance::handleSettingsUpdate(settings::type type, const QJsonDocu
QJsonObject dev = config.object();

// handle hwLedCount update
_hwLedCount = qMax(dev["hardwareLedCount"].toInt(getLedCount()), getLedCount());
_hwLedCount = qMax(dev["hardwareLedCount"].toInt(1), getLedCount());

// force ledString update, if device ByteOrder changed
if (_ledString.colorOrder != stringToColorOrder(dev["colorOrder"].toString("rgb")))
Expand Down
12 changes: 1 addition & 11 deletions sources/base/schema/schema-device.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"type" : "object",
"title" : "edt_dev_general_heading_title",
"required" : true,
"defaultProperties": ["hardwareLedCount", "colorOrder", "refreshTime"],
"defaultProperties": ["colorOrder", "refreshTime"],
"properties" :
{
"type" :
Expand All @@ -12,16 +12,6 @@
"required" : true,
"propertyOrder" : 0
},
"hardwareLedCount" :
{
"type" : "integer",
"title" : "edt_dev_general_hardwareLedCount_title",
"minimum" : 1,
"default" : 1,
"access" : "expert",
"required" : true,
"propertyOrder" : 1
},
"colorOrder" :
{
"type" : "string",
Expand Down
31 changes: 12 additions & 19 deletions sources/leddevice/dev_serial/ProviderRs232.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,28 +271,21 @@ QString ProviderRs232::discoverFirst()
QJsonObject ProviderRs232::discover(const QJsonObject& /*params*/)
{
QJsonObject devicesDiscovered;
devicesDiscovered.insert("ledDeviceType", _activeDeviceType);

QJsonArray deviceList;

// Discover serial Devices
for (auto& port : QSerialPortInfo::availablePorts())
{
if (!port.isNull())
{
QJsonObject portInfo;
portInfo.insert("description", port.description());
portInfo.insert("manufacturer", port.manufacturer());
portInfo.insert("portName", port.portName());
portInfo.insert("productIdentifier", QString("0x%1").arg(port.productIdentifier(), 0, 16));
portInfo.insert("serialNumber", port.serialNumber());
portInfo.insert("systemLocation", port.systemLocation());
portInfo.insert("vendorIdentifier", QString("0x%1").arg(port.vendorIdentifier(), 0, 16));

deviceList.append(portInfo);
}
}
deviceList.push_back(QJsonObject{
{"value", "auto"},
{ "name", "Auto"} });

for (const QSerialPortInfo& info : QSerialPortInfo::availablePorts())
deviceList.push_back(QJsonObject{
{"value", info.portName()},
{ "name", QString("%2 (%1)").arg(info.systemLocation()).arg(info.description()) } });

devicesDiscovered.insert("ledDeviceType", _activeDeviceType);
devicesDiscovered.insert("devices", deviceList);

Debug(_log, "Serial devices discovered: [%s]", QString(QJsonDocument(devicesDiscovered).toJson(QJsonDocument::Compact)).toUtf8().constData());

return devicesDiscovered;
}
26 changes: 26 additions & 0 deletions sources/leddevice/dev_spi/ProviderSpi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#include "ProviderSpi.h"
#include <utils/Logger.h>

#include <QDirIterator>

ProviderSpi::ProviderSpi(const QJsonObject& deviceConfig)
: LedDevice(deviceConfig)
, _deviceName("/dev/spidev0.0")
Expand Down Expand Up @@ -222,3 +224,27 @@ int ProviderSpi::writeBytesEsp32(unsigned size, const uint8_t* data)

return retVal;
}

QJsonObject ProviderSpi::discover(const QJsonObject& /*params*/)
{
QJsonObject devicesDiscovered;
QJsonArray deviceList;
QStringList files;
QDirIterator it("/dev", QStringList() << "spidev*", QDir::System);

while (it.hasNext())
files << it.next();
files.sort();

for (const auto& path : files)
deviceList.push_back(QJsonObject{
{"value", path},
{ "name", path } });

devicesDiscovered.insert("ledDeviceType", _activeDeviceType);
devicesDiscovered.insert("devices", deviceList);

Debug(_log, "SPI devices discovered: [%s]", QString(QJsonDocument(devicesDiscovered).toJson(QJsonDocument::Compact)).toUtf8().constData());

return devicesDiscovered;
}
2 changes: 2 additions & 0 deletions sources/leddevice/dev_spi/ProviderSpi.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class ProviderSpi : public LedDevice
///
int open() override;

QJsonObject discover(const QJsonObject& params) override;

public slots:
///
/// Closes the output device.
Expand Down
4 changes: 0 additions & 4 deletions sources/leddevice/schemas/schema-apa102.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
"output": {
"type": "string",
"title":"edt_dev_spec_spipath_title",
"enum" : ["/dev/spidev0.0","/dev/spidev0.1","/dev/spidev1.0","/dev/spidev1.1"],
"options" : {
"enum_titles" : ["/dev/spidev0.0","/dev/spidev0.1","/dev/spidev1.0","/dev/spidev1.1"]
},
"default" : "/dev/spidev0.0",
"required" : true,
"propertyOrder" : 1
Expand Down
4 changes: 0 additions & 4 deletions sources/leddevice/schemas/schema-apa104.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
"output": {
"type": "string",
"title":"edt_dev_spec_spipath_title",
"enum" : ["/dev/spidev0.0","/dev/spidev0.1","/dev/spidev1.0","/dev/spidev1.1"],
"options" : {
"enum_titles" : ["/dev/spidev0.0","/dev/spidev0.1","/dev/spidev1.0","/dev/spidev1.1"]
},
"default" : "/dev/spidev0.0",
"required" : true,
"propertyOrder" : 1
Expand Down
4 changes: 0 additions & 4 deletions sources/leddevice/schemas/schema-awa_spi.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
"output": {
"type": "string",
"title":"edt_dev_spec_spipath_title",
"enum" : ["/dev/spidev0.0","/dev/spidev0.1","/dev/spidev1.0","/dev/spidev1.1"],
"options" : {
"enum_titles" : ["/dev/spidev0.0","/dev/spidev0.1","/dev/spidev1.0","/dev/spidev1.1"]
},
"default" : "/dev/spidev0.0",
"required" : true,
"propertyOrder" : 1
Expand Down
11 changes: 10 additions & 1 deletion sources/leddevice/schemas/schema-cololight.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
{
"type":"object",
"required":true,
"properties": {
"properties": {
"hardwareLedCount" : {
"type" : "integer",
"title" : "edt_dev_general_hardwareLedCount_title",
"minimum" : 1,
"default" : 1,
"access" : "expert",
"required" : true,
"propertyOrder" : 1
},
"host" : {
"type": "string",
"title":"edt_dev_spec_targetIpHost_title",
Expand Down
4 changes: 0 additions & 4 deletions sources/leddevice/schemas/schema-lpd6803.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
"output": {
"type": "string",
"title":"edt_dev_spec_spipath_title",
"enum" : ["/dev/spidev0.0","/dev/spidev0.1","/dev/spidev1.0","/dev/spidev1.1"],
"options" : {
"enum_titles" : ["/dev/spidev0.0","/dev/spidev0.1","/dev/spidev1.0","/dev/spidev1.1"]
},
"default" : "/dev/spidev0.0",
"required" : true,
"propertyOrder" : 1
Expand Down
4 changes: 0 additions & 4 deletions sources/leddevice/schemas/schema-lpd8806.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
"output": {
"type": "string",
"title":"edt_dev_spec_spipath_title",
"enum" : ["/dev/spidev0.0","/dev/spidev0.1","/dev/spidev1.0","/dev/spidev1.1"],
"options" : {
"enum_titles" : ["/dev/spidev0.0","/dev/spidev0.1","/dev/spidev1.0","/dev/spidev1.1"]
},
"default" : "/dev/spidev0.0",
"required" : true,
"propertyOrder" : 1
Expand Down
11 changes: 10 additions & 1 deletion sources/leddevice/schemas/schema-nanoleaf.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
{
"type":"object",
"required":true,
"properties":{
"properties": {
"hardwareLedCount": {
"type" : "integer",
"title" : "edt_dev_general_hardwareLedCount_title",
"minimum" : 1,
"default" : 1,
"access" : "expert",
"required" : true,
"propertyOrder" : 1
},
"host": {
"type": "string",
"title":"edt_dev_spec_targetIpHost_title",
Expand Down
4 changes: 0 additions & 4 deletions sources/leddevice/schemas/schema-p9813.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
"output": {
"type": "string",
"title":"edt_dev_spec_spipath_title",
"enum" : ["/dev/spidev0.0","/dev/spidev0.1","/dev/spidev1.0","/dev/spidev1.1"],
"options" : {
"enum_titles" : ["/dev/spidev0.0","/dev/spidev0.1","/dev/spidev1.0","/dev/spidev1.1"]
},
"default" : "/dev/spidev0.0",
"required" : true,
"propertyOrder" : 1
Expand Down

0 comments on commit 879e6b3

Please sign in to comment.