Skip to content

Commit

Permalink
Introduce loop() and getTime()
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieucarbou committed Feb 17, 2024
1 parent ce302a8 commit 27e54d9
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 19 deletions.
2 changes: 1 addition & 1 deletion examples/EnergyResetAsync/EnergyResetAsync.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion examples/JSYReadAsync/JSYReadAsync.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
41 changes: 25 additions & 16 deletions src/MycilaJSY.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -327,21 +349,8 @@ void Mycila::JSY::_jsyTask(void* params) {
// Serial.println(xPortGetCoreID());
JSY* jsy = reinterpret_cast<JSY*>(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);
}
10 changes: 9 additions & 1 deletion src/MycilaJSY.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand Down

0 comments on commit 27e54d9

Please sign in to comment.