/*
	main.cpp - This file is a test of the CSBIGCam and CSBIGImage classes.

	1. This software (c)2004 Santa Barbara Instrument Group.
	2. This free software is provided as an example of how 
	    to communicate with SBIG cameras.  It is provided AS-IS
	    without any guarantees by SBIG of suitability for a 
	    particular purpose and without any guarantee to be 
	    bug-free.  If you use it you agree to these terms and
	    agree to do so at your own risk.
 3.  Any distribution of this source code to include these terms.

	Revision History
	Date	 - Modification
	
  2006/08/19  - tested on Suse 10.1 (kernel 2.6.116.13-4) by Jan Soldan. OK.
	2004/04//26 - Initial release - Matt Longmire (SBIG)

*/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>

#include "lpardrv.h"

using namespace std;

#include "csbigcam.h"
#include "csbigimg.h"
#include <usb.h>

#define LINE_LEN 	80
#define LOOP_MAX 	1

int main(/*int argc, char *argv[]*/)
{
	PAR_ERROR 		    err;
	SBIG_FILE_ERROR 	ferr;
	CSBIGImg 			    *pImg	= 0;
	CSBIGCam 			    *pCam	= 0;
	char					    fileName[256];
	
	// Subframe definition:
	int 							nLeft   = 0;
	int								nTop    = 0;
  int								nWidth  = 500;
  int								nHeight = 500;

	for(int i = 1; i <= LOOP_MAX; i++){
				cout << "--------------------------------------" << endl;
				cout << "Grab image number : " << i << endl;

				pImg = new CSBIGImg;
		
				do{ 
								pCam = new CSBIGCam(DEV_USB1);
								if((err = pCam->GetError()) != CE_NO_ERROR)	break;

								// Set subframe:
								pCam->SetSubFrame(nLeft, nTop, nWidth, nHeight);

								if((err = pCam->EstablishLink()) != CE_NO_ERROR) break;
								cout << "Link Established to Camera Type: " << pCam->GetCameraTypeString() << endl;

								cout << "Taking light image on USB..." << endl;
								if((err = pCam->GrabImage(pImg, SBDF_LIGHT_ONLY)) != CE_NO_ERROR)	break;
				
								cout << "Saving  image..." << endl;
								sprintf(fileName, "img%d.fits", i);
								if((ferr = pImg->SaveImage(fileName, SBIF_COMPRESSED)) != SBFE_NO_ERROR) break;		
								//if((ferr = pImg->SaveImage(fileName, SBIF_FITS)) != SBFE_NO_ERROR) break;		

								if((err = pCam->CloseDevice()) != CE_NO_ERROR)	break;
								if((err = pCam->CloseDriver()) != CE_NO_ERROR) 	break;		

				}while (0);

				if(err != CE_NO_ERROR){
						cout << "Camera Error: " << pCam->GetErrorString(err) << endl;
				}else{
						cout << "SUCCESS" << endl;
				}

				delete pImg;
				delete pCam;

				if(err != CE_NO_ERROR) break;
	} 
	return(EXIT_SUCCESS);
}

