From 27e54d9dbf8645a5ec59da8902c744563c1ec027 Mon Sep 17 00:00:00 2001 From: Mathieu Carbou Date: Sat, 17 Feb 2024 22:40:28 +0100 Subject: [PATCH] Introduce loop() and getTime() --- .../EnergyResetAsync/EnergyResetAsync.ino | 2 +- examples/JSYReadAsync/JSYReadAsync.ino | 2 +- src/MycilaJSY.cpp | 41 +++++++++++-------- src/MycilaJSY.h | 10 ++++- 4 files changed, 36 insertions(+), 19 deletions(-) diff --git a/examples/EnergyResetAsync/EnergyResetAsync.ino b/examples/EnergyResetAsync/EnergyResetAsync.ino index 506c67c..4051813 100644 --- a/examples/EnergyResetAsync/EnergyResetAsync.ino +++ b/examples/EnergyResetAsync/EnergyResetAsync.ino @@ -10,7 +10,7 @@ void setup() { continue; // read JSY on pins 17 (JSY RX) and 16 (JSY TX) - jsy.begin(17, 16, &MYCILA_JSY_SERIAL, true, 60, 0); + jsy.begin(17, 16, &MYCILA_JSY_SERIAL, 60, true, 0); } void loop() { diff --git a/examples/JSYReadAsync/JSYReadAsync.ino b/examples/JSYReadAsync/JSYReadAsync.ino index 9f3e4f8..fe61bda 100644 --- a/examples/JSYReadAsync/JSYReadAsync.ino +++ b/examples/JSYReadAsync/JSYReadAsync.ino @@ -10,7 +10,7 @@ void setup() { continue; // read JSY on pins 17 (JSY RX) and 16 (JSY TX) - jsy.begin(17, 16, &MYCILA_JSY_SERIAL, true, 60, 0); + jsy.begin(17, 16, &MYCILA_JSY_SERIAL, 60, true, 0); } void loop() { diff --git a/src/MycilaJSY.cpp b/src/MycilaJSY.cpp index daea383..3b548ac 100644 --- a/src/MycilaJSY.cpp +++ b/src/MycilaJSY.cpp @@ -16,7 +16,7 @@ static const uint8_t JSY_READ_MSG[] = {0x01, 0x03, 0x00, 0x48, 0x00, 0x0E, 0x44, 0x18}; -void Mycila::JSY::begin(const uint8_t jsyRXPin, const uint8_t jsyTXPin, HardwareSerial* serial, const bool async, uint32_t pause, uint8_t core, uint32_t stackSize) { +void Mycila::JSY::begin(const uint8_t jsyRXPin, const uint8_t jsyTXPin, HardwareSerial* serial, uint32_t pause, const bool async, uint8_t core, uint32_t stackSize) { if (_enabled) return; @@ -96,6 +96,26 @@ void Mycila::JSY::end() { } } +void Mycila::JSY::loop() { + if (!_enabled) + return; + switch (_request) { + case JSYState::RESET: + _reset(); + break; + case JSYState::BAUDS: + _updateBaudRate(); + break; + default: + if (millis() - _lastRead >= _pause) { + _read(); + _lastRead = millis(); + } + break; + } + _request = JSYState::IDLE; +} + bool Mycila::JSY::read() { if (!_enabled) return false; @@ -155,6 +175,7 @@ void Mycila::JSY::toJson(const JsonObject& root) const { root["power2"] = power2; root["voltage1"] = voltage1; root["voltage2"] = voltage2; + root["time"] = _lastReadSuccess; } #endif @@ -210,6 +231,7 @@ bool __attribute__((hot)) Mycila::JSY::_read() { energyReturned2 = ((buffer[55] << 24) + (buffer[56] << 16) + (buffer[57] << 8) + buffer[58]) * 0.0001; _state = JSYState::IDLE; + _lastReadSuccess = millis(); return true; } @@ -327,21 +349,8 @@ void Mycila::JSY::_jsyTask(void* params) { // Serial.println(xPortGetCoreID()); JSY* jsy = reinterpret_cast(params); while (jsy->_enabled) { - 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)); + jsy->loop(); + delay(portTICK_PERIOD_MS); } vTaskDelete(NULL); } diff --git a/src/MycilaJSY.h b/src/MycilaJSY.h index 07ec775..9d68088 100644 --- a/src/MycilaJSY.h +++ b/src/MycilaJSY.h @@ -57,13 +57,16 @@ namespace Mycila { void begin(const uint8_t jsyRXPin, const uint8_t jsyTXPin, HardwareSerial* serial = &MYCILA_JSY_SERIAL, - const bool async = false, uint32_t pause = MYCILA_JSY_ASYNC_READ_PAUSE_INTERVAL_MS, + const bool async = false, uint8_t core = MYCILA_JSY_ASYNC_CORE, uint32_t stackSize = MYCILA_JSY_ASYNC_STACK_SIZE); void end(); + // IMPORTANT: DO NOT CALL loop() in async mode + void loop(); + // IMPORTANT: DO NOT CALL read() in async mode: it will have no effect and will return false. bool read(); @@ -96,6 +99,9 @@ namespace Mycila { float getVoltage1() const { return voltage1; } float getVoltage2() const { return voltage2; } + // get the uptime in milliseconds of the last successful read + uint32_t getTime() const { return _lastReadSuccess; } + private: volatile float current1 = 0; // A volatile float current2 = 0; // A @@ -116,6 +122,8 @@ namespace Mycila { gpio_num_t _pinTX = GPIO_NUM_NC; HardwareSerial* _serial = &MYCILA_JSY_SERIAL; uint32_t _pause = MYCILA_JSY_ASYNC_READ_PAUSE_INTERVAL_MS; + uint32_t _lastRead = 0; + uint32_t _lastReadSuccess = 0; volatile bool _async = false; volatile bool _enabled = false; volatile JSYBaudRate _baudRate = JSYBaudRate::UNKNOWN;