Skip to content

Commit 6dfd477

Browse files
authored
Adalight: auto-resume and ESP8266/ESP32 auto-discovery (#494)
* Prefer ESP devices if ESP handshake is enabled * Rescan devices each time when open * Add auto-resume for Adalight device * Update ProviderRs232.cpp
1 parent 4f1e0bd commit 6dfd477

File tree

2 files changed

+65
-9
lines changed

2 files changed

+65
-9
lines changed

Diff for: sources/leddevice/dev_serial/ProviderRs232.cpp

+53-9
Original file line numberDiff line numberDiff line change
@@ -52,27 +52,31 @@ bool ProviderRs232::init(const QJsonObject& deviceConfig)
5252
_baudRate_Hz = deviceConfig["rate"].toInt();
5353
_delayAfterConnect_ms = deviceConfig["delayAfterConnect"].toInt(0);
5454
_espHandshake = deviceConfig["espHandshake"].toBool(false);
55+
_maxRetry = _devConfig["maxRetry"].toInt(60);
5556

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

6264
isInitOK = true;
6365
}
6466
return isInitOK;
6567
}
6668

6769
ProviderRs232::~ProviderRs232()
68-
{
69-
if (_rs232Port.isOpen())
70-
_rs232Port.close();
70+
{
7171
}
7272

7373
int ProviderRs232::open()
7474
{
7575
int retval = -1;
76+
77+
if (_retryMode)
78+
return retval;
79+
7680
_isDeviceReady = false;
7781

7882
// open device physically
@@ -81,6 +85,27 @@ int ProviderRs232::open()
8185
// Everything is OK, device is ready
8286
_isDeviceReady = true;
8387
retval = 0;
88+
89+
_currentRetry = 0;
90+
_retryMode = false;
91+
}
92+
else if (_maxRetry > 0)
93+
{
94+
if (_currentRetry <= 0)
95+
_currentRetry = _maxRetry + 1;
96+
97+
_currentRetry--;
98+
99+
if (_currentRetry > 0)
100+
Warning(_log, "The serial device is not ready... will try to reconnect (try %i/%i).", (_maxRetry - _currentRetry + 1), _maxRetry);
101+
else
102+
Error(_log, "The serial device is not ready... give up.");
103+
104+
if (_currentRetry > 0)
105+
{
106+
_retryMode = true;
107+
QTimer::singleShot(2000, [this]() { _retryMode = false; if (_currentRetry > 0) enableDevice(true); });
108+
}
84109
}
85110
return retval;
86111
}
@@ -133,6 +158,11 @@ int ProviderRs232::close()
133158

134159
QTimer::singleShot(200, this, [this]() { if (_rs232Port.isOpen()) EspTools::goingSleep(_rs232Port); });
135160
EspTools::goingSleep(_rs232Port);
161+
162+
for (int i = 0; i < 6 && _rs232Port.isOpen(); i++)
163+
{
164+
_rs232Port.waitForReadyRead(100);
165+
}
136166
}
137167
else
138168
{
@@ -157,7 +187,7 @@ bool ProviderRs232::powerOff()
157187

158188
bool ProviderRs232::tryOpen(int delayAfterConnect_ms)
159189
{
160-
if (_deviceName.isEmpty() || _rs232Port.portName().isEmpty())
190+
if (_deviceName.isEmpty() || _rs232Port.portName().isEmpty() || (_isAutoDeviceName && _espHandshake))
161191
{
162192
if (!_rs232Port.isOpen())
163193
{
@@ -291,12 +321,18 @@ int ProviderRs232::writeBytes(const qint64 size, const uint8_t* data)
291321
}
292322
}
293323
}
324+
325+
if (_maxRetry > 0 && rc == -1)
326+
{
327+
QTimer::singleShot(2000, this, [=]() { enable(); });
328+
}
329+
294330
return rc;
295331
}
296332

297333
QString ProviderRs232::discoverFirst()
298334
{
299-
for (int round = 0; round < 2; round++)
335+
for (int round = 0; round < 4; round++)
300336
for (auto const& port : QSerialPortInfo::availablePorts())
301337
{
302338
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
@@ -306,17 +342,25 @@ QString ProviderRs232::discoverFirst()
306342
#endif
307343
{
308344
QString infoMessage = QString("%1 (%2 => %3)").arg(port.description()).arg(port.systemLocation()).arg(port.portName());
309-
310-
if (round != 0 ||
311-
(port.description().contains("Bluetooth", Qt::CaseInsensitive) == false &&
345+
quint16 vendor = port.vendorIdentifier();
346+
quint16 prodId = port.productIdentifier();
347+
bool knownESPA = (vendor == 0x303a && (prodId == 0x80c2));
348+
bool knownESPB = (vendor == 0x303a) ||
349+
(vendor == 0x10c4 && (prodId == 0xea60)) ||
350+
(vendor == 0x1A86 && (prodId == 0x7523 || prodId == 0x55d4));
351+
if (round == 3 ||
352+
(_espHandshake && round == 0 && knownESPA) ||
353+
(_espHandshake && round == 1 && knownESPB) ||
354+
(!_espHandshake && round == 2 &&
355+
port.description().contains("Bluetooth", Qt::CaseInsensitive) == false &&
312356
port.systemLocation().contains("ttyAMA0", Qt::CaseInsensitive) == false))
313357
{
314358
Info(_log, "Serial port auto-discovery. Found serial port device: %s", QSTRING_CSTR(infoMessage));
315359
return port.portName();
316360
}
317361
else
318362
{
319-
Warning(_log, "Serial port auto-discovery. Ignoring possible bluetooth device for now, try to find different available serial port: %s", QSTRING_CSTR(infoMessage));
363+
Warning(_log, "Serial port auto-discovery. Skipping this device for now: %s, VID: 0x%x, PID: 0x%x", QSTRING_CSTR(infoMessage), vendor, prodId);
320364
}
321365
}
322366
}

Diff for: sources/leddevice/schemas/schema-adalight.json

+12
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,18 @@
138138
}
139139
},
140140
"propertyOrder" : 11
141+
},
142+
"maxRetry":
143+
{
144+
"type" : "integer",
145+
"format" : "stepper",
146+
"step" : 1,
147+
"title" : "edt_dev_max_retry",
148+
"minimum" : 0,
149+
"maximum" : 120,
150+
"default" : 0,
151+
"required" : true,
152+
"propertyOrder" : 12
141153
}
142154
},
143155
"additionalProperties": true

0 commit comments

Comments
 (0)