summaryrefslogtreecommitdiffstats
path: root/kstars/kstars/indi/apogee/Camera_Example.cpp
blob: f1f6412176c660517434a6d89862a1ce48f261c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
// Example source code for implementing the CCameraIO object

#include "windows.h"
#include "stdio.h"

#include "CameraIO.h"
#include "CameraIO_ISA_9x.h"
#include "CameraIO_PPI_9x.h"

#include "CameraIO_ISA_NT.h"
#include "CameraIO_PPI_NT.h"

#include "CameraIO_PCI.h"


// Error codes returned from config_load
const long CCD_OPEN_NOERR = 0;		// No error detected
const long CCD_OPEN_CFGNAME = 1;	// No config file specified
const long CCD_OPEN_CFGDATA = 2;	// Config missing or missing required data
const long CCD_OPEN_LOOPTST = 3;	// Loopback test failed, no camera found
const long CCD_OPEN_ALLOC = 4;		// Memory alloc failed - system error
const long CCD_OPEN_NTIO = 5;		// NT I/O driver not present

CCameraIO* cam;		// the Camera interface object

// Function declarations for this file
int InitCam( char* cfgname );
long config_load( char* cfgname, short BaseAddress, short RegOffset );
bool CfgGet ( FILE* inifp,
		   char* inisect,
		   char* iniparm,
		   char* retbuff,
		   short bufflen,
		   short* parmlen);

unsigned short hextoi(char* instr);
void trimstr(char* s);

// Initializes the CameraIO object from an INI file specified by cfgname
int InitCam( char* cfgname )
{
	long ret = config_load( cfgname, -1, -1 );
	if ( ret == 0 )
	{
		// We can now access the cam objects members
		cam->Flush();	// Start the camera flushing
		return 0;
	}
	else
	{
		switch ( ret )
		{
		case CCD_OPEN_CFGNAME:
			// "No config file specified."
			break;
		case CCD_OPEN_CFGDATA:
			// "Config file missing or missing required data."
			break;
		case CCD_OPEN_LOOPTST:
			// "Loopback test failed, no camera found"
			break;
		case CCD_OPEN_ALLOC:
			// "Memory allocation failed - system error"
			break;
		case CCD_OPEN_NTIO:
			// "NT I/O driver not present"
			break;
		}
		return ret;
	}
}

// Convert a string to a decimal or hexadecimal integer
unsigned short hextoi(char *instr)
{
    unsigned short val, tot = 0;
	bool IsHEX = false;

	long n = strlen( instr );
	if ( n > 1 )
	{	// Look for hex format e.g. 8Fh, A3H or 0x5D
		if ( instr[ n - 1 ] == 'h' || instr[ n - 1 ] == 'H' )
			IsHEX = true;
		else if ( *instr == '0' && *(instr+1) == 'x' )
		{
			IsHEX = true;
			instr += 2;
		}
	}

	if ( IsHEX )
	{
		while (instr && *instr && isxdigit(*instr))
		{
			val = *instr++ - '0';
			if (9 < val)
				val -= 7;
			tot <<= 4;
			tot |= (val & 0x0f);
		}
	}
	else
		tot = atoi( instr );

	return tot;
}

// Trim trailing spaces from a string
void trimstr(char *s)
{
    char *p;

    p = s + (strlen(s) - 1);
    while (isspace(*p))
        p--;
    *(++p) = '\0';
}


