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

#include <vcl.h>
#include <stdio.h>
#pragma hdrstop

#include "frmMain.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFormMain *FormMain;
//---------------------------------------------------------------------------
// 통신 설정 옵션인 TCommParam 을  설정한다
void __fastcall TFormMain::SetCommParam(void)
{

    CommParam.PortNo = cbCOM->ItemIndex + 1;
    CommParam.BaudRate = rdSpeed->Items->Strings[rdSpeed->ItemIndex].ToInt();
    CommParam.ByteSize = 8;
    switch(rdStopbit->ItemIndex)
    {
        case 0:
            CommParam.StopBits = STOPBIT_1;
            break;
        case 1:
            CommParam.StopBits = STOPBIT_15;
            break;
        case 2:
            CommParam.StopBits = STOPBIT_2;
            break;
    }
    switch(rdParity->ItemIndex)
    {
        case 0:
            CommParam.Parity = PARITY_NONE;
            break;
        case 1:
            CommParam.Parity = PARITY_EVEN;
            break;
        case 2:
            CommParam.Parity = PARITY_ODD;
            break;
    }
}
//---------------------------------------------------------------------------
// 수신된 Data의 값들을 16진수로 보여준다
void __fastcall TFormMain::ShowRcvData(int iLen, char *bData)
{
//    BYTE TempBuff[5];
    BYTE TempBuff[8];

    AnsiString RcvData1;
    int RcvData2;
    int RcvData3;

    POINT pt;

    GetCursorPos (&pt);
//    SetCursorPos (bData[3]+pt.x, bData[2]+pt.y);
    SetCursorPos (bData[2]+pt.x, bData[1]+pt.y);

    CaptureImage->Canvas->MoveTo(pt.x-Left-3, pt.y-Top-45);
//    CaptureImage->Canvas->LineTo(bData[3]+pt.x-Left-3, bData[2]+pt.y-Top-45);
    CaptureImage->Canvas->LineTo(bData[2]+pt.x-Left-3, bData[1]+pt.y-Top-45);

    for(int i=0; i<iLen; i++)
    {
        //sprintf(TempBuff, "%d", bData[i]);
        //RcvData1 = RcvData1 + " [" + (char *)TempBuff + "]";
        //RcvData2 = bData[2];
        //RcvData3 = bData[3];
        RcvData2 = bData[1];
        RcvData3 = bData[2];
    }
    ListBox2->Items->Add(RcvData2);
    ListBox3->Items->Add(RcvData3);
}
//---------------------------------------------------------------------------
// 쓰레드에 의해 Data가 수신되면 SendMessage에 의해 호출된다. 즉 수신버퍼에
// TMessage의 WParam의 길이만큼 Data가 수신된 후 호출된다
void __fastcall TFormMain::UMEvnRxChar(TMessage &Message)
{
    int iLen;
    BYTE RcvBuff[4096];

    if(RadioGroupRecvType->ItemIndex == 1) return;
    iLen = RS232C->ReadData(Message.WParam, RcvBuff);
    ShowRcvData(iLen, RcvBuff);
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::ClearEdit(void)
{
    EditCode->Text = "";
    EditLength->Text = "";
    EditData->Text = "";
}
//---------------------------------------------------------------------------
// 송신할 데이타를 TDataType 구조체에 넣어준다
void __fastcall TFormMain::MakeSendData(void)
{
    if(TxBuff.Data)
    {
        delete []TxBuff.Data;
    }
    ZeroMemory(&TxBuff, sizeof(TDataType));
    TxBuff.Code = (BYTE)EditCode->Text.ToInt();
    TxBuff.Len = (short int)EditLength->Text.ToInt();
    TxBuff.Data = (BYTE *)new BYTE[TxBuff.Len+2];
    strcpy(TxBuff.Data, EditData->Text.c_str());
}
//---------------------------------------------------------------------------

void __fastcall TFormMain::FormCreate(TObject *Sender)
{
    TxBuff.Data = NULL;

//    ClearEdit();
    cbCOM->ItemIndex = 0;
    EditLength->Text = EditData->Text.Length();
    RS232C = new TRS232C();
    RS232C->SetHWND(Handle);
    btnSetup->Click();
    CaptureImage->Canvas->FillRect(ClientRect);
}
//---------------------------------------------------------------------------

void __fastcall TFormMain::FormDestroy(TObject *Sender)
{
    if(TxBuff.Data)
    {
        delete []TxBuff.Data;
    }
    delete RS232C;
}
//---------------------------------------------------------------------------

void __fastcall TFormMain::btnSetupClick(TObject *Sender)
{
    DWORD   dwDtr, dwRts;

    switch(RadioGroupDtr->ItemIndex)
    {
        case 0:
            dwDtr = DTR_DISABLE;
            break;
        case 1:
            dwDtr = DTR_ENABLE;
            break;
        case 2:
            dwDtr = DTR_HANDSHAKE;
            break;
    }
    switch(RadioGroupDtr->ItemIndex)
    {
        case 0:
            dwRts = RTS_DISABLE;
            break;
        case 1:
            dwRts = RTS_ENABLE;
            break;
        case 2:
            dwRts = RTS_HANDSHAKE;
            break;
        case 3:
            dwRts = RTS_TOGGLE;
            break;
    }

    SetCommParam();
    RS232C->SetDtrControl(dwDtr);
    RS232C->SetRtsControl(dwRts);
    if(!RS232C->Open(&CommParam)) ShowMessage("Open Failed!");
    RS232C->ClearTxBuffer();
    RS232C->ClearRxBuffer();
}
//---------------------------------------------------------------------------

void __fastcall TFormMain::btnSendClick(TObject *Sender)
{
    if(!RS232C->IsOpened())
    {
        ShowMessage("Open Failed!");
        return;
    }
    //else ShowMessage("Open Success");
    RS232C->ClearTxBuffer();

    MakeSendData();

    if(!RS232C->WriteData(3, (BYTE *)&TxBuff))
    {
        ShowMessage("Header Send Error");
    }
    //else ShowMessage(IntToStr(&TxBuff));
    if(!RS232C->WriteData(TxBuff.Len,  TxBuff.Data))
    {
        ShowMessage("Body Send Error");
    }

    //if(!RS232C->WriteData(8, (BYTE *)packet))
    //{
    //    ShowMessage("Data Send Error");
    //}

    //else (ShowMessage(IntToStr(TxBuff.Data)));
    unsigned char packet;
    packet = 0x01;
    if (RS232C->WriteData(8, (char*)packet))
    {
        ShowMessage("Data Send OK");
    }
    else ShowMessage("Error");
}
//---------------------------------------------------------------------------

void __fastcall TFormMain::EditDataExit(TObject *Sender)
{
    EditLength->Text = EditData->Text.Length();
}
//---------------------------------------------------------------------------

void __fastcall TFormMain::FormActivate(TObject *Sender)
{
    EditCode->SetFocus();
}
//---------------------------------------------------------------------------

void __fastcall TFormMain::btnRcvClick(TObject *Sender)
{
    int iLen;
    BYTE bRcvBuff[4096];

    iLen = RS232C->ReadData(30, bRcvBuff);
    if(!iLen) ShowMessage("TimeOut");
    else ShowRcvData(iLen, bRcvBuff);
}
//---------------------------------------------------------------------------

void __fastcall TFormMain::RadioGroupRecvTypeClick(TObject *Sender)
{
    btnRcv->Enabled = (RadioGroupRecvType->ItemIndex == 1);
}
//---------------------------------------------------------------------------

__fastcall TFormMain::TFormMain(TComponent* Owner)
    : TForm(Owner)
{
   int i;
   //int j;

   int test_x[10] = {0, 70, 140, 210, 280, 350, 420, 490, 560, 630};
   int test_y[10] = {0, 70, 140, 210, 280, 350, 420, 490, 560, 630};

   //for (i=0; i< 30; i++)
   for (i=0; i< 10; i++)
   {
      pt[i].x = test_x[i];
      pt[i].y = test_y[i];

      is_est[i] = false;
      //is_del[j] = false;
   }
}
//---------------------------------------------------------------------------

void __fastcall TFormMain::imgClearClick(TObject *Sender)
{
    CaptureImage->Canvas->Refresh();
    CaptureImage->Canvas->FillRect(ClientRect);
}
//---------------------------------------------------------------------------

void __fastcall TFormMain::imgImageSaveClick(TObject *Sender)
{


    String file_name = IntToStr(m_numImages + 1) + ".bmp";
    CaptureImage->Picture->SaveToFile(file_name);

    int index = (m_numImages % 10) + 1;

    TImage *img = (TImage *) FindComponent("Image" + IntToStr(index));
    img->Picture->LoadFromFile(file_name);

    m_numImages++;

/*
    int i;
    int index;

    TImage *img;

    for (i=0; i < 10; i++)

    {
        if (is_est[i] == false)
        {
          String file_name;
          file_name = IntToStr(i+1) + ".bmp";

          CaptureImage->Picture->SaveToFile(file_name);

          index = (i + 1) % 10;
          ((TImage*)FindComponent("Image"+IntToStr(index)))->Picture->LoadFromFile(file_name);


          is_est[i] = true;
          break;
        }
    }
*/
}
//---------------------------------------------------------------------------

void __fastcall TFormMain::imgDeleteClick(TObject *Sender)
{
    if (m_numImages > 0) {
        String file_name = IntToStr(m_numImages) + ".bmp";
        DeleteFile(file_name);
        m_numImages--;
    }
    /*
    int i;
    int index1;
    //int isum = 0;


    TImage *img;

    for (i = 0; i < 10; i++)
    {
        if (is_est[i] == true)
        {
            ShowMessage("Index: " + IntToStr(i) + ", Value: " + IntToStr(is_est[i]));
          //String s = i + ": " + is_est[i];
          String file_name;

          file_name = IntToStr(i+1) + ".bmp";
          {
            index1 = (i + 1) % 10;

//            ShowMessage(file_name = IntToStr(i+1) + ".bmp");
            ShowMessage("Image" + IntToStr(index1));
//            ShowMessage("Index: " + IntToStr(i) + ", Value: " + IntToStr(is_est[i]));

            //DeleteFile(file_name);

          }
          is_est[i] = false;
          break;
        }
    }
    */
}
//---------------------------------------------------------------------------

void __fastcall TFormMain::imgExitClick(TObject *Sender)
{
    Close();
}
//---------------------------------------------------------------------------

void __fastcall TFormMain::imgClearAllClick(TObject *Sender)
{
    CaptureImage->Canvas->Refresh();
    CaptureImage->Canvas->FillRect(ClientRect);

    Image1->Canvas->Refresh();
    Image1->Canvas->FillRect(ClientRect);

    Image2->Canvas->Refresh();
    Image2->Canvas->FillRect(ClientRect);

    Image3->Canvas->Refresh();
    Image3->Canvas->FillRect(ClientRect);

    Image4->Canvas->Refresh();
    Image4->Canvas->FillRect(ClientRect);

    Image5->Canvas->Refresh();
    Image5->Canvas->FillRect(ClientRect);

    Image6->Canvas->Refresh();
    Image6->Canvas->FillRect(ClientRect);

    Image7->Canvas->Refresh();
    Image7->Canvas->FillRect(ClientRect);

    Image8->Canvas->Refresh();
    Image8->Canvas->FillRect(ClientRect);

    Image9->Canvas->Refresh();
    Image9->Canvas->FillRect(ClientRect);

    Image10->Canvas->Refresh();
    Image10->Canvas->FillRect(ClientRect);

}
//---------------------------------------------------------------------------

/*
void __fastcall TFormMain::SaveImg1Click(TObject *Sender)
{
    int index;
    CaptureImage->Picture->SaveToFile(file_name);
    Image1->Picture->LoadFromFile("1.bmp");
}
//---------------------------------------------------------------------------

void __fastcall TFormMain::SaveImg2Click(TObject *Sender)
{
    int index;
    CaptureImage->Picture->SaveToFile("2.bmp");
    Image2->Picture->LoadFromFile("2.bmp");
}
//---------------------------------------------------------------------------

void __fastcall TFormMain::SaveImg3Click(TObject *Sender)
{
    int index;
    CaptureImage->Picture->SaveToFile("3.bmp");
    Image3->Picture->LoadFromFile("3.bmp");
}
//---------------------------------------------------------------------------

void __fastcall TFormMain::SaveImg4Click(TObject *Sender)
{
    int index;
    CaptureImage->Picture->SaveToFile("4.bmp");
    Image4->Picture->LoadFromFile("4.bmp");
}
//---------------------------------------------------------------------------
*/

void __fastcall TFormMain::Button1Click(TObject *Sender)
{
    //int iLen;
//    TxBuff.Data = (char *)0x01;

//    iLen = RS232C->ReadData(30, bRcvBuff);
    //iLen = RS232C->WriteData(5, (BYTE *)TxBuff);
//    RS232C->WriteData(3, (char*)&TxBuff);
//    ShowMessage(IntToStr(TxBuff.Data));

//    int iLen;
//    BYTE bRcvBuff[4096];

//    iLen = RS232C->ReadData(30, bRcvBuff);
//    if(!iLen) ShowMessage("TimeOut");
//    else ShowRcvData(iLen, bRcvBuff);

//    unsigned char packet[1] = {0, };
    
}
//---------------------------------------------------------------------------

