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

52 lines
1.7 KiB
C++
Raw Permalink Normal View History

2024-02-10 09:20:36 +00:00
#pragma once
#include <AnalogCard.hpp>
2024-02-27 07:38:15 +00:00
#include <ExpansionCard.hpp>
2024-02-10 09:20:36 +00:00
#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-27 07:38:15 +00:00
class CurrentTransformerCard : public ExpansionCard
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-27 07:38:15 +00:00
bool begin();
2024-02-10 09:20:36 +00:00
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-03-25 07:16:15 +00:00
uint32_t lastConversionLoopTime;
2024-02-10 09:20:36 +00:00
};