Beispiel #1
0
bool ASICCD::ISNewNumber (const char *dev, const char *name, double values[], char *names[], int n)
{
    ASI_ERROR_CODE errCode = ASI_SUCCESS;

     if(!strcmp(dev,getDeviceName()))
     {
         if (!strcmp(name, ControlNP.name))
         {
            double oldValues[ControlNP.nnp];
            for (int i=0; i < ControlNP.nnp; i++)
                oldValues[i] = ControlN[i].value;

            IUUpdateNumber(&ControlNP, values, names, n);

            for (int i=0; i < ControlNP.nnp; i++)
            {
                ASI_BOOL nAuto = *((ASI_BOOL *) ControlN[i].aux1);
                ASI_CONTROL_TYPE nType = *((ASI_CONTROL_TYPE *) ControlN[i].aux0);

                // If value didn't change or if USB bandwidth control is to change, then only continue if ExposureRequest < 250 ms
                if (ControlN[i].value == oldValues[i] || (nType == ASI_BANDWIDTHOVERLOAD && ExposureRequest > 0.25))
                    continue;

                if ( (errCode = ASISetControlValue(m_camInfo->CameraID, nType, ControlN[i].value, ASI_FALSE)) != ASI_SUCCESS)
                {
                    DEBUGF(INDI::Logger::DBG_ERROR, "ASISetControlValue (%s=%g) error (%d)", ControlN[i].name, ControlN[i].value, errCode);
                    ControlNP.s = IPS_ALERT;
                    for (int i=0; i < ControlNP.nnp; i++)
                        ControlN[i].value = oldValues[i];
                    IDSetNumber(&ControlNP, NULL);
                    return false;
                }

                // If it was set to nAuto value to turn it off
                if (nAuto)
                {
                    for (int j=0; j < ControlSP.nsp; j++)
                    {
                        ASI_CONTROL_TYPE swType = *((ASI_CONTROL_TYPE *) ControlS[j].aux);

                        if (swType == nType)
                        {
                            ControlS[j].s = ISS_OFF;
                            break;
                        }
                    }

                    IDSetSwitch(&ControlSP, NULL);
                 }
            }

            ControlNP.s = IPS_OK;
            IDSetNumber(&ControlNP, NULL);
            return true;
         }

     }

    return INDI::CCD::ISNewNumber(dev,name,values,names,n);
}
Beispiel #2
0
bool ASICCD::activateCooler(bool enable)
{
    bool rc = (ASISetControlValue(m_camInfo->CameraID, ASI_COOLER_ON, enable ? ASI_TRUE : ASI_FALSE, ASI_FALSE) == ASI_SUCCESS);
    if (rc == false)
        CoolerSP.s = IPS_ALERT;
    else
    {
        CoolerS[0].s = enable ? ISS_ON : ISS_OFF;
        CoolerS[1].s = enable ? ISS_OFF: ISS_ON;
        CoolerSP.s = IPS_BUSY;
    }
    IDSetSwitch(&CoolerSP, NULL);

    return rc;
}
Beispiel #3
0
bool ASICCD::StartExposure(float duration)
{
  ASI_ERROR_CODE errCode= ASI_SUCCESS;

  if (duration < MIN_DURATION)
  {
    DEBUGF(INDI::Logger::DBG_WARNING, "Exposure shorter than minimum duration %g s requested. Setting exposure time to %g s.", duration, MIN_DURATION);
    duration = MIN_DURATION;
  }

  if (PrimaryCCD.getFrameType() == CCDChip::BIAS_FRAME)
  {
    duration = MIN_DURATION;
    DEBUGF(INDI::Logger::DBG_SESSION, "Bias Frame (s) : %g", MIN_DURATION);
  }

  PrimaryCCD.setExposureDuration(duration);
  ExposureRequest = duration;
  ASISetControlValue(m_camInfo->CameraID, ASI_EXPOSURE, duration*1000*1000, ASI_FALSE);

  // Try exposure for 3 times
  for (int i=0; i < 3; i++)
  {
    if ( (errCode = ASIStartExposure(m_camInfo->CameraID, (PrimaryCCD.getFrameType() == CCDChip::DARK_FRAME) ? ASI_TRUE : ASI_FALSE )) != ASI_SUCCESS)
    {
          DEBUGF(INDI::Logger::DBG_ERROR, "ASIStartExposure error (%d)", errCode);
          // Wait 100ms before trying again
          usleep(100000);
          continue;
    }
  }

  if (errCode != ASI_SUCCESS)
      return false;

  gettimeofday(&ExpStart, NULL);
  if (ExposureRequest > VERBOSE_EXPOSURE)
    DEBUGF(INDI::Logger::DBG_SESSION, "Taking a %g seconds frame...", ExposureRequest);

  InExposure = true;

  //updateControls();

  return true;
}
Beispiel #4
0
bool ASICCD::Connect()
{

  DEBUGF(INDI::Logger::DBG_DEBUG,  "Attempting to open %s...", name);

  sim = isSimulation();

  ASI_ERROR_CODE errCode = ASI_SUCCESS;

  if (sim == false)
        errCode = ASIOpenCamera(m_camInfo->CameraID);

  if (errCode != ASI_SUCCESS)
  {
      DEBUGF(INDI::Logger::DBG_ERROR, "Error connecting to the CCD (%d)", errCode);
      return false;
  }

  if (sim == false)
        errCode = ASIInitCamera(m_camInfo->CameraID);

  if (errCode != ASI_SUCCESS)
  {
      DEBUGF(INDI::Logger::DBG_ERROR, "Error Initializing the CCD (%d)", errCode);
      return false;
  }

  TemperatureUpdateCounter = 0;

#if !defined(OSX_EMBEDED_MODE) && !defined(__CYGWIN__)
  pthread_create( &primary_thread, NULL, &streamVideoHelper, this);
#endif
  DEBUG(INDI::Logger::DBG_SESSION,  "Setting intital bandwidth to AUTO on connection.");
  if ( (errCode = ASISetControlValue(m_camInfo->CameraID, ASI_BANDWIDTHOVERLOAD, 40, ASI_FALSE)) != ASI_SUCCESS)
  {
      DEBUGF(INDI::Logger::DBG_ERROR, "Failed to set initial bandwidth: error (%d)", errCode);
  }
  /* Success! */
  DEBUG(INDI::Logger::DBG_SESSION,  "CCD is online. Retrieving basic data.");

  return true;
}
Beispiel #5
0
int ASICCD::SetTemperature(double temperature)
{
    // If there difference, for example, is less than 0.1 degrees, let's immediately return OK.
    if (fabs(temperature - TemperatureN[0].value) < TEMP_THRESHOLD)
        return 1;

    if (activateCooler(true) == false)
    {
        DEBUG(INDI::Logger::DBG_ERROR, "Failed to activate cooler!");
        return -1;
    }

    if (ASISetControlValue(m_camInfo->CameraID, ASI_TARGET_TEMP, temperature, ASI_TRUE) != ASI_SUCCESS)
    {
        DEBUG(INDI::Logger::DBG_ERROR, "Failed to set temperature!");
        return -1;
    }

    // Otherwise, we set the temperature request and we update the status in TimerHit() function.
    TemperatureRequest = temperature;
    DEBUGF(INDI::Logger::DBG_SESSION, "Setting CCD temperature to %+06.2f C", temperature);
    return 0;
}
Beispiel #6
0
bool ASICCD::StartExposure(float duration)
{
  ASI_ERROR_CODE errCode= ASI_SUCCESS;

  PrimaryCCD.setExposureDuration(duration);
  ExposureRequest = duration;

  DEBUGF(INDI::Logger::DBG_DEBUG, "StartExposure->setexp : %.3fs", duration);
  ASISetControlValue(m_camInfo->CameraID, ASI_EXPOSURE, duration*1000*1000, ASI_FALSE);

  // Try exposure for 3 times
  for (int i=0; i < 3; i++)
  {
    if ( (errCode = ASIStartExposure(m_camInfo->CameraID, (PrimaryCCD.getFrameType() == CCDChip::DARK_FRAME) ? ASI_TRUE : ASI_FALSE )) != ASI_SUCCESS)
    {
          DEBUGF(INDI::Logger::DBG_ERROR, "ASIStartExposure error (%d)", errCode);
          // Wait 100ms before trying again
          usleep(100000);
          continue;
    }
    break;
  }

  if (errCode != ASI_SUCCESS)
      return false;

  gettimeofday(&ExpStart, NULL);
  if (ExposureRequest > VERBOSE_EXPOSURE)
    DEBUGF(INDI::Logger::DBG_SESSION, "Taking a %g seconds frame...", ExposureRequest);

  InExposure = true;

  //updateControls();

  return true;
}
Beispiel #7
0
static void set_cap(int idx, int val) {
  CHECK(ASISetControlValue(cidx, idx, val, 0));
}
Beispiel #8
0
bool ASICCD::ISNewSwitch (const char *dev, const char *name, ISState *states, char *names[], int n)
{
    ASI_ERROR_CODE errCode = ASI_SUCCESS;

    if(!strcmp(dev,getDeviceName()))
    {
        if (!strcmp(name, ControlSP.name))
        {
           IUUpdateSwitch(&ControlSP, states, names, n);

           for (int i=0; i < ControlSP.nsp; i++)
           {
               ASI_CONTROL_TYPE swType = *((ASI_CONTROL_TYPE *) ControlS[i].aux);
               ASI_BOOL swAuto = (ControlS[i].s == ISS_ON) ? ASI_TRUE : ASI_FALSE;

               for (int j=0; j < ControlNP.nnp; j++)
               {
                   ASI_CONTROL_TYPE nType = *((ASI_CONTROL_TYPE *) ControlN[j].aux0);

                   if (swType == nType)
                   {
                        DEBUGF(INDI::Logger::DBG_DEBUG, "ISNewSwitch->SetControlValue %d %.2f", nType, ControlN[j].value);
                       if ( (errCode = ASISetControlValue(m_camInfo->CameraID, nType, ControlN[j].value, swAuto )) != ASI_SUCCESS)
                       {
                           DEBUGF(INDI::Logger::DBG_ERROR, "ASISetControlValue (%s=%g) error (%d)", ControlN[j].name, ControlN[j].value, errCode);
                           ControlNP.s = IPS_ALERT;
                           ControlSP.s = IPS_ALERT;
                           IDSetNumber(&ControlNP, NULL);
                           IDSetSwitch(&ControlSP, NULL);
                           return false;
                       }

                       *((ASI_BOOL *) ControlN[j].aux1) = swAuto;
                       break;

                   }
               }
           }

           ControlSP.s = IPS_OK;
           IDSetSwitch(&ControlSP, NULL);
           return true;

        }

        /* Cooler */
       if (!strcmp (name, CoolerSP.name))
       {
         if (IUUpdateSwitch(&CoolerSP, states, names, n) < 0)
             return false;

         bool rc=false;

         if (CoolerS[0].s == ISS_ON)
           activateCooler(true);
         else
           activateCooler(false);

         return true;
       }

        if (!strcmp(name, VideoFormatSP.name))
        {
            #if !defined(OSX_EMBEDED_MODE) && !defined(__CYGWIN__)
            if (streamer->isBusy())
            {
                VideoFormatSP.s = IPS_ALERT;
                DEBUG(INDI::Logger::DBG_ERROR, "Cannot change format while streaming/recording.");
                IDSetSwitch(&VideoFormatSP, NULL);
                return true;
            }
            #endif

            IUUpdateSwitch(&VideoFormatSP, states, names, n);

            ASI_IMG_TYPE type = getImageType();

            switch (type)
            {
                case ASI_IMG_RAW16:
                    PrimaryCCD.setBPP(16);
                    break;

                default:
                   PrimaryCCD.setBPP(8);
                    break;
            }

            UpdateCCDFrame(PrimaryCCD.getSubX(), PrimaryCCD.getSubY(), PrimaryCCD.getSubW(), PrimaryCCD.getSubH());

            updateRecorderFormat();

            VideoFormatSP.s = IPS_OK;
            IDSetSwitch(&VideoFormatSP, NULL);
            return true;
        }

    }

   return INDI::CCD::ISNewSwitch(dev,name,states,names,n);
}
Beispiel #9
0
bool ASICCD::setupParams()
{
    ASI_ERROR_CODE errCode = ASI_SUCCESS;
    int piNumberOfControls = 0;

    errCode = ASIGetNumOfControls(m_camInfo->CameraID, &piNumberOfControls);

    if (errCode != ASI_SUCCESS)
        DEBUGF(INDI::Logger::DBG_DEBUG, "ASIGetNumOfControls error (%d)", errCode);

  if (piNumberOfControls > 0)
  {
      if (ControlNP.nnp > 0)
          free(ControlN);

      if (ControlSP.nsp > 0)
          free(ControlS);

      createControls(piNumberOfControls);
  }

  // Set minimum ASI_BANDWIDTHOVERLOAD on ARM
  #ifdef LOW_USB_BANDWIDTH
  ASI_CONTROL_CAPS pCtrlCaps;
  for(int j = 0; j < piNumberOfControls; j++)
  {
      ASIGetControlCaps(m_camInfo->CameraID, j, &pCtrlCaps);
      if (pCtrlCaps.ControlType == ASI_BANDWIDTHOVERLOAD)
      {
          DEBUGF(INDI::Logger::DBG_DEBUG, "setupParams->set USB %d", pCtrlCaps.MinValue);
          ASISetControlValue(m_camInfo->CameraID, ASI_BANDWIDTHOVERLOAD, pCtrlCaps.MinValue, ASI_FALSE);
          break;
      }
  }
  #endif

  // Get Image Format
  int w=0, h=0, bin=0;
  ASI_IMG_TYPE imgType;
  ASIGetROIFormat(m_camInfo->CameraID, &w, &h, &bin, &imgType);

  DEBUGF(INDI::Logger::DBG_DEBUG, "CCD ID: %d Width: %d Height: %d Binning: %dx%d Image Type: %d", m_camInfo->CameraID, w, h, bin, bin, imgType);

  // Get video format and bit depth
  int bit_depth = 8;

  switch (imgType)
  {
    case ASI_IMG_RAW16:
      bit_depth = 16;

    default:
      break;
  }

  if (VideoFormatSP.nsp > 0)
      free(VideoFormatS);

  VideoFormatS = NULL;
  int nVideoFormats = 0;

  for (int i=0; i < 8; i++)
  {
        if (m_camInfo->SupportedVideoFormat[i] == ASI_IMG_END)
            break;

        nVideoFormats++;
        VideoFormatS = VideoFormatS == NULL ? (ISwitch *) malloc(sizeof(ISwitch)) : (ISwitch *) realloc(VideoFormatS, nVideoFormats * sizeof(ISwitch));

        ISwitch *oneVF = VideoFormatS + (nVideoFormats-1);

        switch (m_camInfo->SupportedVideoFormat[i])
        {
            case ASI_IMG_RAW8:
                IUFillSwitch(oneVF, "ASI_IMG_RAW8", "Raw 8 bit", (imgType == ASI_IMG_RAW8) ? ISS_ON : ISS_OFF);
                DEBUG(INDI::Logger::DBG_DEBUG, "Supported Video Format: ASI_IMG_RAW8");
                break;

            case ASI_IMG_RGB24:
                IUFillSwitch(oneVF, "ASI_IMG_RGB24", "RGB 24", (imgType == ASI_IMG_RGB24) ? ISS_ON : ISS_OFF);
                DEBUG(INDI::Logger::DBG_DEBUG, "Supported Video Format: ASI_IMG_RGB24");
                break;

            case ASI_IMG_RAW16:
                IUFillSwitch(oneVF, "ASI_IMG_RAW16", "Raw 16 bit", (imgType == ASI_IMG_RAW16) ? ISS_ON : ISS_OFF);
                DEBUG(INDI::Logger::DBG_DEBUG, "Supported Video Format: ASI_IMG_RAW16");
            break;

            case ASI_IMG_Y8:
                IUFillSwitch(oneVF, "ASI_IMG_Y8", "Luma", (imgType == ASI_IMG_Y8) ? ISS_ON : ISS_OFF);
                DEBUG(INDI::Logger::DBG_DEBUG, "Supported Video Format: ASI_IMG_Y8");
                break;

            default:
                DEBUGF(INDI::Logger::DBG_DEBUG, "Unknown video format (%d)", m_camInfo->SupportedVideoFormat[i]);
                break;
        }

        oneVF->aux = (void *) &m_camInfo->SupportedVideoFormat[i];
  }

  VideoFormatSP.nsp = nVideoFormats;
  VideoFormatSP.sp  = VideoFormatS;

  float x_pixel_size, y_pixel_size;
  int x_1, y_1, x_2, y_2;

  x_pixel_size = m_camInfo->PixelSize;
  y_pixel_size = m_camInfo->PixelSize;

   x_1 = y_1 = 0;
   x_2 = m_camInfo->MaxWidth;
   y_2 = m_camInfo->MaxHeight;

  SetCCDParams(x_2 - x_1, y_2 - y_1, bit_depth, x_pixel_size, y_pixel_size);

  // Let's calculate required buffer
  int nbuf;
  nbuf = PrimaryCCD.getXRes() * PrimaryCCD.getYRes() * PrimaryCCD.getBPP() / 8;    //  this is pixel cameraCount
  //nbuf += 512;    //  leave a little extra at the end
  PrimaryCCD.setFrameBufferSize(nbuf);

  //if (HasCooler())
  //{
      long pValue = 0;
      ASI_BOOL isAuto= ASI_FALSE;

      if ( (errCode = ASIGetControlValue(m_camInfo->CameraID, ASI_TEMPERATURE, &pValue, &isAuto)) != ASI_SUCCESS)
          DEBUGF(INDI::Logger::DBG_DEBUG, "ASIGetControlValue temperature error (%d)", errCode);

      TemperatureN[0].value = pValue / 10.0;

      DEBUGF(INDI::Logger::DBG_SESSION,  "The CCD Temperature is %f", TemperatureN[0].value);
      IDSetNumber(&TemperatureNP, NULL);
  //}

  ASIStopVideoCapture(m_camInfo->CameraID);

  DEBUGF(INDI::Logger::DBG_DEBUG, "setupParams ASISetROIFormat (%dx%d,  bin %d, type %d)", m_camInfo->MaxWidth, m_camInfo->MaxHeight, 1, imgType);
  ASISetROIFormat(m_camInfo->CameraID, m_camInfo->MaxWidth, m_camInfo->MaxHeight, 1, imgType);

  #if !defined(OSX_EMBEDED_MODE) && !defined(__CYGWIN__)
  updateRecorderFormat();
  streamer->setRecorderSize(w,h);
  #endif

  return true;

}
Beispiel #10
0
int  main()
{
	int width;
	char* bayer[] = {"RG","BG","GR","GB"};


	int height;
	int i;
	char c;
	bool bresult;

	int time1,time2;
	int count=0;

	char buf[128]={0};

	int CamNum=0;
	
	///long exposure, exp_min, exp_max, exp_step, exp_flag, exp_default;
	//long gain, gain_min, gain_max,gain_step, gain_flag, gain_default;

	IplImage *pRgb;


	int numDevices = ASIGetNumOfConnectedCameras();
	if(numDevices <= 0)
	{
		printf("no camera connected, press any key to exit\n");
		getchar();
		return -1;
	}
	else
		printf("attached cameras:\n");

	ASI_CAMERA_INFO ASICameraInfo;
	

	for(i = 0; i < numDevices; i++)
	{
		ASIGetCameraProperty(&ASICameraInfo, i);	
		printf("%d %s\n",i, ASICameraInfo.Name);
	}

	printf("\nselect one to privew\n");
	scanf("%d", &CamNum);



	if(ASIOpenCamera(CamNum) != ASI_SUCCESS)
	{
		printf("OpenCamera error,are you root?,press any key to exit\n");
		getchar();
		return -1;
	}
	ASIInitCamera(CamNum);


	printf("%s information\n",ASICameraInfo.Name);
	int iMaxWidth, iMaxHeight;

	iMaxWidth = ASICameraInfo.MaxWidth;
	iMaxHeight =  ASICameraInfo.MaxHeight;
	printf("resolution:%dX%d\n", iMaxWidth, iMaxHeight);

	if(ASICameraInfo.IsColorCam)
		printf("Color Camera: bayer pattern:%s\n",bayer[ASICameraInfo.BayerPattern]);	
	else
		printf("Mono camera\n");

	ASI_CONTROL_CAPS ControlCaps;
	int iNumOfCtrl = 0;
	ASIGetNumOfControls(CamNum, &iNumOfCtrl);
	for( i = 0; i < iNumOfCtrl; i++)
	{
		ASIGetControlCaps(CamNum, i, &ControlCaps);
		printf("%s\n", ControlCaps.Name);
	}	


	long ltemp = 0;
	ASI_BOOL bAuto = ASI_FALSE;
	ASIGetControlValue(CamNum, ASI_TEMPERATURE, &ltemp, &bAuto);
	printf("sensor temperature:%02f\n", (float)ltemp/10.0);



	printf("\nPlease input the <width height bin image_type> with one space, ie. 640 480 2 0. use max resolution if input is 0. Press ESC when video window is focused to quit capture\n");
	int bin = 1, Image_type;
	do{
	//	printf("Set format error, please check the width and height\n ASI120's data size(width*height) must be integer multiple of 1024\n");
		printf("Please input the width and height again£¬ie. 640 480\n");
		scanf("%d %d %d %d", &width, &height, &bin, &Image_type);
		if(!width || !height)
		{
			width = iMaxWidth;
			height = iMaxHeight;
		}
	}while(ASI_SUCCESS !=  ASISetROIFormat(CamNum, width, height, bin, (ASI_IMG_TYPE)Image_type));		
	
	printf("\nset image format %d %d %d %d success, start privew, press ESC to stop \n", width, height, bin, Image_type);

	ASIGetROIFormat(CamNum, &width, &height, &bin, (ASI_IMG_TYPE*)&Image_type);
	int displayWid = 640, displayHei = 480;
	if(Image_type == ASI_IMG_RAW16)
		pRgb=cvCreateImage(cvSize(displayWid, displayHei), IPL_DEPTH_16U, 1);
	else if(Image_type == ASI_IMG_RGB24)
		pRgb=cvCreateImage(cvSize(displayWid, displayHei), IPL_DEPTH_8U, 3);
	else
		pRgb=cvCreateImage(cvSize(displayWid, displayHei), IPL_DEPTH_8U, 1);

	long imgSize = width*height*(1 + (Image_type==ASI_IMG_RAW16));
	long displaySize = displayWid*displayHei*(1 + (Image_type==ASI_IMG_RAW16));
	unsigned char* imgBuf = new unsigned char[imgSize];

	
	ASISetControlValue(CamNum, ASI_GAIN, 0, ASI_FALSE);
	
	ASISetControlValue(CamNum, ASI_BANDWIDTHOVERLOAD, 45, ASI_FALSE);//

	unsigned int expms = 100;
	printf("\ninput exposure time(ms)\n");
	scanf("%d", &expms);
	

	bDisplay = 1;
#ifdef _LIN
	pthread_t thread_display;
	pthread_create(&thread_display, NULL, Display, (void*)pRgb);
#elif defined _WINDOWS
	HANDLE thread_setgainexp;
	thread_setgainexp = (HANDLE)_beginthread(Display,  NULL, (void*)pRgb);
#endif

	time1 = GetTickCount();
	int iStrLen = 0, iTextX = 40, iTextY = 60;
	void* retval;
//	int time0, iWaitMs = -1;
//	bool bGetImg;
	ASI_EXPOSURE_STATUS status;
	int iDropped = 0;
	while(bMain)
	{
		ASISetControlValue(CamNum, ASI_EXPOSURE, expms*1000, ASI_FALSE);//

		ASIStartExposure(CamNum, ASI_FALSE);
		usleep(10000);//10ms
		status = ASI_EXP_WORKING;
		while(status == ASI_EXP_WORKING)
		{
	
			ASIGetExpStatus(CamNum, &status);
				
		}
		if(status == ASI_EXP_SUCCESS)
		{
			ASIGetDataAfterExp(CamNum, imgBuf, imgSize);
			if(Image_type==ASI_IMG_RAW16)
			{
				unsigned short *pCv16bit = (unsigned short *)(pRgb->imageData);
				unsigned short *pImg16bit = (unsigned short *)imgBuf;
				for(int y = 0; y < displayHei; y++)
				{
					memcpy(pCv16bit, pImg16bit, displayWid*2);
					pCv16bit+=displayWid;
					pImg16bit+=width;
				}
			}
			else{
				unsigned char *pCv8bit = (unsigned char *)pRgb->imageData;
				unsigned char *pImg8bit = (unsigned char *)imgBuf;
				for(int y = 0; y < displayHei; y++)
				{
					memcpy(pCv8bit, pImg8bit, displayWid);
					pCv8bit+=displayWid;
					pImg8bit+=width;
				}
			}
					
		}
	//		getImageAfterExp((unsigned char*)pRgb->imageData, pRgb->imageSize);
		usleep(500000);

//		bGetImg = getImageData((unsigned char*)pRgb->imageData, pRgb->imageSize, iWaitMs);
		time2 = GetTickCount();
//		printf("waitMs%d, deltaMs%d, %d\n", iWaitMs, time2 - time0, bGetImg);
		count++;
		
		if(time2-time1 > 1000 )
		{
			ASIGetDroppedFrames(CamNum, &iDropped);
			sprintf(buf, "fps:%d dropped frames:%lu ImageType:%d",count, iDropped, Image_type);

			count = 0;
			time1=GetTickCount();	
			printf(buf);
			printf("\n");

		}
		if(Image_type != ASI_IMG_RGB24 && Image_type != ASI_IMG_RAW16)
		{
			iStrLen = strlen(buf);
			CvRect rect = cvRect(iTextX, iTextY - 15, iStrLen* 11, 20);
			cvSetImageROI(pRgb , rect);
			cvSet(pRgb, CV_RGB(180, 180, 180)); 
			cvResetImageROI(pRgb);
		}
		cvText(pRgb, buf, iTextX,iTextY );

		if(bChangeFormat)
		{
			bChangeFormat = 0;
			bDisplay = false;
			pthread_join(thread_display, &retval);
			cvReleaseImage(&pRgb);
	//		stopCapture();
			
			switch(change)
			{
				 case change_imagetype:
					Image_type++;
					if(Image_type > 3)
						Image_type = 0;
					
					break;
				case change_bin:
					if(bin == 1)
					{
						bin = 2;
						width/=2;
						height/=2;
					}
					else 
					{
						bin = 1;
						width*=2;
						height*=2;
					}
					break;
				case change_size_smaller:
					if(width > 320 && height > 240)
					{
						width/= 2;
						height/= 2;
					}
					break;
				
				case change_size_bigger:
				
					if(width*2*bin <= iMaxWidth && height*2*bin <= iMaxHeight)
					{
						width*= 2;
						height*= 2;
					}
					break;
			}
			ASISetROIFormat(CamNum, width, height, bin, (ASI_IMG_TYPE)Image_type);
			ASIGetROIFormat(CamNum, &width, &height, &bin, (ASI_IMG_TYPE*)&Image_type);
			if(Image_type == ASI_IMG_RAW16)
				pRgb=cvCreateImage(cvSize(width,height), IPL_DEPTH_16U, 1);
			else if(Image_type == ASI_IMG_RGB24)
				pRgb=cvCreateImage(cvSize(width,height), IPL_DEPTH_8U, 3);
			else
				pRgb=cvCreateImage(cvSize(width,height), IPL_DEPTH_8U, 1);
			bDisplay = 1;
			pthread_create(&thread_display, NULL, Display, (void*)pRgb);
	//		startCapture(); //start privew
		}
	}
END:
	
	if(bDisplay)
	{
		bDisplay = 0;
#ifdef _LIN
   		pthread_join(thread_display, &retval);
#elif defined _WINDOWS
		Sleep(50);
#endif
	}
	
	//stopCapture();
	//closeCamera();
	ASIStopExposure(CamNum);
	ASICloseCamera(CamNum);
	cvReleaseImage(&pRgb);
	if(imgBuf)
		delete[] imgBuf;
	printf("main function over\n");
	return 1;
}