Skip to content

Commit

Permalink
Adalight: auto-resume and ESP8266/ESP32 auto-discovery (#494)
Browse files Browse the repository at this point in the history
* Prefer ESP devices if ESP handshake is enabled

* Rescan devices each time when open

* Add auto-resume for Adalight device

* Update ProviderRs232.cpp
  • Loading branch information
awawa-dev authored Jan 28, 2023
1 parent 4f1e0bd commit 6dfd477
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 9 deletions.
62 changes: 53 additions & 9 deletions sources/leddevice/dev_serial/ProviderRs232.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,27 +52,31 @@ bool ProviderRs232::init(const QJsonObject& deviceConfig)
_baudRate_Hz = deviceConfig["rate"].toInt();
_delayAfterConnect_ms = deviceConfig["delayAfterConnect"].toInt(0);
_espHandshake = deviceConfig["espHandshake"].toBool(false);
_maxRetry = _devConfig["maxRetry"].toInt(60);

Debug(_log, "Device name : %s", QSTRING_CSTR(_deviceName));
Debug(_log, "Auto selection: %d", _isAutoDeviceName);
Debug(_log, "Baud rate : %d", _baudRate_Hz);
Debug(_log, "ESP handshake : %s", (_espHandshake) ? "ON" : "OFF");
Debug(_log, "Delayed open : %d", _delayAfterConnect_ms);
Debug(_log, "Retry limit : %d", _maxRetry);

isInitOK = true;
}
return isInitOK;
}

ProviderRs232::~ProviderRs232()
{
if (_rs232Port.isOpen())
_rs232Port.close();
{
}

int ProviderRs232::open()
{
int retval = -1;

if (_retryMode)
return retval;

_isDeviceReady = false;

// open device physically
Expand All @@ -81,6 +85,27 @@ int ProviderRs232::open()
// Everything is OK, device is ready
_isDeviceReady = true;
retval = 0;

_currentRetry = 0;
_retryMode = false;
}
else if (_maxRetry > 0)
{
if (_currentRetry <= 0)
_currentRetry = _maxRetry + 1;

_currentRetry--;

if (_currentRetry > 0)
Warning(_log, "The serial device is not ready... will try to reconnect (try %i/%i).", (_maxRetry - _currentRetry + 1), _maxRetry);
else
Error(_log, "The serial device is not ready... give up.");

if (_currentRetry > 0)
{
_retryMode = true;
QTimer::singleShot(2000, [this]() { _retryMode = false; if (_currentRetry > 0) enableDevice(true); });
}
}
return retval;
}
Expand Down Expand Up @@ -133,6 +158,11 @@ int ProviderRs232::close()

QTimer::singleShot(200, this, [this]() { if (_rs232Port.isOpen()) EspTools::goingSleep(_rs232Port); });
EspTools::goingSleep(_rs232Port);

for (int i = 0; i < 6 && _rs232Port.isOpen(); i++)
{
_rs232Port.waitForReadyRead(100);
}
}
else
{
Expand All @@ -157,7 +187,7 @@ bool ProviderRs232::powerOff()

bool ProviderRs232::tryOpen(int delayAfterConnect_ms)
{
if (_deviceName.isEmpty() || _rs232Port.portName().isEmpty())
if (_deviceName.isEmpty() || _rs232Port.portName().isEmpty() || (_isAutoDeviceName && _espHandshake))
{
if (!_rs232Port.isOpen())
{
Expand Down Expand Up @@ -291,12 +321,18 @@ int ProviderRs232::writeBytes(const qint64 size, const uint8_t* data)
}
}
}

if (_maxRetry > 0 && rc == -1)
{
QTimer::singleShot(2000, this, [=]() { enable(); });
}

return rc;
}

QString ProviderRs232::discoverFirst()
{
for (int round = 0; round < 2; round++)
for (int round = 0; round < 4; round++)
for (auto const& port : QSerialPortInfo::availablePorts())
{
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
Expand All @@ -306,17 +342,25 @@ QString ProviderRs232::discoverFirst()
#endif
{
QString infoMessage = QString("%1 (%2 => %3)").arg(port.description()).arg(port.systemLocation()).arg(port.portName());

if (round != 0 ||
(port.description().contains("Bluetooth", Qt::CaseInsensitive) == false &&
quint16 vendor = port.vendorIdentifier();
quint16 prodId = port.productIdentifier();
bool knownESPA = (vendor == 0x303a && (prodId == 0x80c2));
bool knownESPB = (vendor == 0x303a) ||
(vendor == 0x10c4 && (prodId == 0xea60)) ||
(vendor == 0x1A86 && (prodId == 0x7523 || prodId == 0x55d4));
if (round == 3 ||
(_espHandshake && round == 0 && knownESPA) ||
(_espHandshake && round == 1 && knownESPB) ||
(!_espHandshake && round == 2 &&
port.description().contains("Bluetooth", Qt::CaseInsensitive) == false &&
port.systemLocation().contains("ttyAMA0", Qt::CaseInsensitive) == false))
{
Info(_log, "Serial port auto-discovery. Found serial port device: %s", QSTRING_CSTR(infoMessage));
return port.portName();
}
else
{
Warning(_log, "Serial port auto-discovery. Ignoring possible bluetooth device for now, try to find different available serial port: %s", QSTRING_CSTR(infoMessage));
Warning(_log, "Serial port auto-discovery. Skipping this device for now: %s, VID: 0x%x, PID: 0x%x", QSTRING_CSTR(infoMessage), vendor, prodId);
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions sources/leddevice/schemas/schema-adalight.json
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,18 @@
}
},
"propertyOrder" : 11
},
"maxRetry":
{
"type" : "integer",
"format" : "stepper",
"step" : 1,
"title" : "edt_dev_max_retry",
"minimum" : 0,
"maximum" : 120,
"default" : 0,
"required" : true,
"propertyOrder" : 12
}
},
"additionalProperties": true
Expand Down

0 comments on commit 6dfd477

Please sign in to comment.