
#include "SOE.h"
#include "stdio.h"
#include "parameter.h"
#include "FM_RTC.h"


#define eventsOrderRecordStartAddr 4096

typedef struct _soeDataInfo {
    uint16_t mode;	            //故障类型 2byte
    timeInfo time;				//时间 6byte
    float temp;                 //故障发生时刻的值 4byte
} soeDataInfo;
#define soeDataInfoSize sizeof(soeDataInfo)

typedef struct _soeSaveInfo {
    soeDataInfo data[5];

    uint8_t insertPos;          //记录插入数据的位置
    uint8_t outPos;             //记录输出数据的位置
    uint8_t count;              //记录可插入数据插入数据个数
    soeDataInfo *insertData;    //指向能插入数据的位置
    soeDataInfo *outData;       //指向能输出数据的位置
} soeSaveInfo;

typedef struct _soeStorageParameters {
    uint16_t len;               //soe记录次数
    uint16_t pos;               //下次soe记录的位置
} soeStorageParameters;

static soeSaveInfo soeInfo = {0};
static soeStorageParameters soeParameters = {0};



/**
 * @brief  初始化事件顺序记录
 * @param
 */
void eventsOrderRecordStartInit(void)
{
    soeInfo.count = 5;
    soeInfo.insertPos = 0;
    soeInfo.outPos = 0;
    soeInfo.outData = NULL;
    soeInfo.insertData = &soeInfo.data[soeInfo.insertPos];

    read_Flash((uint8_t *)(&soeParameters), sizeof(soeParameters));
}

/**
 * @brief  插入事件顺序记录
 * @param
 */
/*
|       故障类型        |   temp    |
| :-------------------: | :-------: |
| firstStageProtection  | 输出电流  |
| secondStageProtection | 输出电流  |
| thirdStageProtection  | 输出电流  |
|     lowInputLoad      | 输出电压  |
|    overTemperature    | mos管温度 |
|    stopTemperature    | mos管温度 |
|     overchargCurr     | 充电电流  |
*/
void insertEventsOrderRecord(eventsOrderRecordMode mode)
{
    /* 无位置插入时，退出 */
    if (soeInfo.count <= 0 || soeInfo.insertData == NULL) {
        return;
    }

    soeInfo.count--;
    soeInfo.insertData->data.mode = mode;
    getRTC_Time(&soeInfo.insertData->time);

    if (mode == firstStageProtection) {
        soeInfo.insertData->data.temp = getDischargCurrent();
    }

    else if (mode == secondStageProtection) {
        soeInfo.insertData->data.temp = getDischargCurrent();
    }

    else if (mode == thirdStageProtection) {
        soeInfo.insertData->data.temp = getDischargCurrent();
    }

    else if (mode == lowInputLoad) {
        soeInfo.insertData->data.temp = getOutputVoltage();
    }

    else if (mode == overTemperature) {
        soeInfo.insertData->data.temp = getHighSideMosTemperature();
    }

    else if (mode == stopTemperature) {
        soeInfo.insertData->data.temp = getHighSideMosTemperature();
    }

    else if (mode == overchargCurr) {
        soeInfo.insertData->data.temp = getChargCurrent();
    }


    soeInfo.insertPos++;
    if (soeInfo.insertPos >= 5) {
        soeInfo.insertPos = 0;
    }

    if (count <= 0) {
        soeInfo.insertData = NULL;
    } else {
        soeInfo.insertData = &soeInfo.data[soeInfo.insertPos];
    }
        
    soeInfo.outData = &soeInfo.data[soeInfo.outPos];
}


/**
 * @brief  将事件顺序记录在flash中
 * @param
 */
void setEventsOrderRecord(void)
{
    if (soeInfo.outData == NULL || soeInfo.count >= 5) {
        return;
    }
    
    // write_Flash((uint8_t *)(&soeInfo.outData), soeDataInfoSize);

    soeInfo.outPos++;
    if (soeInfo.outPos >= 5) {
        soeInfo.outPos = 0;
    }
    soeInfo.count++;

    if (soeInfo.count >= 5) {
        soeInfo.outData = NULL;
    } else {
        soeInfo.outData = &soeInfo.data[soeInfo.outPos];
    }

    if (soeInfo.insertData == NULL) {
        soeInfo.insertData = &soeInfo.data[soeInfo.insertPos];
    }    
}

/**
 * @brief  将事件从flash中依次读取出来
 * @param
 */

void printfEventsOrderRecord(void)
{

}


