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

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include "NET_ICube_API.h"
#include <stdio.h>

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

int g_iCurrentCam = 0;
bool bSaveRaw=false;
FILE *fp;
//---------------------------------------------------------------------------

long CALLBACK MyCallbackFunc(BYTE * pBuffer, long lBufferSize,PVOID pContext)
{
   if(pBuffer==NULL)
        return -1;

    if(bSaveRaw)
    {
        bSaveRaw=false;
        fp = fopen("ICube.raw", "wb");
        fwrite(pBuffer, sizeof(BYTE), lBufferSize , fp);
        fclose(fp);
    }

return 42;
}
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormCreate(TObject *Sender)
{
#ifndef LIBFILE
	if ( LoadICubeApi("ICubeSDK.dll") != IC_SUCCESS )
	{
		ShowMessage("Can not find ICubeSDK.dll");
		exit(1);
		return;
	}
#endif
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
    if(ICubeSDK_IsOpen(g_iCurrentCam))
    {
        if(ICubeSDK_Close(g_iCurrentCam)!= IC_SUCCESS )
            ShowMessage("Error ICubeSDK_Close ");
    }
#ifndef LIBFILE
	UnloadICubeApi();
#endif
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ButtonInitClick(TObject *Sender)
{
int numDevices = 0;
int nStat;

	numDevices=ICubeSDK_Init ();
	if(numDevices<1)
	{
		ShowMessage("No Devices!");
		return;
	}
	char CamName[NETCAM_NAME_LENGTH];

	ComboBoxCam->Clear();
	for(int i=0; i<numDevices; i++)
	{
		nStat = ICubeSDK_GetName(i, CamName);
		if ( nStat != IC_SUCCESS )
		{
			ShowMessage("Error getting model name ");
			return;
		}
		ComboBoxCam->Text= CamName;

	}
	g_iCurrentCam = 0;
    Timer1->Enabled=true;

}
//---------------------------------------------------------------------------
void __fastcall TForm1::ButtonOpenClick(TObject *Sender)
{
    int stat = ICubeSDK_Open(g_iCurrentCam);
    if ( stat != IC_SUCCESS )
        ShowMessage("Error Open ");
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ButtonCloseClick(TObject *Sender)
{
    int stat = ICubeSDK_Close(g_iCurrentCam);
    if ( stat != IC_SUCCESS )
        ShowMessage("Error Close ");
}
//---------------------------------------------------------------------------

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

   if( ICubeSDK_SetMode(g_iCurrentCam,MODE_640x480)!= IC_SUCCESS )
        ShowMessage("Error ICubeSDK_SetMode ");

    if(ICubeSDK_SetCallback(g_iCurrentCam,CALLBACK_RGB,MyCallbackFunc,NULL)!= IC_SUCCESS )
        ShowMessage("Error ICubeSDK_SetCallback ");

    if( ICubeSDK_Start(g_iCurrentCam,NULL,true,true)!= IC_SUCCESS )
        ShowMessage("Error ICubeSDK_Start ");
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ButtonStopClick(TObject *Sender)
{
    int stat = ICubeSDK_Stop(g_iCurrentCam);
    if ( stat != IC_SUCCESS )
        ShowMessage("Error Stop ");
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ButtonSaveRawClick(TObject *Sender)
{
    bSaveRaw=true;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
    if(ICubeSDK_IsStarted(g_iCurrentCam))
    {
        int nBadFrames,nGoodFrames;
        ICubeSDK_GetBrokenFrames(g_iCurrentCam,&nBadFrames);
        ICubeSDK_GetGoodFrames(g_iCurrentCam,&nGoodFrames);
        StatusBar1->Panels[0].Items[0]->Text = "Goodframes: "+AnsiString(nGoodFrames);
        StatusBar1->Panels[0].Items[1]->Text = "Brokenframes: "+AnsiString(nBadFrames);
    }
}
//---------------------------------------------------------------------------

