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

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include "Tserial_event.h"
#include "Tserial_event.cpp"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"

TForm1 *Form1;
char dalsi_data=0;

void SerialEventManager(uint32 object, uint32 event);
Tserial_event serial_port;
 char a, *pom;
 int i;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
serial_port.setManager(SerialEventManager);
}
//---------------------------------------------------------------------------

/* ======================================================== */
/* ===============  OnCharArrival     ===================== */
/* ======================================================== */
void SerialEventManager(uint32 object, uint32 event)
{
    char *buffer;
    int   size;
    Tserial_event *com;
    char   txt[512];

    com = (Tserial_event *) object;
    if (com!=0)
    {
        switch(event)
        {
            case  SERIAL_CONNECTED  :
                                        Form1->Memo1->Lines->Append(AnsiString("Připojen !"));
                                        //Form1->Shape1->Brush->Color=clLime;
                                        break;
            case  SERIAL_DISCONNECTED  :
                                        Form1->Memo1->Lines->Append(AnsiString("Odpojen !"));
                                        break;
            case  SERIAL_DATA_SENT  :
                                        //OnDataSent();
                                        Form1->Memo1->Lines->Append(AnsiString("Data odeslána !"));
                                        break;
            /*
            case  SERIAL_RING       :
                                        printf("DRING ! \n");
                                        break;
            case  SERIAL_CD_ON      :
                                        printf("Carrier Detected ! \n");
                                        break;
            case  SERIAL_CD_OFF     :
                                        printf("No more carrier ! \n");
                                        break;
            */
            case  SERIAL_DATA_ARRIVAL  :{
                                         Form1->Shape1->Brush->Color=clRed;
                                        size   = com->getDataInSize();
                                        buffer = com->getDataInBuffer();

                                        if (size>511)
                                            size=511;

                                        memcpy(txt,buffer, size);
                                        txt[size] = 0;
                                        com->dataHasBeenRead();
                                        Form1->Memo2->Lines->Append(txt);
                                        Form1->Label5->Caption=Form1->Label2->Caption;
                                        Form1->Label2->Caption=Form1->Label1->Caption;
                                        Form1->Label1->Caption = "0x" + IntToHex(txt[0],2);
                                        Form1->Shape1->Brush->Color=clLime;
                                        break;
                                        }
        }
    }
}


void __fastcall TForm1::Button1Click(TObject *Sender)

{
switch (Form1->ComboBox1->ItemIndex)
        {
         case 0: serial_port.connect("COM1",9600,SERIAL_PARITY_NONE,8,false); break;
         case 1: serial_port.connect("COM2",9600,SERIAL_PARITY_NONE,8,false); break;
        }
                                                                        //CONNECT

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

void __fastcall TForm1::Button2Click(TObject *Sender)
{
//SENT
serial_port.sendData(Form1->Edit1->Text.c_str(), Form1->Edit1->Text.Length());
Sleep(1);
serial_port.sendData("\x01",1); // TWI_CMD_HORNI_SEGMENT
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button3Click(TObject *Sender)
{                                                                 //DISCONNECT
serial_port.disconnect();
Form1->Shape1->Brush->Color=clWhite;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button4Click(TObject *Sender)
{
serial_port.sendData("\x04",1); // EOT
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button5Click(TObject *Sender)
{
                                                                 //remake ?
serial_port.sendData("\x03",1);// TWI_CMD_ZVYSIT_RYCHLOST                                   //SPEED UP

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

void __fastcall TForm1::Button6Click(TObject *Sender)
{                                                              //remake ?
serial_port.sendData("\x04",1); //TWI_CMD_SNIZIT_RYCHLOST                                 //SPEED DOWN
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button7Click(TObject *Sender)
{                                                               //remake ?
serial_port.sendData("\x06",1);  //TWI_CHANGE_MODE                                //SWITCH MODE
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormCloseQuery(TObject *Sender, bool &CanClose)
{

serial_port.disconnect();                                     //FormCloseQuery
Sleep(10) ;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ComboBox1Change(TObject *Sender)
{
Form1->Button2->Enabled=false;
serial_port.disconnect();                           //disconnect
Form1->Shape1->Brush->Color=clWhite;

switch (Form1->ComboBox1->ItemIndex)                //connect
        {
         case 0: serial_port.connect("COM1",9600,SERIAL_PARITY_NONE,8,false); break;
         case 1: serial_port.connect("COM2",9600,SERIAL_PARITY_NONE,8,false); break;
        }
Form1->Button2->Enabled=true;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button9Click(TObject *Sender)
{
Form1->Memo1->Lines->Clear();
Form1->Memo2->Lines->Clear();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button10Click(TObject *Sender)
{

serial_port.sendData("\x20",1);

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

void __fastcall TForm1::Button11Click(TObject *Sender)
{
serial_port.sendData(Form1->Edit2->Text.c_str(), Form1->Edit2->Text.Length());
Sleep(1);
serial_port.sendData("\x02",1); //TWI_CMD_DOLNI_SEGMENT

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

