|
| 1 | +/* EspTools.h |
| 2 | +* |
| 3 | +* MIT License |
| 4 | +* |
| 5 | +* Copyright (c) 2022 awawa-dev |
| 6 | +* |
| 7 | +* Project homesite: https://github.com/awawa-dev/HyperHDR |
| 8 | +* |
| 9 | +* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | +* of this software and associated documentation files (the "Software"), to deal |
| 11 | +* in the Software without restriction, including without limitation the rights |
| 12 | +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | +* copies of the Software, and to permit persons to whom the Software is |
| 14 | +* furnished to do so, subject to the following conditions: |
| 15 | +* |
| 16 | +* The above copyright notice and this permission notice shall be included in all |
| 17 | +* copies or substantial portions of the Software. |
| 18 | +
|
| 19 | +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 25 | +* SOFTWARE. |
| 26 | + */ |
| 27 | + |
| 28 | +#ifndef ESPTOOLS_H |
| 29 | +#define ESPTOOLS_H |
| 30 | + |
| 31 | +class EspTools |
| 32 | +{ |
| 33 | + public: |
| 34 | + |
| 35 | + static void goingSleep(QSerialPort& _rs232Port) |
| 36 | + { |
| 37 | + uint8_t comBuffer[] = { 0x41, 0x77, 0x41, 0x2a, 0xa2, 0x35, 0x68, 0x79, 0x70, 0x65, 0x72, 0x68, 0x64, 0x72 }; |
| 38 | + _rs232Port.write((char*)comBuffer, sizeof(comBuffer)); |
| 39 | + _rs232Port.flush(); |
| 40 | + _rs232Port.clear(); |
| 41 | + } |
| 42 | + |
| 43 | + static void initializeEsp(QSerialPort& _rs232Port, QSerialPortInfo& serialPortInfo, Logger*& _log) |
| 44 | + { |
| 45 | + if (serialPortInfo.productIdentifier() == 0x80c2 && serialPortInfo.vendorIdentifier() == 0x303a) |
| 46 | + { |
| 47 | + Warning(_log, "Detected ESP32-S2 lolin mini type board. HyperHDR skips the reset. State: %i, %i", |
| 48 | + _rs232Port.isDataTerminalReady(), _rs232Port.isRequestToSend()); |
| 49 | + |
| 50 | + uint8_t comBuffer[] = { 0x41, 0x77, 0x41, 0x2a, 0xa2, 0x15, 0x68, 0x79, 0x70, 0x65, 0x72, 0x68, 0x64, 0x72 }; |
| 51 | + _rs232Port.write((char*)comBuffer, sizeof(comBuffer)); |
| 52 | + |
| 53 | + _rs232Port.setDataTerminalReady(true); |
| 54 | + _rs232Port.setRequestToSend(true); |
| 55 | + _rs232Port.setRequestToSend(false); |
| 56 | + } |
| 57 | + else |
| 58 | + { |
| 59 | + // reset to defaults |
| 60 | + _rs232Port.setDataTerminalReady(true); |
| 61 | + _rs232Port.setRequestToSend(false); |
| 62 | + QThread::msleep(50); |
| 63 | + |
| 64 | + // reset device |
| 65 | + _rs232Port.setDataTerminalReady(false); |
| 66 | + _rs232Port.setRequestToSend(true); |
| 67 | + QThread::msleep(100); |
| 68 | + |
| 69 | + // resume device |
| 70 | + _rs232Port.setRequestToSend(false); |
| 71 | + QThread::msleep(100); |
| 72 | + } |
| 73 | + |
| 74 | + // read the reset message, search for AWA tag |
| 75 | + auto start = InternalClock::now(); |
| 76 | + |
| 77 | + while (InternalClock::now() - start < 1000) |
| 78 | + { |
| 79 | + _rs232Port.waitForReadyRead(100); |
| 80 | + if (_rs232Port.bytesAvailable() > 16) |
| 81 | + { |
| 82 | + auto incoming = _rs232Port.readAll(); |
| 83 | + for (int i = 0; i < incoming.length(); i++) |
| 84 | + if (!(incoming[i] == '\n' || |
| 85 | + (incoming[i] >= ' ' && incoming[i] <= 'Z') || |
| 86 | + (incoming[i] >= 'a' && incoming[i] <= 'z'))) |
| 87 | + { |
| 88 | + incoming.replace(incoming[i], '*'); |
| 89 | + } |
| 90 | + QString result = QString(incoming).remove('*').replace('\n', ' ').trimmed(); |
| 91 | + if (result.indexOf("Awa driver", Qt::CaseInsensitive) >= 0) |
| 92 | + { |
| 93 | + Info(_log, "DETECTED DEVICE USING HYPERSERIALESP8266/HYPERSERIALESP32 FIRMWARE (%s) at %i msec", QSTRING_CSTR(result), int(InternalClock::now() - start)); |
| 94 | + start = 0; |
| 95 | + break; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + if (InternalClock::now() <= start) |
| 100 | + break; |
| 101 | + } |
| 102 | + |
| 103 | + if (start != 0) |
| 104 | + Error(_log, "Could not detect HyperSerialEsp8266/HyperSerialESP32 device"); |
| 105 | + } |
| 106 | +}; |
| 107 | + |
| 108 | +#endif |
0 commit comments