/**
******************************************************************************
* @file upp_status.c
* @brief Модуль определяющий состояние УПП (Input Регистры)
******************************************************************************
* @details
******************************************************************************/
#include "upp_main.h" // всё остальное по работе с УПП
#include "upp_status.h"
int cnt_oscil_size = 100;
void UPP_Status_Handler(void)
{
  /* Хендлы для отладочных светодиодов (если выбран режим моргания) */
  GPIO_LED_Dynamic_Handle(&UPP_LEDS.green1);
  GPIO_LED_Dynamic_Handle(&UPP_LEDS.green2);
  GPIO_LED_Dynamic_Handle(&UPP_LEDS.red);
  
  MB_DATA.InRegs.pui.Error = upp.errors->common;
  MB_DATA.InRegs.pui.WorkMode = upp.workmode;
  
  MB_DATA.InRegs.pui.Uall = upp.pm.measured.final.Uamp   * MB_INTERNAL.param.nominal.U;
  MB_DATA.InRegs.pui.Uab = upp.pm.measured.final.U[U_AB] * MB_INTERNAL.param.nominal.U;
  MB_DATA.InRegs.pui.Ubc = upp.pm.measured.final.U[U_BC] * MB_INTERNAL.param.nominal.U;
  MB_DATA.InRegs.pui.Uca = upp.pm.measured.final.U[U_CA] * MB_INTERNAL.param.nominal.U;

  MB_DATA.InRegs.pui.Iall = upp.pm.measured.final.Iamp * MB_INTERNAL.param.nominal.I;
  MB_DATA.InRegs.pui.Ia = upp.pm.measured.final.I[I_A] * MB_INTERNAL.param.nominal.I;
  MB_DATA.InRegs.pui.Ib = upp.pm.measured.final.I[I_B] * MB_INTERNAL.param.nominal.I;
  MB_DATA.InRegs.pui.Ic = upp.pm.measured.final.I[I_C] * MB_INTERNAL.param.nominal.I;
  
  MB_DATA.InRegs.pui.Fall = upp.pm.measured.final.Fmean  * 10;
  MB_DATA.InRegs.pui.Fab = upp.pm.measured.final.F[U_AB] * 10;
  MB_DATA.InRegs.pui.Fbc = upp.pm.measured.final.F[U_BC] * 10;
  MB_DATA.InRegs.pui.Fca = upp.pm.measured.final.F[U_CA] * 10;
  
  
  MB_DATA.InRegs.pui.Trad = upp.pm.measured.final.T[TEMP_RAD] * 10;
  MB_DATA.InRegs.pui.Tshkaf = upp.pm.measured.final.T[TEMP_SHKAF] * 10;
  
  MB_DATA.InRegs.pui.Warnings.warn.NoConnectionPCH = 0; //todo
  MB_DATA.InRegs.pui.Warnings.warn.NoInputVoltage = !upp.pm.f.isU;
  MB_DATA.InRegs.pui.Warnings.warn.CurrentLimited = upp.hangle.f.CurrentLimited;
  MB_DATA.InRegs.pui.Warnings.warn.OverVoltage = !upp.errors->pui.err.OverVoltage;
  MB_DATA.InRegs.pui.Warnings.warn.UnderVoltage = !upp.errors->pui.err.UnderVoltage;
  
  
  static int mb_u_cnt = 0;
  if(mb_u_cnt+4 >= 240)
  {
    mb_u_cnt = 0;
  }
//  MB_INTERNAL.oscil.data[mb_u_cnt++] = (upp.pm.measured.slow.U[U_AB]+1)*127;
//  MB_INTERNAL.oscil.data[mb_u_cnt++] = (upp.pm.measured.slow.U[U_BC]+1)*127;
//  MB_INTERNAL.oscil.data[mb_u_cnt++] = (upp.pm.measured.slow.U[U_CA]+1)*127;
//  MB_INTERNAL.oscil.data[mb_u_cnt++] = (upp.pm.measured.slow.I[I_A]+1)*127;
//  MB_INTERNAL.oscil.data[mb_u_cnt++] = (upp.pm.measured.slow.I[I_B]+1)*127;
//  MB_INTERNAL.oscil.data[mb_u_cnt++] = (upp.pm.measured.slow.I[I_C]+1)*127;
  static int cnt_oscil = 0;
  
  if(cnt_oscil == 0)
  {
    MB_INTERNAL.oscil.data[mb_u_cnt++] = (upp.hangle.Iref)*32;
    MB_INTERNAL.oscil.data[mb_u_cnt++] = (upp.hangle.Imeas)*32;
    MB_INTERNAL.oscil.data[mb_u_cnt++] = (upp.hangle.alpha)*127;
    MB_INTERNAL.oscil.data[mb_u_cnt++] = 0;
  }
  if (++cnt_oscil>cnt_oscil_size)
  {
    cnt_oscil = 0;
  }
  
  
  if(GPIO_Read_Switch(&UPP_DIN.Pusk))
  {
    upp.call->go = 1;
  }
  else
  {
    upp.call->go = 0;
  }
}