• Skip to content
  • Skip to link menu
KDE 3.5 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

kstars

Camera_Example.cpp

Go to the documentation of this file.
00001 // Example source code for implementing the CCameraIO object
00002 
00003 #include "windows.h"
00004 #include "stdio.h"
00005 
00006 #include "CameraIO.h"
00007 #include "CameraIO_ISA_9x.h"
00008 #include "CameraIO_PPI_9x.h"
00009 
00010 #include "CameraIO_ISA_NT.h"
00011 #include "CameraIO_PPI_NT.h"
00012 
00013 #include "CameraIO_PCI.h"
00014 
00015 
00016 // Error codes returned from config_load
00017 const long CCD_OPEN_NOERR = 0;      // No error detected
00018 const long CCD_OPEN_CFGNAME = 1;    // No config file specified
00019 const long CCD_OPEN_CFGDATA = 2;    // Config missing or missing required data
00020 const long CCD_OPEN_LOOPTST = 3;    // Loopback test failed, no camera found
00021 const long CCD_OPEN_ALLOC = 4;      // Memory alloc failed - system error
00022 const long CCD_OPEN_NTIO = 5;       // NT I/O driver not present
00023 
00024 CCameraIO* cam;     // the Camera interface object
00025 
00026 // Function declarations for this file
00027 int InitCam( char* cfgname );
00028 long config_load( char* cfgname, short BaseAddress, short RegOffset );
00029 bool CfgGet ( FILE* inifp,
00030            char* inisect,
00031            char* iniparm,
00032            char* retbuff,
00033            short bufflen,
00034            short* parmlen);
00035 
00036 unsigned short hextoi(char* instr);
00037 void trimstr(char* s);
00038 
00039 // Initializes the CameraIO object from an INI file specified by cfgname
00040 int InitCam( char* cfgname )
00041 {
00042     long ret = config_load( cfgname, -1, -1 );
00043     if ( ret == 0 )
00044     {
00045         // We can now access the cam objects members
00046         cam->Flush();   // Start the camera flushing
00047         return 0;
00048     }
00049     else
00050     {
00051         switch ( ret )
00052         {
00053         case CCD_OPEN_CFGNAME:
00054             // "No config file specified."
00055             break;
00056         case CCD_OPEN_CFGDATA:
00057             // "Config file missing or missing required data."
00058             break;
00059         case CCD_OPEN_LOOPTST:
00060             // "Loopback test failed, no camera found"
00061             break;
00062         case CCD_OPEN_ALLOC:
00063             // "Memory allocation failed - system error"
00064             break;
00065         case CCD_OPEN_NTIO:
00066             // "NT I/O driver not present"
00067             break;
00068         }
00069         return ret;
00070     }
00071 }
00072 
00073 // Convert a string to a decimal or hexadecimal integer
00074 unsigned short hextoi(char *instr)
00075 {
00076     unsigned short val, tot = 0;
00077     bool IsHEX = false;
00078 
00079     long n = strlen( instr );
00080     if ( n > 1 )
00081     {   // Look for hex format e.g. 8Fh, A3H or 0x5D
00082         if ( instr[ n - 1 ] == 'h' || instr[ n - 1 ] == 'H' )
00083             IsHEX = true;
00084         else if ( *instr == '0' && *(instr+1) == 'x' )
00085         {
00086             IsHEX = true;
00087             instr += 2;
00088         }
00089     }
00090 
00091     if ( IsHEX )
00092     {
00093         while (instr && *instr && isxdigit(*instr))
00094         {
00095             val = *instr++ - '0';
00096             if (9 < val)
00097                 val -= 7;
00098             tot <<= 4;
00099             tot |= (val & 0x0f);
00100         }
00101     }
00102     else
00103         tot = atoi( instr );
00104 
00105     return tot;
00106 }
00107 
00108 // Trim trailing spaces from a string
00109 void trimstr(char *s)
00110 {
00111     char *p;
00112 
00113     p = s + (strlen(s) - 1);
00114     while (isspace(*p))
00115         p--;
00116     *(++p) = '\0';
00117 }
00118 
00119 
00120 //-------------------------------------------------------------
00121 // CfgGet
00122 //
00123 // Retrieve a parameter from an INI file. Returns a status code
00124 // and the paramter string in retbuff.
00125 //-------------------------------------------------------------
00126 bool CfgGet ( FILE* inifp,
00127                char  *inisect,
00128                char  *iniparm,
00129                char  *retbuff,
00130                short bufflen,
00131                short *parmlen)
00132 {
00133     short gotsect;
00134     char  tbuf[256];
00135     char  *ss, *eq, *ps, *vs, *ptr;
00136 
00137     rewind( inifp );
00138 
00139     // find the target section
00140 
00141     gotsect = 0;
00142     while (fgets(tbuf,256,inifp) != NULL) {
00143         if ((ss = strchr(tbuf,'[')) != NULL) {
00144             if (strnicmp(ss+1,inisect,strlen(inisect)) == 0) {
00145                 gotsect = 1;
00146                 break;
00147                 }
00148             }
00149         }
00150 
00151     if (!gotsect) {                             // section not found
00152         return false;
00153         }
00154 
00155     while (fgets(tbuf,256,inifp) != NULL) {     // find parameter in sect
00156 
00157         if ((ptr = strrchr(tbuf,'\n')) != NULL) // remove newline if there
00158             *ptr = '\0';
00159 
00160         ps = tbuf+strspn(tbuf," \t");           // find the first non-blank
00161 
00162         if (*ps == ';')                         // Skip line if comment
00163             continue;
00164 
00165         if (*ps == '[') {                       // Start of next section
00166             return false;
00167             }
00168 
00169         eq = strchr(ps,'=');                    // Find '=' sign in string
00170 
00171         if (eq)
00172             vs = eq + 1 + strspn(eq+1," \t");   // Find start of value str
00173         else
00174             continue;
00175 
00176         // found the target parameter
00177 
00178         if (strnicmp(ps,iniparm,strlen(iniparm)) == 0) {
00179 
00180             if ((ptr = strchr(vs,';')) != NULL) // cut off an EOL comment
00181                 *ptr = '\0';
00182 
00183             if (short(strlen(vs)) > bufflen - 1) {// not enough buffer space
00184                 strncpy(retbuff,vs,bufflen - 1);
00185                 retbuff[bufflen - 1] = '\0';    // put EOL in string
00186                 *parmlen = bufflen;
00187                 return true;
00188                 }
00189             else {
00190                 strcpy(retbuff,vs);             // got it
00191                 trimstr(retbuff);               // trim any trailing blanks
00192                 *parmlen = strlen(retbuff);
00193                 return true;
00194                 }
00195             }
00196         }
00197 
00198     return false;                         // parameter not found
00199 }
00200 
00201 // Initializes internal variables to their default value and reads the parameters in the
00202 // specified INI file. Then initializes the camera using current settings. If BaseAddress
00203 // or RegOffset parameters are specified (not equal to -1) then one or both of these
00204 // values are used for the m_BaseAddress and m_RegisterOffset properties overriding those
00205 // settings in the INI file.
00206 long config_load( char* cfgname, short BaseAddress, short RegOffset )
00207 {
00208     short plen;
00209     char retbuf[256];
00210 
00211     if ((strlen(cfgname) == 0) || (cfgname[0] == '\0')) return CCD_OPEN_CFGNAME;
00212 
00213     // attempt to open INI file
00214     FILE* inifp = NULL;
00215 
00216     if ((inifp = fopen(cfgname,"r")) == NULL) return CCD_OPEN_CFGDATA;
00217 
00218     // Check whether we are on an NT platform
00219     OSVERSIONINFO VersionInfo;
00220     memset(&VersionInfo, 0, sizeof(OSVERSIONINFO));
00221     VersionInfo.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
00222     GetVersionEx ( &VersionInfo );
00223     bool IsNT = VersionInfo.dwPlatformId != VER_PLATFORM_WIN32_WINDOWS;
00224 
00225     // System
00226     if (CfgGet (inifp, "system", "interface", retbuf, sizeof(retbuf), &plen))
00227     {
00228         // Assume cam is currently null
00229         if ( stricmp( "isa", retbuf ) == 0 )
00230         {
00231             if ( IsNT )
00232                 cam = new CCameraIO_ISA_NT;
00233             else
00234                 cam = new CCameraIO_ISA_9x;
00235         }
00236         else if ( stricmp( "ppi", retbuf ) == 0 )
00237         {
00238             if ( IsNT )
00239                 cam = new CCameraIO_PPI_NT;
00240             else
00241                 cam = new CCameraIO_PPI_9x;
00242         }
00243         else if ( stricmp( "pci", retbuf ) == 0 )
00244         {
00245             cam = new CCameraIO_PCI;
00246         }
00247 
00248         if ( cam == NULL )
00249         {
00250             fclose( inifp );
00251             return CCD_OPEN_ALLOC;
00252         }
00253     }
00254     else
00255     {
00256         fclose( inifp );
00257         return CCD_OPEN_CFGDATA;
00258     }
00259 
00261     // Settings which are stored in a class member (not in firmware) are already set
00262     // to a default value in the constructor. Settings accessed by get/put functions
00263     // must be set to a default value in this routine, after the base address and
00264     // communication protocal is setup.
00265 
00267     // These settings must done first since they affect communication with the camera
00268 
00269         if ( BaseAddress == -1 )
00270         {
00271             if (CfgGet (inifp, "system", "base", retbuf, sizeof(retbuf), &plen))
00272                 cam->m_BaseAddress = hextoi(retbuf) & 0xFFF;
00273             else
00274             {
00275                 fclose( inifp );
00276                 delete cam;
00277                 cam = NULL;
00278                 return CCD_OPEN_CFGDATA;           // base address MUST be defined
00279             }
00280         }
00281         else
00282             cam->m_BaseAddress = BaseAddress & 0xFFF;
00283 
00284         if ( RegOffset == -1 )
00285         {
00286             if (CfgGet (inifp, "system", "reg_offset", retbuf, sizeof(retbuf), &plen))
00287             {
00288                 unsigned short val = hextoi(retbuf);
00289                 if ( val >= 0x0 && val <= 0xF0 ) cam->m_RegisterOffset = val & 0xF0;
00290             }
00291         }
00292         else
00293         {
00294             if ( RegOffset >= 0x0 && RegOffset <= 0xF0 ) cam->m_RegisterOffset = RegOffset & 0xF0;
00295         }
00296 
00298         // Necessary geometry settings
00299 
00300         if (CfgGet (inifp, "geometry", "rows", retbuf, sizeof(retbuf), &plen))
00301         {
00302             short val = hextoi(retbuf);
00303             if ( val >= 1 && val <= MAXTOTALROWS ) cam->m_Rows = val;
00304         }
00305         else
00306         {
00307             fclose( inifp );
00308             delete cam;
00309             cam = NULL;
00310             return CCD_OPEN_CFGDATA;           // rows MUST be defined
00311         }
00312 
00313         if (CfgGet (inifp, "geometry", "columns", retbuf, sizeof(retbuf), &plen))
00314         {
00315             short val = hextoi(retbuf);
00316             if ( val >= 1 && val <= MAXTOTALCOLUMNS ) cam->m_Columns = val;
00317         }
00318         else
00319         {
00320             fclose( inifp );
00321             delete cam;
00322             cam = NULL;
00323             return CCD_OPEN_CFGDATA;           // columns MUST be defined
00324         }
00325 
00327 
00328         if (CfgGet (inifp, "system", "pp_repeat", retbuf, sizeof(retbuf), &plen))
00329         {
00330             short val = hextoi( retbuf );
00331             if ( val > 0 && val <= 1000 ) cam->m_PPRepeat = val;
00332         }
00333 
00335         // First actual communication with camera if in PPI mode
00336         if ( !cam->InitDriver() )
00337         {
00338             delete cam;
00339             cam = NULL;
00340             fclose( inifp );
00341             if ( IsNT )
00342                 return CCD_OPEN_NTIO;
00343             else
00344                 return CCD_OPEN_LOOPTST;
00345         }
00347         // First actual communication with camera if in ISA mode
00348         cam->Reset();   // Read in command register to set shadow register known state
00350 
00351         if (CfgGet (inifp, "system", "cable", retbuf, sizeof(retbuf), &plen))
00352         {
00353             if (!stricmp("LONG",retbuf))
00354                 cam->write_LongCable( true );
00355             else if ( !stricmp("SHORT",retbuf) )
00356                 cam->write_LongCable( false );
00357         }
00358         else
00359             cam->write_LongCable( false );  // default
00360 
00361         if ( !cam->read_Present() )
00362         {
00363             delete cam;
00364             cam = NULL;
00365             fclose( inifp );
00366 
00367             return CCD_OPEN_LOOPTST;
00368         }
00370     // Set default setting and read other settings from ini file
00371 
00372     cam->write_UseTrigger( false );
00373     cam->write_ForceShutterOpen( false );
00374 
00375     if (CfgGet (inifp, "system", "high_priority", retbuf, sizeof(retbuf), &plen))
00376     {
00377         if (!stricmp("ON",retbuf) || !stricmp("TRUE",retbuf) || !stricmp("1",retbuf))
00378         {
00379             cam->m_HighPriority = true;
00380         }
00381           else if (!stricmp("OFF",retbuf) || !stricmp("FALSE",retbuf) || !stricmp("0",retbuf))
00382         {
00383             cam->m_HighPriority = false;
00384         }
00385     }
00386 
00387     if (CfgGet (inifp, "system", "data_bits", retbuf, sizeof(retbuf), &plen))
00388     {
00389         short val = hextoi( retbuf );
00390         if ( val >= 8 && val <= 18 ) cam->m_DataBits = val;
00391     }
00392 
00393     if (CfgGet (inifp, "system", "sensor", retbuf, sizeof(retbuf), &plen))
00394     {
00395         if ( stricmp( "ccd", retbuf ) == 0 )
00396         {
00397             cam->m_SensorType = Camera_SensorType_CCD;
00398         }
00399         else if ( stricmp( "cmos", retbuf ) == 0 )
00400         {
00401             cam->m_SensorType = Camera_SensorType_CMOS;
00402         }
00403     }
00404 
00405     if (CfgGet (inifp, "system", "mode", retbuf, sizeof(retbuf), &plen))
00406     {
00407         unsigned short val = hextoi(retbuf) & 0xF;
00408         cam->write_Mode( val );
00409     }
00410     else
00411         cam->write_Mode( 0 );           // default
00412 
00413     if (CfgGet (inifp, "system", "test", retbuf, sizeof(retbuf), &plen))
00414     {
00415         unsigned short val = hextoi(retbuf) & 0xF;
00416         cam->write_TestBits( val );
00417     }
00418     else
00419         cam->write_TestBits( 0 );       //default
00420 
00421     if (CfgGet (inifp, "system", "test2", retbuf, sizeof(retbuf), &plen))
00422     {
00423         unsigned short val = hextoi(retbuf) & 0xF;
00424         cam->write_Test2Bits( val );
00425     }
00426     else
00427         cam->write_Test2Bits( 0 );  // default
00428 
00429     cam->write_FastReadout( false );    //default
00430 
00431     if (CfgGet (inifp, "system", "shutter_speed", retbuf, sizeof(retbuf), &plen))
00432     {
00433         if (!stricmp("normal",retbuf))
00434         {
00435             cam->m_FastShutter = false;
00436             cam->m_MaxExposure = 10485.75;
00437             cam->m_MinExposure = 0.01;
00438         }
00439         else if (!stricmp("fast",retbuf))
00440         {
00441             cam->m_FastShutter = true;
00442             cam->m_MaxExposure = 1048.575;
00443             cam->m_MinExposure = 0.001;
00444         }
00445         else if ( !stricmp("dual",retbuf))
00446         {
00447             cam->m_FastShutter = true;
00448             cam->m_MaxExposure = 10485.75;
00449             cam->m_MinExposure = 0.001;
00450         }
00451     }
00452 
00453     if (CfgGet (inifp, "system", "shutter_bits", retbuf, sizeof(retbuf), &plen))
00454     {
00455         unsigned short val = hextoi(retbuf);
00456         cam->m_FastShutterBits_Mode = val & 0x0F;
00457         cam->m_FastShutterBits_Test = ( val & 0xF0 ) >> 4;
00458     }
00459 
00460     if (CfgGet (inifp, "system", "maxbinx", retbuf, sizeof(retbuf), &plen))
00461     {
00462         short val = hextoi(retbuf);
00463         if ( val >= 1 && val <= MAXHBIN ) cam->m_MaxBinX = val;
00464     }
00465 
00466     if (CfgGet (inifp, "system", "maxbiny", retbuf, sizeof(retbuf), &plen))
00467     {
00468         short val = hextoi(retbuf);
00469         if ( val >= 1 && val <= MAXVBIN ) cam->m_MaxBinY = val;
00470     }
00471 
00472     if (CfgGet (inifp, "system", "guider_relays", retbuf, sizeof(retbuf), &plen))
00473     {
00474         if (!stricmp("ON",retbuf) || !stricmp("TRUE",retbuf) || !stricmp("1",retbuf))
00475         {
00476             cam->m_GuiderRelays = true;
00477         }
00478           else if (!stricmp("OFF",retbuf) || !stricmp("FALSE",retbuf) || !stricmp("0",retbuf))
00479         {
00480             cam->m_GuiderRelays = false;
00481         }
00482     }
00483 
00484     if (CfgGet (inifp, "system", "timeout", retbuf, sizeof(retbuf), &plen))
00485     {
00486         double val = atof(retbuf);
00487         if ( val >= 0.0 && val <= 10000.0 ) cam->m_Timeout = val;
00488     }
00489 
00491     // Geometry
00492 
00493     if (CfgGet (inifp, "geometry", "bic", retbuf, sizeof(retbuf), &plen))
00494     {
00495         short val = hextoi(retbuf);
00496         if ( val >= 1 && val <= MAXCOLUMNS ) cam->m_BIC = val;
00497     }
00498 
00499     if (CfgGet (inifp, "geometry", "bir", retbuf, sizeof(retbuf), &plen))
00500     {
00501         short val = hextoi(retbuf);
00502         if ( val >= 1 && val <= MAXROWS ) cam->m_BIR = val;
00503     }
00504 
00505     if (CfgGet (inifp, "geometry", "skipc", retbuf, sizeof(retbuf), &plen))
00506     {
00507         short val = hextoi(retbuf);
00508         if ( val >= 0 && val <= MAXCOLUMNS ) cam->m_SkipC = val;
00509     }
00510 
00511     if (CfgGet (inifp, "geometry", "skipr", retbuf, sizeof(retbuf), &plen))
00512     {
00513         short val = hextoi(retbuf);
00514         if ( val >= 0 && val <= MAXROWS ) cam->m_SkipR = val;
00515     }
00516 
00517     if (CfgGet (inifp, "geometry", "imgcols", retbuf, sizeof(retbuf), &plen))
00518     {
00519         short val = hextoi(retbuf);
00520         if ( val >= 1 && val <= MAXTOTALCOLUMNS ) cam->m_ImgColumns = val;
00521     }
00522     else
00523         cam->m_ImgColumns = cam->m_Columns - cam->m_BIC - cam->m_SkipC;
00524 
00525     if (CfgGet (inifp, "geometry", "imgrows", retbuf, sizeof(retbuf), &plen))
00526     {
00527         short val = hextoi(retbuf);
00528         if ( val >= 1 && val <= MAXTOTALROWS ) cam->m_ImgRows = val;
00529     }
00530     else
00531         cam->m_ImgRows = cam->m_Rows - cam->m_BIR - cam->m_SkipR;
00532 
00533     if (CfgGet (inifp, "geometry", "hflush", retbuf, sizeof(retbuf), &plen))
00534     {
00535         short val = hextoi(retbuf);
00536         if ( val >= 1 && val <= MAXHBIN ) cam->m_HFlush = val;
00537     }
00538 
00539     if (CfgGet (inifp, "geometry", "vflush", retbuf, sizeof(retbuf), &plen))
00540     {
00541         short val = hextoi(retbuf);
00542         if ( val >= 1 && val <= MAXVBIN ) cam->m_VFlush = val;
00543     }
00544 
00545     // Default to full frame
00546     cam->m_NumX = cam->m_ImgColumns;
00547     cam->m_NumY = cam->m_ImgRows;
00548 
00550     // Temperature
00551 
00552     if (CfgGet (inifp, "temp", "control", retbuf, sizeof(retbuf), &plen))
00553     {
00554         if (!stricmp("ON",retbuf) || !stricmp("TRUE",retbuf) || !stricmp("1",retbuf))
00555         {
00556             cam->m_TempControl = true;
00557         }
00558           else if (!stricmp("OFF",retbuf) || !stricmp("FALSE",retbuf) || !stricmp("0",retbuf))
00559         {
00560             cam->m_TempControl = false;
00561         }
00562     }
00563 
00564     if (CfgGet (inifp, "temp", "cal", retbuf, sizeof(retbuf), &plen))
00565     {
00566         short val = hextoi(retbuf);
00567         if ( val >= 1 && val <= 255 ) cam->m_TempCalibration = val;
00568     }
00569 
00570     if (CfgGet (inifp, "temp", "scale", retbuf, sizeof(retbuf), &plen))
00571     {
00572         double val = atof(retbuf);
00573         if ( val >= 1.0 && val <= 10.0 ) cam->m_TempScale = val;
00574     }
00575 
00576     if (CfgGet (inifp, "temp", "target", retbuf, sizeof(retbuf), &plen))
00577     {
00578         double val = atof(retbuf);
00579         if ( val >= -60.0 && val <= 40.0 )
00580             cam->write_CoolerSetPoint( val );
00581         else
00582             cam->write_CoolerSetPoint( -10.0 );
00583     }
00584     else
00585         cam->write_CoolerSetPoint( -10.0 ); //default
00586 
00588     // CCD
00589 
00590     if (CfgGet (inifp, "ccd", "sensor", retbuf, sizeof(retbuf), &plen))
00591     {
00592         if ( plen > 256 ) plen = 256;
00593         memcpy( cam->m_Sensor, retbuf, plen );
00594     }
00595 
00596     if (CfgGet (inifp, "ccd", "color", retbuf, sizeof(retbuf), &plen))
00597     {
00598         if (!stricmp("ON",retbuf) || !stricmp("TRUE",retbuf) || !stricmp("1",retbuf))
00599         {
00600             cam->m_Color = true;
00601         }
00602           else if (!stricmp("OFF",retbuf) || !stricmp("FALSE",retbuf) || !stricmp("0",retbuf))
00603         {
00604             cam->m_Color = false;
00605         }
00606     }
00607 
00608     if (CfgGet (inifp, "ccd", "noise", retbuf, sizeof(retbuf), &plen))
00609     {
00610         cam->m_Noise = atof( retbuf );
00611     }
00612 
00613     if (CfgGet (inifp, "ccd", "gain", retbuf, sizeof(retbuf), &plen))
00614     {
00615         cam->m_Gain = atof( retbuf );
00616     }
00617 
00618     if (CfgGet (inifp, "ccd", "pixelxsize", retbuf, sizeof(retbuf), &plen))
00619     {
00620         cam->m_PixelXSize = atof( retbuf );
00621     }
00622 
00623     if (CfgGet (inifp, "ccd", "pixelysize", retbuf, sizeof(retbuf), &plen))
00624     {
00625         cam->m_PixelYSize = atof( retbuf );
00626     }
00627 
00628     fclose( inifp );
00629     return CCD_OPEN_NOERR;
00630 }
00631 

kstars

Skip menu "kstars"
  • Main Page
  • Modules
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

API Reference

Skip menu "API Reference"
  • keduca
  • kstars
Generated for API Reference by doxygen 1.5.9
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal