
#include "base/delay.h"
#include "base/utility.h"
#include "hardware/adc_cfg.h"
#include "hardware/dac_cfg.h"
#include "hardware/gpio_cfg.h"
#include "hardware/jw3425iic.h"
#include "hardware/power.h"
#include "hardware/timer_cfg.h"
#include "driver/EWDriver.h"
#include "driver/JQDriver.h"
#include "driver/XTDriver.h"
#include "interface/BaseChecker.h"
#include "interface/JQChecker.h"
#include "interface/XTChecker.h"
#include "interface/EWChecker.h"

#include "elec_det.h"
#include "dev_flash.h"
#include "mystring.h"
#include "prot_uc.h"
#include "board.h"
#include "transmit.h"
#include "mystdlib.h"
#include "elec_io.h"



// 此文件实现elec模块的输入输出接口



typedef struct{
  tran_def *tran;
}self_def;

static self_def g_self;

int ConsoleUsart_send_bytes(uint8_t *d,int len)
{
  self_def *s=&g_self;
  if(s->tran){
    array_def *r=arr_creat();
    arr_appends(r,d,len);
    emit tran_reply_signal(s->tran,arr_temp(r));
  }
  return 0;
}


int SaveBoardInfo(void)
{
  sys_param_def *spar=0;
  spar=calloc(1,sizeof(sys_param_def));
  memcpy(spar,sys_param(),sizeof(sys_param_def));
  spar->hard_version=boardinfo_un.boardinfo.hard_v;
  spar->resistor_diff=boardinfo_un.boardinfo.resistor_diff;
  flash_save_param(spar);
  free(spar);
  return 0;
}


int LoadBoardInfo(void)
{
  boardinfo_un.boardinfo.bootflag=1;
  boardinfo_un.boardinfo.hard_v=sys_param()->hard_version;
  boardinfo_un.boardinfo.resistor_diff=sys_param()->resistor_diff;
  return 0;
}



void Ye_RunPlanCheckTest(void)
{
  array_def *data=arr_creat();
  array_def *r=elec_check_with_scheme(data);
  arr_delete(data);
}





// 小板ew协议解码
array_def *protew_decode(protu_def *p,array_def *data)
{
  if((arr_get(data,0)=='E')&&(arr_get(data,1)=='w')){
    str_set(p->str_err,"ok");
    p->cmd=0x01;
    return arr_duplicate(data);
  }
  str_set(p->str_err,"decode for Ew failed.");
  return arr_creat();
}
array_def *protew_encode(protu_def *p,array_def *data)
{
  array_def *r;
  r=arr_duplicate(data);
  return r;
}
protuc_codec_export(ew_slave,protew_decode,protew_encode);

static int ew_slave_dolater(ucport_def *u,uint8_t cmd,array_def *data,char *err_str)
{
  void EW_UsartApp_Push_Cmd(uint8_t* buf, uint16_t len);
  EW_UsartApp_Push_Cmd(arr_data(data),arr_length(data));
  return 0;
}
static ucport_def *ew_slave_fun(tran_def *t, uint8_t cmd,array_def *data)
{
  int ret=0;
  self_def *s=&g_self;
  s->tran=t;
  ucport_def *u=calloc(1,sizeof(ucport_def));
  u->p=t;
  u->dolater=ew_slave_dolater;
  u->dolater(u,cmd,data,"ok");
  return (ucport_def *)u;
}
transmit_export(ew_slave,0x01,ew_slave_fun)





