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

#include <vcl.h>
#pragma hdrstop

#include "mainWindow.h"
//#include "loadImage.h"

#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <memory.h>
#include <stdlib.h>
#include <iostream>
#include "Unit2.h"
#include "project.h"
#include "FCalendarData.h"
#include "Message.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "CPort"
#pragma link "CPortCtl"
#pragma resource "*.dfm"
TForm1 *Form1;
AnsiString Filename;
//---------------------------------------------------------------------------

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


/**************************************************/
/***                                            ***/
/*** Test2.c  -- test interface to inpout32.dll ***/
/***  ( http://www.logix4u.net/inpout32.htm )   ***/
/***                                            ***/
/*** Copyright (C) 2005, Douglas Beattie Jr.    ***/
/***                                            ***/
/***    <beattidp@ieee.org>                     ***/
/***    http://www.hytherion.com/beattidp/      ***/
/***                                            ***/
/**************************************************/
/*	Last Update: 2006.05.14
 */

/*******************************************************/
/*                                                     */
/*  Builds with Borland's Command-line C Compiler      */
/*    (free for public download from Borland.com, at   */
/*  http://www.borland.com/bcppbuilder/freecompiler )  */
/*                                                     */
/*   Compile with:                                     */
/*                                                     */
/*   BCC32 -IC:\BORLAND\BCC55\INCLUDE  TEST2.C         */
/*                                                     */
/*                                                     */
/*  Be sure to change PPORT_BASE (Port Base address)   */
/*  accordingly if your LPT port is addressed          */
/*  elsewhere.                                         */
/*                                                     */
/*******************************************************/


using namespace std;
/* Definitions in the build of inpout32.dll are:            */
/*   short _stdcall Inp32(short PortAddress);               */
/*   void _stdcall Out32(short PortAddress, short data);    */

/* prototype (function typedef) for DLL function Inp32: */

     typedef short (_stdcall *inpfuncPtr)(short portaddr);
     typedef void (_stdcall *oupfuncPtr)(short portaddr, char datum);

#define PPORT_BASE 0x378
bool EXIT = false ;
short NumberOfImage = 1 ;
AnsiString pw = "redcat";
bool connected = false ;

// Prototypes for Test functions
void test_read8(void);
void test_write(void);
void test_write_datum(short datum);

#define BYTES_PER_LINE 16

typedef struct TGA_HEADER
{
   char  idlength;                // 0
   char  colourmaptype;           // 1
   char  datatypecode;            // 2
   char  pad[5];                  // 3
   short x_origin;                // 8
   short y_origin;                // 10
   short width;                   // 12
   short height;                  // 14
   char  bitsperpixel;            // 16
   char  imagedescriptor;         // 17
} TGA_HEADER;

TGA_HEADER header;

int LoadWidth()
{
	return header.width;
}

int LoadHeight()
{
	return header.height;
}

unsigned char *LoadTGA(char *name)
{
	FILE *f;
	unsigned char *data, *p;

	f = fopen(name, "rb");
	printf("here\n");
	if (!f)
		return 0;
	fread(&header, sizeof(TGA_HEADER), 1, f);
	printf("tga %d %d %d\n", header.width, header.height, header.bitsperpixel);
	printf("here2\n");

	p = data = (unsigned char *)malloc(header.width * header.height * header.bitsperpixel);
	if (!data)
		return 0;

	for (int y=header.height-1; y>=0; y--)
  {
    p = data + (y * (int)header.width * 4);

		for (int x=0; x<header.width; x++)
		{
			if (header.bitsperpixel == 32)
			{
				fread(p, 1, 1, f);
				p++;
				fread(p, 1, 1, f);
				p++;
				fread(p, 1, 1, f);
				p++;
				fread(p, 1, 1, f);
				p++;
			}
			else if (header.bitsperpixel == 24)
			{
				fread(p, 1, 1, f);
				p++;
				fread(p, 1, 1, f);
				p++;
				fread(p, 1, 1, f);
				p++;
				*p++ = 0xff;
			}
		}
  }
	printf("here3\n");

	return data;
}


