/*
 * @Author: mypx
 * @Date: 2025-06-23 13:05:04
 * @LastEditTime: 2025-10-16 14:01:47
 * @LastEditors: mypx mypx_coder@163.com
 * @Description: 
 */
/*** 
 * @Author: mypx
 * @Email: mypx_coder@163.com
 * @Date: 2025-06-23 13:05:04
 * @LastEditors: mypx mypx_coder@163.com
 * @Description: 
 */
#ifndef __TEC_CONTROL_H__
#define __TEC_CONTROL_H__
#include "et_ema_filter.h"
#include "etk_pid.h"
#include <rtthread.h>
#include <stdbool.h>

#define EMA_FILTER_ALPHA          0.9
#define EXCEPTION_TEMPERATURE_DEF 45.0f // exception temperature value, control will not be performed if exceeded
#define TEC_CONTROL_PERIOD        1000  // temperature calculation period, unit ms
#define ADC_SAMPLE_PERIOD         500   // ADC sampling period, unit ms

// event flags for temperature control
#define TEC_CONTROL_START_EVENT (1 << 0) // start temperature control event
#define TEC_CONTROL_STOP_EVENT  (1 << 1) // stop temperature control event
#define TEC_TEMP_UPDATE_EVENT   (1 << 2) // target temperature update event

typedef struct
{
    double acq_voltage;   // acquisition and calculation voltage value
    float  target_temper; // target temperature value
    float exception_temper; // exception temperature value, control will not be performed if exceeded
    float cur_temper;
    bool  is_control;       // whether temperature control is active
    bool  is_exception;     // whether temperature is in exception state

    et_integral_separation_pid_t pid;
    rt_event_t                   control_event; // event object for temperature control
    et_ema_filter_t              ema_filter;
} tec_control_t;

int tec_control_init(tec_control_t *tec);

void tec_control_reset(void);

int tec_control_resume(void);

int tec_control_sercie_start(tec_control_t *tec);

int tec_control_pause(void);

float tec_get_target_voltage(float R);

#endif // __TEMPERATURE_H__