-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcam_moravian.cpp
1000 lines (836 loc) · 29.9 KB
/
cam_moravian.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_moravian.cpp
* PHD2 Guiding
*
* 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 openphdguiding.org 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 MORAVIAN_CAMERA
#include "cam_moravian.h"
// #include "gxeth.h" TODO - ethernet camera support
#include "gxusb.h"
static std::vector<gXusb::CARDINAL> __ids;
static void __cdecl _enum_cb(gXusb::CARDINAL id)
{
__ids.push_back(id);
}
static std::vector<gXusb::CARDINAL> _get_ids()
{
Debug.Write("MVN: begin enumerate\n");
__ids.clear();
gXusb::Enumerate(_enum_cb);
Debug.Write(wxString::Format("MVN: enumerate found %u\n", (unsigned int)__ids.size())); // MSVC barfs on %zu
return __ids;
}
struct MCam
{
gXusb::CCamera *m_cam;
MCam() : m_cam(nullptr) { }
~MCam()
{
if (m_cam)
gXusb::Release(m_cam);
}
void Release()
{
if (m_cam)
{
gXusb::Release(m_cam);
m_cam = nullptr;
}
}
void Attach(gXusb::CCamera *cam)
{
if (m_cam)
gXusb::Release(m_cam);
m_cam = cam;
}
gXusb::CCamera *Detach()
{
gXusb::CCamera *ret = m_cam;
m_cam = nullptr;
return ret;
}
operator gXusb::CCamera *() const { return m_cam; }
bool Initialize(gXusb::CARDINAL id)
{
gXusb::CCamera *cam = gXusb::Initialize(id);
Debug.Write(wxString::Format("MVN: init id = %u cam = %p\n", id, cam));
if ((HANDLE) cam == INVALID_HANDLE_VALUE)
cam = nullptr;
Attach(cam);
return !!*this;
}
wxString StrParam(gXusb::CARDINAL idx, const wxString& dflt = wxEmptyString)
{
size_t size = 128;
while (true)
{
char *buf = new char[size];
gXusb::CARDINAL const high = size - 1;
buf[high] = '\0';
if (!gXusb::GetStringParameter(m_cam, idx, high, buf))
{
delete[] buf;
return dflt;
}
if (buf[high])
{
// GetStringParameter output was truncated
delete[] buf;
size += 128;
continue;
}
wxString ret(buf);
delete[] buf;
return ret;
}
}
bool BoolParam(gXusb::CARDINAL idx, bool dflt = false)
{
gXusb::BOOLEAN val;
if (gXusb::GetBooleanParameter(m_cam, idx, &val))
return val ? true : false;
return dflt;
}
int IntParam(gXusb::CARDINAL idx, int dflt = 0)
{
gXusb::CARDINAL val;
if (gXusb::GetIntegerParameter(m_cam, idx, &val))
return static_cast<int>(val);
return dflt;
}
wxString Serial()
{
return StrParam(gspCameraSerial, wxString::Format("ID%d", IntParam(gipCameraId, 1)));
}
float GetValue(gXusb::CARDINAL idx, float dflt = 0.f)
{
gXusb::REAL val;
if (gXusb::GetValue(m_cam, idx, &val))
return static_cast<float>(val);
return dflt;
}
wxString LastError()
{
size_t size = 128;
while (true)
{
char *buf = new char[size];
gXusb::CARDINAL const high = size - 1;
buf[high] = '\0';
gXusb::GetLastErrorString(m_cam, high, buf);
if (buf[high])
{
// output was truncated
delete[] buf;
size += 128;
continue;
}
wxString ret(buf);
delete[] buf;
return ret;
}
}
bool FindCamera(const wxString& camId, wxString *err)
{
Debug.Write(wxString::Format("MVN: find camera id: [%s]\n", camId));
unsigned int cnt = 0;
for (auto id : _get_ids())
{
MCam tmp;
if (!tmp.Initialize(id))
continue;
++cnt;
wxString serial(tmp.Serial());
Debug.Write(wxString::Format("MVN: serial = %s\n", serial));
if (camId == GuideCamera::DEFAULT_CAMERA_ID || camId == serial)
{
Attach(tmp.Detach());
return true;
}
}
if (!cnt)
*err = _("No Moravian cameras detected.");
else
*err = wxString::Format(_("Camera %s not found"), camId);
return false;
}
bool SetBinning(int bin)
{
bool ok = gXusb::SetBinning(m_cam, bin, bin) ? true : false;
if (!ok)
Debug.Write(wxString::Format("MVN: SetBinning(%d): %s\n", bin, LastError()));
return ok;
}
bool SetFan(unsigned int speed)
{
if (speed > (gXusb::CARD8)(-1))
speed = (gXusb::CARD8)(-1);
bool ok = gXusb::SetFan(m_cam, static_cast<gXusb::CARD8>(speed)) ? true : false;
if (!ok)
Debug.Write(wxString::Format("MVN: SetFan(%u): %s\n", speed, LastError()));
return ok;
}
bool GetReadMode(wxString *ret, unsigned int idx)
{
size_t size = 128;
while (true)
{
char *buf = new char[size];
gXusb::CARDINAL const high = size - 1;
buf[high] = '\0';
if (!gXusb::EnumerateReadModes(m_cam, idx, high, buf))
{
delete[] buf;
return false;
}
if (buf[high])
{
// output was truncated
delete[] buf;
size += 128;
continue;
}
*ret = wxString(buf);
delete[] buf;
return true;
}
}
bool SetReadMode(unsigned int mode)
{
bool ok = gXusb::SetReadMode(m_cam, static_cast<gXusb::CARDINAL>(mode)) ? true : false;
if (!ok)
Debug.Write(wxString::Format("MVN: SetReadMode(%u): %s\n", mode, LastError()));
return ok;
}
bool SetGain(unsigned int gain)
{
bool ok = gXusb::SetGain(m_cam, static_cast<gXusb::CARDINAL>(gain)) ? true : false;
if (!ok)
Debug.Write(wxString::Format("MVN: SetGain(%u): %s\n", gain, LastError()));
return ok;
}
wxSize ChipSize()
{
return wxSize(IntParam(gipChipW), IntParam(gipChipD));
}
bool CaptureSync(void *buf, unsigned int size, unsigned int duration, wxByte bpp, const wxRect& frame)
{
double exp = duration * 1e-3; // millis to seconds
gXusb::BOOLEAN ok;
if (bpp == 8)
{
ok = gXusb::GetImageExposure8b(m_cam, exp, false, frame.GetLeft(), frame.GetTop(), frame.GetWidth(), frame.GetHeight(),
size, buf);
}
else
{
ok = gXusb::GetImageExposure16b(m_cam, exp, false, frame.GetLeft(), frame.GetTop(), frame.GetWidth(), frame.GetHeight(),
size, buf);
}
if (!ok)
Debug.Write(wxString::Format("MVN: CaptureSync: %s\n", LastError()));
return ok ? true : false;
}
bool BeginExposure()
{
gXusb::BOOLEAN use_shutter = false;
gXusb::BOOLEAN ok = gXusb::BeginExposure(m_cam, use_shutter);
if (!ok)
Debug.Write(wxString::Format("MVN: BeginExposure: %s\n", LastError()));
return ok ? true : false;
}
bool EndExposure(bool abort)
{
gXusb::BOOLEAN use_shutter = false;
gXusb::BOOLEAN ok = gXusb::EndExposure(m_cam, use_shutter, abort);
if (!ok)
Debug.Write(wxString::Format("MVN: EndExposure: %s\n", LastError()));
return ok ? true : false;
}
bool GetImage(void *buf, unsigned int size, wxByte bpp, const wxRect& frame)
{
gXusb::BOOLEAN ok;
if (bpp == 8)
{
ok = gXusb::GetImage8b(m_cam, frame.GetLeft(), frame.GetTop(), frame.GetWidth(), frame.GetHeight(), size, buf);
}
else
{
ok = gXusb::GetImage16b(m_cam, frame.GetLeft(), frame.GetTop(), frame.GetWidth(), frame.GetHeight(), size, buf);
}
if (!ok)
Debug.Write(wxString::Format("MVN: GetImage: %s\n", LastError()));
return ok ? true : false;
}
};
class MoravianCamera : public GuideCamera
{
wxSize m_maxSize;
wxRect m_frame;
unsigned short m_curBinning;
unsigned int m_curGain;
void *m_buffer;
size_t m_buffer_size;
wxByte m_bpp; // bits per pixel: 8 or 16
MCam m_cam;
bool m_canGuide;
int m_maxGain;
int m_defaultGainPct;
bool m_isColor;
double m_devicePixelSize;
int m_maxMoveMs;
public:
MoravianCamera();
~MoravianCamera();
bool CanSelectCamera() const override { return true; }
bool EnumCameras(wxArrayString& names, wxArrayString& ids) override;
bool Capture(int duration, usImage& img, int options, const wxRect& subframe) override;
bool Connect(const wxString& camId) override;
bool Disconnect() override;
void ShowPropertyDialog() override;
bool HasNonGuiCapture() override { return true; }
bool ST4HasGuideOutput() override;
bool ST4HasNonGuiMove() override { return true; }
bool ST4PulseGuideScope(int direction, int duration) override;
wxByte BitsPerPixel() override;
bool GetDevicePixelSize(double *devPixelSize) override;
int GetDefaultCameraGain() override;
bool SetCoolerOn(bool on) override;
bool SetCoolerSetpoint(double temperature) override;
bool GetCoolerStatus(bool *on, double *setpoint, double *power, double *temperature) override;
bool GetSensorTemperature(double *temperature) override;
};
MoravianCamera::MoravianCamera()
:
m_buffer(nullptr)
{
Name = _T("Moravian Camera");
PropertyDialogType = PROPDLG_WHEN_DISCONNECTED;
Connected = false;
m_hasGuideOutput = false; // updated when connected
HasSubframes = true;
HasGainControl = true; // workaround: ok to set to false later, but brain dialog will crash if we start false then change to true later when the camera is connected
m_defaultGainPct = 0; // TODO: what is a good default? GuideCamera::GetDefaultCameraGain();
int value = pConfig->Profile.GetInt("/camera/moravian/bpp", 16);
m_bpp = value == 8 ? 8 : 16;
}
MoravianCamera::~MoravianCamera()
{
::free(m_buffer);
}
wxByte MoravianCamera::BitsPerPixel()
{
return m_bpp;
}
struct MoravianCameraDlg : public wxDialog
{
wxRadioButton *m_bpp8;
wxRadioButton *m_bpp16;
wxButton *m_refresh;
wxListBox *m_modeNames;
wxCheckBox *m_fan;
std::vector<int> m_modes;
MoravianCameraDlg();
~MoravianCameraDlg();
void OnBpp(wxCommandEvent& event) { LoadCamInfo(); }
void OnRefresh(wxCommandEvent& event) { LoadCamInfo(); }
void LoadCamInfo();
};
MoravianCameraDlg::MoravianCameraDlg()
: wxDialog(wxGetApp().GetTopWindow(), wxID_ANY, _("Moravian Camera Properties"))
{
this->SetSizeHints(wxDefaultSize, wxDefaultSize);
wxBoxSizer *sizer1 = new wxBoxSizer(wxVERTICAL);
wxStaticBoxSizer *sizer2 = new wxStaticBoxSizer(new wxStaticBox(this, wxID_ANY, _("Camera Mode")), wxVERTICAL);
wxBoxSizer *bSizer4 = new wxBoxSizer(wxHORIZONTAL);
m_bpp8 = new wxRadioButton(sizer2->GetStaticBox(), wxID_ANY, _("8-bit"), wxDefaultPosition, wxDefaultSize, 0);
bSizer4->Add(m_bpp8, 0, wxALL, 5);
m_bpp16 = new wxRadioButton(sizer2->GetStaticBox(), wxID_ANY, _("16-bit"), wxDefaultPosition, wxDefaultSize, 0);
bSizer4->Add(m_bpp16, 0, wxALL, 5);
sizer2->Add(bSizer4, 0, 0, 5);
wxBoxSizer *sizer5 = new wxBoxSizer(wxHORIZONTAL);
wxStaticText *staticText1 = new wxStaticText(sizer2->GetStaticBox(), wxID_ANY, _("Read Mode"),
wxDefaultPosition, wxDefaultSize, 0);
staticText1->Wrap(-1);
sizer5->Add(staticText1, 0, wxALIGN_CENTER_VERTICAL | wxLEFT | wxTOP, 5);
m_refresh = new wxButton(sizer2->GetStaticBox(), wxID_ANY, _("Refresh"), wxDefaultPosition, wxDefaultSize, 0);
sizer5->Add(m_refresh, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
sizer2->Add(sizer5, 0, wxEXPAND, 5);
m_modeNames = new wxListBox(sizer2->GetStaticBox(), wxID_ANY, wxDefaultPosition, wxDefaultSize, 0, NULL, wxLB_SINGLE);
sizer2->Add(m_modeNames, 1, wxALL | wxEXPAND, 5);
m_fan = new wxCheckBox(sizer2->GetStaticBox(), wxID_ANY, _("Fan On"), wxDefaultPosition, wxDefaultSize, 0);
sizer2->Add(m_fan, 0, wxALL, 5);
sizer1->Add(sizer2, 1, wxEXPAND, 5);
wxStdDialogButtonSizer *sizer3 = new wxStdDialogButtonSizer();
wxButton *sizer3OK = new wxButton(this, wxID_OK);
wxButton *sizer3Cancel = new wxButton(this, wxID_CANCEL);
sizer3->AddButton(sizer3OK);
sizer3->AddButton(sizer3Cancel);
sizer3->Realize();
sizer1->Add(sizer3, 0, wxEXPAND, 5);
SetSizer(sizer1);
Layout();
Centre(wxBOTH);
m_bpp8->Connect(wxEVT_COMMAND_RADIOBUTTON_SELECTED, wxCommandEventHandler(MoravianCameraDlg::OnBpp), nullptr, this);
m_bpp16->Connect(wxEVT_COMMAND_RADIOBUTTON_SELECTED, wxCommandEventHandler(MoravianCameraDlg::OnBpp), nullptr, this);
m_refresh->Connect(wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(MoravianCameraDlg::OnRefresh), nullptr, this);
}
MoravianCameraDlg::~MoravianCameraDlg()
{
m_bpp8->Disconnect(wxEVT_COMMAND_RADIOBUTTON_SELECTED, wxCommandEventHandler(MoravianCameraDlg::OnBpp), nullptr, this);
m_bpp16->Disconnect(wxEVT_COMMAND_RADIOBUTTON_SELECTED, wxCommandEventHandler(MoravianCameraDlg::OnBpp), nullptr, this);
m_refresh->Disconnect(wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(MoravianCameraDlg::OnRefresh), nullptr, this);
}
void MoravianCameraDlg::LoadCamInfo()
{
wxBusyCursor _busy;
m_modeNames->Clear();
m_modes.clear();
m_fan->SetValue(false);
m_fan->Enable(false);
wxString camId = pFrame->pGearDialog->SelectedCameraId();
MCam cam;
wxString err;
if (!cam.FindCamera(camId, &err))
{
Debug.Write(wxString::Format("MVN: load read modes could not find camera [%s]: %s\n", camId, err));
m_modeNames->AppendString(_("... connect a camera first to get read modes ..."));
return;
}
int sel_mode = pConfig->Profile.GetInt("/camera/moravian/read_mode", -1);
if (sel_mode == -1)
{
int dflt_read_mode = cam.IntParam(gipDefaultReadMode);
sel_mode = dflt_read_mode;
}
wxByte bpp = m_bpp8->GetValue() ? 8 : 16;
unsigned int exp_dur = (cam.IntParam(gipMinimalExposure) + 1000 - 1) / 1000; // ns to ms, rounded up
wxSize chip_size(cam.ChipSize());
usImage tmp;
tmp.Init(chip_size);
wxString mode_name;
int sel_idx = -1;
for (gXusb::CARDINAL mode = 0; cam.GetReadMode(&mode_name, mode); ++mode)
{
Debug.Write(wxString::Format("MVN: read mode[%u] = %s\n", mode, mode_name));
if (!cam.SetReadMode(mode))
continue;
bool ok = cam.CaptureSync(tmp.ImageData, tmp.NPixels * 2, exp_dur, bpp, wxRect(chip_size));
Debug.Write(wxString::Format("MVN: mode %u bpp %u: %s\n", mode, bpp, ok ? "ok" : cam.LastError()));
if (ok)
{
if (mode == sel_mode)
sel_idx = m_modes.size();
m_modeNames->Append(mode_name);
m_modes.push_back(mode);
}
}
if (m_modes.empty())
{
m_modeNames->AppendString(_("... connect a camera first to get read modes ..."));
return;
}
if (sel_idx == -1)
sel_idx = 0;
m_modeNames->SetSelection(sel_idx);
if (cam.BoolParam(gbpFan) && cam.IntParam(gipMaxFan) == 1)
{
m_fan->Enable();
m_fan->SetValue(pConfig->Profile.GetInt("/camera/moravian/fan_speed", 1) ? true : false);
}
}
void MoravianCamera::ShowPropertyDialog()
{
MoravianCameraDlg dlg;
int value = pConfig->Profile.GetInt("/camera/moravian/bpp", m_bpp);
if (value == 8)
dlg.m_bpp8->SetValue(true);
else
dlg.m_bpp16->SetValue(true);
dlg.LoadCamInfo();
if (dlg.ShowModal() == wxID_OK)
{
m_bpp = dlg.m_bpp8->GetValue() ? 8 : 16;
pConfig->Profile.SetInt("/camera/moravian/bpp", m_bpp);
int mode = -1;
if (!dlg.m_modes.empty() && dlg.m_modeNames->GetSelection() >= 0)
mode = dlg.m_modes[dlg.m_modeNames->GetSelection()];
pConfig->Profile.SetInt("/camera/moravian/read_mode", mode);
if (dlg.m_fan->IsEnabled())
pConfig->Profile.SetInt("/camera/moravian/fan_speed", dlg.m_fan->GetValue() ? 1 : 0);
}
}
inline static int cam_gain(int minval, int maxval, int pct)
{
return minval + pct * (maxval - minval) / 100;
}
inline static int gain_pct(int minval, int maxval, int val)
{
return (val - minval) * 100 / (maxval - minval);
}
bool MoravianCamera::EnumCameras(wxArrayString& names, wxArrayString& ids)
{
for (auto id : _get_ids())
{
MCam cam;
if (!cam.Initialize(id))
continue;
wxString desc = cam.StrParam(gspCameraDescription, _("unknown")).Trim();
wxString serial = cam.Serial();
wxString name = wxString::Format("%s [%s]", desc, serial);
Debug.Write(wxString::Format("MVN: %s\n", name));
names.push_back(name);
ids.push_back(serial);
}
return false;
}
bool MoravianCamera::Connect(const wxString& camId)
{
wxString err;
if (!m_cam.FindCamera(camId, &err))
{
return CamConnectFailed(err);
}
int drv_major = m_cam.IntParam(gipDriverMajor);
int drv_minor = m_cam.IntParam(gipDriverMinor);
int drv_build = m_cam.IntParam(gipDriverBuild);
int fw_major = m_cam.IntParam(gipFirmwareMajor);
int fw_minor = m_cam.IntParam(gipFirmwareMinor);
int fw_build = m_cam.IntParam(gipFirmwareBuild);
int flash_major = m_cam.IntParam(gipFlashMajor);
int flash_minor = m_cam.IntParam(gipFlashMinor);
int flash_build = m_cam.IntParam(gipFlashBuild);
Debug.Write(wxString::Format("MVN: Driver %d.%d.%d | Firmware %d.%d.%d | Flash %d.%d.%d\n",
drv_major, drv_minor, drv_build,
fw_major, fw_minor, fw_build,
flash_major, flash_minor, flash_build));
Name = m_cam.StrParam(gspCameraDescription, _T("Moravian Camera"));
bool connected = m_cam.BoolParam(gbpConnected);
HasSubframes = m_cam.BoolParam(gbpSubFrame);
bool has_read_modes = m_cam.BoolParam(gbpReadModes);
bool has_shutter = m_cam.BoolParam(gbpShutter);
HasShutter = false; // TODO: handle camera with shutter
HasCooler = m_cam.BoolParam(gbpCooler);
bool has_fan = m_cam.BoolParam(gbpFan);
Debug.Write(wxString::Format("MVN: HasShutter: %d HasCooler: %d HasFan: %d\n", has_shutter, HasCooler, has_fan));
if (has_fan)
{
int max_fan = m_cam.IntParam(gipMaxFan);
int speed = pConfig->Profile.GetInt("/camera/moravian/fan_speed", 1);
if (speed > max_fan)
speed = max_fan;
m_cam.SetFan(speed);
Debug.Write(wxString::Format("MVN: set fan speed %u / %u\n", speed, max_fan));
}
m_hasGuideOutput = m_cam.BoolParam(gbpGuide);
m_maxMoveMs = m_hasGuideOutput ? m_cam.IntParam(gipMaximalMoveTime) : 0;
Debug.Write(wxString::Format("MVN: CanPulseGuide: %s MaxMove: %d\n",
m_hasGuideOutput ? "yes" : "no", m_maxMoveMs));
bool rgb = m_cam.BoolParam(gbpRGB);
bool cmy = m_cam.BoolParam(gbpCMY);
bool cmyg = m_cam.BoolParam(gbpCMYG);
m_isColor = rgb || cmy || cmyg;
Debug.Write(wxString::Format("MVN: IsColorCam = %d (rgb:%d cmy:%d cmyg:%d)\n", m_isColor, rgb, cmy, cmyg));
int pxwidth = m_cam.IntParam(gipPixelW); // nm
int pxheight = m_cam.IntParam(gipPixelD); // nm
m_devicePixelSize = (double) std::min(pxwidth, pxheight) / 1000.; // microns
int maxbinx = m_cam.IntParam(gipMaxBinningX);
int maxbiny = m_cam.IntParam(gipMaxBinningY);
MaxBinning = std::min(maxbinx, maxbiny);
if (Binning > MaxBinning)
Binning = MaxBinning;
m_maxSize = m_cam.ChipSize();
FullSize.x = m_maxSize.x / Binning;
FullSize.y = m_maxSize.y / Binning;
m_curBinning = Binning;
if (!m_cam.SetBinning(Binning))
{
wxString err = m_cam.LastError();
Disconnect();
return CamConnectFailed(err);
}
::free(m_buffer);
m_buffer_size = m_maxSize.x * m_maxSize.y * 2; // big enough for 16 bpp, even if we only use 8 bpp
m_buffer = ::malloc(m_buffer_size);
int max_exp_ms = m_cam.IntParam(gipMaximalExposure);
int nr_read_modes = m_cam.IntParam(gipReadModes);
int dflt_read_mode = m_cam.IntParam(gipDefaultReadMode);
bool can_get_gain = m_cam.BoolParam(gbpGain);
if (can_get_gain)
Debug.Write(wxString::Format("MVN: GetGain: %.3f\n", m_cam.GetValue(gvADCGain)));
m_maxGain = m_cam.IntParam(gipMaxGain);
int default_gain = 0; // TODO: ask moravian
m_defaultGainPct = gain_pct(0, m_maxGain, default_gain);
Debug.Write(wxString::Format("MVN: gain range = %d .. %d default = %ld (%d%%)\n",
0, m_maxGain, default_gain, m_defaultGainPct));
unsigned int new_gain = cam_gain(0, m_maxGain, GuideCameraGain);
Debug.Write(wxString::Format("MVN: set gain %d%% %d\n", GuideCameraGain, new_gain));
if (!m_cam.SetGain(new_gain))
{
wxString err = m_cam.LastError();
Disconnect();
return CamConnectFailed(err);
}
m_curGain = new_gain;
unsigned int read_mode = pConfig->Profile.GetInt("/camera/moravian/read_mode", dflt_read_mode);
wxString mode_name;
if (!m_cam.GetReadMode(&mode_name, read_mode))
mode_name = "unknown";
Debug.Write(wxString::Format("MVN: setting read mode %u (%s) bpp = %u\n", read_mode, mode_name, m_bpp));
if (!m_cam.SetReadMode(read_mode))
{
wxString err = m_cam.LastError();
Disconnect();
return CamConnectFailed(err);
}
Connected = true;
return false;
}
bool MoravianCamera::Disconnect()
{
m_cam.Release();
Connected = false;
::free(m_buffer);
m_buffer = nullptr;
return false;
}
bool MoravianCamera::GetDevicePixelSize(double *devPixelSize)
{
if (!Connected)
return true;
*devPixelSize = m_devicePixelSize;
return false;
}
int MoravianCamera::GetDefaultCameraGain()
{
return m_defaultGainPct;
}
bool MoravianCamera::SetCoolerOn(bool on)
{
// TODO
return true;
}
bool MoravianCamera::SetCoolerSetpoint(double temperature)
{
// TODO
return true;
}
bool MoravianCamera::GetCoolerStatus(bool *on, double *setpoint, double *power, double *temperature)
{
// TODO
return true;
}
bool MoravianCamera::GetSensorTemperature(double *temperature)
{
double const BAD_TEMP = -99999.;
double val = m_cam.GetValue(gvChipTemperature, BAD_TEMP);
if (val == BAD_TEMP)
return true;
*temperature = val;
return false;
}
bool MoravianCamera::Capture(int duration, usImage& img, int options, const wxRect& subframe)
{
unsigned int new_gain = cam_gain(0, m_maxGain, GuideCameraGain);
if (new_gain != m_curGain)
{
Debug.Write(wxString::Format("MVN: set gain %d%% %d\n", GuideCameraGain, new_gain));
if (!m_cam.SetGain(new_gain))
return true;
m_curGain = new_gain;
}
if (Binning != m_curBinning)
{
if (!m_cam.SetBinning(Binning))
{
Debug.Write(wxString::Format("MVN: SetBinning(%u): %s\n", Binning, m_cam.LastError()));
return true;
}
Debug.Write(wxString::Format("MVN: SetBinning(%u): ok\n", Binning));
FullSize.x = m_maxSize.x / Binning;
FullSize.y = m_maxSize.y / Binning;
m_curBinning = Binning;
}
if (img.Init(FullSize))
{
DisconnectWithAlert(CAPT_FAIL_MEMORY);
return true;
}
bool useSubframe = UseSubframes;
if (subframe.width <= 0 || subframe.height <= 0)
useSubframe = false;
wxRect frame;
void *buf;
unsigned int bufsz;
if (useSubframe)
{
frame = subframe;
buf = m_buffer;
bufsz = m_buffer_size;
}
else
{
frame = wxRect(FullSize);
if (m_bpp == 8)
{
buf = m_buffer;
bufsz = m_buffer_size;
}
else
{
buf = img.ImageData;
bufsz = img.NPixels * 2;
}
}
if (duration <= 1000)
{
// short exposure -- use synchronous API
if (!m_cam.CaptureSync(buf, bufsz, duration, m_bpp, frame))
return true;
}
else
{
// long exposure -- use async API so it can be interrupted
if (!m_cam.BeginExposure())
return true;
if (WorkerThread::MilliSleep(duration, WorkerThread::INT_ANY))
{
// interrupted
m_cam.EndExposure(true);
return true;
}
if (!m_cam.EndExposure(false))
{
m_cam.EndExposure(true);
return true;
}
if (!m_cam.GetImage(buf, bufsz, m_bpp, frame))
return true;
}
if (useSubframe)
{
img.Subframe = subframe;
// Clear out the image
img.Clear();
if (m_bpp == 8)
{
const unsigned char *src = (const unsigned char *) buf;
for (int y = 0; y < subframe.height; y++)
{
unsigned short *dst = img.ImageData + (y + subframe.y) * FullSize.GetWidth() + subframe.x;
for (int x = 0; x < subframe.width; x++)
*dst++ = *src++;
}
}
else
{
const unsigned short *src = (const unsigned short *) buf;
for (int y = 0; y < subframe.height; y++)
{
unsigned short *dst = img.ImageData + (y + subframe.y) * FullSize.GetWidth() + subframe.x;
for (int x = 0; x < subframe.width; x++)
*dst++ = *src++;
}
}
}
else
{
if (m_bpp == 8)
{
unsigned short *dst = img.ImageData;
const unsigned char *src = (const unsigned char *) buf;
for (unsigned int i = 0; i < img.NPixels; i++)
*dst++ = *src++;
}
else
{
// 16-bit mode and no subframe: data is already in img.ImageData
}
}
if (options & CAPTURE_SUBTRACT_DARK)
SubtractDark(img);
if (m_isColor && Binning == 1 && (options & CAPTURE_RECON))
QuickLRecon(img);
return false;
}
bool MoravianCamera::ST4HasGuideOutput()
{
return m_canGuide;
}
bool MoravianCamera::ST4PulseGuideScope(int direction, int duration)
{
while (duration > 0)
{
int dur = std::min(duration, m_maxMoveMs);
gXusb::INT16 radur, decdur;
switch (direction) {
case NORTH: radur = 0; decdur = +dur; break;
case SOUTH: radur = 0; decdur = -dur; break;
case EAST: radur = +dur; decdur = 0; break;
case WEST: radur = -dur; decdur = 0; break;
default: return true;
}
if (!gXusb::MoveTelescope(m_cam, radur, decdur))
{
Debug.Write(wxString::Format("MVN: MoveTelescope: %s\n", m_cam.LastError()));
return true;
}
MountWatchdog timeout(dur, 5000);
if (dur > 10)
if (WorkerThread::MilliSleep(dur - 10))
return true;
while (true)
{
if (WorkerThread::MilliSleep(5))
return true;
gXusb::BOOLEAN val;
if (!gXusb::MoveInProgress(m_cam, &val))
{
Debug.Write(wxString::Format("MVN: MoveInProgress: %s\n", m_cam.LastError()));
return true;
}
if (!val)
break;
if (timeout.Expired())
{
Debug.Write("MVN: timed-out waiting for MoveInProgress to clear\n");
return true;
}
}
duration -= dur;
}
return false;
}
GuideCamera *MoravianCameraFactory::MakeMoravianCamera()
{
return new MoravianCamera();
}
#endif // MORAVIAN_CAMERA