#include "hp203b.h"
#include "i2c.h"
#include  "filter.h"
#include "anemometer_dev.h"

/**************************** 
*名称:set_mode
*功能:配置从机模式,设置通道及采样率
*参数:无
*
*
*返回:无
*****************************/
void hp203_set_mode()
{
    uint8_t cmd[1] = {HP20X_CONVERT_OSR1024};
    HAL_I2C_Master_Transmit(&hi2c3, HP20X_ADDRESSCMD, cmd, 1, 0xff);
}


/**************************** 
*名称:Hp203bReadPressure
*功能:获取气压数据
*参数:
*			
*			 
*返回:气压
*****************************/
BOOL Hp203bReadPressure(float *press)
{
    long Hp203b_Pressure = 0;
    uint8_t Hp203bPressure_Temp[3] = {0};
    uint8_t read_command[1] = {HP20X_READ_P};

    HAL_I2C_Master_Transmit(&hi2c3, HP20X_ADDRESSCMD, read_command, 1, 0xff);
    HAL_I2C_Master_Receive(&hi2c3, HP20X_ADDRESSCMD, Hp203bPressure_Temp, 3, 0xff);
    
    Hp203b_Pressure = Hp203bPressure_Temp[0];
    Hp203b_Pressure <<= 8;
    Hp203b_Pressure |= Hp203bPressure_Temp[1];
    Hp203b_Pressure <<= 8;
    Hp203b_Pressure |= Hp203bPressure_Temp[2];
    
    *press = (float)Hp203b_Pressure / 100.0f;
    if(*press < 300 || *press > 1200)
    {
        *press = 0;
        return FALSE;
    }
    return TRUE;
}

/**************************** 
*名称:Hp203bReadTempture
*功能:获取温度数据
*参数:
*			
*			 
*返回:温度
*****************************/
BOOL Hp203bReadTempture(float *press)
{
    uint32_t Hp203b_tempture = 0;
    uint8_t Hp203bPressure_Temp[3] = {0};
    uint8_t read_command[1] = {0x32};

    HAL_I2C_Master_Transmit(&hi2c3, HP20X_ADDRESSCMD, read_command, 1, 0xff);
    HAL_I2C_Master_Receive(&hi2c3, HP20X_ADDRESSCMD, Hp203bPressure_Temp, 3, 0xff);
    Hp203b_tempture = (Hp203bPressure_Temp[0] & 0x0F)<<16;
    Hp203b_tempture |= Hp203bPressure_Temp[1]<<8;
    Hp203b_tempture |= Hp203bPressure_Temp[2];
    
    if((Hp203b_tempture>>19) & 1){
        Hp203b_tempture = (((~Hp203b_tempture) + 1) & 0xFFFFF);
        *press = -(float)Hp203b_tempture / 100.0f;
    }
    else{
        *press = (float)Hp203b_tempture / 100.0f;
    }
    if(*press<-50||*press>95){
        *press = 0;
        return FALSE;
    }
    return TRUE;
}

static float calculateAverage(float arr[], int avgLength) {
    float sum = 0;
    
    // 遍历数组(最多10个元素),收集非零值直到达到指定数量
    for (int i = 0; i < 10; ++i) {
            sum += arr[i];
    }
    
    // 计算平均值并限制最大值
    float average = sum / avgLength;
    return average;
}
/**************************** 
*名称:get_press_data
*功能:获取气压与备用温度数据
*参数:Press--气压值
*
*
*返回:无
*****************************/
#define COLLECT_HB203_DATA_NUM  10
BOOL get_HP203_data(float* tempdata, float* press)
{
    uint8_t ret;
    uint8_t temp_falt = 0;
    uint8_t press_falt = 0;
//    压强
    float collect_pressure[COLLECT_HB203_DATA_NUM]={0x00};
   
    for(int i=0; i<COLLECT_HB203_DATA_NUM; i++){
        hp203_set_mode();
        osDelay(5);
        ret = Hp203bReadPressure(&collect_pressure[i]);
        if(ret == FALSE)
        {
            press_falt++;
            continue;
            //goto error_return;
        }
        osDelay(5);
    }
    if(press_falt >= COLLECT_HB203_DATA_NUM)
    {
        goto error_return;
    }
    //求平均
    float tmp_press = calculateAverage(collect_pressure, COLLECT_HB203_DATA_NUM - press_falt);
    if(tmp_press < 300)
    {
//        return FALSE;
        goto error_return;
    }
    if(tmp_press > 1200)
    {
//        return FALSE;
        goto error_return;
    }
//    温度
    float collect_tempture[COLLECT_HB203_DATA_NUM]={0x00};
   
    for(int i=0; i<COLLECT_HB203_DATA_NUM; i++){
        hp203_set_mode();
        osDelay(5);
        ret = Hp203bReadTempture(&collect_tempture[i]);
        if(ret == FALSE)
        {
            temp_falt++;
            continue;
            //goto error_return;
        }
        osDelay(5);
    }
    if(temp_falt >= COLLECT_HB203_DATA_NUM)
    {
        goto error_return;
    }
    //求平均
    float tmp_tempture = calculateAverage(collect_tempture, COLLECT_HB203_DATA_NUM - temp_falt);
    if(tmp_tempture < -50)
    {
//        return FALSE;
        goto error_return;
    }
    if(tmp_tempture > 95)
    {
//        return FALSE;
        goto error_return;
    }
    
    *tempdata = tmp_tempture;
    *press = tmp_press;
    return TRUE;
    
error_return:
    *tempdata = 0;
    *press = 0;
    return FALSE;
}