-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCurrentTransformerCard.cpp
106 lines (90 loc) · 2.72 KB
/
CurrentTransformerCard.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <CurrentTransformerCard.hpp>
CurrentTransformerCard::CurrentTransformerCard(AnalogCard* analogCard, uint8_t pin, float *voltage, std::function<float(uint16_t)> adcToCurrent, uint32_t conversionInterval)
{
this->analogCard = analogCard;
this->pin = pin;
this->voltage = voltage;
this->adcToCurrent = adcToCurrent;
this->conversionInterval = conversionInterval;
}
void CurrentTransformerCard::bindFRAM(FRAM *fram, uint32_t framAddress)
{
this->fram = fram;
this->framAddress = framAddress;
}
bool CurrentTransformerCard::begin()
{
// Is analogCard a nullptr?
if (this->analogCard == nullptr) {
return false;
}
this->beginConversion();
return true;
}
void CurrentTransformerCard::loop()
{
if (this->lastConversionTime == 0) {
this->lastConversionTime = millis();
}
if (millis() - lastConversionLoopTime > this->conversionInterval) {
this->beginConversion();
lastConversionLoopTime = millis();
}
}
void CurrentTransformerCard::beginConversion()
{
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();
}
void CurrentTransformerCard::setEnergy(float energy)
{
this->energy = energy;
if (this->autoSave) {
this->saveEnergy();
}
for (auto const& callback : this->callbacks) {
callback.second(this->current, this->energy);
}
}
void CurrentTransformerCard::resetEnergy()
{
this->setEnergy(0);
}
float CurrentTransformerCard::getCurrent()
{
return this->current;
}
double CurrentTransformerCard::getEnergy()
{
return this->energy;
}
uint8_t CurrentTransformerCard::registerCallback(std::function<void(float, double)> callback) {
this->callbacks[this->handler_count] = callback;
return this->handler_count++;
}
void CurrentTransformerCard::unregisterCallback(uint8_t handler) {
this->callbacks.erase(handler);
}
void CurrentTransformerCard::saveEnergy(){
this->fram->write(this->framAddress, (uint8_t*)&this->energy, sizeof(this->energy));
}
void CurrentTransformerCard::loadEnergy(){
this->fram->read(this->framAddress, (uint8_t*)&this->energy, sizeof(this->energy));
if (this->energy < 0 || isnan(this->energy)) {
this->energy = 0;
}
}
void CurrentTransformerCard::setEnergyAutoSave(bool autoSave){
this->autoSave = autoSave;
}
float CurrentTransformerCard::getVoltage(){
return *this->voltage;
}
float CurrentTransformerCard::getPower(){
return this->current * *this->voltage;
}
uint8_t CurrentTransformerCard::getType() {
return CARD_TYPE_CT;
}