Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion assets/webconfig/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1188,5 +1188,6 @@
"led_editor_context_moving": "Left click the mouse to accept the position",
"conf_leds_disabled_notification" : "Some LEDs are disabled by the user!",
"conf_leds_layout_context" : "Right click on the LED to display the context menu. With the CTRL key selects the object below.",
"conf_leds_layout_btn_zoom" : "Zoom"
"conf_leds_layout_btn_zoom" : "Zoom",
"edt_serial_espHandshake" : "Esp8266/ESP32 handshake"
}
102 changes: 94 additions & 8 deletions sources/leddevice/dev_serial/ProviderRs232.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
// qt includes
#include <QSerialPortInfo>
#include <QEventLoop>
#include <QThread>

#include <chrono>
#include <utils/InternalClock.h>

// Constants
constexpr std::chrono::milliseconds WRITE_TIMEOUT{ 1000 }; // device write timeout in ms
Expand All @@ -22,6 +24,7 @@ ProviderRs232::ProviderRs232(const QJsonObject& deviceConfig)
, _isAutoDeviceName(false)
, _delayAfterConnect_ms(0)
, _frameDropCounter(0)
, _espHandshake(true)
{
}

Expand All @@ -46,11 +49,13 @@ bool ProviderRs232::init(const QJsonObject& deviceConfig)
_isAutoDeviceName = _deviceName.toLower() == "auto";
_baudRate_Hz = deviceConfig["rate"].toInt();
_delayAfterConnect_ms = deviceConfig["delayAfterConnect"].toInt(0);
_espHandshake = deviceConfig["espHandshake"].toBool(false);

Debug(_log, "deviceName : %s", QSTRING_CSTR(_deviceName));
Debug(_log, "AutoDevice : %d", _isAutoDeviceName);
Debug(_log, "baudRate_Hz : %d", _baudRate_Hz);
Debug(_log, "delayAfCon ms: %d", _delayAfterConnect_ms);
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);

isInitOK = true;
}
Expand All @@ -59,6 +64,8 @@ bool ProviderRs232::init(const QJsonObject& deviceConfig)

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

int ProviderRs232::open()
Expand All @@ -76,6 +83,24 @@ int ProviderRs232::open()
return retval;
}

void ProviderRs232::waitForExitStats()
{
if (_rs232Port.isOpen())
{
if (_rs232Port.bytesAvailable() > 16)
{
auto incoming = QString(_rs232Port.readAll());

Info(_log, "Received: %s", QSTRING_CSTR(incoming));
}
if (!_isDeviceReady)
{
Debug(_log, "Close UART: %s", QSTRING_CSTR(_deviceName));
_rs232Port.close();
}
}
}

int ProviderRs232::close()
{
int retval = 0;
Expand All @@ -89,9 +114,20 @@ int ProviderRs232::close()
{
Debug(_log, "Flush was successful");
}
Debug(_log, "Close UART: %s", QSTRING_CSTR(_deviceName));
_rs232Port.close();
// Everything is OK -> device is closed


if (_espHandshake)
{
// read the statistics on goodbye
QTimer::singleShot(6000, this, &ProviderRs232::waitForExitStats);
connect(&_rs232Port, &QSerialPort::readyRead, this, &ProviderRs232::waitForExitStats);
}
else
{
Debug(_log, "Close UART: %s", QSTRING_CSTR(_deviceName));
_rs232Port.close();
}

}
return retval;
}
Expand Down Expand Up @@ -150,11 +186,61 @@ bool ProviderRs232::tryOpen(int delayAfterConnect_ms)

