 
#include "main.h"
#include "cmsis_os.h"
#include "elog.h"

#if 1

extern void  get_h2s_3(float *concentration, double voltage,  double t);
extern void  get_h2s_2(double *concentration, double voltage,  double t);
extern void usart6_receive_enable();
extern void usart6_send_enable();
extern void max3243_enable();
extern void max3160_232_FULL_mode();
extern void max3160_485_send_mode();
extern void max3160_485_receive_mode();
extern  void uart2_power_enable();
extern  void uart3_power_enable();
extern  void uart4_power_enable();
extern  void uart6_power_enable();

void usart6_receive_enable()
{
  /*GPIO默认低电平*/
  HAL_GPIO_WritePin(R_W_GPIO_Port,R_W_Pin, RESET);
} 


void usart6_send_enable()
{
  HAL_GPIO_WritePin(R_W_GPIO_Port,R_W_Pin, SET);
} 

void max3243_enable()
{
  /*  使能 3243 工作， 系统默认低电平， 启动开启工作*/
  HAL_GPIO_WritePin(MAX3242_EN_GPIO_Port,MAX3242_EN_Pin, SET);
} 


void max3160_232_FULL_mode()
{
  /*232_485选择 : 232-拉低  ， DPLX 拉低 全双*/
  HAL_GPIO_WritePin(SEL_232_485_GPIO_Port,SEL_232_485_Pin, RESET);
  HAL_GPIO_WritePin(HDPLX_GPIO_Port,HDPLX_Pin, RESET);

} 

void max3160_485_send_mode()
{
  /*232_485选择: 485-拉高  ， DPLX 拉高 半双 ，  DE 拉低使能发送 */
  HAL_GPIO_WritePin(SEL_232_485_GPIO_Port,SEL_232_485_Pin, SET);
  HAL_GPIO_WritePin(HDPLX_GPIO_Port,HDPLX_Pin, RESET);
  HAL_GPIO_WritePin(DE485_GPIO_Port, DE485_Pin, SET);
} 

void max3160_485_receive_mode()
{
  /*232_485选择 -拉高  ， DPLX 拉低 全双，  DE 拉高接收 默认高电平 */
  HAL_GPIO_WritePin(SEL_232_485_GPIO_Port,SEL_232_485_Pin, SET);
  HAL_GPIO_WritePin(HDPLX_GPIO_Port,HDPLX_Pin, SET);
  HAL_GPIO_WritePin(DE485_GPIO_Port, DE485_Pin, RESET);

} 


void uart2_power_enable()
{
 
  HAL_GPIO_WritePin(SENSOR2_EN_GPIO_Port,SENSOR2_EN_Pin, SET);
} 
void uart3_power_enable()
{
  HAL_GPIO_WritePin(SENSOR3_EN_GPIO_Port,SENSOR3_EN_Pin, SET);
} 

void uart4_power_enable()
{
  HAL_GPIO_WritePin(SENSOR4_EN_GPIO_Port,SENSOR4_EN_Pin, SET);
} 

void uart6_power_enable()
{
  HAL_GPIO_WritePin(SENSOR6_EN_GPIO_Port,SENSOR6_EN_Pin, SET);
} 


double  get_h2s(double voltage,  double t)
{
    /*
        H2S_con = a20 *voltage *ET
        Et = a3*T^3 + a2*T^2 + a1*T + a0 温度补偿因子
        Sulphide_con =  H2S_con * G
        a20:  20度时 电压值 0.007939553 20度时得斜率
        voltage 毫伏
    */
   double a0 = 3.149951E+00;
   double a1 = -1.768957E-01;
   double a2 = 3.656828E-03;
   double a3 = -1.176627E-05;

   double a20 = 7.939553E-03;
   double Ug = 7.0E+00;

   return ( voltage - Ug) * a20 *( a3*t*t*t+ a2*t*t + a1*t + a0 );

}

void  get_h2s_2(double *concentration, double voltage,  double t)
{
    /*
        H2S_con = a20 *voltage *ET
        Et = a3*T^3 + a2*T^2 + a1*T + a0 温度补偿因子
        Sulphide_con =  H2S_con * G
        a20:  20度时 电压值 0.007939553 20度时得斜率
        voltage 毫伏
    */
   double a0 = 3.149951E+00;
   double a1 = -1.768957E-01;
   double a2 = 3.656828E-03;
   double a3 = -1.176627E-05;

   double a20 = 7.939553E-03;
   double Ug = 7.0E+00;

   *concentration =  ( voltage - Ug) * a20 *( a3*t*t*t+ a2*t*t + a1*t + a0 );
}


void  get_h2s_3(float *concentration, double voltage,  double t)
{
    /*
        H2S_con = a20 *voltage *ET
        Et = a3*T^3 + a2*T^2 + a1*T + a0 温度补偿因子
        Sulphide_con =  H2S_con * G
        a20:  20度时 电压值 0.007939553 20度时得斜率
        voltage 毫伏
    */
   double a0 = 3.149951E+00;
   double a1 = -1.768957E-01;
   double a2 = 3.656828E-03;
   double a3 = -1.176627E-05;

   double a20 = 7.939553E-03;
   double Ug = 7.0E+00;

   *concentration =  ( voltage - Ug) * a20 *( a3*t*t*t+ a2*t*t + a1*t + a0 );
}

#endif