/**
 * @brief Class for measuring current and power consumption using a current transformer and an ADC.
 * 
 */
#pragma once

#include <ESPMegaPRO.h>

class ESPMega_CT {
    public:
        /**
         * @brief Construct a new ESPMega_CT object
         * 
         * @param analog_pin The analog pin to which the current transformer is connected.
         * @param adc_to_watts A function pointer to a function that converts ADC values to watts.
         * @param fram_address The address in FRAM where the energy consumption data is stored.
         */
        ESPMega_CT(uint8_t analog_pin,float(*adc_to_watts)(uint16_t adc_value), uint32_t fram_address);

        /**
         * @brief Initializes the object.
         * 
         */
        void begin();

        /**
         * @brief Updates the power and energy consumption values.
         * 
         */
        void loop();

        /**
         * @brief Returns the current power consumption in watts.
         * 
         * @return float The current power consumption in watts.
         */
        float get_power();

        /**
         * @brief Returns the total energy consumption in watt-hours.
         * 
         * @return long double The total energy consumption in watt-hours.
         */
        long double get_energy();

        /**
         * @brief Resets the energy consumption value to zero.
         * 
         */
        void reset_energy();

    private:
        uint8_t analog_pin;
        uint32_t fram_address;
        unsigned long last_conversion_timestamp;
        float power;
        long double energy;
        float (*adc_to_watts)(uint16_t adc_value);
        float adc_to_watts_builtin(uint16_t adc_value);
};