#ifndef PERFORMANCE_MONITOR_H
#define PERFORMANCE_MONITOR_H

#include "main.h"
#include <stdint.h>

// 性能监控配置
#define PERF_MON_MAX_TASKS 8
#define PERF_MON_SAMPLE_PERIOD_MS 100

// 任务ID定义
typedef enum {
    PERF_TASK_ADC_PROCESSING = 0,
    PERF_TASK_CORRECTION,
    PERF_TASK_DATA_PACKET,
    PERF_TASK_RS485_TX,
    PERF_TASK_FATFS_WRITE,
    PERF_TASK_USB_PROCESS,
    PERF_TASK_SYSTEM_MONITOR,
    PERF_TASK_IDLE
} PerfTaskID_t;

// 任务性能统计
typedef struct {
    uint32_t total_time_us;     // 总执行时间(微秒)
    uint32_t max_time_us;       // 最大执行时间
    uint32_t min_time_us;       // 最小执行时间
    uint32_t call_count;        // 调用次数
    uint32_t avg_time_us;       // 平均执行时间
    float cpu_usage_percent;    // CPU使用率百分比
} TaskPerfStats_t;

// 系统性能统计
typedef struct {
    TaskPerfStats_t tasks[PERF_MON_MAX_TASKS];
    uint32_t total_cpu_usage_percent;
    uint32_t free_heap_size;
    uint32_t min_free_heap_size;
    uint32_t stack_usage_percent;
    uint32_t sample_period_ms;
    uint32_t last_update_tick;
} SystemPerfStats_t;

// 函数声明
void PerformanceMonitor_Init(void);
void PerformanceMonitor_TaskStart(PerfTaskID_t task_id);
void PerformanceMonitor_TaskEnd(PerfTaskID_t task_id);
void PerformanceMonitor_Update(void);
void PerformanceMonitor_GetStats(SystemPerfStats_t *stats);
void PerformanceMonitor_ResetStats(void);
uint32_t PerformanceMonitor_GetFreeHeapSize(void);
uint32_t PerformanceMonitor_GetStackUsage(void);

#endif // PERFORMANCE_MONITOR_H