2024-02-12 09:15:31 +00:00
|
|
|
#include <CurrentTransformerCard.hpp>
|
2024-02-10 09:20:36 +00:00
|
|
|
|
2024-02-12 09:15:31 +00:00
|
|
|
CurrentTransformerCard::CurrentTransformerCard(AnalogCard* analogCard, uint8_t pin, float *voltage, std::function<float(uint16_t)> adcToCurrent, uint32_t conversionInterval)
|
2024-02-10 09:20:36 +00:00
|
|
|
{
|
|
|
|
this->analogCard = analogCard;
|
|
|
|
this->pin = pin;
|
|
|
|
this->voltage = voltage;
|
|
|
|
this->adcToCurrent = adcToCurrent;
|
|
|
|
}
|
|
|
|
|
2024-02-12 09:15:31 +00:00
|
|
|
void CurrentTransformerCard::bindFRAM(FRAM *fram, uint32_t framAddress)
|
2024-02-10 09:20:36 +00:00
|
|
|
{
|
|
|
|
this->fram = fram;
|
|
|
|
this->framAddress = framAddress;
|
|
|
|
}
|
|
|
|
|
2024-02-27 07:38:15 +00:00
|
|
|
bool CurrentTransformerCard::begin()
|
2024-02-10 09:20:36 +00:00
|
|
|
{
|
2024-02-27 07:38:15 +00:00
|
|
|
// Is analogCard a nullptr?
|
|
|
|
if (this->analogCard == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
2024-02-10 09:20:36 +00:00
|
|
|
this->beginConversion();
|
2024-02-27 07:38:15 +00:00
|
|
|
return true;
|
2024-02-10 09:20:36 +00:00
|
|
|
}
|
|
|
|
|
2024-02-12 09:15:31 +00:00
|
|
|
void CurrentTransformerCard::loop()
|
2024-02-10 09:20:36 +00:00
|
|
|
{
|
|
|
|
if (this->lastConversionTime == 0) {
|
|
|
|
this->lastConversionTime = millis();
|
|
|
|
}
|
|
|
|
static uint32_t lastConversionLoopTime = 0;
|
|
|
|
if (millis() - lastConversionLoopTime > this->conversionInterval) {
|
|
|
|
this->beginConversion();
|
|
|
|
lastConversionLoopTime = millis();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-12 09:15:31 +00:00
|
|
|
void CurrentTransformerCard::beginConversion()
|
2024-02-10 09:20:36 +00:00
|
|
|
{
|
|
|
|
uint16_t adcValue = this->analogCard->analogRead(this->pin);
|
|
|
|
this->current = this->adcToCurrent(adcValue);
|
|
|
|
this->setEnergy(this->energy + this->current * *this->voltage * (millis() - this->lastConversionTime) / 3600000); // in Wh
|
|
|
|
this->lastConversionTime = millis();
|
|
|
|
}
|
|
|
|
|
2024-02-12 09:15:31 +00:00
|
|
|
void CurrentTransformerCard::setEnergy(float energy)
|
2024-02-10 09:20:36 +00:00
|
|
|
{
|
|
|
|
this->energy = energy;
|
|
|
|
if (this->autoSave) {
|
|
|
|
this->saveEnergy();
|
|
|
|
}
|
2024-02-12 09:15:31 +00:00
|
|
|
for (auto const& callback : this->callbacks) {
|
|
|
|
callback.second(this->current, this->energy);
|
|
|
|
}
|
2024-02-10 09:20:36 +00:00
|
|
|
}
|
|
|
|
|
2024-02-12 09:15:31 +00:00
|
|
|
void CurrentTransformerCard::resetEnergy()
|
2024-02-10 09:20:36 +00:00
|
|
|
{
|
|
|
|
this->setEnergy(0);
|
|
|
|
}
|
|
|
|
|
2024-02-12 09:15:31 +00:00
|
|
|
float CurrentTransformerCard::getCurrent()
|
2024-02-10 09:20:36 +00:00
|
|
|
{
|
|
|
|
return this->current;
|
|
|
|
}
|
|
|
|
|
2024-02-12 09:15:31 +00:00
|
|
|
double CurrentTransformerCard::getEnergy()
|
2024-02-10 09:20:36 +00:00
|
|
|
{
|
|
|
|
return this->energy;
|
|
|
|
}
|
|
|
|
|
2024-02-12 09:15:31 +00:00
|
|
|
uint8_t CurrentTransformerCard::registerCallback(std::function<void(float, double)> callback) {
|
2024-02-10 09:20:36 +00:00
|
|
|
this->callbacks[this->handler_count] = callback;
|
|
|
|
return this->handler_count++;
|
|
|
|
}
|
2024-02-12 09:15:31 +00:00
|
|
|
void CurrentTransformerCard::unregisterCallback(uint8_t handler) {
|
2024-02-10 09:20:36 +00:00
|
|
|
this->callbacks.erase(handler);
|
|
|
|
}
|
|
|
|
|
2024-02-12 09:15:31 +00:00
|
|
|
void CurrentTransformerCard::saveEnergy(){
|
2024-02-10 09:36:59 +00:00
|
|
|
this->fram->write(this->framAddress, (uint8_t*)&this->energy, sizeof(this->energy));
|
2024-02-10 09:20:36 +00:00
|
|
|
}
|
2024-02-12 09:15:31 +00:00
|
|
|
void CurrentTransformerCard::loadEnergy(){
|
2024-02-10 09:36:59 +00:00
|
|
|
this->fram->read(this->framAddress, (uint8_t*)&this->energy, sizeof(this->energy));
|
2024-02-10 09:20:36 +00:00
|
|
|
}
|
|
|
|
|
2024-02-12 09:15:31 +00:00
|
|
|
void CurrentTransformerCard::setEnergyAutoSave(bool autoSave){
|
2024-02-10 09:20:36 +00:00
|
|
|
this->autoSave = autoSave;
|
|
|
|
}
|
|
|
|
|
2024-02-12 09:15:31 +00:00
|
|
|
float CurrentTransformerCard::getVoltage(){
|
2024-02-10 09:20:36 +00:00
|
|
|
return *this->voltage;
|
|
|
|
}
|
|
|
|
|
2024-02-12 09:15:31 +00:00
|
|
|
float CurrentTransformerCard::getPower(){
|
2024-02-10 09:20:36 +00:00
|
|
|
return this->current * *this->voltage;
|
2024-02-12 09:20:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t CurrentTransformerCard::getType() {
|
|
|
|
return CARD_TYPE_CT;
|
2024-02-10 09:20:36 +00:00
|
|
|
}
|