if (!serialPortInfo.isNull())
{
if (!_rs232Port.open(QIODevice::ReadWrite))
if (!_rs232Port.isOpen() && !_rs232Port.open(QIODevice::ReadWrite))
{
this->setInError(_rs232Port.errorString());
return false;
}

if (_espHandshake)
{
disconnect(&_rs232Port, &QSerialPort::readyRead, nullptr, nullptr);

// reset to defaults
_rs232Port.setDataTerminalReady(true);
_rs232Port.setRequestToSend(false);
QThread::msleep(50);

// reset device
_rs232Port.setDataTerminalReady(false);
_rs232Port.setRequestToSend(true);
QThread::msleep(100);

// resume device
_rs232Port.setRequestToSend(false);
QThread::msleep(100);

// read the reset message, search for AWA tag
auto start = InternalClock::now();

while(InternalClock::now() - start < 1000)
{
_rs232Port.waitForReadyRead(100);
if (_rs232Port.bytesAvailable() > 16)
{
auto incoming = _rs232Port.readAll();
for (int i = 0; i < incoming.length(); i++)
if (!(incoming[i] == '\n' ||
(incoming[i] >= ' ' && incoming[i] <= 'Z') ||
(incoming[i] >= 'a' && incoming[i] <= 'z')))
{
incoming.replace(incoming[i], '*');
}
QString result = QString(incoming).remove('*').replace('\n',' ').trimmed();
if (result.indexOf("Awa driver",Qt::CaseInsensitive) >= 0)
{
Info(_log, "DETECTED DEVICE USING HYPERSERIALESP8266/HYPERSERIALESP32 FIRMWARE (%s) at %i msec", QSTRING_CSTR(result), int(InternalClock::now() - start));
start = 0;
break;
}
}
if (InternalClock::now() <= start)
break;
}

if (start != 0)
Error(_log, "Could not detect HyperSerialEsp8266/HyperSerialESP32 device");
}
}
else
{
Expand Down
5 changes: 5 additions & 0 deletions sources/leddevice/dev_serial/ProviderRs232.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ protected slots:
///
void setInError(const QString& errorMsg) override;

public slots:
void waitForExitStats();

private:

///
Expand All @@ -114,6 +117,8 @@ protected slots:

/// Frames dropped, as write failed
int _frameDropCounter;

bool _espHandshake;
};

#endif // PROVIDERRS232_H
28 changes: 21 additions & 7 deletions sources/leddevice/schemas/schema-adalight.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@
"default": 2000000,
"propertyOrder" : 3
},
"espHandshake" :
{
"type" : "boolean",
"format": "checkbox",
"title" : "edt_serial_espHandshake",
"default" : true,
"required" : true,
"options": {
"dependencies": {
"awa_mode": true
}
},
"propertyOrder" : 4
},
"white_channel_calibration": {
"type": "boolean",
"format": "checkbox",
Expand All @@ -32,7 +46,7 @@
"awa_mode": true
}
},
"propertyOrder" : 4
"propertyOrder" : 5
},
"white_channel_limit": {
"type": "number",
Expand All @@ -49,7 +63,7 @@
"white_channel_calibration": true
}
},
"propertyOrder" : 5
"propertyOrder" : 6
},
"white_channel_red": {
"type": "integer",
Expand All @@ -65,7 +79,7 @@
"white_channel_calibration": true
}
},
"propertyOrder" : 6
"propertyOrder" : 7
},
"white_channel_green": {
"type": "integer",
Expand All @@ -81,7 +95,7 @@
"white_channel_calibration": true
}
},
"propertyOrder" : 7
"propertyOrder" : 8
},
"white_channel_blue": {
"type": "integer",
Expand All @@ -97,7 +111,7 @@
"white_channel_calibration": true
}
},
"propertyOrder" : 8
"propertyOrder" : 9
},
"delayAfterConnect": {
"type": "integer",
Expand All @@ -110,7 +124,7 @@
"awa_mode": false
}
},
"propertyOrder" : 9
"propertyOrder" : 10
},
"lightberry_apa102_mode": {
"type": "boolean",
Expand All @@ -123,7 +137,7 @@
"awa_mode": false
}
},
"propertyOrder" : 10
"propertyOrder" : 11
}
},
"additionalProperties": true
Expand Down