/* After successful initialization, these 2 variables
   will contain function pointers.
 */
     inpfuncPtr inp32fp;
     oupfuncPtr oup32fp;


/* Wrapper functions for the function pointers
    - call these functions to perform I/O.
 */
     short  Inp32 (short portaddr)
     {
          return (inp32fp)(portaddr);
     }

     void  Out32 (short portaddr, char datum)
     {
          (oup32fp)(portaddr,datum);
     }



bool SendSerialData(short * mdata){
          for(int cnt = 0 ; cnt<28; cnt++){
            Form1->ComPort->Write((void *)mdata[cnt],1);
          }
   return true ;
}

int LoadImagetoSP(AnsiString fn,int numberofadres)
{
     unsigned char *image;
     unsigned int addr= 0x75300/2 * numberofadres;
// load image
	image = LoadTGA(fn.c_str());
	if (!image)
	{
		printf("couldn't load TGA %s\n", fn.c_str());
		exit(1);
	}

	if (LoadWidth() % BYTES_PER_LINE != 0)
	{
		printf("TGA width not multiple of %d\n", BYTES_PER_LINE);
		exit(1);
	}

	unsigned int color;
	int	bcount=0;
	unsigned int colorq;
        short aa=1;
        int red   ;
        int green ;
        int blue   ;
        int alpha  ;
        unsigned char r, g, b, a;
        char Message[28] ;


                               short addr1;
                               addr1 = addr>>20;
                               addr1 = addr1<<2;
                               addr1= addr1|0xc3;

                               short addr2;
                               addr2 = addr>>16;
                               addr2 = addr2 & 0xF;
                               addr2 = addr2<<2;
                               addr2= addr2|0xc3;
                 
                               short addr3;
                               addr3 = addr>>12;
                               addr3 = addr3 & 0xF;
                               addr3 = addr3<<2;
                               addr3= addr3|0xc3;

                               short addr4;
                               addr4 = addr>>8;
                               addr4 = addr4 & 0xF;
                               addr4 = addr4<<2;
                               addr4= addr4|0xc3;
                 
                               short addr5;
                               addr5 = addr>>4;
                               addr5 = addr5 & 0xF;
                               addr5 = addr5<<2;
                               addr5= addr5|0xc3;
                 
                               short addr6;
                               addr6 = addr & 0xF;
                               addr6 = addr6<<2;
                               addr6= addr6|0xc3;

                      Message[0] = 0xC2;               //                                Out32(PPORT_BASE,0xC2);
                      Message[1] = 0xc3;               //                                Out32(PPORT_BASE,0xc3);
                      Message[2] = 0xc3;               //                                Out32(PPORT_BASE,0xc3);
                      Message[3] = addr1;              //                                Out32(PPORT_BASE,addr1);
                      Message[4] = addr1^0x2;          //                                Out32(PPORT_BASE,addr1^0x2);
                      Message[5] = 0xc3;               //                                Out32(PPORT_BASE,0xc3);
                      Message[6] = addr2;              //                                Out32(PPORT_BASE,addr2);
                      Message[7] = addr2^0x2;         //                                Out32(PPORT_BASE,addr2^0x2);
                      Message[8] = 0xc3;               //                                Out32(PPORT_BASE,0xc3);
                      Message[9] = addr3;              //                                Out32(PPORT_BASE,addr3);
                      Message[10] = addr3^0x2;         //                                Out32(PPORT_BASE,addr3^0x2);
                      Message[11] = 0xc3;              //                                Out32(PPORT_BASE,0xc3);
                      Message[12] = addr4;             //                                Out32(PPORT_BASE,addr4);
                      Message[13] = addr4^0x2;         //                                Out32(PPORT_BASE,addr4^0x2);
                      Message[14] = 0xc3;              //                                Out32(PPORT_BASE,0xc3);
                      Message[15] = addr5;             //                                Out32(PPORT_BASE,addr5);
                      Message[16] = addr5^0x2;         //                                Out32(PPORT_BASE,addr5^0x2);
                      Message[17] = 0xc3;              //                                Out32(PPORT_BASE,0xc3);
                      Message[18] = addr6;             //                                Out32(PPORT_BASE,addr6);
                      Message[19] = addr6^0x2;         //                                Out32(PPORT_BASE,addr6^0x2);
                      
         for(int cnt = 0 ; cnt<20; cnt++){
                Form1->ComPort->WriteStr(Message[cnt]);
          }



        for (int y=0; y<600; y++){
                Form1->BitBtn6->Caption = "finished " + IntToStr(y/6);
		for (int x=0; x<800; x++){
                      b = *image++;
                      g = *image++;
                      r = *image++;
                      a = *image++;
                      red   = 0;
                      green = 0;
                      blue  = 0;
                      alpha = 0;

                      #ifdef GRAYSCALE
                      r = g = b = ((int)r + g + b) / 3;     // make image grayscale
                      #endif
        
                      color = (((r >> 5) & 0x7) << 5) | (((g >> 6) & 0x3) << 3) | (((b >> 5) & 0x7) << 0);

 /*                     red = r;
                      red = ((red<<0)& 0x000000ff) ;
                      green = g;
                      green =((green<<8)& 0x0000ff00) ;
                      blue = b;
                      blue =((blue<<16)& 0x00ff0000) ;
                      alpha = a;
                      alpha =((alpha<<24) & 0xff000000) ;
                      colorq = red+green+blue;

                      Form2->Canvas->Pixels[x][y] =  colorq;
*/
                           short clr1 = color>>4;
                              clr1 &=0xf;
                              clr1 = clr1<<2;
                              clr1 |= 0xc3;

                              short clr2=color&0xF;
                              clr2 = clr2<<2;
                              clr2 |=0xc3;

//                      Message[20] = 0xc3;                  //                           Out32(PPORT_BASE,0xc3);
//                      Message[21] = 0xcf;                  //                           Out32(PPORT_BASE,0xcf);
                      Message[22] = clr1;                  //                           Out32(PPORT_BASE,clr1);
                      Message[23] = clr1^0x2;              //                           Out32(PPORT_BASE,clr1^0x2);

//                      Message[24] = 0xc3;                  //                           Out32(PPORT_BASE,0xc3);
//                      Message[25] = 0xcf;                  //                           Out32(PPORT_BASE,0xcf);
                      Message[26] = clr2;                  //                           Out32(PPORT_BASE,clr4);
                      Message[27] = clr2^0x2;              //                           Out32(PPORT_BASE,clr4^0x2);

          if (PAUSE) {
                while (!GOOOO) {
                      Application->ProcessMessages();        
                      if (EXIT) return -1 ;
                }
          }

//         for(int cnt = 0 ; cnt<28; cnt++){
//           if (Form1->cbenter->Checked)
//                Form1->ComPort->Write((void *)Message[cnt],1);
//           else{
                Form1->ComPort->WriteStr(Message[22]);
                Form1->ComPort->WriteStr(Message[23]);
                Form1->ComPort->WriteStr(Message[26]);
                Form1->ComPort->WriteStr(Message[27]);
//          }


                            if (EXIT) return -1 ;
                }
        }
        return 0;
}


