Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adalight: auto-resume and ESP8266/ESP32 auto-discovery #494

Merged
merged 4 commits into from
Jan 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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