//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "MainUnit.h"

//add this next line manually. Make sure you add ..\lib\lc4hs_bor.lib to the project
#include "..\include\lc4hs.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"


static void ErrorDlg(const System::AnsiString Msg) {
  MessageDlg(Msg, mtError, TMsgDlgButtons() << mbOK, 0);
}

static void InfoDlg(const System::AnsiString Msg) {
  MessageDlg(Msg, mtInformation, TMsgDlgButtons() << mbOK, 0);
}

TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::InitButtonClick(TObject *Sender)
{
  InitButton->Caption = "working...";
  InitButton->Enabled = false;
  InitButton->Update();
  try {
    if (Lc4hsInit(hCard))
      InfoDlg("Initialised OK");
    else
      ErrorDlg("Could not initialise card");
  }
  __finally {
    InitButton->Enabled = true;
    InitButton->Caption = "Init";
  }
}

//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
  hCard = Lc4hsOpen("4HS1");
  if (hCard == INVALID_HANDLE_VALUE) {
    ErrorDlg("Cannot open driver");
    Close();
  }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FinishButtonClick(TObject *Sender)
{
  if (!Lc4hsFinish(hCard)) ErrorDlg("Could not initialise card");
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ReadButtonClick(TObject *Sender)
{
  FLOAT Result[4];
  if (Lc4hsGetCurrentWeight4(hCard, Result))
    Status->Caption = Format("%.3f %.3f %.3f %.3f",
      ARRAYOFCONST((Result[0], Result[1], Result[2], Result[3])));
  else
    ErrorDlg("Could not read card");
}
//---------------------------------------------------------------------------

