#pragma hdrstop

#include "WakePort.h"

#pragma package(smart_init)

//--------------------------- Конструктор: ----------------------------------

__fastcall TWakePort::TWakePort()
{
  SetBaudRate(BAUD_RATE);
}

//---------------------------- Деструктор: ----------------------------------

__fastcall TWakePort::~TWakePort()
{
//
}

//---------------------------------------------------------------------------
//------------------------ Вспомогательные методы: --------------------------
//---------------------------------------------------------------------------

//--------------------- Преобразование кода в проценты: ---------------------

int __fastcall TWakePort::Code2Val(int c)
{                         
  return(int((double)c * T_UNITS / TEN_MAX + 0.5));
}

//-------------------- Преобразование процентов в код: ----------------------

int __fastcall TWakePort::Val2Code(int v)
{
  return(int((double)v * TEN_MAX / T_UNITS + 0.5));
}

//---------------------------------------------------------------------------
//------------------------- Специальные команды: ----------------------------
//---------------------------------------------------------------------------

//---------------------------- CMD_SET_MODE: --------------------------------

void __fastcall TWakePort::SetMode(int m)
{
  _ENTER_COMMAND(CmdCrSec)
  NewCmd(CMD_SET_MODE, "SetMode");
  AddByte(m);
  SendCmd();
  _LEAVE_COMMAND(CmdCrSec)
}

//------------------------- CMD_SET_SPOOL_MODE: -----------------------------

void __fastcall TWakePort::SetSpoolMode(int m)
{
  _ENTER_COMMAND(CmdCrSec)
  NewCmd(CMD_SET_SPOOL_MODE, "SetSpoolMode");
  AddByte(m);
  SendCmd();
  _LEAVE_COMMAND(CmdCrSec)
}

//--------------------------- CMD_SET_CAPSTAN: ------------------------------

void __fastcall TWakePort::SetCapstan(int m)
{
  _ENTER_COMMAND(CmdCrSec)
  NewCmd(CMD_SET_CAPSTAN, "SetCapstan");
  AddByte(m);
  SendCmd();
  _LEAVE_COMMAND(CmdCrSec)
}

//----------------------------- CMD_SET_TC: ---------------------------------

void __fastcall TWakePort::SetTC(int m)
{
  _ENTER_COMMAND(CmdCrSec)
  NewCmd(CMD_SET_TC, "SetTC");
  AddByte(m);
  SendCmd();
  _LEAVE_COMMAND(CmdCrSec)
}

//-------------------------- CMD_SET_SOLENOIDS: -----------------------------

void __fastcall TWakePort::SetSolenoids(int s)
{
  _ENTER_COMMAND(CmdCrSec)
  NewCmd(CMD_SET_SOLENOIDS, "SetSolenoids");
  AddByte(s);
  SendCmd();
  _LEAVE_COMMAND(CmdCrSec)
}

//--------------------------- CMD_SET_TENSION: ------------------------------

void __fastcall TWakePort::SetTension(int mode, ten_t &t)
{
  _ENTER_COMMAND(CmdCrSec)
  NewCmd(CMD_SET_TENSION, "SetTension");
  AddByte(mode);
  AddWord(Val2Code(t.m1));
  AddWord(Val2Code(t.m2));
  SendCmd();
  _LEAVE_COMMAND(CmdCrSec)
}

//-------------------------- CMD_GET_TENSION: -------------------------------

void __fastcall TWakePort::GetTension(int mode, ten_t &t)
{
  _ENTER_COMMAND(CmdCrSec)
  NewCmd(CMD_GET_TENSION, "GetTension");
  AddByte(mode);
  SendCmd();
  t.m1 = Code2Val(GetWord());
  t.m2 = Code2Val(GetWord());
  _LEAVE_COMMAND(CmdCrSec)
}

//---------------------------- CMD_SET_SPOOL: -------------------------------

