forked from OpenPHDGuiding/phd2
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcam_indi.cpp
1427 lines (1232 loc) · 43.4 KB
/
cam_indi.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
/*
* cam_indi.cpp
* PHD Guiding
*
* Created by Geoffrey Hausheer.
* Copyright (c) 2009 Geoffrey Hausheer
* Copyright (c) 2014 Patrick Chevalley
* Copyright (c) 2020 Andy Galasso
* All rights reserved.
*
* This source code is distributed under the following "BSD" license
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of Craig Stark, Stark Labs nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "phd.h"
#ifdef INDI_CAMERA
#include "cam_indi.h"
#include "camera.h"
#include "config_indi.h"
#include "image_math.h"
#include "indi_gui.h"
#include "phdindiclient.h"
#include <libindi/basedevice.h>
#include <libindi/indiproperty.h>
#include <thread>
#include <iostream>
#include <mutex>
class CapturedFrame
{
public:
void *m_data;
size_t m_size;
char m_format[MAXINDIBLOBFMT];
CapturedFrame() {
m_data = nullptr;
m_size = 0;
m_format[0] = 0;
}
~CapturedFrame() {
#ifdef INDI_SHARED_BLOB_SUPPORT
IDSharedBlobFree(m_data);
#else
free(m_data);
#endif
}
// Take ownership of this blob's data, so INDI won't overwrite/free the memory
void steal(IBLOB *bp) {
m_data = bp->blob;
m_size = bp->size;
strncpy(m_format, bp->format, MAXINDIBLOBFMT);
bp->blob = nullptr;
bp->size = 0;
}
};
class CameraINDI : public GuideCamera, public PhdIndiClient
{
private:
ISwitchVectorProperty *connection_prop;
INumberVectorProperty *expose_prop;
INumberVectorProperty *frame_prop;
INumber *frame_x;
INumber *frame_y;
INumber *frame_width;
INumber *frame_height;
ISwitchVectorProperty *frame_type_prop;
INumberVectorProperty *ccdinfo_prop;
INumberVectorProperty *binning_prop;
INumber *binning_x;
INumber *binning_y;
ISwitchVectorProperty *video_prop;
ITextVectorProperty *camera_port;
INDI::BaseDevice *camera_device;
INumberVectorProperty *pulseGuideNS_prop;
INumber *pulseN_prop;
INumber *pulseS_prop;
INumberVectorProperty *pulseGuideEW_prop;
INumber *pulseE_prop;
INumber *pulseW_prop;
wxMutex sync_lock;
wxCondition sync_cond;
bool guide_active;
GuideAxis guide_active_axis;
IndiGui *m_gui;
wxMutex m_lastFrame_lock;
wxCondition m_lastFrame_cond;
CapturedFrame *m_lastFrame;
usImage *StackImg;
int StackFrames;
volatile bool stacking; // TODO: use a wxCondition to signal completion
bool has_blob;
bool has_old_videoprop;
bool first_frame;
volatile bool modal;
bool ready;
wxByte m_bitsPerPixel;
double PixSize;
double PixSizeX;
double PixSizeY;
wxRect m_maxSize;
wxByte m_curBinning;
bool HasBayer;
long INDIport;
wxString INDIhost;
wxString INDICameraName;
long INDICameraCCD;
wxString INDICameraCCDCmd;
wxString INDICameraBlobName;
bool INDICameraForceVideo;
bool INDICameraForceExposure;
wxRect m_roi;
bool ConnectToDriver(RunInBg *ctx);
void SetCCDdevice();
void ClearStatus();
void CheckState();
void CameraDialog();
void CameraSetup();
bool ReadFITS(CapturedFrame *cf, usImage& img, bool takeSubframe, const wxRect& subframe);
bool StackStream(CapturedFrame *cf);
void SendBinning();
// Update the last frame, discarding any missed frame
void updateLastFrame(IBLOB *bp);
// Wait until a frame was acquired, for limited amout of time.
// If non null is returned, caller is responsible for deletion
CapturedFrame *waitFrame(unsigned long waitTime);
protected:
void newDevice(INDI::BaseDevice *dp) override;
void removeDevice(INDI::BaseDevice *dp) override;
void newProperty(INDI::Property *property) override;
void removeProperty(INDI::Property *property) override {}
void newBLOB(IBLOB *bp) override;
void newSwitch(ISwitchVectorProperty *svp) override;
void newNumber(INumberVectorProperty *nvp) override;
void newMessage(INDI::BaseDevice *dp, int messageID) override;
void newText(ITextVectorProperty *tvp) override;
void newLight(ILightVectorProperty *lvp) override {}
void IndiServerConnected() override;
void IndiServerDisconnected(int exit_code) override;
public:
std::mutex sendMutex;
CameraINDI();
~CameraINDI();
bool Connect(const wxString& camId) override;
bool Disconnect() override;
bool HasNonGuiCapture() override;
wxByte BitsPerPixel() override;
bool GetDevicePixelSize(double *pixSize) override;
void ShowPropertyDialog() override;
bool Capture(int duration, usImage& img, int options, const wxRect& subframe) override;
bool ST4PulseGuideScope(int direction, int duration) override;
bool ST4HasNonGuiMove() override;
};
CameraINDI::CameraINDI()
:
sync_cond(sync_lock),
m_lastFrame_cond(m_lastFrame_lock),
m_gui(nullptr)
{
m_lastFrame = nullptr;
ClearStatus();
// load the values from the current profile
INDIhost = pConfig->Profile.GetString("/indi/INDIhost", _T("localhost"));
INDIport = pConfig->Profile.GetLong("/indi/INDIport", 7624);
INDICameraName = pConfig->Profile.GetString("/indi/INDIcam", _T("INDI Camera"));
INDICameraCCD = pConfig->Profile.GetLong("/indi/INDIcam_ccd", 0);
INDICameraForceVideo = pConfig->Profile.GetBoolean("/indi/INDIcam_forcevideo", false);
INDICameraForceExposure = pConfig->Profile.GetBoolean("/indi/INDIcam_forceexposure", false);
Name = wxString::Format("INDI Camera [%s]", INDICameraName);
SetCCDdevice();
PropertyDialogType = PROPDLG_ANY;
HasSubframes = true;
m_bitsPerPixel = 0;
HasBayer = false;
}
CameraINDI::~CameraINDI()
{
if (m_gui)
IndiGui::DestroyIndiGui(&m_gui);
DisconnectIndiServer();
}
void CameraINDI::ClearStatus()
{
// reset properties pointer
connection_prop = nullptr;
expose_prop = nullptr;
frame_prop = nullptr;
frame_type_prop = nullptr;
ccdinfo_prop = nullptr;
binning_prop = nullptr;
video_prop = nullptr;
camera_port = nullptr;
camera_device = nullptr;
pulseGuideNS_prop = nullptr;
pulseGuideEW_prop = nullptr;
// reset connection status
has_blob = false;
Connected = false;
ready = false;
m_hasGuideOutput = false;
PixSize = PixSizeX = PixSizeY = 0.0;
updateLastFrame(nullptr);
guide_active = false;
sync_cond.Broadcast(); // just in case worker thread was blocked waiting for guide pulse to complete
}
CapturedFrame *CameraINDI::waitFrame(unsigned long waitTime)
{
wxMutexLocker lck(m_lastFrame_lock);
if (!m_lastFrame) {
m_lastFrame_cond.WaitTimeout(waitTime);
}
if (m_lastFrame) {
auto ret = m_lastFrame;
m_lastFrame = nullptr;
return ret;
}
return nullptr;
}
void CameraINDI::updateLastFrame(IBLOB *blob)
{
bool notify = false;
{
wxMutexLocker lck(m_lastFrame_lock);
if (m_lastFrame != nullptr) {
delete m_lastFrame;
m_lastFrame = nullptr;
}
if (blob) {
m_lastFrame = new CapturedFrame();
m_lastFrame->steal(blob);
notify = true;
}
}
if (notify) {
Debug.Write(wxString::Format("lastFrame signaled Camera is ready\n"));
m_lastFrame_cond.Broadcast();
}
}
void CameraINDI::CheckState()
{
// Check if the device has all the required properties for our usage.
DEBUG_INFO("cam_indi.cpp | CheckState | modal=%d ready=%d",modal,ready);
if (has_blob && camera_device && Connected && (expose_prop || video_prop))
{
if (!ready)
{
Debug.Write(wxString::Format("INDI Camera is ready\n"));
ready = true;
first_frame = true;
if (modal)
modal = false;
}
}
}
void CameraINDI::newDevice(INDI::BaseDevice *dp)
{
DEBUG_INFO("cam_indi.cpp | new Device");
if (strcmp(dp->getDeviceName(), INDICameraName.mb_str(wxConvUTF8)) == 0)
{
// The camera object
camera_device = dp;
}
}
void CameraINDI::newSwitch(ISwitchVectorProperty *svp)
{
// we go here every time a Switch state change
if (INDIConfig::Verbose())
Debug.Write(wxString::Format("INDI Camera Received Switch: %s = %i\n", svp->name, svp->sp->s));
if (strcmp(svp->name, "CONNECTION") == 0)
{
ISwitch *connectswitch = IUFindSwitch(svp, "CONNECT");
if (connectswitch->s == ISS_ON)
{
Connected = true;
}
else
{
if (ready)
{
ClearStatus();
// call Disconnect in the main thread since that will
// want to join the INDI worker thread which is
// probably the current thread
PhdApp::ExecInMainThread(
[this]() {
DisconnectWithAlert(_("INDI camera disconnected"), NO_RECONNECT);
});
}
}
}
}
void CameraINDI::newMessage(INDI::BaseDevice *dp, int messageID)
{
// we go here every time the camera driver send a message
DEBUG_INFO("%s",dp->messageQueue(messageID).data());
if (INDIConfig::Verbose())
Debug.Write(wxString::Format("INDI Camera Received message: %s\n", dp->messageQueue(messageID)));
}
inline static const char *StateStr(IPState st)
{
switch (st) {
default: case IPS_IDLE: return "Idle";
case IPS_OK: return "Ok";
case IPS_BUSY: return "Busy";
case IPS_ALERT: return "Alert";
}
}
void CameraINDI::newNumber(INumberVectorProperty *nvp)
{
// we go here every time a Number value change
if (INDIConfig::Verbose())
{
if (strcmp(nvp->name, "CCD_EXPOSURE") == 0 )
{
// rate limit this one, it's too noisy
static double s_lastval;
if (nvp->np->value > 0.0 && fabs(nvp->np->value - s_lastval) < 0.5)
return;
s_lastval = nvp->np->value;
}
std::ostringstream os;
for (int i = 0; i < nvp->nnp; i++)
{
if (i) os << ',';
os << nvp->np[i].name << ':' << nvp->np[i].value;
}
Debug.Write(wxString::Format("INDI Camera Received Number: %s = %s state = %s\n", nvp->name, os.str().c_str(), StateStr(nvp->s)));
}
if (nvp == ccdinfo_prop)
{
PixSize = IUFindNumber(ccdinfo_prop, "CCD_PIXEL_SIZE")->value;
PixSizeX = IUFindNumber(ccdinfo_prop, "CCD_PIXEL_SIZE_X")->value;
PixSizeY = IUFindNumber(ccdinfo_prop, "CCD_PIXEL_SIZE_Y")->value;
m_maxSize.x = IUFindNumber(ccdinfo_prop, "CCD_MAX_X")->value;
m_maxSize.y = IUFindNumber(ccdinfo_prop, "CCD_MAX_Y")->value;
// defer defining FullSize since it is not simply derivable from max size and binning
// no: FullSize = wxSize(m_maxSize.x / Binning, m_maxSize.y / Binning);
m_bitsPerPixel = IUFindNumber(ccdinfo_prop, "CCD_BITSPERPIXEL")->value;
}
else if (nvp == binning_prop)
{
MaxBinning = wxMin(binning_x->max, binning_y->max);
m_curBinning = wxMin(binning_x->value, binning_y->value);
if (Binning > MaxBinning)
Binning = MaxBinning;
// defer defining FullSize since it is not simply derivable from max size and binning
// no: FullSize = wxSize(m_maxSize.x / Binning, m_maxSize.y / Binning);
}
else if (nvp == pulseGuideEW_prop || nvp == pulseGuideNS_prop)
{
bool notify = false;
{
wxMutexLocker lck(sync_lock);
if (guide_active && nvp->s != IPS_BUSY &&
((guide_active_axis == GUIDE_RA && nvp == pulseGuideEW_prop) ||
(guide_active_axis == GUIDE_DEC && nvp == pulseGuideNS_prop)))
{
guide_active = false;
notify = true;
}
else if (!guide_active && nvp->s == IPS_BUSY)
{
guide_active = true;
guide_active_axis = nvp == pulseGuideEW_prop ? GUIDE_RA : GUIDE_DEC;
}
}
if (notify)
sync_cond.Broadcast();
}
}
void CameraINDI::newText(ITextVectorProperty *tvp)
{
// we go here every time a Text value change
if (INDIConfig::Verbose())
Debug.Write(wxString::Format("INDI Camera Received Text: %s = %s\n", tvp->name, tvp->tp->text));
}
void CameraINDI::newBLOB(IBLOB *bp)
{
// we go here every time a new blob is available
// this is normally the image from the camera
if (INDIConfig::Verbose())
Debug.Write(wxString::Format("INDI Camera Received BLOB %s len=%d size=%d\n", bp->name, bp->bloblen, bp->size));
if (expose_prop && !INDICameraForceVideo)
{
if (bp->name == INDICameraBlobName)
{
updateLastFrame(bp);
}
}
else if (video_prop)
{
if (modal && !stacking)
{
CapturedFrame cf;
cf.steal(bp);
StackStream(&cf);
}
}
}
void CameraINDI::newProperty(INDI::Property *property)
{
// Here we receive a list of all the properties after the connection
// Updated values are not received here but in the newTYPE() functions above.
// We keep the vector for each interesting property to send some data later.
wxString PropName(property->getName());
INDI_PROPERTY_TYPE Proptype = property->getType();
if (INDIConfig::Verbose())
Debug.Write(wxString::Format("INDI Camera Property: %s\n", property->getName()));
if (Proptype == INDI_BLOB)
{
if (INDIConfig::Verbose())
Debug.Write(wxString::Format("INDI Camera Found BLOB property for %s %s\n", property->getDeviceName(), PropName));
if (PropName == INDICameraBlobName)
{
has_blob = 1;
// set option to receive blob and messages for the selected CCD
setBLOBMode(B_ALSO, INDICameraName.mb_str(wxConvUTF8), INDICameraBlobName.mb_str(wxConvUTF8));
#ifdef INDI_SHARED_BLOB_SUPPORT
// Allow faster mode provided we don't modify the blob content or free/realloc it
enableDirectBlobAccess(INDICameraName.mb_str(wxConvUTF8), INDICameraBlobName.mb_str(wxConvUTF8));
#endif
}
}
else if (PropName == INDICameraCCDCmd + "EXPOSURE" && Proptype == INDI_NUMBER)
{
if (INDIConfig::Verbose())
Debug.Write(wxString::Format("INDI Camera Found CCD_EXPOSURE for %s %s\n", property->getDeviceName(), PropName));
expose_prop = property->getNumber();
}
else if (PropName == INDICameraCCDCmd + "FRAME" && Proptype == INDI_NUMBER)
{
if (INDIConfig::Verbose())
Debug.Write(wxString::Format("INDI Camera Found CCD_FRAME for %s %s\n", property->getDeviceName(), PropName));
frame_prop = property->getNumber();
frame_x = IUFindNumber(frame_prop, "X");
frame_y = IUFindNumber(frame_prop, "Y");
frame_width = IUFindNumber(frame_prop, "WIDTH");
frame_height = IUFindNumber(frame_prop, "HEIGHT");
}
else if (PropName == INDICameraCCDCmd + "FRAME_TYPE" && Proptype == INDI_SWITCH)
{
if (INDIConfig::Verbose())
Debug.Write(wxString::Format("INDI Camera Found CCD_FRAME_TYPE for %s %s\n", property->getDeviceName(), PropName));
frame_type_prop = property->getSwitch();
}
else if (PropName == INDICameraCCDCmd + "BINNING" && Proptype == INDI_NUMBER)
{
if (INDIConfig::Verbose())
Debug.Write(wxString::Format("INDI Camera Found CCD_BINNING for %s %s\n", property->getDeviceName(), PropName));
binning_prop = property->getNumber();
binning_x = IUFindNumber(binning_prop, "HOR_BIN");
binning_y = IUFindNumber(binning_prop, "VER_BIN");
newNumber(binning_prop);
}
else if (PropName == INDICameraCCDCmd + "CFA" && Proptype == INDI_TEXT)
{
if (INDIConfig::Verbose())
Debug.Write(wxString::Format("INDI Camera Found CCD_CFA for %s %s\n", property->getDeviceName(), PropName));
ITextVectorProperty *cfa_prop = property->getText();
IText *cfa_type = IUFindText(cfa_prop, "CFA_TYPE");
if (cfa_type && cfa_type->text && *cfa_type->text)
{
if (INDIConfig::Verbose())
Debug.Write(wxString::Format("INDI Camera CFA_TYPE is %s\n", cfa_type->text));
HasBayer = true;
}
}
else if (PropName == INDICameraCCDCmd + "VIDEO_STREAM" && Proptype == INDI_SWITCH)
{
if (INDIConfig::Verbose())
Debug.Write(wxString::Format("INDI Camera Found Video %s %s\n", property->getDeviceName(), PropName));
video_prop = property->getSwitch();
has_old_videoprop = false;
}
else if (PropName == "VIDEO_STREAM" && Proptype == INDI_SWITCH)
{
if (INDIConfig::Verbose())
Debug.Write(wxString::Format("INDI Camera Found Video %s %s\n", property->getDeviceName(), PropName));
video_prop = property->getSwitch();
has_old_videoprop = true;
}
else if (PropName == "DEVICE_PORT" && Proptype == INDI_TEXT)
{
if (INDIConfig::Verbose())
Debug.Write(wxString::Format("INDI Camera Found device port for %s \n", property->getDeviceName()));
camera_port = property->getText();
}
else if (PropName == "CONNECTION" && Proptype == INDI_SWITCH)
{
if (INDIConfig::Verbose())
Debug.Write(wxString::Format("INDI Camera Found CONNECTION for %s %s\n", property->getDeviceName(), PropName));
// Check the value here in case the device is already connected
connection_prop = property->getSwitch();
ISwitch *connectswitch = IUFindSwitch(connection_prop, "CONNECT");
Connected = (connectswitch->s == ISS_ON);
}
else if (PropName == "DRIVER_INFO" && Proptype == INDI_TEXT)
{
if (camera_device && (camera_device->getDriverInterface() & INDI::BaseDevice::GUIDER_INTERFACE))
m_hasGuideOutput = true; // Device supports guiding
}
else if (PropName == "TELESCOPE_TIMED_GUIDE_NS" && Proptype == INDI_NUMBER)
{
pulseGuideNS_prop = property->getNumber();
pulseN_prop = IUFindNumber(pulseGuideNS_prop, "TIMED_GUIDE_N");
pulseS_prop = IUFindNumber(pulseGuideNS_prop, "TIMED_GUIDE_S");
}
else if (PropName == "TELESCOPE_TIMED_GUIDE_WE" && Proptype == INDI_NUMBER)
{
pulseGuideEW_prop = property->getNumber();
pulseW_prop = IUFindNumber(pulseGuideEW_prop, "TIMED_GUIDE_W");
pulseE_prop = IUFindNumber(pulseGuideEW_prop, "TIMED_GUIDE_E");
}
else if (PropName == INDICameraCCDCmd + "INFO" && Proptype == INDI_NUMBER)
{
ccdinfo_prop = property->getNumber();
newNumber(ccdinfo_prop);
}
CheckState();
}
bool CameraINDI::Connect(const wxString& camId)
{
INDICameraName =camId; //QHY MOD
wxString choice = camId; //return the selected item from the list box
std::string aaa = std::string(choice.ToStdString());
DEBUG_INFO("cam_indi | Connect xxx1 | %s",aaa.c_str());
pConfig->Profile.SetString("/indi/INDIcam", camId);
// If not configured open the setup dialog
if (INDICameraName == wxT("INDI Camera"))
{
CameraSetup();
DEBUG_INFO("cam_indi | Connect | CameraSetup()");
}
Debug.Write(wxString::Format("INDI Camera connecting to device [%s]\n", INDICameraName));
aaa = std::string(INDICameraName.ToStdString());
DEBUG_INFO("cam_indi | Connect xxx2 | %s",aaa.c_str());
// define server to connect to.
setServer(INDIhost.mb_str(wxConvUTF8), INDIport);
// Receive messages only for our camera.
watchDevice(INDICameraName.mb_str(wxConvUTF8));
// Connect to server.
if (connectServer())
{
Debug.Write(wxString::Format("INDI Camera: connectServer done ready = %d\n", ready));
return !ready;
}
// last chance to fix the setup
CameraSetup();
setServer(INDIhost.mb_str(wxConvUTF8), INDIport);
watchDevice(INDICameraName.mb_str(wxConvUTF8));
if (connectServer())
{
Debug.Write(wxString::Format("INDI Camera: connectServer [2] done ready = %d\n", ready));
return !ready;
}
return true;
}
wxByte CameraINDI::BitsPerPixel()
{
return m_bitsPerPixel;
}
bool CameraINDI::Disconnect()
{
// Disconnect from server (no-op if not connected)
DisconnectIndiServer();
return false;
}
bool CameraINDI::ConnectToDriver(RunInBg *r)
{
modal = true;
// wait for the device port property
wxLongLong msec = wxGetUTCTimeMillis();
DEBUG_INFO("cam_indi.cpp | ConnectToDriver | start");
// Connect the camera device
while (!connection_prop && wxGetUTCTimeMillis() - msec < 15 * 1000)
{
if (r->IsCanceled())
{
modal = false;
return false;
}
DEBUG_INFO("cam_indi.cpp | ConnectToDriver | %d",wxGetUTCTimeMillis() - msec);
wxMilliSleep(20);
}
if (!connection_prop)
{
r->SetErrorMsg(_("connection timed-out"));
modal = false;
return false;
}
DEBUG_INFO("cam_indi.cpp | ConnectToDriver | 1 %d",modal);
connectDevice(INDICameraName.mb_str(wxConvUTF8));
DEBUG_INFO("cam_indi.cpp | ConnectToDriver | 2 %d",modal);
CheckState(); //add by QHY
CheckState(); //add by QHY
msec = wxGetUTCTimeMillis();
while (modal && wxGetUTCTimeMillis() - msec < 30 * 1000)
{
if (r->IsCanceled())
{
modal = false;
return false;
}
wxMilliSleep(20);
}
if (!ready)
{
r->SetErrorMsg(_("Connection timed-out"));
}
DEBUG_INFO("cam_indi.cpp | ConnectToDriver | 3 %d",modal);
modal = false;
return ready;
}
void CameraINDI::IndiServerConnected()
{
// After connection to the server
DEBUG_INFO("cam_indi.cpp | IndiserverConnected | connect5?");
struct ConnectInBg : public ConnectCameraInBg
{
CameraINDI *cam;
ConnectInBg(CameraINDI *cam_) : cam(cam_) { }
bool Entry()
{
return !cam->ConnectToDriver(this);
}
};
ConnectInBg bg(this);
if (bg.Run())
{
Debug.Write(wxString::Format("INDI Camera bg connection failed canceled=%d\n", bg.IsCanceled()));
CamConnectFailed(wxString::Format(_("Cannot connect to camera %s: %s"), INDICameraName, bg.GetErrorMsg()));
Connected = false;
Disconnect();
}
else
{
Debug.Write("INDI Camera bg connection succeeded\n");
Connected = true;
}
}
void CameraINDI::IndiServerDisconnected(int exit_code)
{
Debug.Write("INDI Camera: serverDisconnected\n");
// after disconnection we reset the connection status and the properties pointers
ClearStatus();
// in case the connection lost we must reset the client socket
if (exit_code == -1)
DisconnectWithAlert(_("INDI server disconnected"), NO_RECONNECT);
}
void CameraINDI::removeDevice(INDI::BaseDevice *dp)
{
ClearStatus();
DisconnectWithAlert(_("INDI camera disconnected"), NO_RECONNECT);
}
void CameraINDI::ShowPropertyDialog()
{
if (Connected)
{
// show the devices INDI dialog
CameraDialog();
}
else
{
// show the server and device configuration
CameraSetup();
}
}
void CameraINDI::CameraDialog()
{
if (m_gui)
{
m_gui->Show();
}
else
{
IndiGui::ShowIndiGui(&m_gui, INDIhost, INDIport, false, false);
}
}
void CameraINDI::CameraSetup()
{
GearDialog *pGearDialog;
DEBUG_INFO("cam_indi.cpp | CameraSetup()");
// show the server and device configuration
INDIConfig indiDlg(wxGetApp().GetTopWindow(), _("INDI Camera Selection"), INDI_TYPE_CAMERA);
indiDlg.INDIhost = INDIhost;
indiDlg.INDIport = INDIport;
indiDlg.INDIDevName = INDICameraName;
indiDlg.INDIDevCCD = INDICameraCCD;
indiDlg.INDIForceVideo = INDICameraForceVideo;
indiDlg.INDIForceExposure = INDICameraForceExposure;
std::string aaa = std::string(INDICameraName.ToStdString());
DEBUG_INFO("cam_indi.cpp | CameraSetup() | INDICameraName %s",aaa.c_str());
// initialize with actual values
indiDlg.SetSettings();
// try to connect to server
indiDlg.Connect();
DEBUG_INFO("cam_indi.cpp | connect 3");
if (indiDlg.ShowModal() == wxID_OK)
{
// if OK save the values to the current profile
indiDlg.SaveSettings();
INDIhost = indiDlg.INDIhost;
INDIport = indiDlg.INDIport;
INDICameraName = indiDlg.INDIDevName;
// if(pGearDialog->glCameraIdFromShm != "INDI Camera")
// {
// INDICameraName = pGearDialog->glCameraIdFromShm;
// }
INDICameraCCD = indiDlg.INDIDevCCD;
INDICameraForceVideo = indiDlg.INDIForceVideo;
INDICameraForceExposure = indiDlg.INDIForceExposure;
pConfig->Profile.SetString("/indi/INDIhost", INDIhost);
pConfig->Profile.SetLong("/indi/INDIport", INDIport);
pConfig->Profile.SetString("/indi/INDIcam", INDICameraName);
pConfig->Profile.SetLong("/indi/INDIcam_ccd", INDICameraCCD);
pConfig->Profile.SetBoolean("/indi/INDIcam_forcevideo", INDICameraForceVideo);
pConfig->Profile.SetBoolean("/indi/INDIcam_forceexposure", INDICameraForceExposure);
Name = INDICameraName;
SetCCDdevice();
}
indiDlg.Disconnect();
}
void CameraINDI::SetCCDdevice()
{
if (INDICameraCCD == 0)
{
INDICameraBlobName = "CCD1";
INDICameraCCDCmd = "CCD_";
}
else
{
INDICameraBlobName = "CCD2";
INDICameraCCDCmd = "GUIDER_";
}
}
bool CameraINDI::ReadFITS(CapturedFrame *frame, usImage& img, bool takeSubframe, const wxRect& subframe)
{
fitsfile *fptr; // FITS file pointer
int status = 0; // CFITSIO status value MUST be initialized to zero!
// load blob to CFITSIO
if (fits_open_memfile(&fptr,
"",
READONLY,
&frame->m_data,
&frame->m_size,
0,
nullptr,
&status))
{
pFrame->Alert(_("Unsupported type or read error loading FITS file"));
return true;
}
int hdutype;
if (fits_get_hdu_type(fptr, &hdutype, &status) || hdutype != IMAGE_HDU)
{
pFrame->Alert(_("FITS file is not of an image"));
PHD_fits_close_file(fptr);
return true;
}
// Get HDUs and size
int naxis;
fits_get_img_dim(fptr, &naxis, &status);
long fits_size[2];
fits_get_img_size(fptr, 2, fits_size, &status);
int xsize = (int) fits_size[0];
int ysize = (int) fits_size[1];
int nhdus = 0;
fits_get_num_hdus(fptr, &nhdus, &status);
if (nhdus != 1 || naxis != 2)
{
pFrame->Alert(_("Unsupported type or read error loading FITS file"));
PHD_fits_close_file(fptr);
return true;
}
long fpixel[3] = {1, 1, 1};
if (takeSubframe)
{
if (FullSize == UNDEFINED_FRAME_SIZE)
{
// should never happen since we arranged not to take a subframe
// unless full frame size is known
Debug.Write("internal error: taking subframe before full frame\n");
PHD_fits_close_file(fptr);
return true;
}
if (img.Init(FullSize))
{
pFrame->Alert(_("Memory allocation error"));
PHD_fits_close_file(fptr);
return true;
}
img.Clear();
img.Subframe = subframe;
unsigned short *rawdata = new unsigned short[xsize * ysize];
if (fits_read_pix(fptr, TUSHORT, fpixel, xsize * ysize, nullptr, rawdata, nullptr, &status))
{
pFrame->Alert(_("Error reading data"));
PHD_fits_close_file(fptr);
delete[] rawdata;
return true;
}
int i = 0;
for (int y = 0; y < subframe.height; y++)
{
unsigned short *dataptr = img.ImageData + (y + subframe.y) * img.Size.GetWidth() + subframe.x;
memcpy(dataptr, &rawdata[i], subframe.width * sizeof(unsigned short));
i += subframe.width;
}
delete[] rawdata;
}
else
{
FullSize.Set(xsize, ysize);
if (img.Init(FullSize))
{
pFrame->Alert(_("Memory allocation error"));
PHD_fits_close_file(fptr);
return true;
}
// Read image
if (fits_read_pix(fptr, TUSHORT, fpixel, xsize * ysize, nullptr, img.ImageData, nullptr, &status))
{
pFrame->Alert(_("Error reading data"));
PHD_fits_close_file(fptr);
return true;
}
}
PHD_fits_close_file(fptr);
return false;
}
bool CameraINDI::StackStream(CapturedFrame *cf)
{
if (!StackImg)
return true;
if (cf->m_size != StackImg->NPixels)
{
Debug.Write(wxString::Format("INDI Camera: discarding blob with size %d, expected %u\n", cf->m_size, StackImg->NPixels));
return true;
}
// Add new blob to stacked image
stacking = true;
unsigned short *outptr = StackImg->ImageData;
const unsigned char *inptr = (unsigned char *) cf->m_data;
for (int i = 0; i < StackImg->NPixels; i++)
*outptr++ += (unsigned short) *inptr++;
++StackFrames;
stacking = false;
return false;
}
void CameraINDI::SendBinning()
{
binning_x->value = Binning;
binning_y->value = Binning;
Debug.Write(wxString::Format("INDI Camera: send binning %u\n", Binning));