/*
 * storage.h
 *
 *  Created on: Aug 27, 2024
 *      Author: Thinkpad
 */

#ifndef INCLUDE_STORAGE_H_
#define INCLUDE_STORAGE_H_

#include <stdint.h>
#include <string.h>
#include "cmcng.h"

#define  Addr_Dev_Flag_Offset               0x00

#define  Addr_Sens_Amount_Offset            0x0E
#define  Addr_Smart_Mode_Offset             0x0F
#define  Addr_Loop_Cng_Offset               0x10

#define  Addr_Loop_PlanB_Cng_Offset         0x30
#define  Addr_Flag_Sample_In                0x30
#define  Addr_Sample_Max_Amplitude          0x31
#define  Addr_Flag_Balance_Ori_In           0x33
#define  Addr_Balance_Ori_In_Max_Cnt        0x34
#define  Addr_Flag_Release_Ori_PlanB        0x36
#define  Addr_Release_Ori_PlanB_TimeOut     0x37
#define  Addr_Release_Ori_PlanB_Weight      0x38
#define  Addr_Release_Ori_Amplitude         0x39
#define  Addr_Flag_Release_Rate             0x3B
#define  Addr_Release_Rate_First            0x3C
#define  Addr_Release_Rate_Second           0x3D
#define  Addr_Release_Rate_Weight           0x3E
#define  Addr_Release_Rate_Mode             0x3F

#define  Addr_Loop_Sens_List_Offset         0x40

#define Addr_Sens_Array_In                  0x40
#define Addr_Sens_Array_Out                 0x50


#define            MAX_LOOP_SENS_AMOUNT     4
extern const uint16_t SensTable[4];
extern const uint16_t SensTable_1[4];

#pragma pack(1)

typedef struct _Loop_Sample_CNG_
{
    uint8_t flag;
    uint16_t max_amplitude;         // 最大幅值
}Loop_Sample_Cng;

typedef struct _Loop_Balance_Ori_CNG_
{
    uint8_t flag;
    uint16_t max_cnt ;            // 最大补偿次数
}Loop_Balance_Ori_Cng;            // 漂移补偿

typedef struct _Loop_Release_Ori_PlanB_
{
    uint8_t flag_weight;
    uint16_t max_amplitude; //最大幅值
    uint8_t timeout;        // 单位：分钟
}Loop_Release_Ori_PlanB;

typedef struct _Loop_Release_Change_Rate_
{
    uint8_t flag_weight;
    uint8_t rate_first;
    uint8_t rate_second;
    uint8_t mode;
}Loop_Release_Change_Rate;


typedef struct _Loop_Balance_PlanB_
{
    Loop_Sample_Cng sample_cng;
    Loop_Balance_Ori_Cng balance_ori_cng;
    Loop_Release_Ori_PlanB release_ori_planB;
    Loop_Release_Change_Rate release_change_rate;
}Loop_Balance_PlanB;

extern Loop_Balance_PlanB  g_loop_balance_planB;


#define     SENS_Default_Amount         4

typedef enum
{
    Freq_High = 0,
    Freq_Middle_High,
    Freq_Middle_Low,
    Freq_Low
} Freq_Level;

typedef enum
{
    Smart_Mode_Disable = 0,
    Smart_Mode_Enable
} Freq_Smart_Mode;

typedef enum
{
    LNum_1  = 0,
    LNum_2,
    LNum_3,
    LNum_4
} Loop_Num;


typedef struct
{
    uint8_t sensitvity;         // 高四位表示灵敏度的数量，低四位表示当前灵敏度的序号（从0开始）
    uint8_t delay_time;
    uint8_t output_mode;
    uint8_t direction_mode;     // 方向判别模式，0 触发模式， 非0 表示方向判别模式
    uint8_t loopFreq_Level;     // 高低频 0 High, 1 Middle High; 2 Middle Low, 3 Low
    uint8_t loopSafe_Timeout;   // 线圈安全模式， 0表示关闭，非0表示开启，以10秒为单位
    uint8_t exist_mode;         // 0 永久存在，非0，表示有限存在的时间，单位为10秒
    uint8_t rfu;
}Loop_Cng_Unit;
// extern Loop_Cng_Unit       g_loop_cng_unit[LOOP_CAPTURE_MAX];

typedef struct
{
    uint8_t             smart_mode;     // 0 disable
    Loop_Cng_Unit       loop_cng[LOOP_CAPTURE_MAX];
}Loop_Cng_Info;
extern Loop_Cng_Info       g_loop_cng_info;

typedef struct
{
    uint16_t sens_in;
    uint16_t sens_out;
}Loop_Single_Sens;

typedef struct
{
    uint8_t total;
    Loop_Single_Sens sens[MAX_LOOP_SENS_AMOUNT];
}Loop_Sens_List;
extern Loop_Sens_List g_loop_sens_list;



typedef struct _DBN_BLE_STATE_
{
    uint8_t flag;       // if need to report
    uint8_t enable;     // if enable
    uint8_t send_flag;
    uint8_t obj_amount;
    uint8_t cmd;
    uint8_t sens_type;
    uint8_t dat_len ;
    uint8_t dat_offset;
    uint8_t pkg_amount;
    uint8_t pkg_seq;
    uint16_t interval;
    uint32_t counter;
    uint8_t timeout_min;       // minute
    uint32_t timeout_counter;
} DBN_BLE_State;






#pragma pack()

void set_flp_level(uint8_t loop_num, uint8_t freq_level);

void set_factory_param(void);
void storage_dev(void);
void para_store_init(void);
void test_factory(void);


#endif /* INCLUDE_STORAGE_H_ */