bool WriteData(unsigned int addr,unsigned int data){
      HINSTANCE hLib;

    /* Load the library */
     hLib = LoadLibrary("inpout32.dll");

     if (hLib == NULL) {
          cout<<"LoadLibrary Failed."<<endl;
          return false;
     }

     /* get the address of the function */

     inp32fp = (inpfuncPtr) GetProcAddress(hLib, "Inp32");

     if (inp32fp == NULL) {
          cout<<"GetProcAddress for Inp32 Failed."<<endl;
          return false;
     }


     oup32fp = (oupfuncPtr) GetProcAddress(hLib, "Out32");

     if (oup32fp == NULL) {
          cout<<"GetProcAddress for Oup32 Failed"<<endl;
          return false;
     }

                 short addr1;
                 addr1 = addr>>20;
                 addr1 = addr1<<2;
                 addr1= addr1|0xc3;

                 short addr2;
                 addr2 = addr>>16;
                 addr2 = addr2 & 0xF;
                 addr2 = addr2<<2;
                 addr2= addr2|0xc3;
                 
                 short addr3;
                 addr3 = addr>>12;
                 addr3 = addr3 & 0xF;
                 addr3 = addr3<<2;
                 addr3= addr3|0xc3;
                 
                 short addr4;
                 addr4 = addr>>8;
                 addr4 = addr4 & 0xF;
                 addr4 = addr4<<2;
                 addr4= addr4|0xc3;
                 
                 short addr5;
                 addr5 = addr>>4;
                 addr5 = addr5 & 0xF;
                 addr5 = addr5<<2;
                 addr5= addr5|0xc3;
                 
                 short addr6;
                 addr6 = addr & 0xF;
                 addr6 = addr6<<2;
                 addr6= addr6|0xc3;
                 
                 Out32(PPORT_BASE,0xC2);
                 Out32(PPORT_BASE,0xc3);
                 Out32(PPORT_BASE,0xc3);
                 
                 Out32(PPORT_BASE,addr1);
                 Out32(PPORT_BASE,addr1^0x2);

                 Out32(PPORT_BASE,0xc3);
                 Out32(PPORT_BASE,addr2);
                 Out32(PPORT_BASE,addr2^0x2);

                 Out32(PPORT_BASE,0xc3);
                 Out32(PPORT_BASE,addr3);
                 Out32(PPORT_BASE,addr3^0x2);

                 Out32(PPORT_BASE,0xc3);
                 Out32(PPORT_BASE,addr4);
                 Out32(PPORT_BASE,addr4^0x2);

                 Out32(PPORT_BASE,0xc3);
                 Out32(PPORT_BASE,addr5);
                 Out32(PPORT_BASE,addr5^0x2);

                 Out32(PPORT_BASE,0xc3);
                 Out32(PPORT_BASE,addr6);
                 Out32(PPORT_BASE,addr6^0x2);

			unsigned short data1 = data>>4;
			data1 &=0x0f;
                	data1 = data1<<2;
			data1 |= 0xc3;

  			unsigned short  data2 = data & 0x0f;
			data2 = data2<<2;
			data2 |=0xc3;

                      Out32(PPORT_BASE,0xc3);
			Out32(PPORT_BASE,0xcf);
			Out32(PPORT_BASE, data1);
			Out32(PPORT_BASE,data1^0x2);

                      Out32(PPORT_BASE,0xc3);
			Out32(PPORT_BASE,0xcf);
			Out32(PPORT_BASE, data2);
			Out32(PPORT_BASE,data2^0x2);

     /* finished - unload library and exit */
     FreeLibrary(hLib);

     return true;

}