void __fastcall TWakePort::SetSpool(int m1, int m2)
{
  _ENTER_COMMAND(CmdCrSec)
  NewCmd(CMD_SET_SPOOL, "SetSpool");
  AddWord(Val2Code(m1));
  AddWord(Val2Code(m2));
  SendCmd();
  _LEAVE_COMMAND(CmdCrSec)
}

//---------------------------- CMD_SET_AUDIO: -------------------------------

void __fastcall TWakePort::SetAudio(int a)
{
  _ENTER_COMMAND(CmdCrSec)
  NewCmd(CMD_SET_AUDIO, "SetAudio");
  AddByte(a);
  SendCmd();
  _LEAVE_COMMAND(CmdCrSec)
}

//--------------------------- CMD_GET_STATE: --------------------------------

void __fastcall TWakePort::GetState(state_t &s)
{
  _ENTER_COMMAND(CmdCrSec)
  NewCmd(CMD_GET_STATE, "GetState");
  SendCmd();
  s.leds.cont = GetByte();
  s.leds.slow = GetByte();
  s.leds.norm = GetByte();
  s.leds.fast = GetByte();
  s.b = GetByte();
  s.a = GetByte();
  s.s = GetByte();
  s.t1 = Code2Val(GetWord());
  s.t2 = Code2Val(GetWord());
  s.m1 = Code2Val(GetWord());
  s.m2 = Code2Val(GetWord());
  s.r1 = Code2Val(GetWord());
  s.r2 = Code2Val(GetWord());
  _LEAVE_COMMAND(CmdCrSec)
}

//--------------------------- CMD_SET_PARAMS: -------------------------------

void __fastcall TWakePort::SetParams(params_t &p)
{
  _ENTER_COMMAND(CmdCrSec)
  NewCmd(CMD_SET_PARAMS, "SetParams");
  AddByte(p.kp1);
  AddByte(p.ki1);
  AddByte(p.kd1);
  AddByte(p.kp2);
  AddByte(p.ki2);
  AddByte(p.kd2);
  AddWord(p.CapStart);
  AddWord(p.EndTau);
  AddWord(p.EmForce);
  AddWord(p.AsBrkDel);
  AddWord(p.AsPreDel);
  AddWord(p.AsTenDel);
  AddByte(p.TrOptions);
  AddWord(p.CtOptions);
  AddByte(p.sys);
  SendCmd();
  _LEAVE_COMMAND(CmdCrSec)
}

//--------------------------- CMD_GET_PARAMS: -------------------------------

void __fastcall TWakePort::GetParams(params_t &p)
{
  _ENTER_COMMAND(CmdCrSec)
  NewCmd(CMD_GET_PARAMS, "GetParams");
  SendCmd();
  p.kp1 = GetByte();
  p.ki1 = GetByte();
  p.kd1 = GetByte();
  p.kp2 = GetByte();
  p.ki2 = GetByte();
  p.kd2 = GetByte();
  p.CapStart = GetWord();
  p.EndTau = GetWord();
  p.EmForce = GetWord();
  p.AsBrkDel = GetWord();
  p.AsPreDel = GetWord();
  p.AsTenDel = GetWord();
  p.TrOptions = GetByte();
  p.CtOptions = GetWord();
  p.sys = GetByte();
  _LEAVE_COMMAND(CmdCrSec)
}

//------------------------------ CMD_EE_SAVE: -------------------------------

void __fastcall TWakePort::EESave(void)
{
  _ENTER_COMMAND(CmdCrSec)
  NewCmd(CMD_EE_SAVE, "EESave");
  SetTo(LONG_RX_TIMEOUT, DEFAULT_TX_TIMEOUT);
  SendCmd();
  SetTo(DEFAULT_RX_TIMEOUT, DEFAULT_TX_TIMEOUT);
  _LEAVE_COMMAND(CmdCrSec)
}

//------------------------------ CMD_DEFAULT: -------------------------------

void __fastcall TWakePort::Default(void)
{
  _ENTER_COMMAND(CmdCrSec)
  NewCmd(CMD_DEFAULT, "Default");
  SendCmd();
  _LEAVE_COMMAND(CmdCrSec)
}

//---------------------------------------------------------------------------

