currently the whole SmartShunt data structure is copied to the BatteryStats instance in every loop, even though the data cannot possibly have changed. this is quite an expensive task to do in every loop. this change tracks the last update timestamp and only does the copy operation if an actual updated data structure was received from the smart shunt.
37 lines
1008 B
C++
37 lines
1008 B
C++
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
#include "VictronSmartShunt.h"
|
|
#include "Configuration.h"
|
|
#include "PinMapping.h"
|
|
#include "MessageOutput.h"
|
|
|
|
|
|
bool VictronSmartShunt::init(bool verboseLogging)
|
|
{
|
|
MessageOutput.println("[VictronSmartShunt] Initialize interface...");
|
|
|
|
const PinMapping_t& pin = PinMapping.get();
|
|
MessageOutput.printf("[VictronSmartShunt] Interface rx = %d, tx = %d\r\n",
|
|
pin.battery_rx, pin.battery_tx);
|
|
|
|
if (pin.battery_rx < 0) {
|
|
MessageOutput.println("[VictronSmartShunt] Invalid pin config");
|
|
return false;
|
|
}
|
|
|
|
auto tx = static_cast<gpio_num_t>(pin.battery_tx);
|
|
auto rx = static_cast<gpio_num_t>(pin.battery_rx);
|
|
|
|
VeDirectShunt.init(rx, tx, &MessageOutput, verboseLogging);
|
|
return true;
|
|
}
|
|
|
|
void VictronSmartShunt::loop()
|
|
{
|
|
VeDirectShunt.loop();
|
|
|
|
if (VeDirectShunt.getLastUpdate() <= _lastUpdate) { return; }
|
|
|
|
_stats->updateFrom(VeDirectShunt.veFrame);
|
|
_lastUpdate = VeDirectShunt.getLastUpdate();
|
|
}
|