int LoadImagetoPP(AnsiString fn,int numberofadres)
{
     HINSTANCE hLib;


	unsigned char *image;

	//unsigned int addr=0x300;

    unsigned int addr= 0x75300/2 * numberofadres ;


// load image
	image = LoadTGA(fn.c_str());
	if (!image)
	{
		printf("couldn't load TGA %s\n", fn.c_str());
		exit(1);
	}

	if (LoadWidth() % BYTES_PER_LINE != 0)
	{
		printf("TGA width not multiple of %d\n", BYTES_PER_LINE);
		exit(1);
	}


     /* Load the library */
     hLib = LoadLibrary("inpout32.dll");

     if (hLib == NULL) {
          cout<<"LoadLibrary Failed."<<endl;
          return -1;
     }

     /* get the address of the function */

     inp32fp = (inpfuncPtr) GetProcAddress(hLib, "Inp32");

     if (inp32fp == NULL) {
          cout<<"GetProcAddress for Inp32 Failed."<<endl;
          return -1;
     }


     oup32fp = (oupfuncPtr) GetProcAddress(hLib, "Out32");

     if (oup32fp == NULL) {
          cout<<"GetProcAddress for Oup32 Failed"<<endl;
          return -1;
     }


/*******************************************************/
/** IF WE REACHED HERE, INITIALIZED SUCCESSFUL    ******/
/*******************************************************/


	unsigned char color;
	int	bcount=0;
	unsigned int colorq;

short aa=1;
//FILE * fp=fopen("d.txt","r");
int red   ;
int green ;
int blue   ;
int alpha  ;



                 short addr1;
                 addr1 = addr>>20;
                 addr1 = addr1<<2;
                 addr1= addr1|0xc3;

                 short addr2;
                 addr2 = addr>>16;
                 addr2 = addr2 & 0xF;
                 addr2 = addr2<<2;
                 addr2= addr2|0xc3;
                 
                 short addr3;
                 addr3 = addr>>12;
                 addr3 = addr3 & 0xF;
                 addr3 = addr3<<2;
                 addr3= addr3|0xc3;
                 
                 short addr4;
                 addr4 = addr>>8;
                 addr4 = addr4 & 0xF;
                 addr4 = addr4<<2;
                 addr4= addr4|0xc3;
                 
                 short addr5;
                 addr5 = addr>>4;
                 addr5 = addr5 & 0xF;
                 addr5 = addr5<<2;
                 addr5= addr5|0xc3;
                 
                 short addr6;
                 addr6 = addr & 0xF;
                 addr6 = addr6<<2;
                 addr6= addr6|0xc3;
                 
                 Out32(PPORT_BASE,0xC2);
                 Out32(PPORT_BASE,0xc3);
                 Out32(PPORT_BASE,0xc3);
                 
                 Out32(PPORT_BASE,addr1);
                 Out32(PPORT_BASE,addr1^0x2);

                 Out32(PPORT_BASE,0xc3);
                 Out32(PPORT_BASE,addr2);
                 Out32(PPORT_BASE,addr2^0x2);

                 Out32(PPORT_BASE,0xc3);
                 Out32(PPORT_BASE,addr3);
                 Out32(PPORT_BASE,addr3^0x2);



                 Out32(PPORT_BASE,0xc3);
                 Out32(PPORT_BASE,addr4);
                 Out32(PPORT_BASE,addr4^0x2);


                 Out32(PPORT_BASE,0xc3);
                 Out32(PPORT_BASE,addr5);
                 Out32(PPORT_BASE,addr5^0x2);



                 Out32(PPORT_BASE,0xc3);
                 Out32(PPORT_BASE,addr6);
                 Out32(PPORT_BASE,addr6^0x2);



for (int y=0; y<600; y++){
		for (int x=0; x<800; x++)
		{

           unsigned char r, g, b, a;
			b = *image++;
			g = *image++;
			r = *image++;
			a = *image++;
                        red   = 0;
                        green = 0;
                        blue  = 0;
                        alpha = 0;

                       color = (((r>> 6) & 0x3) << 6) | (((g >> 6) & 0x3) << 3) | (((b>> 6) & 0x3) << 1);
//                      color = (((r >> 5) & 0x7) << 5) | (((g >> 6) & 0x3) << 3) | (((b >> 5) & 0x7) << 0);

/*if (addr == 0 ) {
     switch (NumberOfImage){
     case 4 : {color = 0x80;break; }
     case 3 : {color = 0x40;break; }
     case 2 : {color = 0x20;break; }
     case 1 : {color = 0x10;break; }
     case 0 : {color = 0x01;break; }
     }
     addr += 1 ;
}
*/

			red = r;
                        red = ((red<<0)& 0x000000ff) ;
                        green = g;
                        green =((green<<8)& 0x0000ff00) ;
                        blue = b;
                        blue =((blue<<16)& 0x00ff0000) ;
                        alpha = a;
                        alpha =((alpha<<24) & 0xff000000) ;
                        colorq = red+green+blue;


                        Form2->Canvas->Pixels[x][y] =  colorq ;

			unsigned char clr1 = color>>4;
			clr1 &=0x0f;
                	clr1 = clr1<<2;
			clr1 |= 0xc3;

  			unsigned char clr4 = color & 0x0f;
			clr4 = clr4<<2;
			clr4 |=0xc3;



//                      Out32(PPORT_BASE,0xc3);
//			Out32(PPORT_BASE,0xcf);
			Out32(PPORT_BASE, clr1);
			Out32(PPORT_BASE,clr1^0x2);


//                      Out32(PPORT_BASE,0xc3);
//			Out32(PPORT_BASE,0xcf);
			Out32(PPORT_BASE, clr4);
			Out32(PPORT_BASE,clr4^0x2);

		}

 }
     /* finished - unload library and exit */
     FreeLibrary(hLib);

     //fclose(fp);
    return 0;
}


