#ifndef CORRECTION_H
#define CORRECTION_H

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

// 使用ARM DSP库进行矩阵运算
#ifdef ARM_MATH_CM4
#include "arm_math.h"
#define USE_ARM_DSP 1
#else
#define USE_ARM_DSP 0
#endif

// 校正参数结构体
typedef struct {
    // 偏移参数
    float offset_x, offset_y, offset_z;
    
    // 3x3校正矩阵 (行优先存储)
    float correction_matrix[9];
    
    // ARM DSP矩阵实例
    #if USE_ARM_DSP
    arm_matrix_instance_f32 matrix_inst;
    #endif
    
    // 校正参数有效标志
    uint8_t params_valid;
} CorrectionParams_t;

// 校正结果结构体
typedef struct {
    float corrected_x;
    float corrected_y; 
    float corrected_z;
    uint32_t timestamp;
    uint8_t correction_applied;
} CorrectionResult_t;

// 函数声明
void Init_CorrectionParams(CorrectionParams_t *params);
void Set_CorrectionMatrix(CorrectionParams_t *params, const float *matrix);
void Set_CorrectionOffsets(CorrectionParams_t *params, float offset_x, float offset_y, float offset_z);
HAL_StatusTypeDef Apply_Correction(int32_t raw_x, int32_t raw_y, int32_t raw_z, 
                                   CorrectionResult_t *result, 
                                   const CorrectionParams_t *params);
void Load_CorrectionParams_FromFlash(CorrectionParams_t *params);
HAL_StatusTypeDef Save_CorrectionParams_ToFlash(const CorrectionParams_t *params);

#endif // CORRECTION_H