//
// Created by robtor on 10.01.23.
//

#ifndef FLOATPUMP_PRESSURECHANNEL_H
#define FLOATPUMP_PRESSURECHANNEL_H

#include <cstdint>
#include <memory>
#include "stm32f4xx_hal.h"

namespace floatpump::io {

    class PressureChannel {

    public:
        PressureChannel(ADC_HandleTypeDef *adc, GPIO_TypeDef *gpio, uint16_t port, const uint16_t avg_size = 50);;

        void poll();

        [[nodiscard]] uint16_t getRaw() const;

        [[nodiscard]] int8_t getPercent() const;

        [[maybe_unused]] void calibrateManualLow(uint16_t low);

        [[maybe_unused]] void calibrateManualHigh(uint16_t high);

        void calibrateLow();

        void calibrateHigh();

        void LinkCalibConfig(uint16_t *low, uint16_t *high);

    private:
        const uint16_t m_avg_size = 50;
        const uint16_t m_avg_delay = 10;
        const uint16_t m_cooldown = 500;

        uint16_t *m_lowcalib = new uint16_t(0);
        uint16_t *m_highcalib = new uint16_t(65535);

        uint16_t m_raw = 0;
        int8_t m_percent = 0;

        ADC_HandleTypeDef *m_adc;
        GPIO_TypeDef *m_gpio;
        uint16_t m_gpio_port;
    };

} // io

#endif //FLOATPUMP_PRESSURECHANNEL_H
