/
gphoto_ccd.cpp
1379 lines (1142 loc) · 39.1 KB
/
gphoto_ccd.cpp
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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Driver type: GPhoto Camera INDI Driver
Copyright (C) 2009 Geoffrey Hausheer
Copyright (C) 2013 Jasem Mutlaq (mutlaqja AT ikarustech DOT com)
This library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this library; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <memory>
#include <time.h>
#include <math.h>
#include <unistd.h>
#include <sys/time.h>
#include <indidevapi.h>
#include <eventloop.h>
#include <indilogger.h>
#include "config.h"
extern "C" {
#include "gphoto_driver.h"
#include "gphoto_readimage.h"
}
#include "gphoto_ccd.h"
#define FOCUS_TAB "Focus"
#define MAX_DEVICES 5 /* Max device cameraCount */
#define POLLMS 1000
#define STREAMPOLLMS 50
#define FOCUS_TIMER 50
#define MAX_RETRIES 3
static int cameraCount;
static GPhotoCCD *cameras[MAX_DEVICES];
/**********************************************************
*
* IMPORRANT: List supported camera models in initializer of deviceTypes structure
*
**********************************************************/
static void cleanup() {
for (int i = 0; i < cameraCount; i++) {
delete cameras[i];
}
}
void ISInit()
{
static bool isInit = false;
if (!isInit)
{
// Let's just create one camera for now
cameraCount = 1;
cameras[0] = new GPhotoCCD();
atexit(cleanup);
isInit = true;
}
}
void ISGetProperties(const char *dev) {
ISInit();
for (int i = 0; i < cameraCount; i++) {
GPhotoCCD *camera = cameras[i];
if (dev == NULL || !strcmp(dev, camera->name)) {
camera->ISGetProperties(dev);
if (dev != NULL)
break;
}
}
}
void ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int num) {
ISInit();
for (int i = 0; i < cameraCount; i++) {
GPhotoCCD *camera = cameras[i];
if (dev == NULL || !strcmp(dev, camera->name)) {
camera->ISNewSwitch(dev, name, states, names, num);
if (dev != NULL)
break;
}
}
}
void ISNewText(const char *dev, const char *name, char *texts[], char *names[], int num) {
ISInit();
for (int i = 0; i < cameraCount; i++) {
GPhotoCCD *camera = cameras[i];
if (dev == NULL || !strcmp(dev, camera->name)) {
camera->ISNewText(dev, name, texts, names, num);
if (dev != NULL)
break;
}
}
}
void ISNewNumber(const char *dev, const char *name, double values[], char *names[], int num)
{
ISInit();
for (int i = 0; i < cameraCount; i++) {
GPhotoCCD *camera = cameras[i];
if (dev == NULL || !strcmp(dev, camera->name)) {
camera->ISNewNumber(dev, name, values, names, num);
if (dev != NULL)
break;
}
}
}
void ISNewBLOB(const char *dev, const char *name, int sizes[], int blobsizes[], char *blobs[], char *formats[], char *names[], int n) {
INDI_UNUSED(dev);
INDI_UNUSED(name);
INDI_UNUSED(sizes);
INDI_UNUSED(blobsizes);
INDI_UNUSED(blobs);
INDI_UNUSED(formats);
INDI_UNUSED(names);
INDI_UNUSED(n);
}
void ISSnoopDevice(XMLEle *root)
{
ISInit();
for (int i = 0; i < cameraCount; i++)
{
GPhotoCCD *camera = cameras[i];
camera->ISSnoopDevice(root);
}
}
//==========================================================================
GPhotoCCD::GPhotoCCD()
{
// For now let's set name to default name. In the future, we need to to support multiple devices per one driver
if (*getDeviceName() == '\0')
strncpy(name, getDefaultName(), MAXINDINAME);
else
strncpy(name, getDeviceName(), MAXINDINAME);
gphotodrv = NULL;
frameInitialized=false;
on_off[0] = strdup("On");
on_off[1] = strdup("Off");
setVersion(INDI_GPHOTO_VERSION_MAJOR, INDI_GPHOTO_VERSION_MINOR);
}
//==========================================================================
GPhotoCCD::~GPhotoCCD()
{
free(on_off[0]);
free(on_off[1]);
expTID = 0;
}
const char * GPhotoCCD::getDefaultName()
{
return (const char *)"GPhoto CCD";
}
void GPhotoCCD::debugTriggered(bool enable)
{
gphoto_set_debug(enable ? 1 : 0);
gphoto_read_set_debug(enable ? 1 : 0);
}
bool GPhotoCCD::initProperties()
{
// Init parent properties first
INDI::CCD::initProperties();
initFocuserProperties(getDeviceName(), FOCUS_TAB);
IUFillText(&mPortT[0], "PORT" , "Port", "");
IUFillTextVector(&PortTP, mPortT, NARRAY(mPortT), getDeviceName(), "SHUTTER_PORT" , "Shutter Release", MAIN_CONTROL_TAB, IP_RW, 0, IPS_IDLE);
IUFillNumber(&mMirrorLockN[0], "MIRROR_LOCK_SECONDS" , "Seconds", "%1.0f", 0, 10, 1, 0);
IUFillNumberVector(&mMirrorLockNP, mMirrorLockN, 1, getDeviceName(), "MIRROR_LOCK" , "Mirror Lock", MAIN_CONTROL_TAB, IP_RW, 60, IPS_IDLE);
//We don't know how many items will be in the switch yet
IUFillSwitchVector(&mIsoSP, NULL, 0, getDeviceName(), "CCD_ISO", "ISO", IMAGE_SETTINGS_TAB, IP_RW, ISR_1OFMANY, 60, IPS_IDLE);
IUFillSwitchVector(&mFormatSP, NULL, 0, getDeviceName(), "CAPTURE_FORMAT", "Capture Format", IMAGE_SETTINGS_TAB, IP_RW, ISR_1OFMANY, 60, IPS_IDLE);
IUFillSwitch(&autoFocusS[0], "Set", "", ISS_OFF);
IUFillSwitchVector(&autoFocusSP, autoFocusS, 1, getDeviceName(), "Auto Focus", "", FOCUS_TAB, IP_RW, ISR_1OFMANY, 0, IPS_IDLE);
IUFillSwitch(&transferFormatS[0], "FITS", "", ISS_ON);
IUFillSwitch(&transferFormatS[1], "Native", "", ISS_OFF);
IUFillSwitchVector(&transferFormatSP, transferFormatS, 2, getDeviceName(), "Transfer Format", "", IMAGE_SETTINGS_TAB, IP_RW, ISR_1OFMANY, 0, IPS_IDLE);
IUFillSwitch(&livePreviewS[0], "Enable", "", ISS_OFF);
IUFillSwitch(&livePreviewS[1], "Disable", "", ISS_ON);
IUFillSwitchVector(&livePreviewSP, livePreviewS, 2, getDeviceName(), "VIDEO_STREAM", "Preview", MAIN_CONTROL_TAB, IP_RW, ISR_1OFMANY, 0, IPS_IDLE);
PrimaryCCD.setMinMaxStep("CCD_EXPOSURE", "CCD_EXPOSURE_VALUE", 0.001, 3600, 1, false);
// Most cameras have this by default, so let's set it as default.
IUSaveText(&BayerT[2], "RGGB");
SetCCDCapability(CCD_CAN_SUBFRAME | CCD_HAS_BAYER);
SetFocuserCapability(FOCUSER_HAS_VARIABLE_SPEED);
FocusSpeedN[0].min=0;
FocusSpeedN[0].max=3;
FocusSpeedN[0].step=1;
FocusSpeedN[0].value=1;
/* JM 2014-05-20 Make PrimaryCCD.ImagePixelSizeNP writable since we can't know for now the pixel size and bit depth from gphoto */
PrimaryCCD.getCCDInfo()->p = IP_RW;
setDriverInterface(getDriverInterface() | FOCUSER_INTERFACE);
return true;
}
void GPhotoCCD::ISGetProperties(const char *dev)
{
INDI::CCD::ISGetProperties(dev);
defineText(&PortTP);
loadConfig(true, "DEVICE_PORT");
if (isConnected())
{
if(PortTP.tp[0].text && strlen(PortTP.tp[0].text))
{
defineNumber(&mMirrorLockNP);
}
if (mIsoSP.nsp > 0)
defineSwitch(&mIsoSP);
if (mFormatSP.nsp > 0)
defineSwitch(&mFormatSP);
defineSwitch(&transferFormatSP);
defineSwitch(&livePreviewSP);
defineSwitch(&autoFocusSP);
defineSwitch(&FocusMotionSP);
defineNumber(&FocusSpeedNP);
defineNumber(&FocusTimerNP);
ShowExtendedOptions();
}
// Add Debug, Simulator, and Configuration controls
addAuxControls();
}
bool GPhotoCCD::updateProperties()
{
INDI::CCD::updateProperties();
if (isConnected())
{
if(PortTP.tp[0].text && strlen(PortTP.tp[0].text))
{
defineNumber(&mMirrorLockNP);
}
if (mIsoSP.nsp > 0)
defineSwitch(&mIsoSP);
if (mFormatSP.nsp > 0)
defineSwitch(&mFormatSP);
defineSwitch(&transferFormatSP);
defineSwitch(&livePreviewSP);
defineSwitch(&autoFocusSP);
defineSwitch(&FocusMotionSP);
defineNumber(&FocusSpeedNP);
defineNumber(&FocusTimerNP);
imageBP=getBLOB("CCD1");
imageB=imageBP->bp;
// Dummy values until first capture is done
SetCCDParams(1280, 1024, 8, 5.4, 5.4);
if (sim == false)
{
ShowExtendedOptions();
DEBUG(INDI::Logger::DBG_SESSION, "Please update the camera pixel size in the Image Info section. The camera resolution will be updated after the first exposure is complete.");
}
//timerID = SetTimer(POLLMS);
} else
{
if (mIsoSP.nsp > 0)
deleteProperty(mIsoSP.name);
if (mFormatSP.nsp > 0)
deleteProperty(mFormatSP.name);
deleteProperty(mMirrorLockNP.name);
deleteProperty(livePreviewSP.name);
deleteProperty(autoFocusSP.name);
deleteProperty(transferFormatSP.name);
deleteProperty(FocusMotionSP.name);
deleteProperty(FocusSpeedNP.name);
deleteProperty(FocusTimerNP.name);
HideExtendedOptions();
//rmTimer(timerID);
}
return true;
}
bool GPhotoCCD::ISNewText (const char *dev, const char *name, char *texts[], char *names[], int n)
{
if(strcmp(dev,getDeviceName())==0)
{
if(strcmp(name,PortTP.name)==0)
{
PortTP.s=IPS_OK;
IUUpdateText(&PortTP,texts,names,n);
IDSetText(&PortTP, NULL);
return true;
}
if(CamOptions.find(name) != CamOptions.end())
{
cam_opt *opt = CamOptions[name];
if (opt->widget->type != GP_WIDGET_TEXT)
{
DEBUGF(INDI::Logger::DBG_ERROR, "ERROR: Property '%s'is not a string", name);
return false;
}
if (opt->widget->readonly)
{
DEBUGF(INDI::Logger::DBG_WARNING, "WARNING: Property %s is read-only", name);
IDSetText(&opt->prop.text, NULL);
return false;
}
if (IUUpdateText(&opt->prop.text, texts, names, n) < 0)
return false;
gphoto_set_widget_text(gphotodrv, opt->widget, texts[0]);
opt->prop.num.s = IPS_OK;
IDSetText(&opt->prop.text, NULL);
return true;
}
}
return INDI::CCD::ISNewText(dev, name, texts, names, n);
}
bool GPhotoCCD::ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n)
{
if (strcmp(dev, getDeviceName()) == 0)
{
if (!strcmp(name, mIsoSP.name))
{
if (IUUpdateSwitch(&mIsoSP, states, names, n) < 0)
return false;
for (int i = 0; i < mIsoSP.nsp; i++)
{
if (mIsoS[i].s == ISS_ON)
{
if (sim == false)
gphoto_set_iso(gphotodrv, i);
mIsoSP.s = IPS_OK;
IDSetSwitch(&mIsoSP, NULL);
break;
}
}
}
if (!strcmp(name, mFormatSP.name))
{
int prevSwitch = IUFindOnSwitchIndex(&mFormatSP);
if (IUUpdateSwitch(&mFormatSP, states, names, n) < 0)
return false;
ISwitch *sp = IUFindOnSwitch(&mFormatSP);
if (sp)
{
if (strstr(sp->label, "+"))
{
DEBUGF(INDI::Logger::DBG_ERROR, "%s format is not supported.", sp->label);
IUResetSwitch(&mFormatSP);
mFormatSP.s = IPS_ALERT;
mFormatSP.sp[prevSwitch].s = ISS_ON;
IDSetSwitch(&mFormatSP, NULL);
return false;
}
}
for (int i = 0; i < mFormatSP.nsp; i++)
{
if (mFormatS[i].s == ISS_ON)
{
if (sim == false)
gphoto_set_format(gphotodrv, i);
mFormatSP.s = IPS_OK;
IDSetSwitch(&mFormatSP, NULL);
// We need to get frame W and H if format changes
frameInitialized = false;
break;
}
}
}
if (!strcmp(name, transferFormatSP.name))
{
IUUpdateSwitch(&transferFormatSP, states, names, n);
transferFormatSP.s = IPS_OK;
IDSetSwitch(&transferFormatSP, NULL);
// We need to get frame W and H if transfer format changes
frameInitialized = false;
return true;
}
if (!strcmp(name, autoFocusSP.name))
{
IUResetSwitch(&autoFocusSP);
if (gphoto_auto_focus(gphotodrv) == GP_OK)
autoFocusSP.s = IPS_OK;
else
autoFocusSP.s = IPS_ALERT;
IDSetSwitch(&autoFocusSP, NULL);
return true;
}
if (!strcmp(name, UploadSP.name))
{
IUUpdateSwitch(&UploadSP, states, names, n);
UploadSP.s = IPS_OK;
IDSetSwitch(&UploadSP, NULL);
if (!sim)
gphoto_set_upload_settings(gphotodrv, IUFindOnSwitchIndex(&UploadSP));
return true;
}
if (!strcmp(name, livePreviewSP.name))
{
IUUpdateSwitch(&livePreviewSP, states, names, n);
if (livePreviewS[0].s == ISS_ON)
{
livePreviewSP.s = IPS_BUSY;
SetTimer(STREAMPOLLMS);
}
else
livePreviewSP.s = IPS_IDLE;
IDSetSwitch(&livePreviewSP, NULL);
return true;
}
if (strstr(name, "FOCUS"))
{
return processFocuserSwitch(dev, name, states, names, n);
}
if(CamOptions.find(name) != CamOptions.end())
{
cam_opt *opt = CamOptions[name];
if (opt->widget->type != GP_WIDGET_RADIO
&& opt->widget->type != GP_WIDGET_MENU
&& opt->widget->type != GP_WIDGET_TOGGLE)
{
DEBUGF(INDI::Logger::DBG_ERROR, "ERROR: Property '%s'is not a switch (%d)", name, opt->widget->type);
return false;
}
if (opt->widget->readonly)
{
DEBUGF(INDI::Logger::DBG_WARNING, "WARNING: Property %s is read-only", name);
IDSetSwitch(&opt->prop.sw, NULL);
return false;
}
if (IUUpdateSwitch(&opt->prop.sw, states, names, n) < 0)
return false;
if (opt->widget->type == GP_WIDGET_TOGGLE)
{
gphoto_set_widget_num(gphotodrv, opt->widget, opt->item.sw[ON_S].s == ISS_ON);
} else
{
for (int i = 0; i < opt->prop.sw.nsp; i++)
{
if (opt->item.sw[i].s == ISS_ON)
{
gphoto_set_widget_num(gphotodrv, opt->widget, i);
break;
}
}
}
opt->prop.sw.s = IPS_OK;
IDSetSwitch(&opt->prop.sw, NULL);
return true;
}
}
return INDI::CCD::ISNewSwitch(dev, name, states, names, n);
}
bool GPhotoCCD::ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n)
{
if (strcmp(dev, getDeviceName()) == 0)
{
if (strstr(name, "FOCUS_"))
return processFocuserNumber(dev, name, values, names, n);
if (!strcmp(name, mMirrorLockNP.name))
{
IUUpdateNumber(&mMirrorLockNP, values, names, n);
mMirrorLockNP.s = IPS_OK;
IDSetNumber(&mMirrorLockNP, NULL);
return true;
}
if(CamOptions.find(name) != CamOptions.end())
{
cam_opt *opt = CamOptions[name];
if (opt->widget->type != GP_WIDGET_RANGE)
{
DEBUGF(INDI::Logger::DBG_ERROR, "ERROR: Property '%s'is not a string", name);
return false;
}
if (opt->widget->readonly)
{
DEBUGF(INDI::Logger::DBG_WARNING, "WARNING: Property %s is read-only", name);
return false;
}
if (IUUpdateNumber(&opt->prop.num, values, names, n) < 0)
return false;
gphoto_set_widget_num(gphotodrv, opt->widget, values[0]);
opt->prop.num.s = IPS_OK;
IDSetNumber(&opt->prop.num, NULL);
return true;
}
}
return INDI::CCD::ISNewNumber(dev, name, values, names, n);
}
bool GPhotoCCD::Connect()
{
sim = isSimulation();
int setidx;
const char **options;
int max_opts;
const char *port = NULL;
fprintf(stderr, "Mirror lock value: %f\n", mMirrorLockN[0].value);
if(PortTP.tp[0].text && strlen(PortTP.tp[0].text)) {
port = PortTP.tp[0].text;
}
if (sim == false && ! (gphotodrv = gphoto_open(port)))
{
DEBUG(INDI::Logger::DBG_ERROR, "Can not open camera: Power OK?");
return false;
}
if (mFormatS)
{
free(mFormatS);
mFormatS=NULL;
}
if (sim)
{
setidx =0;
max_opts=1;
const char *fmts[] = { "Custom"};
options = fmts;
}
else
{
setidx = gphoto_get_format_current(gphotodrv);
options = gphoto_get_formats(gphotodrv, &max_opts);
}
if (max_opts > 0)
{
mFormatS = create_switch("FORMAT", options, max_opts, setidx);
mFormatSP.sp = mFormatS;
mFormatSP.nsp = max_opts;
ISwitch *sp = IUFindOnSwitch(&mFormatSP);
if (sp && strstr(sp->label, "+"))
{
IUResetSwitch(&mFormatSP);
int i=0;
// Prefer RAW format in case selected format is not supported.
for (i=0; i < mFormatSP.nsp; i++)
{
if (!strcmp("RAW", mFormatSP.sp[i].label))
{
mFormatS[i].s = ISS_ON;
break;
}
}
if (i == mFormatSP.nsp)
{
DEBUGF(INDI::Logger::DBG_ERROR, "%s format is not supported. Please select another format.", sp->label);
mFormatSP.s = IPS_ALERT;
}
IDSetSwitch(&mFormatSP, NULL);
}
}
if (mIsoS)
free(mIsoS);
if (sim)
{
setidx=0;
max_opts=4;
const char *isos[] = { "100", "200", "400", "800" };
options = isos;
}
else
{
setidx = gphoto_get_iso_current(gphotodrv);
options = gphoto_get_iso(gphotodrv, &max_opts);
}
mIsoS = create_switch("ISO", options, max_opts, setidx);
mIsoSP.sp = mIsoS;
mIsoSP.nsp = max_opts;
DEBUGF(INDI::Logger::DBG_SESSION, "%s is online.", getDeviceName());
frameInitialized = false;
return true;
}
bool GPhotoCCD::Disconnect()
{
if (sim)
return true;
gphoto_close(gphotodrv);
gphotodrv = NULL;
frameInitialized=false;
DEBUGF(INDI::Logger::DBG_SESSION, "%s is offline.", getDeviceName());
return true;
}
bool GPhotoCCD::StartExposure(float duration)
{
if (PrimaryCCD.isExposing())
{
DEBUG(INDI::Logger::DBG_ERROR, "GPhoto driver is already exposing. Can not abort.");
return false;
}
if (mFormatS != NULL)
{
ISwitch *sp = IUFindOnSwitch(&mFormatSP);
if (sp == NULL)
{
DEBUG(INDI::Logger::DBG_ERROR, "Please select a format before capturing an image.");
return false;
}
}
/* start new exposure with last ExpValues settings.
* ExpGo goes busy. set timer to read when done
*/
int expms = (int)ceil(duration*1000);
PrimaryCCD.setExposureDuration(duration);
if (sim == false && gphoto_start_exposure(gphotodrv, expms, mMirrorLockN[0].value) < 0)
{
DEBUG(INDI::Logger::DBG_ERROR, "Error starting exposure");
return false;
}
ExposureRequest = duration;
gettimeofday(&ExpStart, NULL);
InExposure = true;
DEBUGF(INDI::Logger::DBG_SESSION, "Starting %g sec exposure", duration);
SetTimer(POLLMS);
return true;
}
bool GPhotoCCD::UpdateCCDFrame(int x, int y, int w, int h)
{
if (transferFormatS[0].s != ISS_ON)
{
DEBUG(INDI::Logger::DBG_ERROR, "Subframing is only supported in FITS transport mode.");
return false;
}
PrimaryCCD.setFrame(x,y,w,h);
return true;
}
float GPhotoCCD::CalcTimeLeft()
{
double timesince;
double timeleft;
struct timeval now;
gettimeofday(&now,NULL);
timesince=(double)(now.tv_sec * 1000.0 + now.tv_usec/1000) - (double)(ExpStart.tv_sec * 1000.0 + ExpStart.tv_usec/1000);
timesince=timesince/1000;
timeleft=ExposureRequest-timesince;
return timeleft;
}
void GPhotoCCD::TimerHit()
{
long timeleft=1e6;
int timerID=-1;
if (isConnected() == false)
return;
if (livePreviewSP.s == IPS_BUSY)
{
bool rc = capturePreview();
if (rc)
timerID = SetTimer(STREAMPOLLMS);
else
{
livePreviewSP.s = IPS_ALERT;
DEBUG(INDI::Logger::DBG_ERROR, "Error capturing preview.");
livePreviewS[0].s = ISS_OFF;
livePreviewS[1].s = ISS_ON;
IDSetSwitch(&livePreviewSP, NULL);
}
}
if (FocusTimerNP.s == IPS_BUSY)
{
char errMsg[MAXRBUF];
if ( gphoto_manual_focus(gphotodrv, focusSpeed, errMsg) != GP_OK)
{
DEBUGF(INDI::Logger::DBG_ERROR, "Focusing failed: %s", errMsg);
FocusTimerNP.s = IPS_ALERT;
FocusTimerN[0].value = 0 ;
}
else
{
FocusTimerN[0].value -= FOCUS_TIMER;
if (FocusTimerN[0].value <= 0)
{
FocusTimerN[0].value = 0;
FocusTimerNP.s = IPS_OK;
}
else if (timerID == -1)
timerID = SetTimer(FOCUS_TIMER);
}
IDSetNumber(&FocusTimerNP, NULL);
}
if (InExposure)
{
timeleft = CalcTimeLeft();
if (timeleft < 1.0)
{
if (timeleft > 0.25 && timerID == -1)
{
// a quarter of a second or more
// just set a tighter timer
timerID = SetTimer(250);
}
else
{
if (timeleft > 0.07 && timerID == -1)
{
// use an even tighter timer
timerID = SetTimer(50);
}
else
{
// it's real close now, so spin on it
while (!sim && timeleft > 0)
{
int slv;
slv = 100000 * timeleft;
usleep(slv);
}
PrimaryCCD.setExposureLeft(0);
InExposure = false;
/* grab and save image */
bool rc = grabImage();
if (rc == false)
{
PrimaryCCD.setExposureFailed();
}
}
}
}
else
{
DEBUGF(INDI::Logger::DBG_DEBUG, "Capture in progress. Time left %ld", timeleft);
if (timerID == -1)
SetTimer(POLLMS);
}
PrimaryCCD.setExposureLeft(timeleft);
}
}
void GPhotoCCD::UpdateExtendedOptions (void *p)
{
GPhotoCCD *cam = (GPhotoCCD *)p;
cam->UpdateExtendedOptions();
}
void GPhotoCCD::UpdateExtendedOptions (bool force)
{
map<string, cam_opt *>::iterator it;
if(! expTID)
{
for ( it = CamOptions.begin() ; it != CamOptions.end(); it++ )
{
cam_opt *opt = (*it).second;
if(force || gphoto_widget_changed(opt->widget))
{
gphoto_read_widget(opt->widget);
UpdateWidget(opt);
}
}
}
optTID = IEAddTimer (1000, GPhotoCCD::UpdateExtendedOptions, this);
}
bool GPhotoCCD::grabImage()
{
//char ext[16];
uint8_t *memptr = PrimaryCCD.getFrameBuffer();
size_t memsize;
int fd, naxis=2, w, h, bpp=8;
if (sim)
{
w= PrimaryCCD.getXRes();
h= PrimaryCCD.getYRes();
size_t fullbuf_size = w*h + 512;
uint8_t * fullbuf = (uint8_t *) malloc(fullbuf_size);
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
fullbuf[i * w + j] = rand() % 255;
// Starting address if subframing
memptr = fullbuf + (PrimaryCCD.getSubY() * PrimaryCCD.getXRes()) + PrimaryCCD.getSubX();
memsize = PrimaryCCD.getSubW() * PrimaryCCD.getSubH() * PrimaryCCD.getBPP()/8;
PrimaryCCD.setFrameBuffer(memptr);
PrimaryCCD.setFrameBufferSize(memsize, false);
//PrimaryCCD.setResolution(w, h);
//PrimaryCCD.setFrame(0, 0, w, h);
PrimaryCCD.setNAxis(naxis);
PrimaryCCD.setBPP(bpp);
ExposureComplete(&PrimaryCCD);
return true;
}
// If only save to SD Card, let's not upload back to client
/*if (UploadS[GP_UPLOAD_SDCARD].s == ISS_ON)
{
DEBUG(INDI::Logger::DBG_SESSION, "Exposure complete. Image saved to SD Card.");
ExposureComplete(&PrimaryCCD);
return true;
}*/
if (transferFormatS[0].s == ISS_ON)
{
char tmpfile[] = "/tmp/indi_XXXXXX";
//dcraw can't read from stdin, so we need to write to disk then read it back
fd = mkstemp(tmpfile);
int ret = gphoto_read_exposure_fd(gphotodrv, fd);
if (ret != GP_OK || fd == -1)
{
if (fd == -1)
DEBUGF(INDI::Logger::DBG_ERROR, "Exposure failed to save image. Cannot create temp file %s", tmpfile);
else
DEBUGF(INDI::Logger::DBG_ERROR, "Exposure failed to save image... %s", gp_result_as_string(ret));
unlink(tmpfile);
return false;
}
if (!strcmp(gphoto_get_file_extension(gphotodrv), "unknown"))
{
DEBUG(INDI::Logger::DBG_ERROR, "Exposure failed.");
return false;
}
/* We're done exposing */
DEBUG(INDI::Logger::DBG_SESSION, "Exposure done, downloading image...");
if(strcasecmp(gphoto_get_file_extension(gphotodrv), "jpg") == 0 ||
strcasecmp(gphoto_get_file_extension(gphotodrv), "jpeg") == 0)
{
if (read_jpeg(tmpfile, &memptr, &memsize, &naxis, &w, &h))
{
DEBUG(INDI::Logger::DBG_ERROR, "Exposure failed to parse jpeg.");
unlink(tmpfile);
return false;
}
DEBUGF(INDI::Logger::DBG_DEBUG, "read_jpeg: memsize (%d) naxis (%d) w (%d) h (%d) bpp (%d)", memsize, naxis, w, h, bpp);
}
else
{
if (read_dcraw(tmpfile, &memptr, &memsize, &naxis, &w, &h, &bpp))
{
DEBUG(INDI::Logger::DBG_ERROR, "Exposure failed to parse raw image.");
unlink(tmpfile);
return false;
}
DEBUGF(INDI::Logger::DBG_DEBUG, "read_dcraw: memsize (%d) naxis (%d) w (%d) h (%d) bpp (%d)", memsize, naxis, w, h, bpp);
unlink(tmpfile);
}
PrimaryCCD.setImageExtension("fits");
// If subframing is requested
if (frameInitialized && (PrimaryCCD.getSubH() < PrimaryCCD.getYRes() || PrimaryCCD.getSubW() < PrimaryCCD.getXRes()))
{
int subFrameSize = PrimaryCCD.getSubW() * PrimaryCCD.getSubH() * bpp/8 * ((naxis == 3) ? 3 : 1);
int oneFrameSize = PrimaryCCD.getSubW() * PrimaryCCD.getSubH() * bpp/8;
uint8_t *subframeBuf = (uint8_t *) malloc(subFrameSize);
int startY= PrimaryCCD.getSubY();
int endY = startY + PrimaryCCD.getSubH();
int lineW = PrimaryCCD.getSubW() * bpp/8;
int subX = PrimaryCCD.getSubX();
if (naxis == 2)
{
for (int i=startY ; i < endY; i++)
memcpy(subframeBuf + (i - startY) * lineW, memptr + (i * w + subX) * bpp/8 , lineW);
}
else
{
uint8_t *subR = subframeBuf;
uint8_t *subG = subframeBuf + oneFrameSize;
uint8_t *subB = subframeBuf + oneFrameSize * 2;
uint8_t *startR = memptr;
uint8_t *startG = memptr + (w * h * bpp/8);
uint8_t *startB = memptr + (w * h * bpp/8 * 2);
for (int i=startY; i < endY; i++)
{
memcpy(subR + (i-startY) * lineW, startR + (i * w + subX) * bpp/8 , lineW );
memcpy(subG + (i-startY) * lineW, startG + (i * w + subX) * bpp/8 , lineW );
memcpy(subB + (i-startY) * lineW, startB + (i * w + subX) * bpp/8 , lineW );