__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
//Image1->Canvas->Draw()
}
//---------------------------------------------------------------------------


void __fastcall TForm1::Button1Click(TObject *Sender)
{
 /*  AnsiString s = EdFn->Text;
   Form2->Show();
   int j = LoadImagetoPP(s,rgimage->ItemIndex);
*/   
}
//---------------------------------------------------------------------------


void TForm1::ShowImage(AnsiString fn){

     unsigned char *image;
     unsigned int colorq;
        int red   ;
        int green ;
        int blue   ;
        int alpha  ;
        unsigned char r, g, b, a;
// load image
	image = LoadTGA(fn.c_str());
	if (!image){
                printf("couldn't load TGA %s\n", fn.c_str());
		exit(1);}

	if (LoadWidth() % BYTES_PER_LINE != 0){
		printf("TGA width not multiple of %d\n", BYTES_PER_LINE);
		exit(1);
	}

        for (int y=0; y<600; y++){
                Application->ProcessMessages() ;
		for (int x=0; x<800; x++){
                      b = *image++;
                      g = *image++;
                      r = *image++;
                      a = *image++;
                      red = r;
                      green = g;
                      blue = b;
                      alpha = a;
                      Image1->Canvas->Pixels[x][y] =  ((red<<0)& 0x000000ff)+((green<<8)& 0x0000ff00)+((blue<<16)& 0x00ff0000);
                }
        }
//        Image1->Picture->SaveToFile("c:\\deneme.bmp")  ;
}


