ESPMegaPRO-v3-SDK/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/CurrentTransformerCard.hpp

50 lines
1.6 KiB
C++
Raw Normal View History

2024-02-10 09:20:36 +00:00
#pragma once
#include <AnalogCard.hpp>
#include <FRAM.h>
#include <map>
2024-02-12 09:20:16 +00:00
#define CARD_TYPE_CT 4
2024-02-12 09:15:31 +00:00
2024-02-10 09:20:36 +00:00
/**
* @brief The CurrentTransformer class is a class for reading the current and energy from a current transformer that's connected to the AnalogCard.
* Also supports storing energy to FRAM.
*/
2024-02-12 09:15:31 +00:00
class CurrentTransformerCard
2024-02-10 09:20:36 +00:00
{
public:
2024-02-12 09:15:31 +00:00
CurrentTransformerCard(AnalogCard* analogCard, uint8_t pin, float *voltage, std::function<float(uint16_t)> adcToCurrent, uint32_t conversionInterval);
2024-02-10 09:30:23 +00:00
void bindFRAM(FRAM *fram, uint32_t framAddress); // Takes 16 bytes of FRAM (long double energy)
2024-02-10 09:20:36 +00:00
void begin();
void loop();
void beginConversion();
void setEnergy(float energy);
void resetEnergy();
float getCurrent();
2024-02-12 09:15:31 +00:00
double getEnergy();
2024-02-10 09:20:36 +00:00
float getPower();
void saveEnergy();
void loadEnergy();
void setEnergyAutoSave(bool autoSave);
float getVoltage();
2024-02-10 09:36:59 +00:00
uint8_t registerCallback(std::function<void(float, double)> callback);
2024-02-10 09:20:36 +00:00
void unregisterCallback(uint8_t handler);
2024-02-12 09:20:16 +00:00
uint8_t getType();
2024-02-10 09:20:36 +00:00
private:
AnalogCard* analogCard;
uint8_t pin;
2024-02-10 09:30:23 +00:00
uint32_t framAddress;
2024-02-10 09:20:36 +00:00
FRAM *fram;
uint32_t conversionInterval;
bool autoSave;
float calibration;
2024-02-10 09:36:59 +00:00
double energy;
2024-02-10 09:20:36 +00:00
float current;
float *voltage;
uint32_t lastConversionTime;
std::function<float(uint16_t)> adcToCurrent; // std::function that convert adc value to current in amps
uint8_t handler_count = 0;
2024-02-10 09:36:59 +00:00
std::map<uint8_t,std::function<void(float, double)>> callbacks;
2024-02-10 09:20:36 +00:00
};