/*
 * mppt.h
 *
 *  Created on: Jun 11, 2025
 *      Author: janik
 */

#ifndef INC_MPPT_H_
#define INC_MPPT_H_

#include <stdint.h>

typedef struct {
    float last_vin;
    float last_iin;
    float iref;        // Output current limit setpoint (mA)
    float step;        // Adjustment step size (mA)
    float iref_min;    // Minimum allowed current (mA)
    float iref_max;    // Maximum allowed current (mA)
    float dv_min;      // Minimum dV threshold (mV)
    float deadband;    // Relative dP deadband (e.g. 0.005 = 0.5%)
} MPPTController;

/**
 * @brief Initialize MPPT state
 * @param mppt Pointer to MPPT state structure
 * @param initial_iref Initial current reference
 * @param step Step size for Iref adjustments
 * @param iref_min Minimum allowed current
 * @param iref_max Maximum allowed current
 * @param dv_min Minimum dv threshold
 */
void MPPT_IncCond_Init(MPPTController *mppt,
                       float initial_iref,
                       float step,
                       float iref_min,
                       float iref_max,
					   float dv_min);

/**
 * @brief Update MPPT controller and return new current target
 * @param vin Input voltage (filtered)
 * @param iin Input current (filtered)
 * @return New current reference (in float, amps)
 */
float MPPT_IncCond_Update(MPPTController *mppt, float vin, float iin);


#endif /* INC_MPPT_H_ */