void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
if (odImage->Execute())
{
  Filename =  odImage->FileName ;
  EdFn->Text = Filename ;
  ShowImage(Filename);
}
}
//---------------------------------------------------------------------------


void __fastcall TForm1::BitBtn2Click(TObject *Sender)
{
  if (ComPort->Connected) {
    ComPort->Close();
    StatusBar1->SimpleText = "Port is closed";

    BitBtn2->Caption = "CONNECT";
    connected = false ;
    Panel4->Visible = false ;
    BitBtn6->Enabled = true ;
    BitBtn7->Enabled = false ;
  }
  else {
//    FMessage->sMessage = "Now Connecting BlueRadios..." ;
    StatusBar1->SimpleText  = "Now Connecting BlueRadios..." ;
//    FMessage->Show();
    ComPort->Open();
    Timer2->Enabled = true ;
    StatusBar1->SimpleText ="Port is opened";
    BitBtn2->Caption = "DISCONNECT " + ComPort->Port;
    BitBtn6->Enabled = true ;
    BitBtn7->Enabled = false ;
  }

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

void __fastcall TForm1::BitBtn3Click(TObject *Sender)
{
        ComPort->ShowSetupDialog();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::BitBtn4Click(TObject *Sender)
{
 if (ComPort->Connected) {
  AnsiString Str = edData->Text;
  if (cbenter->Checked){
          ComPort->WriteStr(Str+"\n");
        }
        else ComPort->WriteStr(Str);
  }
  else {
  StatusBar1->SimpleText = "Port is not open!";
  }

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


void __fastcall TForm1::Button2Click(TObject *Sender)
{
 if (ComPort->Connected) {
  AnsiString Str;
        ComPort->ReadStr(*&Str,10);
        RichEdit->Lines->Add(Str);
}
}
//---------------------------------------------------------------------------


void __fastcall TForm1::BitBtn6Click(TObject *Sender)
{
 if (ComPort->Connected) {
        BitBtn6->Enabled = false ;
        AnsiString s = EdFn->Text;
//       Form2->Show();
       BitBtn7->Enabled = true ;
       int j = LoadImagetoSP(s,rgimage->ItemIndex);
       BitBtn7->Enabled = false ;
       PAUSE = false ;
       GOOOO = true ;
 BitBtn6->Enabled = true ;
 }
  else {
  StatusBar1->SimpleText = "Port is not open!";
  }

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


void __fastcall TForm1::ComPortRxChar(TObject *Sender, int Count)
{
    if (EXIT) return ;
    AnsiString Str;
    ComPort->ReadStr(*&Str,Count);
    RichEdit->Lines->Add(Str);

    if (!connected && Str =="1") {
        connected = true ;
        StatusBar1->SimpleText ="Port is opened";
        BitBtn2->Caption = "DISCONNECT " + ComPort->Port;
        Panel4->Visible = true ;
        Panel6->Enabled = true ;

    }  else {
        if (ComPort->Connected) {
//            ComPort->Close();
            Panel4->Visible = false ;
            Panel6->Enabled = false ;
//           StatusBar1->SimpleText = "Port is closed";
//           BitBtn2->Caption = "CONNECT";
            connected = false ;
            ShowMessage("Password is not true! Please enter correct Password...") ;
        }
    }

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

void __fastcall TForm1::BitBtn7Click(TObject *Sender)
{
  if (!PAUSE) {
        PAUSE = true ;
        GOOOO = false ;
  } else {
        PAUSE = false ;
        GOOOO = true ;
  }

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


void __fastcall TForm1::BitBtn8Click(TObject *Sender)
{
 EXIT = true ;
 Form1->Close();
        
}
//---------------------------------------------------------------------------

void __fastcall TForm1::BitBtn5Click(TObject *Sender)
{
        Form3->ShowModal();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ComboBox1Change(TObject *Sender)
{
     NumberOfImage = ComboBox1->ItemIndex;

     rgimage->Items->Clear();
     switch (NumberOfImage){
     case 4 : {rgimage->Items->Add("Load as First  Image");rgimage->Items->Add("Load as Second Image");rgimage->Items->Add("Load as Third  Image");rgimage->Items->Add("Load as Fourth Image"); rgimage->Items->Add("Load as Fifth  Image");break; }
     case 3 : {rgimage->Items->Add("Load as First  Image");rgimage->Items->Add("Load as Second Image");rgimage->Items->Add("Load as Third  Image");rgimage->Items->Add("Load as Fourth Image");break; }
     case 2 : {rgimage->Items->Add("Load as First  Image");rgimage->Items->Add("Load as Second Image");rgimage->Items->Add("Load as Third  Image");break; }
     case 1 : {rgimage->Items->Add("Load as First  Image");rgimage->Items->Add("Load as Second Image");break; }
     case 0 : {rgimage->Items->Add("Load as First  Image");break; }
     }

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

void __fastcall TForm1::Button4Click(TObject *Sender)
{
/*     switch (NumberOfImage){
     case 4 : {WriteData(0,0x80);break; }
     case 3 : {WriteData(0,0x40);break; }
     case 2 : {WriteData(0,0x20);break; }
     case 1 : {WriteData(0,0x10);break; }
     case 0 : {WriteData(0,0x01);break; }
     }
 */
}
//---------------------------------------------------------------------------

void __fastcall TForm1::mePasswordChange(TObject *Sender)
{
 pw = mePassword->Text ;        
}
//---------------------------------------------------------------------------


/*
*/

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

void __fastcall TForm1::Timer2Timer(TObject *Sender)
{
      Timer2->Enabled = false ;
//      FMessage->Close();
      if (ComPort->Connected) {
                ComPort->WriteStr(pw);
 //               RichEdit->Lines->Add(pw);
       }

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


