Skip to content

Commit

Permalink
some potential refactorings...
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieucarbou committed Feb 8, 2025
1 parent 4f74d8a commit 3f1df23
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 59 deletions.
40 changes: 24 additions & 16 deletions src/MycilaJSY.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -644,11 +644,7 @@ void Mycila::JSY::begin(HardwareSerial& serial,
const int8_t txPin,
const BaudRate baudRate,
const uint8_t destinationAddress,
const uint16_t model,
const bool async,
const uint8_t core,
const uint32_t stackSize,
const uint32_t pause) {
const uint16_t model) {
if (_enabled)
return;

Expand Down Expand Up @@ -724,8 +720,6 @@ void Mycila::JSY::begin(HardwareSerial& serial,

_destinationAddress = destinationAddress;
LOGI(TAG, "Detected JSY-MK-%X @ 0x%02X with speed %" PRIu32 " bauds", _model, _lastAddress, _baudRate);

assert(!async || xTaskCreateUniversal(_jsyTask, "jsyTask", stackSize, this, MYCILA_JSY_ASYNC_PRIORITY, &_taskHandle, core) == pdPASS);
}

void Mycila::JSY::end() {
Expand All @@ -739,7 +733,8 @@ void Mycila::JSY::end() {
_baudRate = BaudRate::UNKNOWN;
_lastAddress = MYCILA_JSY_ADDRESS_UNKNOWN;
_model = MYCILA_JSY_MK_UNKNOWN;
data.clear();
_time = 0;
_connected = false;
_serial->end();
}
}
Expand All @@ -748,7 +743,7 @@ void Mycila::JSY::end() {
// read
///////////////////////////////////////////////////////////////////////////////

bool Mycila::JSY::_read(const uint8_t address, uint16_t model) {
bool Mycila::JSY::_readMetrics(const uint8_t address, uint16_t model, Data& data) {
if (!_enabled)
return false;

Expand Down Expand Up @@ -822,6 +817,7 @@ bool Mycila::JSY::_read(const uint8_t address, uint16_t model) {
if (result == ReadResult::READ_TIMEOUT) {
// reset live values in case of read timeout
data.clear();
_connected = data.isConnected();
_mutexOp.unlock();
if (_callback) {
_callback(EventType::EVT_READ_TIMEOUT);
Expand All @@ -832,6 +828,7 @@ bool Mycila::JSY::_read(const uint8_t address, uint16_t model) {
if (result == ReadResult::READ_ERROR_COUNT || result == ReadResult::READ_ERROR_CRC) {
// reset live values in case of read failure
data.clear();
_connected = data.isConnected();
_mutexOp.unlock();
if (_callback) {
_callback(EventType::EVT_READ_ERROR);
Expand Down Expand Up @@ -1122,15 +1119,14 @@ bool Mycila::JSY::_read(const uint8_t address, uint16_t model) {
break;
}

bool changed = data != parsed;

if (changed)
data = parsed;

_connected = parsed.isConnected();
_time = millis();

_mutexOp.unlock();

std::unique_lock lock(data._mutexData);
bool changed = data != parsed;
if (changed)
data = parsed;
if (_callback) {
_callback(EventType::EVT_READ);
if (changed) {
Expand All @@ -1141,6 +1137,16 @@ bool Mycila::JSY::_read(const uint8_t address, uint16_t model) {
return true;
}

///////////////////////////////////////////////////////////////////////////////
// readMetricsAsync
///////////////////////////////////////////////////////////////////////////////

bool Mycila::JSY::_readMetricsAsync(uint8_t address, uint16_t model, Data& data, uint8_t core, uint32_t stackSize, uint32_t pause) {
if (!_enabled)
return false;
return xTaskCreateUniversal(_jsyTask, "jsyTask", stackSize, this, MYCILA_JSY_ASYNC_PRIORITY, &_taskHandle, core) == pdPASS;
}

///////////////////////////////////////////////////////////////////////////////
// readModel
///////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -1479,7 +1485,9 @@ void Mycila::JSY::toJson(const JsonObject& root) const {
root["enabled"] = _enabled;
root["time"] = _time;
root["speed"] = _baudRate;
data.toJson(root);
root["address"] = _lastAddress;
root["model"] = _model;
root["model_name"] = Mycila::JSY::getModelName(_model);
}
#endif

Expand Down
97 changes: 54 additions & 43 deletions src/MycilaJSY.h
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,17 @@ namespace Mycila {

class Data {
public:
/**
* @brief lock the data for reading.
* This must be called before reading the data.
*/
void lock() const { _mutexData.lock_shared(); }

/**
* @brief unlock the data after reading.
*/
void unlock() const { _mutexData.unlock_shared(); }

uint8_t address = MYCILA_JSY_ADDRESS_UNKNOWN; // device address
uint16_t model = MYCILA_JSY_MK_UNKNOWN; // device model

Expand Down Expand Up @@ -317,6 +328,8 @@ namespace Mycila {
// clear all values
void clear();

bool isConnected() const { return aggregate.frequency > 0; }

// compare two data
bool operator==(const Data& other) const;
// compare two data
Expand All @@ -338,27 +351,6 @@ namespace Mycila {

~JSY() { end(); }

/**
* @brief Initialize the JSY with the given RX and TX pins.
* @param serial The serial port to use
* @param rxPin RX board pin connected to the TX of the JSY
* @param txPin TX board pin connected to the RX of the JSY
* @param async If true, the JSY will be read in a separate task (default: false)
* @param core The core to use for the async task (default: MYCILA_JSY_ASYNC_CORE)
* @param stackSize The stack size of the async task (default: MYCILA_JSY_ASYNC_STACK_SIZE)
* @param pause Time in milliseconds to wait between each read in async mode (default: MYCILA_JSY_ASYNC_READ_PAUSE_MS)
* @note The baud rate and model is automatically detected.
*/
void begin(HardwareSerial& serial, // NOLINT
int8_t rxPin,
int8_t txPin,
bool async,
uint8_t core = MYCILA_JSY_ASYNC_CORE,
uint32_t stackSize = MYCILA_JSY_ASYNC_STACK_SIZE,
uint32_t pause = MYCILA_JSY_ASYNC_READ_PAUSE_MS) {
begin(serial, rxPin, txPin, BaudRate::UNKNOWN, MYCILA_JSY_ADDRESS_BROADCAST, MYCILA_JSY_MK_UNKNOWN, async, core, stackSize, pause);
}

/**
* @brief Initialize the JSY with the given RX and TX pins.
* @param serial The serial port to use
Expand All @@ -367,21 +359,14 @@ namespace Mycila {
* @param baudRate The baud rate of the JSY. If set to BaudRate::UNKNOWN, the baud rate is automatically detected
* @param destinationAddress The address of the device to communicate with (1-255) or MYCILA_JSY_ADDRESS_BROADCAST for all devices
* @param model The model of the JSY. If set to MYCILA_JSY_MK_UNKNOWN, the model is automatically detected
* @param async If true, the JSY will be read in a separate task (default: false)
* @param core The core to use for the async task (default: MYCILA_JSY_ASYNC_CORE)
* @param stackSize The stack size of the async task (default: MYCILA_JSY_ASYNC_STACK_SIZE)
* @param pause Time in milliseconds to wait between each read in async mode (default: MYCILA_JSY_ASYNC_READ_PAUSE_MS)
*
*/
void begin(HardwareSerial& serial, // NOLINT
int8_t rxPin,
int8_t txPin,
BaudRate baudRate = BaudRate::UNKNOWN,
uint8_t destinationAddress = MYCILA_JSY_ADDRESS_BROADCAST,
uint16_t model = MYCILA_JSY_MK_UNKNOWN,
bool async = false,
uint8_t core = MYCILA_JSY_ASYNC_CORE,
uint32_t stackSize = MYCILA_JSY_ASYNC_STACK_SIZE,
uint32_t pause = MYCILA_JSY_ASYNC_READ_PAUSE_MS);
uint16_t model = MYCILA_JSY_MK_UNKNOWN);

/**
* @brief Ends the JSY communication.
Expand Down Expand Up @@ -470,19 +455,49 @@ namespace Mycila {
bool setMode(uint8_t address, Mode mode) { return _setMode(address, readModel(address), mode); }

/**
* @brief Read the JSY values.
* @brief Read the JSY metrics.
* @param data The data to fill with the metrics
* @return true if the read was successful
* @note This function is blocking until the data is read or the timeout is reached.
*/
bool read() { return _read(_destinationAddress, _model); }
bool readMetrics(Data& data) { return _readMetrics(_destinationAddress, _model, data); }

/**
* @brief Read the JSY values.
* @brief Read the JSY metrics.
* @param address The address of the device to read (1-255) or MYCILA_JSY_ADDRESS_BROADCAST for all devices
* @param data The data to fill with the metrics
* @return true if the read was successful
* @note This function is blocking until the data is read or the timeout is reached.
*/
bool read(uint8_t address) { return _read(address, readModel(address)); }
bool readMetrics(uint8_t address, Data& data) { return _readMetrics(address, readModel(address), data); }

/**
* @brief Read the JSY metrics asynchronously.
* @param data The data to fill with the metrics
* @param core The core to use for the async task (default: MYCILA_JSY_ASYNC_CORE)
* @param stackSize The stack size of the async task (default: MYCILA_JSY_ASYNC_STACK_SIZE)
* @param pause Time in milliseconds to wait between each read in async mode (default: MYCILA_JSY_ASYNC_READ_PAUSE_MS)
* @return true if the task initialization was successful
*/
bool readMetricsAsync(Data& data,
uint8_t core = MYCILA_JSY_ASYNC_CORE,
uint32_t stackSize = MYCILA_JSY_ASYNC_STACK_SIZE,
uint32_t pause = MYCILA_JSY_ASYNC_READ_PAUSE_MS) { return _readMetricsAsync(_destinationAddress, _model, data, core, stackSize, pause); }

/**
* @brief Read the JSY metrics asynchronously.
* @param address The address of the device to read (1-255) or MYCILA_JSY_ADDRESS_BROADCAST for all devices
* @param data The data to fill with the metrics
* @param core The core to use for the async task (default: MYCILA_JSY_ASYNC_CORE)
* @param stackSize The stack size of the async task (default: MYCILA_JSY_ASYNC_STACK_SIZE)
* @param pause Time in milliseconds to wait between each read in async mode (default: MYCILA_JSY_ASYNC_READ_PAUSE_MS)
* @return true if the task initialization was successful
*/
bool readMetricsAsync(uint8_t address,
Data& data,
uint8_t core = MYCILA_JSY_ASYNC_CORE,
uint32_t stackSize = MYCILA_JSY_ASYNC_STACK_SIZE,
uint32_t pause = MYCILA_JSY_ASYNC_READ_PAUSE_MS) { return _readMetricsAsync(address, readModel(address), data, core, stackSize, pause); }

/**
* @brief Reset the energy counters of the JSY.
Expand Down Expand Up @@ -563,15 +578,9 @@ namespace Mycila {
uint32_t getTime() const { return _time; }

// check if the device is connected to the grid, meaning if last read was successful
bool isConnected() const { return data.aggregate.frequency > 0; }

void setCallback(Callback callback) { _callback = callback; }
bool isConnected() const { return _connected; }

public:
/**
* @brief Access the runtime JSY data.
*/
Data data;
void onEvent(Callback callback) { _callback = callback; }

private:
Callback _callback = nullptr;
Expand All @@ -581,6 +590,7 @@ namespace Mycila {
std::timed_mutex _mutexOp;
TaskHandle_t _taskHandle;
uint32_t _time = 0;
bool _connected = false;
uint32_t _pause = MYCILA_JSY_ASYNC_READ_PAUSE_MS;
uint8_t _destinationAddress = MYCILA_JSY_ADDRESS_BROADCAST;
uint8_t _lastAddress = MYCILA_JSY_ADDRESS_UNKNOWN;
Expand All @@ -602,7 +612,8 @@ namespace Mycila {
};

bool _set(uint8_t address, uint8_t newAddress, BaudRate newBaudRate);
bool _read(uint8_t address, uint16_t model);
bool _readMetrics(uint8_t address, uint16_t model, Data& data);
bool _readMetricsAsync(uint8_t address, uint16_t model, Data& data, uint8_t core, uint32_t stackSize, uint32_t pause);
Mode _readMode(uint8_t address, uint16_t model);
bool _setMode(uint8_t address, uint16_t model, Mode mode);

Expand Down

0 comments on commit 3f1df23

Please sign in to comment.