Debian Codegraph @

fli_ccd.cpp

raw
1 /*
2 FLI CCD
3 INDI Interface for Finger Lakes Instrument CCDs
4 Copyright (C) 2003-2016 Jasem Mutlaq ([email protected])
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
20 2016.05.16: Added CCD Cooler Power (JM)
21
22 */
23
24 #include <memory>
25 #include <time.h>
26 #include <math.h>
27 #include <unistd.h>
28 #include <sys/time.h>
29
30 #include "config.h"
31
32 #include "indidevapi.h"
33 #include "eventloop.h"
34
35 #include "fli_ccd.h"
36
37 #define MAX_CCD_TEMP 45 /* Max CCD temperature */
38 #define MIN_CCD_TEMP -55 /* Min CCD temperature */
39 #define MAX_X_BIN 16 /* Max Horizontal binning */
40 #define MAX_Y_BIN 16 /* Max Vertical binning */
41 #define TEMP_THRESHOLD .25 /* Differential temperature threshold (C)*/
42
43 static std::unique_ptr<FLICCD> fliCCD(new FLICCD());
44
45 const flidomain_t Domains[] = { FLIDOMAIN_USB, FLIDOMAIN_SERIAL, FLIDOMAIN_PARALLEL_PORT, FLIDOMAIN_INET };
46
47 FLICCD::FLICCD()
48 {
49 setVersion(FLI_CCD_VERSION_MAJOR, FLI_CCD_VERSION_MINOR);
50 }
51
52 FLICCD::~FLICCD()
53 {
54 delete [] CameraModeS;
55 }
56
57 const char *FLICCD::getDefaultName()
58 {
59 return "FLI CCD";
60 }
61
62 bool FLICCD::initProperties()
63 {
64 // Init parent properties first
65 INDI::CCD::initProperties();
66
67 CaptureFormat mono = {"INDI_MONO", "Mono", 16, true};
68 addCaptureFormat(mono);
69
70 IUFillSwitch(&PortS[0], "USB", "USB", ISS_ON);
71 IUFillSwitch(&PortS[1], "SERIAL", "Serial", ISS_OFF);
72 IUFillSwitch(&PortS[2], "PARALLEL", "Parallel", ISS_OFF);
73 IUFillSwitch(&PortS[3], "INET", "INet", ISS_OFF);
74 IUFillSwitchVector(&PortSP, PortS, 4, getDeviceName(), "PORTS", "Port", MAIN_CONTROL_TAB, IP_WO, ISR_1OFMANY, 0,
75 IPS_IDLE);
76
77 IUFillText(&CamInfoT[0], "Model", "Model", "");
78 IUFillText(&CamInfoT[1], "HW Rev", "HW Rev", "");
79 IUFillText(&CamInfoT[2], "FW Rev", "FW Rev", "");
80 IUFillTextVector(&CamInfoTP, CamInfoT, 3, getDeviceName(), "Model", "", IMAGE_INFO_TAB, IP_RO, 60, IPS_IDLE);
81
82 IUFillNumber(&CoolerN[0], "CCD_COOLER_VALUE", "Cooling Power (%)", "%+06.2f", 0., 100., .2, 0.0);
83 IUFillNumberVector(&CoolerNP, CoolerN, 1, getDeviceName(), "CCD_COOLER_POWER", "Cooling Power", MAIN_CONTROL_TAB,
84 IP_RO, 60, IPS_IDLE);
85
86 // Number of flush pre-exposure
87 IUFillNumber(&FlushN[0], "FLUSH_COUNT", "Count", "%.f", 0., 16., 1, 0);
88 IUFillNumberVector(&FlushNP, FlushN, 1, getDeviceName(), "CCD_FLUSH_COUNT", "N Flush", OPTIONS_TAB,
89 IP_RW, 60, IPS_IDLE);
90
91 // Background Flushing
92 IUFillSwitch(&BackgroundFlushS[0], "ENABLED", "Enabled", ISS_ON);
93 IUFillSwitch(&BackgroundFlushS[1], "DISABLED", "Disabled", ISS_OFF);
94 IUFillSwitchVector(&BackgroundFlushSP, BackgroundFlushS, 2, getDeviceName(), "CCD_BACKGROUND_FLUSH", "BKG. Flush",
95 OPTIONS_TAB, IP_RW, ISR_1OFMANY, 0, IPS_IDLE);
96
97 SetCCDCapability(CCD_CAN_ABORT | CCD_CAN_BIN | CCD_CAN_SUBFRAME | CCD_HAS_COOLER | CCD_HAS_SHUTTER);
98
99 PrimaryCCD.setMinMaxStep("CCD_EXPOSURE", "CCD_EXPOSURE_VALUE", 0.04, 3600, 1, false);
100
101 addAuxControls();
102
103 return true;
104 }
105
106 void FLICCD::ISGetProperties(const char *dev)
107 {
108 INDI::CCD::ISGetProperties(dev);
109
110 defineProperty(&PortSP);
111 }
112
113 bool FLICCD::updateProperties()
114 {
115 INDI::CCD::updateProperties();
116
117 if (isConnected())
118 {
119 defineProperty(&CamInfoTP);
120 defineProperty(&CoolerNP);
121 defineProperty(&FlushNP);
122 defineProperty(&BackgroundFlushSP);
123
124 setupParams();
125
126 if (CameraModeS != nullptr)
127 defineProperty(&CameraModeSP);
128
129 timerID = SetTimer(getCurrentPollingPeriod());
130 }
131 else
132 {
133 deleteProperty(CamInfoTP.name);
134 deleteProperty(CoolerNP.name);
135 deleteProperty(FlushNP.name);
136 deleteProperty(BackgroundFlushSP.name);
137
138 if (CameraModeS != nullptr)
139 deleteProperty(CameraModeSP.name);
140
141 rmTimer(timerID);
142 }
143
144 return true;
145 }
146
147 bool FLICCD::ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n)
148 {
149 if (dev != nullptr && strcmp(dev, getDeviceName()) == 0)
150 {
151 if (!strcmp(name, FlushNP.name))
152 {
153 int err = 0;
154 long nflushes = values[0];
155
156 if ((err = FLISetNFlushes(fli_dev, nflushes)))
157 {
158 LOGF_DEBUG("Error: FLISetNFlushes() failed. %s.", strerror(-err));
159 FlushNP.s = IPS_ALERT;
160 IDSetNumber(&FlushNP, nullptr);
161 return true;
162 }
163
164 IUUpdateNumber(&FlushNP, values, names, n);
165 FlushNP.s = IPS_OK;
166 IDSetNumber(&FlushNP, nullptr);
167 return true;
168 }
169 }
170
171 return INDI::CCD::ISNewNumber(dev, name, values, names, n);
172 }
173
174 bool FLICCD::ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n)
175 {
176 if (strcmp(dev, getDeviceName()) == 0)
177 {
178 // Ports
179 if (!strcmp(name, PortSP.name))
180 {
181 if (IUUpdateSwitch(&PortSP, states, names, n) < 0)
182 return false;
183
184 PortSP.s = IPS_OK;
185 IDSetSwitch(&PortSP, nullptr);
186 return true;
187 }
188
189 // Background Flushing
190 if (!strcmp(name, BackgroundFlushSP.name))
191 {
192 int err = 0;
193 bool enabled = !strcmp(IUFindOnSwitchName(states, names, n), "ENABLED");
194 if ((err = FLIControlBackgroundFlush(fli_dev, enabled ? FLI_BGFLUSH_START : FLI_BGFLUSH_STOP)))
195 {
196 LOGF_ERROR("Error: FLIControlBackgroundFlush() %s failed. %s.", (enabled ? "starting" : "stopping"), strerror(-err));
197 BackgroundFlushSP.s = IPS_ALERT;
198 IDSetSwitch(&BackgroundFlushSP, nullptr);
199 return true;
200 }
201
202 if (!enabled)
203 LOG_WARN("Turning off camera background flushing may lead to undesirable behavior. Only change if you know what you are doing.");
204
205 IUUpdateSwitch(&BackgroundFlushSP, states, names, n);
206 BackgroundFlushSP.s = IPS_OK;
207 IDSetSwitch(&BackgroundFlushSP, nullptr);
208 return true;
209 }
210
211 // Camera Modes
212 if (!strcmp(name, CameraModeSP.name) && CameraModeS != nullptr)
213 {
214 int currentIndex = IUFindOnSwitchIndex(&CameraModeSP);
215 LIBFLIAPI errCode = 0;
216 IUUpdateSwitch(&CameraModeSP, states, names, n);
217 flimode_t cameraModelIndex = static_cast<flimode_t>(IUFindOnSwitchIndex(&CameraModeSP));
218 if ( (errCode = FLISetCameraMode(fli_dev, cameraModelIndex)))
219 {
220 LOGF_ERROR("Error: FLISetCameraMode(%ld) failed. %s.", cameraModelIndex, strerror(-errCode));
221 IUResetSwitch(&CameraModeSP);
222 CameraModeS[currentIndex].s = ISS_ON;
223 CameraModeSP.s = IPS_ALERT;
224 }
225 else
226 {
227 LOG_WARN(("Camera mode is updated. Please capture a bias frame now before proceeding further to synchronize the change."));
228 CameraModeSP.s = IPS_OK;
229 }
230
231 IDSetSwitch(&CameraModeSP, nullptr);
232 return true;
233 }
234 }
235
236 // Nobody has claimed this, so, ignore it
237 return INDI::CCD::ISNewSwitch(dev, name, states, names, n);
238 }
239
240 bool FLICCD::Connect()
241 {
242 int err = 0;
243
244 LOG_DEBUG("Attempting to find FLI CCD...");
245
246 sim = isSimulation();
247
248 if (sim)
249 {
250 LOG_DEBUG("Simulator used.");
251 return true;
252 }
253
254 int portSwitchIndex = IUFindOnSwitchIndex(&PortSP);
255
256 if (findFLICCD(Domains[portSwitchIndex]) == false)
257 {
258 LOG_ERROR("Error: no cameras were detected.");
259 return false;
260 }
261
262 if ((err = FLIOpen(&fli_dev, FLICam.name, FLIDEVICE_CAMERA | FLICam.domain)))
263 {
264 LOGF_ERROR("Error: FLIOpen() failed. %s.", strerror(-err));
265 return false;
266 }
267
268 /* Success! */
269 LOGF_DEBUG("CCD %s is online.", FLICam.name);
270 return true;
271 }
272
273 bool FLICCD::Disconnect()
274 {
275 int err;
276
277 if (sim)
278 return true;
279
280 if ((err = FLIClose(fli_dev)))
281 {
282 LOGF_DEBUG("Error: FLIClose() failed. %s.", strerror(-err));
283 return false;
284 }
285
286 LOG_INFO("Camera is offline.");
287 return true;
288 }
289
290 bool FLICCD::setupParams()
291 {
292 int err = 0;
293
294 LOG_DEBUG("Retieving camera parameters...");
295
296 char hw_rev[16] = {0}, fw_rev[16] = {0};
297
298 //////////////////////
299 // 1. Get Camera Model
300 //////////////////////
301
302 if (sim)
303 IUSaveText(&CamInfoT[0], getDeviceName());
304 else if ((err = FLIGetModel(fli_dev, FLICam.model, 32)))
305 {
306 LOGF_ERROR("FLIGetModel() failed. %s.", strerror(-err));
307 return false;
308 }
309 else
310 {
311 LOGF_DEBUG("FLIGetModel() succeed -> %s", FLICam.model);
312 IUSaveText(&CamInfoT[0], FLICam.model);
313 }
314
315 ///////////////////////////
316 // 2. Get Hardware revision
317 ///////////////////////////
318 if (sim)
319 FLICam.HWRevision = 1;
320 else if ((err = FLIGetHWRevision(fli_dev, &FLICam.HWRevision)))
321 {
322 LOGF_ERROR("FLIGetHWRevision() failed. %s.", strerror(-err));
323 return false;
324 }
325 else
326 {
327 snprintf(hw_rev, 16, "%ld", FLICam.HWRevision);
328 IUSaveText(&CamInfoT[1], hw_rev);
329 LOGF_DEBUG("FLIGetHWRevision() succeed -> %s", hw_rev);
330 }
331
332 ///////////////////////////
333 // 3. Get Firmware revision
334 ///////////////////////////
335 if (sim)
336 FLICam.FWRevision = 1;
337 else if ((err = FLIGetFWRevision(fli_dev, &FLICam.FWRevision)))
338 {
339 LOGF_ERROR("FLIGetFWRevision() failed. %s.", strerror(-err));
340 return false;
341 }
342 else
343 {
344 snprintf(fw_rev, 16, "%ld", FLICam.FWRevision);
345 IUSaveText(&CamInfoT[2], fw_rev);
346 LOGF_DEBUG("FLIGetFWRevision() succeed -> %s", fw_rev);
347 }
348
349 IDSetText(&CamInfoTP, nullptr);
350 ///////////////////////////
351 // 4. Get Pixel size
352 ///////////////////////////
353 if (sim)
354 {
355 FLICam.x_pixel_size = 5.4 / 1e6;
356 FLICam.y_pixel_size = 5.4 / 1e6;
357 }
358 else if ((err = FLIGetPixelSize(fli_dev, &FLICam.x_pixel_size, &FLICam.y_pixel_size)))
359 {
360 LOGF_ERROR("FLIGetPixelSize() failed. %s.", strerror(-err));
361 return false;
362 }
363 else
364 {
365 FLICam.x_pixel_size *= 1e6;
366 FLICam.y_pixel_size *= 1e6;
367 LOGF_DEBUG("FLIGetPixelSize() succeed -> %g x %g", FLICam.x_pixel_size,
368 FLICam.y_pixel_size);
369 }
370
371 ///////////////////////////
372 // 5. Get array area
373 ///////////////////////////
374 if (sim)
375 {
376 FLICam.Array_Area[0] = FLICam.Array_Area[1] = 0;
377 FLICam.Array_Area[2] = 1280;
378 FLICam.Array_Area[3] = 1024;
379 }
380 else if ((err = FLIGetArrayArea(fli_dev, &FLICam.Array_Area[0], &FLICam.Array_Area[1], &FLICam.Array_Area[2],
381 &FLICam.Array_Area[3])))
382 {
383 LOGF_ERROR("FLIGetArrayArea() failed. %s.", strerror(-err));
384 return false;
385 }
386 else
387 {
388 LOGF_DEBUG("FLIGetArrayArea() succeed -> %ld x %ld + %ld x %ld", FLICam.Array_Area[0],
389 FLICam.Array_Area[1], FLICam.Array_Area[2], FLICam.Array_Area[3]);
390 }
391
392 ///////////////////////////
393 // 6. Get visible area
394 ///////////////////////////
395 if (sim)
396 {
397 FLICam.Visible_Area[0] = FLICam.Visible_Area[1] = 0;
398 FLICam.Visible_Area[2] = 1280;
399 FLICam.Visible_Area[3] = 1024;
400 }
401 else if ((err = FLIGetVisibleArea(fli_dev, &FLICam.Visible_Area[0], &FLICam.Visible_Area[1],
402 &FLICam.Visible_Area[2], &FLICam.Visible_Area[3])))
403 {
404 LOGF_ERROR("FLIGetVisibleArea() failed. %s.", strerror(-err));
405 return false;
406 }
407 else
408 {
409 LOGF_DEBUG("FLIGetVisibleArea() succeed -> %ld x %ld + %ld x %ld", FLICam.Visible_Area[0],
410 FLICam.Visible_Area[1], FLICam.Visible_Area[2], FLICam.Visible_Area[3]);
411 }
412
413 ///////////////////////////
414 // 7. Get temperature
415 ///////////////////////////
416 if (sim)
417 FLICam.temperature = 25.0;
418 else if ((err = FLIGetTemperature(fli_dev, &FLICam.temperature)))
419 {
420 LOGF_ERROR("FLIGetTemperature() failed. %s.", strerror(-err));
421 return false;
422 }
423 else
424 {
425 TemperatureN[0].value = FLICam.temperature; /* CCD chip temperatre (degrees C) */
426 TemperatureN[0].min = MIN_CCD_TEMP;
427 TemperatureN[0].max = MAX_CCD_TEMP;
428 IUUpdateMinMax(&TemperatureNP);
429 IDSetNumber(&TemperatureNP, nullptr);
430 LOGF_DEBUG("FLIGetTemperature() succeed -> %f", FLICam.temperature);
431 }
432
433 SetCCDParams(FLICam.Visible_Area[2] - FLICam.Visible_Area[0], FLICam.Visible_Area[3] - FLICam.Visible_Area[1], 16,
434 FLICam.x_pixel_size, FLICam.y_pixel_size);
435
436 if (!sim)
437 {
438 /* Default frame type is NORMAL */
439 if ((err = FLISetFrameType(fli_dev, FLI_FRAME_TYPE_NORMAL)))
440 {
441 LOGF_DEBUG("FLISetFrameType() failed. %s.", strerror(-err));
442 return false;
443 }
444
445 /* X horizontal binning */
446 if ((err = FLISetHBin(fli_dev, PrimaryCCD.getBinX())))
447 {
448 LOGF_ERROR("FLISetBin() failed. %s.", strerror(-err));
449 return false;
450 }
451
452 /* Y vertical binning */
453 if ((err = FLISetVBin(fli_dev, PrimaryCCD.getBinY())))
454 {
455 LOGF_ERROR("FLISetVBin() failed. %s.", strerror(-err));
456 return false;
457 }
458 }
459
460 int nbuf = PrimaryCCD.getXRes() * PrimaryCCD.getYRes() * PrimaryCCD.getBPP() / 8;
461 PrimaryCCD.setFrameBufferSize(nbuf);
462
463 ///////////////////////////
464 // 8. Get Modes
465 ///////////////////////////
466 std::vector<std::string> cameraModeValues;
467 flimode_t index = 0;
468 char cameraModeValue[MAXINDILABEL];
469 while ( FLIGetCameraModeString(fli_dev, index, cameraModeValue, MAXINDILABEL ) == 0)
470 {
471 cameraModeValues.push_back(cameraModeValue);
472 index++;
473 }
474
475 if (index > 0)
476 {
477 if (CameraModeS)
478 delete [] CameraModeS;
479 CameraModeS = new ISwitch[index];
480 for (int i = 0; i < index; i++)
481 IUFillSwitch(CameraModeS + i, cameraModeValues.at(i).c_str(), cameraModeValues.at(i).c_str(), ISS_OFF);
482
483 IUFillSwitchVector(&CameraModeSP, CameraModeS, index, getDeviceName(), "CAMERA_MODES", "Modes", OPTIONS_TAB, IP_RW,
484 ISR_1OFMANY, 0, IPS_IDLE);
485 FLIGetCameraMode(fli_dev, &index);
486 CameraModeS[index].s = ISS_ON;
487 }
488
489 return true;
490 }
491
492 int FLICCD::SetTemperature(double temperature)
493 {
494 int err = 0;
495
496 if (!sim && (err = FLISetTemperature(fli_dev, temperature)))
497 {
498 LOGF_ERROR("FLISetTemperature() failed. %s.", strerror(-err));
499 return -1;
500 }
501
502 FLICam.temperature = temperature;
503
504 LOGF_INFO("Setting CCD temperature to %.2f C", temperature);
505 return 0;
506 }
507
508 bool FLICCD::StartExposure(float duration)
509 {
510 int err = 0;
511
512 if (PrimaryCCD.getFrameType() == INDI::CCDChip::BIAS_FRAME)
513 {
514 // TODO check if this work with the SDK
515 duration = 0;
516 }
517
518 if (!sim)
519 {
520 if ((err = FLISetExposureTime(fli_dev, (long)(duration * 1000))))
521 {
522 LOGF_ERROR("FLISetExposureTime() failed. %s.", strerror(-err));
523 return false;
524 }
525 if ((err = FLIExposeFrame(fli_dev)))
526 {
527 LOGF_ERROR("FLIExposeFrame() failed. %s.", strerror(-err));
528 return false;
529 }
530 }
531
532 PrimaryCCD.setExposureDuration(duration);
533 ExposureRequest = duration;
534
535 gettimeofday(&ExpStart, nullptr);
536 LOGF_DEBUG("Taking a %g seconds frame...", ExposureRequest);
537
538 InExposure = true;
539 return true;
540 }
541
542 bool FLICCD::AbortExposure()
543 {
544 int err = 0;
545
546 if (!sim && (err = FLICancelExposure(fli_dev)))
547 {
548 LOGF_ERROR("FLICancelExposure() failed. %s.", strerror(-err));
549 return false;
550 }
551
552 InExposure = false;
553 return true;
554 }
555
556 bool FLICCD::UpdateCCDFrameType(INDI::CCDChip::CCD_FRAME fType)
557 {
558 if (sim)
559 return true;
560
561 int err = 0;
562 switch (fType)
563 {
564 case INDI::CCDChip::BIAS_FRAME:
565 case INDI::CCDChip::DARK_FRAME:
566 if ((err = FLISetFrameType(fli_dev, FLI_FRAME_TYPE_DARK)))
567 {
568 LOGF_ERROR("FLISetFrameType() failed. %s.", strerror(-err));
569 return false;
570 }
571 break;
572
573 case INDI::CCDChip::LIGHT_FRAME:
574 case INDI::CCDChip::FLAT_FRAME:
575 if ((err = FLISetFrameType(fli_dev, FLI_FRAME_TYPE_NORMAL)))
576 {
577 LOGF_ERROR("FLISetFrameType() failed. %s.", strerror(-err));
578 return false;
579 }
580 break;
581 }
582
583 return true;
584 }
585
586 bool FLICCD::UpdateCCDFrame(int x, int y, int w, int h)
587 {
588 int err = 0;
589
590 long bin_right = x + (w / PrimaryCCD.getBinX());
591 long bin_bottom = y + (h / PrimaryCCD.getBinY());
592
593 if ( (x + w) > PrimaryCCD.getXRes())
594 {
595 LOGF_ERROR("Error: invalid frame requested (%d,%d) size(%d,%d)", x, y, w, h);
596 return false;
597 }
598 else if ( (y + h) > PrimaryCCD.getYRes())
599 {
600 LOGF_ERROR("Error: invalid frame requested (%d,%d) size(%d,%d)", x, y, w, h);
601 return false;
602 }
603
604 LOGF_DEBUG("Binning (%dx%d). Final FLI image area is (%d, %d), (%ld, %ld). Size (%dx%d)", PrimaryCCD.getBinX(),
605 PrimaryCCD.getBinY(),
606 x, y, bin_right, bin_bottom, w / PrimaryCCD.getBinX(), h / PrimaryCCD.getBinY());
607
608 if (!sim && (err = FLISetImageArea(fli_dev, x, y, bin_right, bin_bottom)))
609 {
610 LOGF_ERROR("FLISetImageArea() failed. %s.", strerror(-err));
611 return false;
612 }
613
614 // Set UNBINNED coords
615 PrimaryCCD.setFrame(x, y, w, h);
616
617 int nbuf = (w / PrimaryCCD.getBinX()) * (h / PrimaryCCD.getBinY()) * (PrimaryCCD.getBPP() / 8);
618 PrimaryCCD.setFrameBufferSize(nbuf);
619
620 return true;
621 }
622
623 bool FLICCD::UpdateCCDBin(int binx, int biny)
624 {
625 int err = 0;
626
627 /* X horizontal binning */
628 if (!sim && (err = FLISetHBin(fli_dev, binx)))
629 {
630 LOGF_ERROR("FLISetBin() failed. %s.", strerror(-err));
631 return false;
632 }
633
634 /* Y vertical binning */
635 if (!sim && (err = FLISetVBin(fli_dev, biny)))
636 {
637 LOGF_ERROR("FLISetVBin() failed. %s.", strerror(-err));
638 return false;
639 }
640
641 PrimaryCCD.setBin(binx, biny);
642
643 return UpdateCCDFrame(PrimaryCCD.getSubX(), PrimaryCCD.getSubY(), PrimaryCCD.getSubW(), PrimaryCCD.getSubH());
644 }
645
646 // Downloads the image from the CCD.
647 bool FLICCD::grabImage()
648 {
649 std::unique_lock<std::mutex> guard(ccdBufferLock);
650 int err = 0;
651 uint8_t *image = PrimaryCCD.getFrameBuffer();
652 int row_size = PrimaryCCD.getSubW() / PrimaryCCD.getBinX() * PrimaryCCD.getBPP() / 8;
653 int width = PrimaryCCD.getSubW() / PrimaryCCD.getBinX();
654 int height = PrimaryCCD.getSubH() / PrimaryCCD.getBinY();
655
656 if (sim)
657 {
658 for (int i = 0; i < height; i++)
659 for (int j = 0; j < row_size; j++)
660 image[i * row_size + j] = rand() % 255;
661 }
662 else
663 {
664 bool success = true;
665 for (int i = 0; i < height; i++)
666 {
667 if ((err = FLIGrabRow(fli_dev, image + (i * row_size), width)))
668 {
669 /* print this error once but read to the end to flush the array */
670 if (success)
671 {
672 LOGF_ERROR("FLIGrabRow() failed at row %d. %s.", i, strerror(-err));
673 success = false;
674 }
675 }
676 }
677
678 if (!success)
679 return false;
680 }
681 guard.unlock();
682
683 LOG_INFO("Download complete.");
684
685 ExposureComplete(&PrimaryCCD);
686
687 return true;
688 }
689
690 void FLICCD::TimerHit()
691 {
692 int timerID = -1;
693 int err = 0;
694 long timeleft = 0;
695 double ccdTemp = 0;
696 double ccdPower = 0;
697 long camera_status = 0;
698
699 if (isConnected() == false)
700 return; // No need to reset timer if we are not connected anymore
701
702 if (InExposure)
703 {
704 if (sim)
705 {
706 PrimaryCCD.setExposureLeft(0);
707 InExposure = false;
708 grabImage();
709 }
710 else
711 {
712 if ((err = FLIGetDeviceStatus(fli_dev, &camera_status)))
713 {
714 LOGF_ERROR("FLIGetDeviceStatus() failed. %s.", strerror(-err));
715 SetTimer(getCurrentPollingPeriod());
716 return;
717 }
718 LOGF_DEBUG("FLIGetDeviceStatus() succeed -> %ld", camera_status);
719
720 if ((err = FLIGetExposureStatus(fli_dev, &timeleft)))
721 {
722 LOGF_ERROR("FLIGetExposureStatus() failed. %s.", strerror(-err));
723 SetTimer(getCurrentPollingPeriod());
724 return;
725 }
726 LOGF_DEBUG("FLIGetExposureStatus() succeed -> %ld", timeleft);
727
728 if (!sim && (((camera_status == FLI_CAMERA_STATUS_UNKNOWN) && (timeleft == 0)) ||
729 ((camera_status != FLI_CAMERA_STATUS_UNKNOWN) && ((camera_status & FLI_CAMERA_DATA_READY) != 0))))
730 {
731
732 /* We're done exposing */
733 LOG_INFO("Exposure done, downloading image...");
734
735 PrimaryCCD.setExposureLeft(0);
736 InExposure = false;
737 /* grab and save image */
738 grabImage();
739 }
740 else
741 {
742 LOGF_DEBUG("Exposure in progress. Time left: %ld seconds", timeleft / 1000);
743 PrimaryCCD.setExposureLeft(timeleft / 1000.0);
744 }
745 }
746 }
747
748 switch (TemperatureNP.s)
749 {
750 case IPS_IDLE:
751 case IPS_OK:
752 if (sim == false)
753 {
754 if ((err = FLIGetTemperature(fli_dev, &ccdTemp)))
755 {
756 TemperatureNP.s = IPS_IDLE;
757 IDSetNumber(&TemperatureNP, nullptr);
758 LOGF_ERROR("FLIGetTemperature() failed. %s.", strerror(-err));
759 break;
760 }
761 if ((err = FLIGetCoolerPower(fli_dev, &ccdPower)))
762 {
763 CoolerNP.s = IPS_IDLE;
764 IDSetNumber(&TemperatureNP, nullptr);
765 IDSetNumber(&TemperatureNP, "FLIGetCoolerPower() failed. %s.", strerror(-err));
766 break;
767 }
768 }
769
770 if (fabs(TemperatureN[0].value - ccdTemp) >= TEMP_THRESHOLD)
771 {
772 TemperatureN[0].value = ccdTemp;
773 IDSetNumber(&TemperatureNP, nullptr);
774 }
775
776 if (fabs(CoolerN[0].value - ccdPower) >= TEMP_THRESHOLD)
777 {
778 CoolerN[0].value = ccdPower;
779 CoolerNP.s = TemperatureNP.s;
780 IDSetNumber(&CoolerNP, nullptr);
781 }
782 break;
783
784 case IPS_BUSY:
785 if (sim)
786 {
787 ccdTemp = FLICam.temperature;
788 TemperatureN[0].value = ccdTemp;
789 }
790 else
791 {
792 if ((err = FLIGetTemperature(fli_dev, &ccdTemp)))
793 {
794 TemperatureNP.s = IPS_IDLE;
795 IDSetNumber(&TemperatureNP, nullptr);
796 LOGF_ERROR("FLIGetTemperature() failed. %s.", strerror(-err));
797 break;
798 }
799
800 if ((err = FLIGetCoolerPower(fli_dev, &ccdPower)))
801 {
802 CoolerNP.s = IPS_IDLE;
803 IDSetNumber(&TemperatureNP, "FLIGetCoolerPower() failed. %s.", strerror(-err));
804 break;
805 }
806 }
807
808 // if (fabs(FLICam.temperature - ccdTemp) <= TEMP_THRESHOLD)
809 // {
810 // TemperatureNP.s = IPS_OK;
811 // IDSetNumber(&TemperatureNP, nullptr);
812 // }
813
814 if (fabs(CoolerN[0].value - ccdPower) >= TEMP_THRESHOLD)
815 {
816 CoolerN[0].value = ccdPower;
817 CoolerNP.s = TemperatureNP.s;
818 IDSetNumber(&CoolerNP, nullptr);
819 }
820
821 TemperatureN[0].value = ccdTemp;
822 IDSetNumber(&TemperatureNP, nullptr);
823 break;
824
825 case IPS_ALERT:
826 break;
827 }
828
829 if (timerID == -1)
830 SetTimer(getCurrentPollingPeriod());
831 return;
832 }
833
834 bool FLICCD::findFLICCD(flidomain_t domain)
835 {
836 char **names;
837 long err;
838
839 LOGF_DEBUG("In find Camera, the domain is %ld", domain);
840
841 if ((err = FLIList(domain | FLIDEVICE_CAMERA, &names)))
842 {
843 LOGF_ERROR("FLIList() failed. %s", strerror(-err));
844 return false;
845 }
846
847 if (names != nullptr && names[0] != nullptr)
848 {
849 LOGF_DEBUG("Devices list: %s", names);
850
851 for (int i = 0; names[i] != nullptr; i++)
852 {
853 for (int j = 0; names[i][j] != '\0'; j++)
854 if (names[i][j] == ';')
855 {
856 names[i][j] = '\0';
857 break;
858 }
859 }
860
861 FLICam.domain = domain;
862
863 switch (domain)
864 {
865 case FLIDOMAIN_PARALLEL_PORT:
866 FLICam.dname = strdup("parallel port");
867 break;
868
869 case FLIDOMAIN_USB:
870 FLICam.dname = strdup("USB");
871 break;
872
873 case FLIDOMAIN_SERIAL:
874 FLICam.dname = strdup("serial");
875 break;
876
877 case FLIDOMAIN_INET:
878 FLICam.dname = strdup("inet");
879 break;
880
881 default:
882 FLICam.dname = strdup("Unknown domain");
883 }
884
885 FLICam.name = strdup(names[0]);
886
887 if ((err = FLIFreeList(names)))
888 {
889 LOGF_ERROR("FLIFreeList() failed. %s.", strerror(-err));
890 return false;
891 }
892
893 } /* end if */
894 else
895 {
896 LOG_ERROR("FLIList returned empty result!");
897
898 if ((err = FLIFreeList(names)))
899 {
900 LOGF_ERROR("FLIFreeList() failed. %s.", strerror(-err));
901 return false;
902 }
903
904 return false;
905 }
906
907 LOG_DEBUG("FindFLICCD() finished successfully.");
908
909 return true;
910 }
911
912 bool FLICCD::saveConfigItems(FILE *fp)
913 {
914 // Save CCD Config
915 INDI::CCD::saveConfigItems(fp);
916
917 IUSaveConfigNumber(fp, &FlushNP);
918 IUSaveConfigSwitch(fp, &BackgroundFlushSP);
919
920 if (CameraModeS)
921 IUSaveConfigSwitch(fp, &CameraModeSP);
922
923 return true;
924 }
925
926 void FLICCD::debugTriggered(bool enable)
927 {
928 if (enable)
929 FLISetDebugLevel(nullptr, FLIDEBUG_INFO);
930 else
931 FLISetDebugLevel(nullptr, FLIDEBUG_WARN);
932 }