//-------------------------------------------------------------
// CfgGet
//
// Retrieve a parameter from an INI file. Returns a status code
// and the paramter string in retbuff.
//-------------------------------------------------------------
bool CfgGet ( FILE* inifp,
               char  *inisect,
               char  *iniparm,
               char  *retbuff,
               short bufflen,
               short *parmlen)
{
    short gotsect;
    char  tbuf[256];
    char  *ss, *eq, *ps, *vs, *ptr;

	rewind( inifp );

    // find the target section

    gotsect = 0;
    while (fgets(tbuf,256,inifp) != NULL) {
        if ((ss = strchr(tbuf,'[')) != NULL) {
            if (strnicmp(ss+1,inisect,strlen(inisect)) == 0) {
                gotsect = 1;
                break;
                }
            }
        }

    if (!gotsect) {                             // section not found
        return false;
        }

    while (fgets(tbuf,256,inifp) != NULL) {     // find parameter in sect

        if ((ptr = strrchr(tbuf,'\n')) != NULL) // remove newline if there
            *ptr = '\0';

        ps = tbuf+strspn(tbuf," \t");           // find the first non-blank

        if (*ps == ';')                         // Skip line if comment
            continue;

        if (*ps == '[') {                       // Start of next section
            return false;
            }

        eq = strchr(ps,'=');                    // Find '=' sign in string

        if (eq)
            vs = eq + 1 + strspn(eq+1," \t");   // Find start of value str
        else
            continue;

        // found the target parameter

        if (strnicmp(ps,iniparm,strlen(iniparm)) == 0) {

            if ((ptr = strchr(vs,';')) != NULL) // cut off an EOL comment
                *ptr = '\0';

            if (short(strlen(vs)) > bufflen - 1) {// not enough buffer space
                strncpy(retbuff,vs,bufflen - 1);
                retbuff[bufflen - 1] = '\0';    // put EOL in string
                *parmlen = bufflen;
                return true;
                }
            else {
                strcpy(retbuff,vs);             // got it
                trimstr(retbuff);               // trim any trailing blanks
                *parmlen = strlen(retbuff);
                return true;
                }
            }
        }

    return false;                         // parameter not found
}

