/*
 * @Author: mypx
 * @Date: 2025-09-05 15:17:15
 * @LastEditTime: 2025-09-05 15:51:49
 * @LastEditors: mypx mypx_coder@163.com
 * @Description: Slope calculation module header file
 */
#ifndef __ETK_SLOPE_H__
#define __ETK_SLOPE_H__

#include "etk_ringbuffer.h"
#include <math.h>
#include <stddef.h>

#ifdef __cplusplus
extern "C"
{
#endif

#ifndef ETK_SLOPE_EPSILON
#define ETK_SLOPE_EPSILON 1e-12 // Numerical calculation precision threshold
#endif

typedef struct
{
    etk_ringbuffer_t rb;
} etk_slope_t;

/**
 * @brief Initialize slope calculation module
 * @param slope Slope calculation instance
 * @param buffer Buffer pointer
 * @param buffer_size Buffer size
 * @param type Data type
 */
void etk_slope_init(etk_slope_t *slope, void *buffer, size_t buffer_size, EtkRingBufDataType type);

/**
 * @brief Update data and calculate current slope
 * @param slope Slope calculation instance
 * @param new_value New data value
 * @return double Calculated slope value, returns 0.0 when insufficient data
 */
double etk_slope_update_slope(etk_slope_t *slope, double new_value);

/**
 * @brief Get current slope from buffer data (without adding new data)
 * @param slope Slope calculation instance
 * @return double Calculated slope value, returns 0.0 when insufficient data
 */
double etk_slope_get_current(etk_slope_t *slope);

/**
 * @brief Reset slope calculator, clear all historical data
 * @param slope Slope calculation instance
 */
void etk_slope_reset(etk_slope_t *slope);

#ifdef __cplusplus
}
#endif

#endif // __ETK_SLOPE_H__
