From a31641390ff2b600d73546f262aeb2e28d19320b Mon Sep 17 00:00:00 2001 From: Mathieu Carbou Date: Sat, 27 Jan 2024 19:08:13 +0100 Subject: [PATCH] Fix: do not change the speed of the JSY by default and detect the current speed instead --- .github/workflows/ci.yml | 18 ++ README.md | 14 ++ docs/index.md | 14 ++ examples/EnergyReset/EnergyReset.ino | 27 ++ .../EnergyResetAsync/EnergyResetAsync.ino | 26 ++ examples/JSYRead/JSYRead.ino | 9 +- examples/JSYReadAsync/JSYReadAsync.ino | 8 +- examples/SetSpeed/SetSpeed.ino | 41 +++ platformio.ini | 30 ++- src/MycilaJSY.cpp | 235 ++++++++++++------ src/MycilaJSY.h | 42 ++-- 11 files changed, 359 insertions(+), 105 deletions(-) create mode 100644 examples/EnergyReset/EnergyReset.ino create mode 100644 examples/EnergyResetAsync/EnergyResetAsync.ino create mode 100644 examples/SetSpeed/SetSpeed.ino diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0bade24..426dc26 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -79,6 +79,15 @@ jobs: - name: Build JSYReadAsync run: arduino-cli compile --library . --warnings all -b ${{ matrix.board }} "examples/JSYReadAsync/JSYReadAsync.ino" + - name: Build EnergyReset + run: arduino-cli compile --library . --warnings all -b ${{ matrix.board }} "examples/EnergyReset/EnergyReset.ino" + + - name: Build EnergyResetAsync + run: arduino-cli compile --library . --warnings all -b ${{ matrix.board }} "examples/EnergyResetAsync/EnergyResetAsync.ino" + + - name: Build SetSpeed + run: arduino-cli compile --library . --warnings all -b ${{ matrix.board }} "examples/SetSpeed/SetSpeed.ino" + platformio: name: PlatformIO needs: cpplint @@ -120,3 +129,12 @@ jobs: - name: Build JSYReadAsync run: platformio ci "examples/JSYReadAsync/JSYReadAsync.ino" -l '.' -b ${{ matrix.board }} + + - name: Build EnergyReset + run: platformio ci "examples/EnergyReset/EnergyReset.ino" -l '.' -b ${{ matrix.board }} + + - name: Build EnergyResetAsync + run: platformio ci "examples/EnergyResetAsync/EnergyResetAsync.ino" -l '.' -b ${{ matrix.board }} + + - name: Build SetSpeed + run: platformio ci "examples/SetSpeed/SetSpeed.ino" -l '.' -b ${{ matrix.board }} diff --git a/README.md b/README.md index 5a9ba86..c839b9b 100644 --- a/README.md +++ b/README.md @@ -59,3 +59,17 @@ void loop() { // access values } ``` + +### Energy reset + +```c++ +Mycila::JSY.resetEnergy(); +``` + +### Update Baud rate (change speed) + +```c++ +if (Mycila::JSY.isEnabled() && Mycila::JSY.getBaudRate() != Mycila::JSYBaudRate::BAUD_38400 && Mycila::JSY.updateBaudRate(Mycila::JSYBaudRate::BAUD_38400)) { + ESP.restart(); +} +``` diff --git a/docs/index.md b/docs/index.md index 5a9ba86..c839b9b 100644 --- a/docs/index.md +++ b/docs/index.md @@ -59,3 +59,17 @@ void loop() { // access values } ``` + +### Energy reset + +```c++ +Mycila::JSY.resetEnergy(); +``` + +### Update Baud rate (change speed) + +```c++ +if (Mycila::JSY.isEnabled() && Mycila::JSY.getBaudRate() != Mycila::JSYBaudRate::BAUD_38400 && Mycila::JSY.updateBaudRate(Mycila::JSYBaudRate::BAUD_38400)) { + ESP.restart(); +} +``` diff --git a/examples/EnergyReset/EnergyReset.ino b/examples/EnergyReset/EnergyReset.ino new file mode 100644 index 0000000..fbc2de2 --- /dev/null +++ b/examples/EnergyReset/EnergyReset.ino @@ -0,0 +1,27 @@ +#include +#include + +void setup() { + Serial.begin(115200); + while (!Serial) + continue; + + Mycila::Logger.forwardTo(&Serial); + + // read JSY on pins 17 (JSY RX) and 16 (JSY TX) + Mycila::JSY.begin(17, 16); +} + +void loop() { + if (Mycila::JSY.read()) { + + JsonDocument doc; + Mycila::JSY.toJson(doc.to()); + serializeJson(doc, Serial); + Serial.println(); + + if (Mycila::JSY.energy1 > 0 || Mycila::JSY.energy2 > 0 || Mycila::JSY.energyReturned1 > 0 || Mycila::JSY.energyReturned2 > 0) + Mycila::JSY.resetEnergy(); + } + delay(1000); +} diff --git a/examples/EnergyResetAsync/EnergyResetAsync.ino b/examples/EnergyResetAsync/EnergyResetAsync.ino new file mode 100644 index 0000000..09fd87b --- /dev/null +++ b/examples/EnergyResetAsync/EnergyResetAsync.ino @@ -0,0 +1,26 @@ +#include +#include + +void setup() { + Serial.begin(115200); + while (!Serial) + continue; + + Mycila::Logger.forwardTo(&Serial); + + // read JSY on pins 17 (JSY RX) and 16 (JSY TX) + Mycila::JSY.begin(17, 16, &Serial2, true, 60, 0); +} + +void loop() { + if (Mycila::JSY.isEnabled()) { + JsonDocument doc; + Mycila::JSY.toJson(doc.to()); + serializeJson(doc, Serial); + Serial.println(); + + if (Mycila::JSY.energy1 > 0 || Mycila::JSY.energy2 > 0 || Mycila::JSY.energyReturned1 > 0 || Mycila::JSY.energyReturned2 > 0) + Mycila::JSY.resetEnergy(); + } + delay(1000); +} diff --git a/examples/JSYRead/JSYRead.ino b/examples/JSYRead/JSYRead.ino index 5f7f0e0..86f5a66 100644 --- a/examples/JSYRead/JSYRead.ino +++ b/examples/JSYRead/JSYRead.ino @@ -1,14 +1,15 @@ #include +#include void setup() { Serial.begin(115200); while (!Serial) continue; - Mycila::JSY.begin(17, 16, Mycila::JSYBaudRate::BAUD_38400, &Serial2); - // Mycila::JSY.begin(17, 16, Mycila::JSYBaudRate::BAUD_19200, &Serial2); - // Mycila::JSY.begin(17, 16, Mycila::JSYBaudRate::BAUD_9600, &Serial2); - // Mycila::JSY.begin(17, 16, Mycila::JSYBaudRate::BAUD_4800, &Serial2); + Mycila::Logger.forwardTo(&Serial); + + // read JSY on pins 17 (JSY RX) and 16 (JSY TX) + Mycila::JSY.begin(17, 16); } void loop() { diff --git a/examples/JSYReadAsync/JSYReadAsync.ino b/examples/JSYReadAsync/JSYReadAsync.ino index 964eb94..ed59920 100644 --- a/examples/JSYReadAsync/JSYReadAsync.ino +++ b/examples/JSYReadAsync/JSYReadAsync.ino @@ -1,11 +1,15 @@ #include +#include void setup() { Serial.begin(115200); while (!Serial) continue; - Mycila::JSY.begin(17, 16, Mycila::JSYBaudRate::BAUD_38400, &Serial2, true, 60, 0); + Mycila::Logger.forwardTo(&Serial); + + // read JSY on pins 17 (JSY RX) and 16 (JSY TX) + Mycila::JSY.begin(17, 16, &Serial2, true, 60, 0); } void loop() { @@ -15,5 +19,5 @@ void loop() { serializeJson(doc, Serial); Serial.println(); } - delay(5000); + delay(1000); } diff --git a/examples/SetSpeed/SetSpeed.ino b/examples/SetSpeed/SetSpeed.ino new file mode 100644 index 0000000..8923d28 --- /dev/null +++ b/examples/SetSpeed/SetSpeed.ino @@ -0,0 +1,41 @@ +#include +#include + +void setup() { + Serial.begin(115200); + while (!Serial) + continue; + + Mycila::Logger.forwardTo(&Serial); + + // read JSY on pins 17 (JSY RX) and 16 (JSY TX) + Mycila::JSY.begin(17, 16); + + if (Mycila::JSY.isEnabled()) { + Mycila::Logger.info("APP", "JSY is enabled at %d bauds", Mycila::JSY.getBaudRate()); + + if (Mycila::JSY.getBaudRate() != Mycila::JSYBaudRate::BAUD_38400) { + Mycila::Logger.warn("APP", "JSY baud rate wrong"); + + if (Mycila::JSY.updateBaudRate(Mycila::JSYBaudRate::BAUD_38400)) { + Mycila::Logger.info("APP", "JSY baud rate updated"); + ESP.restart(); + + } else { + Mycila::Logger.error("APP", "JSY baud rate update failed"); + } + } + } else { + Mycila::Logger.info("APP", "JSY is disabled"); + } +} + +void loop() { + if (Mycila::JSY.read()) { + JsonDocument doc; + Mycila::JSY.toJson(doc.to()); + serializeJson(doc, Serial); + Serial.println(); + } + delay(1000); +} diff --git a/platformio.ini b/platformio.ini index c41506a..ab94349 100644 --- a/platformio.ini +++ b/platformio.ini @@ -1,8 +1,31 @@ +[platformio] +lib_dir = . +; src_dir = examples/JSYRead +; src_dir = examples/JSYReadAsync +; src_dir = examples/EnergyReset +; src_dir = examples/EnergyResetAsync +src_dir = examples/SetSpeed +default_envs = esp32 + [env] -build_flags = -Wall -Wextra + +build_flags = + -Wall -Wextra + -Wunused -Wmisleading-indentation -Wduplicated-cond -Wlogical-op -Wnull-dereference + -std=c++17 + -std=gnu++17 + -O0 + -ggdb -ggdb3 -g3 + -D CONFIG_ARDUHAL_LOG_COLORS + -D CORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG +build_unflags = + -std=gnu++11 + lib_deps = bblanchon/ArduinoJson mathieucarbou/MycilaLogger +lib_compat_mode = strict +lib_ldf_mode = deep+ upload_protocol = esptool upload_port = /dev/cu.usbserial-0001 @@ -11,11 +34,6 @@ monitor_port = /dev/cu.usbserial-0001 monitor_speed = 115200 monitor_filters = esp32_exception_decoder, log2file -[platformio] -lib_dir = . -src_dir = examples/JSYRead -; src_dir = examples/JSYReadAsync - [env:esp32] platform = espressif32@6.5.0 board = esp32dev diff --git a/src/MycilaJSY.cpp b/src/MycilaJSY.cpp index 2a8aa23..1857246 100644 --- a/src/MycilaJSY.cpp +++ b/src/MycilaJSY.cpp @@ -13,7 +13,7 @@ static const uint8_t JSY_READ_MSG[] = {0x01, 0x03, 0x00, 0x48, 0x00, 0x0E, 0x44, 0x18}; -void Mycila::JSYClass::begin(const uint8_t jsyRXPin, const uint8_t jsyTXPin, const JSYBaudRate baudRate, HardwareSerial* serial, const bool async, uint32_t pause, uint8_t core, uint32_t stackSize) { +void Mycila::JSYClass::begin(const uint8_t jsyRXPin, const uint8_t jsyTXPin, HardwareSerial* serial, const bool async, uint32_t pause, uint8_t core, uint32_t stackSize) { if (_enabled) return; @@ -33,50 +33,35 @@ void Mycila::JSYClass::begin(const uint8_t jsyRXPin, const uint8_t jsyTXPin, con return; } + _serial->begin((uint32_t)JSYBaudRate::BAUD_38400, SERIAL_8N1, _pinTX, _pinRX); + _async = async; _pause = pause; _serial = serial; + _baudRate = _detectBauds(); Logger.info(TAG, "Enable JSY..."); Logger.debug(TAG, "- JSY RX Pin: %u", _pinRX); Logger.debug(TAG, "- JSY TX Pin: %u", _pinTX); Logger.debug(TAG, "- Async: %s", _async ? "true" : "false"); - Logger.debug(TAG, "- Baud Rate: %u bps", (uint32_t)baudRate); + Logger.debug(TAG, "- Detected speed: %u bauds", (uint32_t)_baudRate); + + if (_baudRate == JSYBaudRate::UNKNOWN) { + Logger.warn(TAG, "Unable to read JSY at any supported speed. Trying to fix..."); + _serial->begin(4800, SERIAL_8N1, _pinTX, _pinRX); + _requestedBaudRate = JSYBaudRate::BAUD_38400; + if (_updateBaudRate()) { + _baudRate = _detectBauds(); + Logger.debug(TAG, "- Detected speed: %u bauds", (uint32_t)_baudRate); + } - const JSYBaudRate baudRates[] = {JSYBaudRate::BAUD_4800, JSYBaudRate::BAUD_9600, JSYBaudRate::BAUD_19200, JSYBaudRate::BAUD_38400}; - bool ok = false; - for (int i = 0; i < 4; i++) { - _serial->begin((uint32_t)baudRates[i], SERIAL_8N1, _pinTX, _pinRX); - while (!_serial) - yield(); - if (_read(4)) { - Logger.debug(TAG, "- Detected baud Rate: %u bps", (uint32_t)baudRates[i]); - if (baudRates[i] == baudRate) { - ok = true; - break; - } - _setBaudRate(baudRate); - _serial->end(); - _serial->begin((uint32_t)baudRate, SERIAL_8N1, _pinTX, _pinRX); - while (!_serial) - yield(); - if (!_read(4)) { - Logger.error(TAG, "Unable to read JSY data after baud rate change"); - break; - } - ok = true; - break; - } else { + if (_baudRate == JSYBaudRate::UNKNOWN) { + Logger.error(TAG, "Unable to read JSY at any supported speed. Disabling JSY."); _serial->end(); + return; } } - if (!ok) { - Logger.error(TAG, "Unable to read JSY data with any baud rate"); - _serial->end(); - return; - } - if (_async && xTaskCreateUniversal(_jsyTask, "jsyTask", stackSize, this, 1, nullptr, core) != pdPASS) { Logger.error(TAG, "Unable to create JSY async task"); return; @@ -86,31 +71,84 @@ void Mycila::JSYClass::begin(const uint8_t jsyRXPin, const uint8_t jsyTXPin, con } void Mycila::JSYClass::end() { - if (_disable()) { + if (_enabled) { + Logger.info(TAG, "Disable JSY..."); + _enabled = false; + _baudRate = JSYBaudRate::UNKNOWN; + while (_state != JSYState::IDLE) + delay(60); + current1 = 0; + current2 = 0; + energy1 = 0; + energy2 = 0; + energyReturned1 = 0; + energyReturned2 = 0; + frequency = 0; + power1 = 0; + power2 = 0; + powerFactor1 = 0; + powerFactor2 = 0; + voltage1 = 0; + voltage2 = 0; _serial->end(); } } -void Mycila::JSYClass::endAndResetEnergy() { - _disable(); +bool Mycila::JSYClass::read() { + if (!_enabled) + return false; - Logger.warn(TAG, "Energy Reset..."); + if (_async) + return false; - const uint8_t data[] = {0x01, 0x10, 0x00, 0x0C, 0x00, 0x02, 0x04, 0x00, 0x00, 0x00, 0x00, 0xF3, 0xFA}; - _serial->write(data, 13); - _serial->flush(); - _drop(); + return _read(); +} - // Note: do not end() _serial: ESP needs to restart right after sending the reset command - Logger.debug(TAG, "Energy Reset done"); +bool Mycila::JSYClass::resetEnergy() { + if (!_enabled) + return false; + + Logger.info(TAG, "Trying to clear JSY Energy data..."); + + if (_async) { + _request = JSYState::RESET; + return true; + } + + bool success = _reset(); + + if (success) + Logger.info(TAG, "Request sent to JSY!"); + else + Logger.warn(TAG, "Unable to clear energy data: JSY busy"); + + return success; } -bool Mycila::JSYClass::read() { +bool Mycila::JSYClass::updateBaudRate(const JSYBaudRate baudRate) { if (!_enabled) return false; - if (_async) + + if (baudRate == JSYBaudRate::UNKNOWN) return false; - return _read(); + + Logger.info(TAG, "Trying to update JSY baud rate to %u...", (uint32_t)baudRate); + + _requestedBaudRate = baudRate; + + if (_async) { + _request = JSYState::BAUDS; + return true; + } + + bool success = _updateBaudRate(); + + if (success) + Logger.info(TAG, "Request sent to JSY!"); + else + Logger.warn(TAG, "Unable to update baud rate: JSY busy"); + + return success; } void Mycila::JSYClass::toJson(const JsonObject& root) const { @@ -140,22 +178,26 @@ bool Mycila::JSYClass::_read(uint8_t maxCount) { } bool __attribute__((hot)) Mycila::JSYClass::_read() { - if (_reading) + if (_state != JSYState::IDLE) return false; - _reading = true; + _state = JSYState::READ; _serial->write(JSY_READ_MSG, 8); _serial->flush(); uint8_t buffer[SUCCESSFUL_JSY_READ_COUNT]; size_t count = 0; - while (count < SUCCESSFUL_JSY_READ_COUNT && _serial->available()) + // Serial.printf("Read: "); + while (count < SUCCESSFUL_JSY_READ_COUNT && _serial->available()) { buffer[count++] = _serial->read(); + // Serial.printf("%02X", buffer[count - 1]); + } + // Serial.printf(" (%d)\n", count); count += _drop(); if (count != SUCCESSFUL_JSY_READ_COUNT || buffer[0] != 0x01) { - _reading = false; + _state = JSYState::IDLE; return false; } @@ -177,15 +219,41 @@ bool __attribute__((hot)) Mycila::JSYClass::_read() { powerFactor2 = ((buffer[51] << 24) + (buffer[52] << 16) + (buffer[53] << 8) + buffer[54]) * 0.001; energyReturned2 = ((buffer[55] << 24) + (buffer[56] << 16) + (buffer[57] << 8) + buffer[58]) * 0.0001; - _reading = false; + _state = JSYState::IDLE; return true; } -bool Mycila::JSYClass::_setBaudRate(JSYBaudRate baudRate) { - Logger.debug(TAG, "Set JSY baud rate to %u bps...", (uint32_t)baudRate); +bool Mycila::JSYClass::_reset() { + if (_state != JSYState::IDLE) + return false; + + _state = JSYState::RESET; + + const uint8_t data[] = {0x01, 0x10, 0x00, 0x0C, 0x00, 0x02, 0x04, 0x00, 0x00, 0x00, 0x00, 0xF3, 0xFA}; + + for (size_t i = 0; i < 30; i++) { + _serial->write(data, 13); + _serial->flush(); + _drop(); + delay(60); + } + + _state = JSYState::IDLE; + return true; +} + +bool Mycila::JSYClass::_updateBaudRate() { + if (_requestedBaudRate == JSYBaudRate::UNKNOWN) + return false; + + if (_state != JSYState::IDLE) + return false; + + _state = JSYState::BAUDS; uint8_t data[] = {0x00, 0x10, 0x00, 0x04, 0x00, 0x01, 0x02, 0x01, 0x00, 0x00, 0x00}; - switch (baudRate) { + + switch (_requestedBaudRate) { case JSYBaudRate::BAUD_4800: data[8] = 0x05; data[9] = 0x6B; @@ -211,46 +279,44 @@ bool Mycila::JSYClass::_setBaudRate(JSYBaudRate baudRate) { break; } - _serial->write(data, 11); - _serial->flush(); - _drop(); + for (size_t i = 0; i < 30; i++) { + _serial->write(data, 11); + _serial->flush(); + _drop(); + delay(60); + } - Logger.debug(TAG, "JSY baud rate updated."); + _requestedBaudRate = JSYBaudRate::UNKNOWN; + _state = JSYState::IDLE; return true; } size_t Mycila::JSYClass::_drop() { size_t count = 0; + // Serial.printf("Drop: "); while (_serial->available()) { + // Serial.printf("%02X", _serial->read()); _serial->read(); count++; } + // Serial.printf(" (%d)\n", count); return count; } -bool Mycila::JSYClass::_disable() { - if (_enabled) { - Logger.info(TAG, "Disable JSY..."); - _enabled = false; - while (_reading) - delay(100); - current1 = 0; - current2 = 0; - energy1 = 0; - energy2 = 0; - energyReturned1 = 0; - energyReturned2 = 0; - frequency = 0; - power1 = 0; - power2 = 0; - powerFactor1 = 0; - powerFactor2 = 0; - voltage1 = 0; - voltage2 = 0; - return true; +Mycila::JSYBaudRate Mycila::JSYClass::_detectBauds() { + const JSYBaudRate baudRates[] = {JSYBaudRate::BAUD_38400, JSYBaudRate::BAUD_19200, JSYBaudRate::BAUD_9600, JSYBaudRate::BAUD_4800}; + for (int i = 0; i < 4; i++) { + Logger.debug(TAG, "Trying to read JSY at %u bauds...", (uint32_t)baudRates[i]); + _serial->updateBaudRate((uint32_t)baudRates[i]); + while (!_serial) + continue; + _serial->flush(); + _drop(); + if (_read(5)) + return baudRates[i]; } - return false; + return JSYBaudRate::UNKNOWN; } void Mycila::JSYClass::_jsyTask(void* params) { @@ -258,7 +324,18 @@ void Mycila::JSYClass::_jsyTask(void* params) { // Serial.println(xPortGetCoreID()); JSYClass* jsy = reinterpret_cast(params); while (jsy->_enabled) { - jsy->_read(); + switch (jsy->_request) { + case JSYState::RESET: + jsy->_reset(); + break; + case JSYState::BAUDS: + jsy->_updateBaudRate(); + break; + default: + jsy->_read(); + break; + } + jsy->_request = JSYState::IDLE; if (jsy->_enabled) // https://esp32developer.com/programming-in-c-c/tasks/tasks-vs-co-routines delay(max(portTICK_PERIOD_MS, jsy->_pause)); diff --git a/src/MycilaJSY.h b/src/MycilaJSY.h index 8467495..a3985fc 100644 --- a/src/MycilaJSY.h +++ b/src/MycilaJSY.h @@ -26,18 +26,26 @@ namespace Mycila { enum class JSYBaudRate { + UNKNOWN = 0, BAUD_4800 = 4800, BAUD_9600 = 9600, BAUD_19200 = 19200, BAUD_38400 = 38400, }; + enum class JSYState { + IDLE = 0, + READ = 1, + RESET = 2, + BAUDS = 3, + }; + class JSYClass { public: // jsyRXPin: pin connected to the RX of the JSY, jsyTXPin: pin connected to the TX of the JSY + // The baud rate is automatically detected void begin(const uint8_t jsyRXPin, const uint8_t jsyTXPin, - const JSYBaudRate baudRate = JSYBaudRate::BAUD_38400, HardwareSerial* serial = &Serial2, const bool async = false, uint32_t pause = MYCILA_JSY_ASYNC_READ_PAUSE_INTERVAL_MS, @@ -46,19 +54,21 @@ namespace Mycila { void end(); - // ends the JSY reading and resets - // note that after this call, I need to restart the ESP so that the reset can be achieved. - // TODO: check if we can instead do a voltage cycle (LOW - HIGH) on the pins and restart Serial2 - void endAndResetEnergy(); + // IMPORTANT: DO NOT CALL read() in async mode: it will have no effect and will return false. + bool read(); - gpio_num_t getRXPin() const { return _pinRX; } - gpio_num_t getTXPin() const { return _pinTX; } - bool isEnabled() const { return _enabled; } + // Resets energy counters. Returns true if the reset was successful, or will be done asynchronously. + bool resetEnergy(); + + // Try to change the baud rate of the JSY. Returns true if the baud rate was changed, or will be done asynchronously. + bool updateBaudRate(const JSYBaudRate baudRate); void toJson(const JsonObject& root) const; - // IMPORTANT: DO NOT CALL read() in async mode: it will have no effect and will return false - bool read(); + gpio_num_t getRXPin() const { return _pinRX; } + gpio_num_t getTXPin() const { return _pinTX; } + JSYBaudRate getBaudRate() const { return _baudRate; } + bool isEnabled() const { return _enabled; } public: volatile float current1 = 0; // A @@ -78,18 +88,22 @@ namespace Mycila { private: gpio_num_t _pinRX = GPIO_NUM_NC; gpio_num_t _pinTX = GPIO_NUM_NC; + JSYBaudRate _baudRate = JSYBaudRate::UNKNOWN; HardwareSerial* _serial = &Serial2; uint32_t _pause = MYCILA_JSY_ASYNC_READ_PAUSE_INTERVAL_MS; volatile bool _async = false; volatile bool _enabled = false; - volatile bool _reading = false; + volatile JSYState _state = JSYState::IDLE; + volatile JSYState _request = JSYState::IDLE; + volatile JSYBaudRate _requestedBaudRate = JSYBaudRate::UNKNOWN; private: - bool _disable(); - bool _read(); bool _read(uint8_t maxCount); - bool _setBaudRate(JSYBaudRate baudRate); + bool _read(); + bool _reset(); + bool _updateBaudRate(); size_t _drop(); + JSYBaudRate _detectBauds(); static void _jsyTask(void* pvParameters); };