// Initializes internal variables to their default value and reads the parameters in the
// specified INI file. Then initializes the camera using current settings. If BaseAddress
// or RegOffset parameters are specified (not equal to -1) then one or both of these
// values are used for the m_BaseAddress and m_RegisterOffset properties overriding those
// settings in the INI file.
long config_load( char* cfgname, short BaseAddress, short RegOffset )
{
    short plen;
    char retbuf[256];

    if ((strlen(cfgname) == 0) || (cfgname[0] == '\0')) return CCD_OPEN_CFGNAME;

    // attempt to open INI file
    FILE* inifp = NULL;

	if ((inifp = fopen(cfgname,"r")) == NULL) return CCD_OPEN_CFGDATA;

	// Check whether we are on an NT platform
	OSVERSIONINFO VersionInfo;
	memset(&VersionInfo, 0, sizeof(OSVERSIONINFO));
	VersionInfo.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
	GetVersionEx ( &VersionInfo );
	bool IsNT = VersionInfo.dwPlatformId != VER_PLATFORM_WIN32_WINDOWS;

    // System
    if (CfgGet (inifp, "system", "interface", retbuf, sizeof(retbuf), &plen))
	{
		// Assume cam is currently null
		if ( stricmp( "isa", retbuf ) == 0 )
		{
			if ( IsNT )
				cam = new CCameraIO_ISA_NT;
			else
				cam = new CCameraIO_ISA_9x;
		}
		else if ( stricmp( "ppi", retbuf ) == 0 )
		{
			if ( IsNT )
				cam = new CCameraIO_PPI_NT;
			else
				cam = new CCameraIO_PPI_9x;
		}
		else if ( stricmp( "pci", retbuf ) == 0 )
		{
			cam = new CCameraIO_PCI;
		}

		if ( cam == NULL )
		{
			fclose( inifp );
			return CCD_OPEN_ALLOC;
		}
	}
    else
	{
		fclose( inifp );
		return CCD_OPEN_CFGDATA;
	}

	/////////////////////////////////////////////////////////////////////////////////
	// Settings which are stored in a class member (not in firmware) are already set
	// to a default value in the constructor. Settings accessed by get/put functions
	// must be set to a default value in this routine, after the base address and
	// communication protocal is setup.

	/////////////////////////////////////////////////////////////////////////////////
	// These settings must done first since they affect communication with the camera

		if ( BaseAddress == -1 )
		{
			if (CfgGet (inifp, "system", "base", retbuf, sizeof(retbuf), &plen))
				cam->m_BaseAddress = hextoi(retbuf) & 0xFFF;
			else
			{
				fclose( inifp );
				delete cam;
				cam = NULL;
				return CCD_OPEN_CFGDATA;           // base address MUST be defined
			}
		}
		else
			cam->m_BaseAddress = BaseAddress & 0xFFF;

		if ( RegOffset == -1 )
		{
			if (CfgGet (inifp, "system", "reg_offset", retbuf, sizeof(retbuf), &plen))
			{
				unsigned short val = hextoi(retbuf);
				if ( val >= 0x0 && val <= 0xF0 ) cam->m_RegisterOffset = val & 0xF0;
			}
		}
		else
		{
			if ( RegOffset >= 0x0 && RegOffset <= 0xF0 ) cam->m_RegisterOffset = RegOffset & 0xF0;
		}

		/////////////////////////////////////////////////////////////////////////////////
		// Necessary geometry settings

		if (CfgGet (inifp, "geometry", "rows", retbuf, sizeof(retbuf), &plen))
		{
			short val = hextoi(retbuf);
			if ( val >= 1 && val <= MAXTOTALROWS ) cam->m_Rows = val;
		}
		else
		{
			fclose( inifp );
			delete cam;
			cam = NULL;
			return CCD_OPEN_CFGDATA;           // rows MUST be defined
		}

		if (CfgGet (inifp, "geometry", "columns", retbuf, sizeof(retbuf), &plen))
		{
			short val = hextoi(retbuf);
			if ( val >= 1 && val <= MAXTOTALCOLUMNS ) cam->m_Columns = val;
		}
		else
		{
			fclose( inifp );
			delete cam;
			cam = NULL;
			return CCD_OPEN_CFGDATA;           // columns MUST be defined
		}

		/////////////////////////////////////////////////////////////////////////////////

		if (CfgGet (inifp, "system", "pp_repeat", retbuf, sizeof(retbuf), &plen))
		{
			short val = hextoi( retbuf );
			if ( val > 0 && val <= 1000 ) cam->m_PPRepeat = val;
		}

		/////////////////////////////////////////////////////////////////////////////////
		// First actual communication with camera if in PPI mode
		if ( !cam->InitDriver() )
		{
			delete cam;
			cam = NULL;
			fclose( inifp );
			if ( IsNT )
				return CCD_OPEN_NTIO;
			else
				return CCD_OPEN_LOOPTST;
		}
		/////////////////////////////////////////////////////////////////////////////////
		// First actual communication with camera if in ISA mode
		cam->Reset();	// Read in command register to set shadow register known state
		/////////////////////////////////////////////////////////////////////////////////

		if (CfgGet (inifp, "system", "cable", retbuf, sizeof(retbuf), &plen))
		{
			if (!stricmp("LONG",retbuf))
				cam->write_LongCable( true );
			else if ( !stricmp("SHORT",retbuf) )
				cam->write_LongCable( false );
		}
		else
			cam->write_LongCable( false );	// default

		if ( !cam->read_Present() )
		{
			delete cam;
			cam = NULL;
			fclose( inifp );

			return CCD_OPEN_LOOPTST;
		}
	/////////////////////////////////////////////////////////////////////////////////
	// Set default setting and read other settings from ini file

	cam->write_UseTrigger( false );
	cam->write_ForceShutterOpen( false );

	if (CfgGet (inifp, "system", "high_priority", retbuf, sizeof(retbuf), &plen))
	{
		if (!stricmp("ON",retbuf) || !stricmp("TRUE",retbuf) || !stricmp("1",retbuf))
		{
			cam->m_HighPriority = true;
		}
		  else if (!stricmp("OFF",retbuf) || !stricmp("FALSE",retbuf) || !stricmp("0",retbuf))
		{
			cam->m_HighPriority = false;
		}
	}

	if (CfgGet (inifp, "system", "data_bits", retbuf, sizeof(retbuf), &plen))
	{
		short val = hextoi( retbuf );
		if ( val >= 8 && val <= 18 ) cam->m_DataBits = val;
	}

	if (CfgGet (inifp, "system", "sensor", retbuf, sizeof(retbuf), &plen))
	{
		if ( stricmp( "ccd", retbuf ) == 0 )
		{
			cam->m_SensorType = Camera_SensorType_CCD;
		}
		else if ( stricmp( "cmos", retbuf ) == 0 )
		{
			cam->m_SensorType = Camera_SensorType_CMOS;
		}
	}

    if (CfgGet (inifp, "system", "mode", retbuf, sizeof(retbuf), &plen))
	{
        unsigned short val = hextoi(retbuf) & 0xF;
        cam->write_Mode( val );
    }
	else
		cam->write_Mode( 0 );			// default

    if (CfgGet (inifp, "system", "test", retbuf, sizeof(retbuf), &plen))
	{
        unsigned short val = hextoi(retbuf) & 0xF;
        cam->write_TestBits( val );
    }
	else
		cam->write_TestBits( 0 );		//default

    if (CfgGet (inifp, "system", "test2", retbuf, sizeof(retbuf), &plen))
	{
        unsigned short val = hextoi(retbuf) & 0xF;
        cam->write_Test2Bits( val );
    }
	else
		cam->write_Test2Bits( 0 );	// default

	cam->write_FastReadout( false );	//default

    if (CfgGet (inifp, "system", "shutter_speed", retbuf, sizeof(retbuf), &plen))
	{
        if (!stricmp("normal",retbuf))
		{
			cam->m_FastShutter = false;
			cam->m_MaxExposure = 10485.75;
			cam->m_MinExposure = 0.01;
		}
		else if (!stricmp("fast",retbuf))
		{
			cam->m_FastShutter = true;
			cam->m_MaxExposure = 1048.575;
			cam->m_MinExposure = 0.001;
		}
		else if ( !stricmp("dual",retbuf))
		{
			cam->m_FastShutter = true;
			cam->m_MaxExposure = 10485.75;
			cam->m_MinExposure = 0.001;
		}
    }

    if (CfgGet (inifp, "system", "shutter_bits", retbuf, sizeof(retbuf), &plen))
	{
		unsigned short val = hextoi(retbuf);
		cam->m_FastShutterBits_Mode = val & 0x0F;
		cam->m_FastShutterBits_Test = ( val & 0xF0 ) >> 4;
	}

    if (CfgGet (inifp, "system", "maxbinx", retbuf, sizeof(retbuf), &plen))
	{
        short val = hextoi(retbuf);
        if ( val >= 1 && val <= MAXHBIN ) cam->m_MaxBinX = val;
	}

    if (CfgGet (inifp, "system", "maxbiny", retbuf, sizeof(retbuf), &plen))
	{
        short val = hextoi(retbuf);
        if ( val >= 1 && val <= MAXVBIN ) cam->m_MaxBinY = val;
	}

    if (CfgGet (inifp, "system", "guider_relays", retbuf, sizeof(retbuf), &plen))
	{
		if (!stricmp("ON",retbuf) || !stricmp("TRUE",retbuf) || !stricmp("1",retbuf))
		{
			cam->m_GuiderRelays = true;
		}
		  else if (!stricmp("OFF",retbuf) || !stricmp("FALSE",retbuf) || !stricmp("0",retbuf))
		{
			cam->m_GuiderRelays = false;
		}
	}

    if (CfgGet (inifp, "system", "timeout", retbuf, sizeof(retbuf), &plen))
	{
        double val = atof(retbuf);
		if ( val >= 0.0 && val <= 10000.0 ) cam->m_Timeout = val;
    }

	/////////////////////////////////////////////////////////////////////////////////
    // Geometry

    if (CfgGet (inifp, "geometry", "bic", retbuf, sizeof(retbuf), &plen))
	{
        short val = hextoi(retbuf);
        if ( val >= 1 && val <= MAXCOLUMNS ) cam->m_BIC = val;
	}

    if (CfgGet (inifp, "geometry", "bir", retbuf, sizeof(retbuf), &plen))
	{
        short val = hextoi(retbuf);
        if ( val >= 1 && val <= MAXROWS ) cam->m_BIR = val;
	}

	if (CfgGet (inifp, "geometry", "skipc", retbuf, sizeof(retbuf), &plen))
	{
        short val = hextoi(retbuf);
        if ( val >= 0 && val <= MAXCOLUMNS ) cam->m_SkipC = val;
	}

	if (CfgGet (inifp, "geometry", "skipr", retbuf, sizeof(retbuf), &plen))
	{
        short val = hextoi(retbuf);
        if ( val >= 0 && val <= MAXROWS ) cam->m_SkipR = val;
	}

    if (CfgGet (inifp, "geometry", "imgcols", retbuf, sizeof(retbuf), &plen))
	{
        short val = hextoi(retbuf);
        if ( val >= 1 && val <= MAXTOTALCOLUMNS ) cam->m_ImgColumns = val;
	}
	else
		cam->m_ImgColumns = cam->m_Columns - cam->m_BIC - cam->m_SkipC;

    if (CfgGet (inifp, "geometry", "imgrows", retbuf, sizeof(retbuf), &plen))
	{
        short val = hextoi(retbuf);
        if ( val >= 1 && val <= MAXTOTALROWS ) cam->m_ImgRows = val;
	}
	else
		cam->m_ImgRows = cam->m_Rows - cam->m_BIR - cam->m_SkipR;

    if (CfgGet (inifp, "geometry", "hflush", retbuf, sizeof(retbuf), &plen))
	{
        short val = hextoi(retbuf);
        if ( val >= 1 && val <= MAXHBIN ) cam->m_HFlush = val;
	}

    if (CfgGet (inifp, "geometry", "vflush", retbuf, sizeof(retbuf), &plen))
	{
        short val = hextoi(retbuf);
        if ( val >= 1 && val <= MAXVBIN ) cam->m_VFlush = val;
	}

	// Default to full frame
	cam->m_NumX = cam->m_ImgColumns;
	cam->m_NumY = cam->m_ImgRows;

	/////////////////////////////////////////////////////////////////////////////////
	// Temperature

    if (CfgGet (inifp, "temp", "control", retbuf, sizeof(retbuf), &plen))
	{
		if (!stricmp("ON",retbuf) || !stricmp("TRUE",retbuf) || !stricmp("1",retbuf))
		{
			cam->m_TempControl = true;
		}
		  else if (!stricmp("OFF",retbuf) || !stricmp("FALSE",retbuf) || !stricmp("0",retbuf))
		{
			cam->m_TempControl = false;
		}
    }

    if (CfgGet (inifp, "temp", "cal", retbuf, sizeof(retbuf), &plen))
	{
        short val = hextoi(retbuf);
		if ( val >= 1 && val <= 255 ) cam->m_TempCalibration = val;
    }

    if (CfgGet (inifp, "temp", "scale", retbuf, sizeof(retbuf), &plen))
	{
        double val = atof(retbuf);
		if ( val >= 1.0 && val <= 10.0 ) cam->m_TempScale = val;
    }

    if (CfgGet (inifp, "temp", "target", retbuf, sizeof(retbuf), &plen))
	{
        double val = atof(retbuf);
        if ( val >= -60.0 && val <= 40.0 )
			cam->write_CoolerSetPoint( val );
		else
			cam->write_CoolerSetPoint( -10.0 );
    }
	else
		cam->write_CoolerSetPoint( -10.0 );	//default

	/////////////////////////////////////////////////////////////////////////////////
	// CCD

	if (CfgGet (inifp, "ccd", "sensor", retbuf, sizeof(retbuf), &plen))
	{
		if ( plen > 256 ) plen = 256;
		memcpy( cam->m_Sensor, retbuf, plen );
    }

	if (CfgGet (inifp, "ccd", "color", retbuf, sizeof(retbuf), &plen))
	{
		if (!stricmp("ON",retbuf) || !stricmp("TRUE",retbuf) || !stricmp("1",retbuf))
		{
			cam->m_Color = true;
		}
		  else if (!stricmp("OFF",retbuf) || !stricmp("FALSE",retbuf) || !stricmp("0",retbuf))
		{
			cam->m_Color = false;
		}
    }

    if (CfgGet (inifp, "ccd", "noise", retbuf, sizeof(retbuf), &plen))
	{
		cam->m_Noise = atof( retbuf );
    }

	if (CfgGet (inifp, "ccd", "gain", retbuf, sizeof(retbuf), &plen))
	{
		cam->m_Gain = atof( retbuf );
    }

    if (CfgGet (inifp, "ccd", "pixelxsize", retbuf, sizeof(retbuf), &plen))
	{
		cam->m_PixelXSize = atof( retbuf );
    }

    if (CfgGet (inifp, "ccd", "pixelysize", retbuf, sizeof(retbuf), &plen))
	{
		cam->m_PixelYSize = atof( retbuf );
    }

	fclose( inifp );
    return CCD_OPEN_NOERR;
}