-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patharchon.cpp
More file actions
8372 lines (7204 loc) · 340 KB
/
archon.cpp
File metadata and controls
8372 lines (7204 loc) · 340 KB
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
/**
* @file archon.cpp
* @brief camera interface functions
* @details
* @author David Hale <dhale@astro.caltech.edu>
*
*/
#include "archon.h"
#include <sstream> // for std::stringstream
#include <iomanip> // for setfil, setw, etc.
#include <iostream> // for hex, uppercase, etc.
#include <algorithm>
#include <cctype>
#include <string>
#include <fstream>
#include <array>
#include <utility>
namespace Archon {
// Archon::Interface constructor
//
Interface::Interface() {
this->archon_busy = false;
this->modeselected = false;
this->firmwareloaded = false;
this->msgref = 0;
this->lastframe = 0;
this->frame.index = 0;
this->frame.next_index = 0;
this->abort = false;
this->taplines = 0;
this->configlines = 0;
this->logwconfig = false;
this->image_data = nullptr;
this->image_data_bytes = 0;
this->image_data_allocated = 0;
this->is_longexposure_set = false;
this->is_window = false;
this->is_autofetch = false;
this->win_hstart = 0;
this->win_hstop = 2047;
this->win_vstart = 0;
this->win_vstop = 2047;
this->tapline0_store = "";
this->taplines_store = 0;
this->n_hdrshift = 16;
this->backplaneversion="";
this->trigin_state="disabled";
this->trigin_expose = 0;
this->trigin_untimed = 0;
this->trigin_readout = 0;
this->lastcubeamps = this->camera.cubeamps();
this->trigin_expose_enable = DEF_TRIGIN_EXPOSE_ENABLE;
this->trigin_expose_disable = DEF_TRIGIN_EXPOSE_DISABLE;
this->trigin_untimed_enable = DEF_TRIGIN_UNTIMED_ENABLE;
this->trigin_untimed_disable = DEF_TRIGIN_UNTIMED_DISABLE;
this->trigin_readout_enable = DEF_TRIGIN_READOUT_ENABLE;
this->trigin_readout_disable = DEF_TRIGIN_READOUT_DISABLE;
this->shutenable_enable = DEF_SHUTENABLE_ENABLE;
this->shutenable_disable = DEF_SHUTENABLE_DISABLE;
// pre-size the modtype and modversion vectors to hold the max number of modules
//
this->modtype.resize( nmods );
this->modversion.resize( nmods );
// TODO I should change these to STL maps instead
//
this->frame.bufsample.resize( Archon::nbufs );
this->frame.bufcomplete.resize( Archon::nbufs );
this->frame.bufmode.resize( Archon::nbufs );
this->frame.bufbase.resize( Archon::nbufs );
this->frame.bufframen.resize( Archon::nbufs );
this->frame.bufwidth.resize( Archon::nbufs );
this->frame.bufheight.resize( Archon::nbufs );
this->frame.bufpixels.resize( Archon::nbufs );
this->frame.buflines.resize( Archon::nbufs );
this->frame.bufrawblocks.resize( Archon::nbufs );
this->frame.bufrawlines.resize( Archon::nbufs );
this->frame.bufrawoffset.resize( Archon::nbufs );
this->frame.buftimestamp.resize( Archon::nbufs );
this->frame.bufretimestamp.resize( Archon::nbufs );
this->frame.buffetimestamp.resize( Archon::nbufs );
}
// Archon::Interface deconstructor
//
Interface::~Interface() = default;
/**************** Archon::Interface::interface ******************************/
long Interface::interface(std::string &iface) {
std::string function = "Archon::Interface::interface";
iface = "STA-Archon";
logwrite(function, iface);
return 0;
}
/**************** Archon::Interface::interface ******************************/
/***** Archon::Interface::do_power ******************************************/
/**
* @brief set/get the power state
* @param[in] state_in input string contains requested power state
* @param[out] retstring return string contains the current power state
* @return ERROR or NO_ERROR
*
*/
long Interface::do_power(std::string state_in, std::string &retstring) {
std::string function = "Archon::Interface::do_power";
std::stringstream message;
long error = ERROR;
if ( !this->archon.isconnected() ) { // nothing to do if no connection open to controller
this->camera.log_error( function, "connection not open to controller" );
return( ERROR );
}
// set the Archon power state as requested
//
if ( !state_in.empty() ) { // received something
std::transform( state_in.begin(), state_in.end(), state_in.begin(), ::toupper ); // make uppercase
if ( state_in == "ON" ) {
error = this->archon_cmd( POWERON ); // send POWERON command to Archon
if ( error == NO_ERROR ) std::this_thread::sleep_for( std::chrono::seconds(2) ); // wait 2s to ensure power is stable
}
else
if ( state_in == "OFF" ) {
error = this->archon_cmd( POWEROFF ); // send POWEROFF command to Archon
if ( error == NO_ERROR ) std::this_thread::sleep_for( std::chrono::milliseconds(200) ); // wait 200ms to ensure power is off
}
else {
message.str(""); message << "unrecognized argument " << state_in << ": expected {on|off}";
this->camera.log_error( function, message.str() );
return( ERROR );
}
if ( error != NO_ERROR ) {
message.str(""); message << "setting Archon power " << state_in;
this->camera.log_error( function, message.str() );
return( ERROR );
}
}
// Read the Archon power state directly from Archon
//
std::string power;
error = get_status_key( "POWER", power );
if ( error != NO_ERROR ) return( ERROR );
int status=-1;
try { status = std::stoi( power ); }
catch (std::invalid_argument &) {
this->camera.log_error( function, "unable to convert power status message to integer" );
return(ERROR);
}
catch (std::out_of_range &) {
this->camera.log_error( function, "power status out of range" );
return(ERROR);
}
// set the power status (or not) depending on the value extracted from the STATUS message
//
switch( status ) {
case -1: // no POWER token found in status message
this->camera.log_error( function, "unable to find power in Archon status message" );
return( ERROR );
case 0: // usually an internal error
this->camera.power_status = "UNKNOWN";
break;
case 1: // no configuration applied
this->camera.power_status = "NOT_CONFIGURED";
break;
case 2: // power is off
this->camera.power_status = "OFF";
break;
case 3: // some modules powered, some not
this->camera.power_status = "INTERMEDIATE";
break;
case 4: // power is on
this->camera.power_status = "ON";
break;
case 5: // system is in standby
this->camera.power_status = "STANDBY";
break;
default: // should be impossible
message.str(""); message << "unknown power status: " << status;
this->camera.log_error( function, message.str() );
return( ERROR );
}
message.str(""); message << "POWER:" << this->camera.power_status;
this->camera.async.enqueue( message.str() );
retstring = this->camera.power_status;
return(NO_ERROR);
}
/***** Archon::Interface::do_power ******************************************/
/***** Archon::Interface::configure_controller ******************************/
/**
* @brief parse controller-related keys from the configuration file
* @details the config file was read by server.config.read_config() in main()
* @return ERROR or NO_ERROR
*
*/
long Interface::configure_controller() {
std::string function = "Archon::Interface::configure_controller";
std::stringstream message;
int applied=0;
long error;
// Must re-init all values to start-up defaults in case this function is
// called again to re-load the config file (such as if a HUP is received)
// and the new config file may not have everything defined.
// This ensures nothing is carried over from any previous config.
//
this->camera_info.hostname = "";
this->archon.sethost( "" );
this->camera_info.port = -1;
this->archon.setport( -1 );
this->n_hdrshift = 16;
this->camera.firmware[0] = "";
this->exposeparam = "";
this->longexposeparam.clear();
this->trigin_exposeparam = "";
this->trigin_untimedparam = "";
this->trigin_readoutparam = "";
this->trigin_expose_enable = DEF_TRIGIN_EXPOSE_ENABLE;
this->trigin_expose_disable = DEF_TRIGIN_EXPOSE_DISABLE;
this->trigin_untimed_enable = DEF_TRIGIN_UNTIMED_ENABLE;
this->trigin_untimed_disable = DEF_TRIGIN_UNTIMED_DISABLE;
this->trigin_readout_enable = DEF_TRIGIN_READOUT_ENABLE;
this->trigin_readout_disable = DEF_TRIGIN_READOUT_DISABLE;
this->shutenable_enable = DEF_SHUTENABLE_ENABLE;
this->shutenable_disable = DEF_SHUTENABLE_DISABLE;
// loop through the entries in the configuration file, stored in config class
//
for (int entry=0; entry < this->config.n_entries; entry++) {
if (config.param[entry].compare(0, 9, "ARCHON_IP")==0) {
this->camera_info.hostname = config.arg[entry];
this->archon.sethost( config.arg[entry] );
message.str(""); message << "CONFIG:" << config.param[entry] << "=" << config.arg[entry];
logwrite( function, message.str() );
this->camera.async.enqueue( message.str() );
applied++;
}
if (config.param[entry].compare(0, 11, "ARCHON_PORT")==0) { // ARCHON_PORT
int port;
try {
port = std::stoi( config.arg[entry] );
} catch (std::invalid_argument &) {
this->camera.log_error( function, "unable to convert port number to integer" );
return ERROR;
} catch (std::out_of_range &) {
this->camera.log_error( function, "port number out of integer range" );
return ERROR;
}
this->camera_info.port = port;
this->archon.setport(port);
message.str(""); message << "CONFIG:" << config.param[entry] << "=" << config.arg[entry];
logwrite( function, message.str() );
this->camera.async.enqueue( message.str() );
applied++;
}
if (config.param[entry].compare(0, 12, "AMPS_AS_CUBE")==0) {
std::string dontcare;
if ( this->camera.cubeamps( config.arg[entry], dontcare ) == ERROR ) {
this->camera.log_error( function, "setting cubeamps" );
return ERROR;
}
}
if (config.param[entry].compare(0, 12, "EXPOSE_PARAM")==0) { // EXPOSE_PARAM
this->exposeparam = config.arg[entry];
message.str(""); message << "CONFIG:" << config.param[entry] << "=" << config.arg[entry];
logwrite( function, message.str() );
this->camera.async.enqueue( message.str() );
applied++;
}
if ( config.param[entry] == "LONGEXPOSE_PARAM" ) { // LONGEXPOSE_PARAM
this->longexposeparam = config.arg[entry];
message.str(""); message << "CONFIG:" << config.param[entry] << "=" << config.arg[entry];
logwrite( function, message.str() );
this->camera.async.enqueue( message.str() );
applied++;
}
if (config.param[entry].compare(0, 19, "TRIGIN_EXPOSE_PARAM")==0) { // TRIGIN_EXPOSE_PARAM
this->trigin_exposeparam = config.arg[entry];
message.str(""); message << "CONFIG:" << config.param[entry] << "=" << config.arg[entry];
logwrite( function, message.str() );
this->camera.async.enqueue( message.str() );
applied++;
}
if (config.param[entry].compare(0, 20, "TRIGIN_UNTIMED_PARAM")==0) { // TRIGIN_UNTIMED_PARAM
this->trigin_untimedparam = config.arg[entry];
message.str(""); message << "CONFIG:" << config.param[entry] << "=" << config.arg[entry];
logwrite( function, message.str() );
this->camera.async.enqueue( message.str() );
applied++;
}
if (config.param[entry].compare(0, 20, "TRIGIN_READOUT_PARAM")==0) { // TRIGIN_READOUT_PARAM
this->trigin_readoutparam = config.arg[entry];
message.str(""); message << "CONFIG:" << config.param[entry] << "=" << config.arg[entry];
logwrite( function, message.str() );
this->camera.async.enqueue( message.str() );
applied++;
}
if (config.param[entry].compare(0, 20, "TRIGIN_EXPOSE_ENABLE")==0) { // TRIGIN_EXPOSE_ENABLE
int enable;
try {
enable = std::stoi( config.arg[entry] );
} catch (std::invalid_argument &) {
this->camera.log_error( function, "unable to convert TRIGIN_EXPOSE_ENABLE to integer" );
return ERROR;
} catch (std::out_of_range &) {
this->camera.log_error( function, "TRIGIN_EXPOSE_ENABLE out of integer range" );
return ERROR;
}
this->trigin_expose_enable = enable;
message.str(""); message << "CONFIG:" << config.param[entry] << "=" << config.arg[entry];
logwrite( function, message.str() );
this->camera.async.enqueue( message.str() );
applied++;
}
if (config.param[entry].compare(0, 21, "TRIGIN_EXPOSE_DISABLE")==0) { // TRIGIN_EXPOSE_DISABLE
int disable;
try {
disable = std::stoi( config.arg[entry] );
} catch (std::invalid_argument &) {
this->camera.log_error( function, "unable to convert TRIGIN_EXPOSE_DISABLE to integer" );
return ERROR;
} catch (std::out_of_range &) {
this->camera.log_error( function, "TRIGIN_EXPOSE_DISABLE out of integer range" );
return ERROR;
}
this->trigin_expose_disable = disable;
message.str(""); message << "CONFIG:" << config.param[entry] << "=" << config.arg[entry];
logwrite( function, message.str() );
this->camera.async.enqueue( message.str() );
applied++;
}
if (config.param[entry].compare(0, 21, "TRIGIN_UNTIMED_ENABLE")==0) { // TRIGIN_UNTIMED_ENABLE
int enable;
try {
enable = std::stoi( config.arg[entry] );
} catch (std::invalid_argument &) {
this->camera.log_error( function, "unable to convert TRIGIN_UNTIMED_ENABLE to integer" );
return ERROR;
} catch (std::out_of_range &) {
this->camera.log_error( function, "TRIGIN_UNTIMED_ENABLE out of integer range" );
return ERROR;
}
this->trigin_untimed_enable = enable;
message.str(""); message << "CONFIG:" << config.param[entry] << "=" << config.arg[entry];
logwrite( function, message.str() );
this->camera.async.enqueue( message.str() );
applied++;
}
if (config.param[entry].compare(0, 22, "TRIGIN_UNTIMED_DISABLE")==0) { // TRIGIN_UNTIMED_DISABLE
int disable;
try {
disable = std::stoi( config.arg[entry] );
} catch (std::invalid_argument &) {
this->camera.log_error( function, "unable to convert TRIGIN_UNTIMED_DISABLE to integer" );
return ERROR;
} catch (std::out_of_range &) {
this->camera.log_error( function, "TRIGIN_UNTIMED_DISABLE out of integer range" );
return ERROR;
}
this->trigin_untimed_disable = disable;
message.str(""); message << "CONFIG:" << config.param[entry] << "=" << config.arg[entry];
logwrite( function, message.str() );
this->camera.async.enqueue( message.str() );
applied++;
}
if (config.param[entry].compare(0, 21, "TRIGIN_READOUT_ENABLE")==0) { // TRIGIN_READOUT_ENABLE
int enable;
try {
enable = std::stoi( config.arg[entry] );
} catch (std::invalid_argument &) {
this->camera.log_error( function, "unable to convert TRIGIN_READOUT_ENABLE to integer" );
return ERROR;
} catch (std::out_of_range &) {
this->camera.log_error( function, "TRIGIN_READOUT_ENABLE out of integer range" );
return ERROR;
}
this->trigin_readout_enable = enable;
message.str(""); message << "CONFIG:" << config.param[entry] << "=" << config.arg[entry];
logwrite( function, message.str() );
this->camera.async.enqueue( message.str() );
applied++;
}
if (config.param[entry].compare(0, 22, "TRIGIN_READOUT_DISABLE")==0) { // TRIGIN_READOUT_DISABLE
int disable;
try {
disable = std::stoi( config.arg[entry] );
} catch (std::invalid_argument &) {
this->camera.log_error( function, "unable to convert TRIGIN_READOUT_DISABLE to integer" );
return ERROR;
} catch (std::out_of_range &) {
this->camera.log_error( function, "TRIGIN_READOUT_DISABLE out of integer range" );
return ERROR;
}
this->trigin_readout_disable = disable;
message.str(""); message << "CONFIG:" << config.param[entry] << "=" << config.arg[entry];
logwrite( function, message.str() );
this->camera.async.enqueue( message.str() );
applied++;
}
if (config.param[entry].compare(0, 16, "SHUTENABLE_PARAM")==0) { // SHUTENABLE_PARAM
this->shutenableparam = config.arg[entry];
message.str(""); message << "CONFIG:" << config.param[entry] << "=" << config.arg[entry];
logwrite( function, message.str() );
this->camera.async.enqueue( message.str() );
applied++;
}
if (config.param[entry].compare(0, 17, "SHUTENABLE_ENABLE")==0) { // SHUTENABLE_ENABLE
int enable;
try {
enable = std::stoi( config.arg[entry] );
} catch (std::invalid_argument &) {
this->camera.log_error( function, "unable to convert SHUTENABLE_ENABLE to integer" );
return ERROR;
} catch (std::out_of_range &) {
this->camera.log_error( function, "SHUTENABLE_ENABLE out of integer range" );
return ERROR;
}
this->shutenable_enable = enable;
message.str(""); message << "CONFIG:" << config.param[entry] << "=" << config.arg[entry];
logwrite( function, message.str() );
this->camera.async.enqueue( message.str() );
applied++;
}
if (config.param[entry].compare(0, 18, "SHUTENABLE_DISABLE")==0) { // SHUTENABLE_DISABLE
int disable;
try {
disable = std::stoi( config.arg[entry] );
} catch (std::invalid_argument &) {
this->camera.log_error( function, "unable to convert SHUTENABLE_DISABLE to integer" );
return ERROR;
} catch (std::out_of_range &) {
this->camera.log_error( function, "SHUTENABLE_DISABLE out of integer range" );
return ERROR;
}
this->shutenable_disable = disable;
message.str(""); message << "CONFIG:" << config.param[entry] << "=" << config.arg[entry];
logwrite( function, message.str() );
this->camera.async.enqueue( message.str() );
applied++;
}
// .firmware and .readout_time are STL maps but (for now) only one Archon per computer
// so map always to 0
//
if (config.param[entry].compare(0, 16, "DEFAULT_FIRMWARE")==0) {
this->camera.firmware[0] = config.arg[entry];
message.str(""); message << "CONFIG:" << config.param[entry] << "=" << config.arg[entry];
logwrite( function, message.str() );
this->camera.async.enqueue( message.str() );
applied++;
}
if (config.param[entry].compare(0, 16, "HDR_SHIFT")==0) {
std::string dontcare;
this->hdrshift( config.arg[entry], dontcare );
message.str(""); message << "CONFIG:" << config.param[entry] << "=" << config.arg[entry];
logwrite( function, message.str() );
this->camera.async.enqueue( message.str() );
applied++;
}
if (config.param[entry].compare(0, 12, "READOUT_TIME")==0) {
int readtime;
try {
readtime = std::stoi ( config.arg[entry] );
} catch (std::invalid_argument &) {
this->camera.log_error( function, "unable to convert readout time to integer" );
return ERROR;
} catch (std::out_of_range &) {
this->camera.log_error( function, "readout time out of integer range" );
return ERROR;
}
this->camera.readout_time[0] = readtime;
message.str(""); message << "CONFIG:" << config.param[entry] << "=" << config.arg[entry];
logwrite( function, message.str() );
this->camera.async.enqueue( message.str() );
applied++;
}
if (config.param[entry].compare(0, 5, "IMDIR")==0) {
this->camera.imdir( config.arg[entry] );
message.str(""); message << "CONFIG:" << config.param[entry] << "=" << config.arg[entry];
logwrite( function, message.str() );
this->camera.async.enqueue( message.str() );
applied++;
}
if (config.param[entry].compare(0, 7, "DIRMODE")==0) {
std::string s( config.arg[entry] );
std::stringstream mode_bit;
mode_t mode=0;
for ( size_t i=0; i < s.length(); i++ ) {
try {
mode = (mode << 3);
mode_bit.str(""); mode_bit << s.at(i);
mode |= std::stoi( mode_bit.str() );
} catch (std::invalid_argument &) {
this->camera.log_error( function, "unable to convert mode bit to integer" );
return ERROR;
} catch (std::out_of_range &) {
this->camera.log_error( function, "out of range converting dirmode bit" );
return ERROR;
}
}
this->camera.set_dirmode( mode );
message.str(""); message << "CONFIG:" << config.param[entry] << "=" << config.arg[entry];
logwrite( function, message.str() );
this->camera.async.enqueue( message.str() );
applied++;
}
if (config.param[entry].compare(0, 8, "BASENAME")==0) {
this->camera.basename( config.arg[entry] );
message.str(""); message << "CONFIG:" << config.param[entry] << "=" << config.arg[entry];
logwrite( function, message.str() );
this->camera.async.enqueue( message.str() );
applied++;
}
}
message.str("");
if (applied==0) {
message << "ERROR: ";
error = ERROR;
} else {
error = NO_ERROR;
}
message << "applied " << applied << " configuration lines to controller";
error == NO_ERROR ? logwrite( function, message.str() ) : this->camera.log_error( function, message.str() ) ;
return error;
}
/***** Archon::Interface::configure_controller ******************************/
/**************** Archon::Interface::prepare_image_buffer *******************/
/**
* @fn prepare_image_buffer
* @brief prepare image_data buffer, allocating memory as needed
* @param none
* @return NO_ERROR if successful or ERROR on error
*
*/
long Interface::prepare_image_buffer() {
std::string function = "Archon::Interface::prepare_image_buffer";
std::stringstream message;
// If there is already a correctly-sized buffer allocated,
// then don't do anything except initialize that space to zero.
//
if ( (this->image_data != nullptr) &&
(this->image_data_bytes != 0) &&
(this->image_data_allocated == this->image_data_bytes) ) {
memset(this->image_data, 0, this->image_data_bytes);
message.str(""); message << "initialized " << this->image_data_bytes << " bytes of image_data memory";
logwrite(function, message.str());
} else {
// If memory needs to be re-allocated, delete the old buffer
if (this->image_data != nullptr) {
logwrite(function, "deleting old image_data buffer");
delete [] this->image_data;
this->image_data=nullptr;
}
// Allocate new memory
//
if (this->image_data_bytes != 0) {
this->image_data = new char[this->image_data_bytes];
this->image_data_allocated=this->image_data_bytes;
message.str(""); message << "allocated " << this->image_data_bytes << " bytes for image_data";
logwrite(function, message.str());
} else {
this->camera.log_error( function, "cannot allocate zero-length image memory" );
return ERROR;
}
}
return NO_ERROR;
}
/**************** Archon::Interface::prepare_image_buffer *******************/
/**************** Archon::Interface::connect_controller *********************/
/**
* @fn connect_controller
* @brief
* @param none (devices_in here for future expansion)
* @return
*
*/
long Interface::connect_controller(const std::string& devices_in="") {
std::string function = "Archon::Interface::connect_controller";
std::stringstream message;
int adchans=0;
long error = ERROR;
if ( this->archon.isconnected() ) {
logwrite(function, "camera connection already open");
return NO_ERROR;
}
// Initialize the camera connection
//
logwrite(function, "opening a connection to the camera system");
if ( this->archon.Connect() != 0 ) {
message.str(""); message << "connecting to " << this->camera_info.hostname << ":" << this->camera_info.port << ": " << strerror(errno);
this->camera.log_error( function, message.str() );
return ERROR;
}
message.str("");
message << "socket connection to " << this->camera_info.hostname << ":" << this->camera_info.port << " "
<< "established on fd " << this->archon.getfd();
logwrite(function, message.str());
// Get the current system information for the installed modules
//
std::string reply;
error = this->archon_cmd( SYSTEM, reply ); // first the whole reply in one string
std::vector<std::string> lines, tokens;
Tokenize( reply, lines, " " ); // then each line in a separate token "lines"
for ( const auto& line : lines ) {
Tokenize( line, tokens, "_=" ); // finally break each line into tokens to get module, type and version
if ( tokens.size() != 3 ) continue; // need 3 tokens
std::string version;
int module=0;
int type=0;
// get the module number
//
if ( tokens[0].compare( 0, 9, "BACKPLANE" ) == 0 ) {
if ( tokens[1] == "VERSION" ) this->backplaneversion = tokens[2];
continue;
}
// get the module and type of each module from MODn_TYPE
//
if ( ( tokens[0].compare( 0, 3, "MOD" ) == 0 ) && ( tokens[1] == "TYPE" ) ) {
try {
module = std::stoi( tokens[0].substr(3) );
type = std::stoi( tokens[2] );
} catch (std::invalid_argument &) {
message.str(""); message << "unable to convert module or type from " << tokens[0] << "=" << tokens[1] << " to integer";
this->camera.log_error( function, message.str() );
return ERROR;
} catch (std::out_of_range &) {
message.str(""); message << "module " << tokens[0].substr(3) << " or type " << tokens[1] << " out of range";
this->camera.log_error( function, message.str() );
return ERROR;
}
} else continue;
// get the module version
//
if ( tokens[1] == "VERSION" ) version = tokens[2]; else version = "";
// now store it permanently
//
if ( (module > 0) && (module <= nmods) ) {
try {
this->modtype.at(module-1) = type; // store the type in a vector indexed by module
this->modversion.at(module-1) = version; // store the type in a vector indexed by module
} catch (std::out_of_range &) {
message.str(""); message << "requested module " << module << " out of range {1:" << nmods;
this->camera.log_error( function, message.str() );
}
} else { // else should never happen
message.str(""); message << "module " << module << " outside range {1:" << nmods << "}";
this->camera.log_error( function, message.str() );
return ERROR;
}
// Use the module type to resize the gain and offset vectors,
// but always use the largest possible value allowed.
//
if ( type == 2 ) adchans = ( adchans < MAXADCCHANS ? MAXADCCHANS : adchans ); // ADC module (type=2) found
if ( type == 17 ) adchans = ( adchans < MAXADMCHANS ? MAXADMCHANS : adchans ); // ADM module (type=17) found
this->gain.resize( adchans );
this->offset.resize( adchans );
// Check that the AD modules are installed in the correct slot
//
if ( ( type == 2 || type == 17 ) && ( module < 5 || module > 8 ) ) {
message.str(""); message << "AD module (type=" << type << ") cannot be in slot " << module << ". Use slots 5-8";
this->camera.log_error( function, message.str() );
return ERROR;
}
} // end for ( auto line : lines )
// empty the Archon log
//
error = this->fetchlog();
// Make sure the following systemkeys are added.
// They can be changed at any time by a command but since they have defaults
// they don't require a command so this ensures they get into the systemkeys db.
//
std::stringstream keystr;
keystr << "HDRSHIFT=" << this->n_hdrshift << "// number of HDR right-shift bits";
this->systemkeys.addkey( keystr.str() );
return error;
}
/**************** Archon::Interface::connect_controller *********************/
/**************** Archon::Interface::disconnect_controller ******************/
/**
* @fn disconnect_controller
* @brief
* @param none
* @return
*
*/
long Interface::disconnect_controller() {
std::string function = "Archon::Interface::disconnect_controller";
long error;
if (!this->archon.isconnected()) {
logwrite(function, "connection already closed");
return (NO_ERROR);
}
// close the socket file descriptor to the Archon controller
//
error = this->archon.Close();
// Free the memory
//
if (this->image_data != nullptr) {
logwrite(function, "releasing allocated device memory");
delete [] this->image_data;
this->image_data=nullptr;
}
// On success, write the value to the log and return
//
if (error == NO_ERROR) {
logwrite(function, "Archon connection terminated");
} else {
// Throw an error for any other errors
this->camera.log_error( function, "disconnecting Archon camera" );
}
return error;
}
/**************** Archon::Interface::disconnect_controller ******************/
/**************** Archon::Interface::native *********************************/
/**
* @fn native
* @brief send native commands directly to Archon and log result
* @param std::string cmd
* @return long ret from archon_cmd() call
*
* This function simply calls archon_cmd() then breaks the reply into
* space-delimited tokens and puts each token into the asynchronous
* message queue. The result is that the reply comes out one line at
* a time on the async port.
*
*/
long Interface::native(const std::string& cmd) {
std::string function = "Archon::Interface::native";
std::stringstream message;
std::string reply;
std::vector<std::string> tokens;
long ret = archon_cmd(cmd, reply);
if (!reply.empty()) {
// Tokenize the reply and put each non-empty token into the asynchronous message queue.
// The reply message begins and ends with "CMD:BEGIN" and "CMD:END" and
// each line of the reply is prepended with "CMD:" where CMD is the native command
// which generated the message.
//
message << cmd << ":BEGIN";
this->camera.async.enqueue( message.str() );
Tokenize(reply, tokens, " ");
for (const auto & token : tokens) {
if ( ! token.empty() && token != "\n" ) {
message.str(""); message << cmd << ":" << token;
this->camera.async.enqueue( message.str() );
}
}
message.str(""); message << cmd << ":END";
this->camera.async.enqueue( message.str() );
}
return ret;
}
/**************** Archon::Interface::native *********************************/
/**************** Archon::Interface::archon_cmd *****************************/
/**
* @fn archon_cmd
* @brief send a command to Archon
* @param cmd
* @param reply (optional)
* @return ERROR, BUSY or NO_ERROR
*
*/
long Interface::archon_cmd(std::string cmd) { // use this form when the calling
std::string reply; // function doesn't need to look at the reply
return( archon_cmd(cmd, reply) );
}
long Interface::archon_cmd(std::string cmd, std::string &reply) {
std::string function = "Archon::Interface::archon_cmd";
std::stringstream message;
int retval;
char check[4];
char buffer[4096]; //!< temporary buffer for holding Archon replies
int error = NO_ERROR;
if (!this->archon.isconnected()) { // nothing to do if no connection open to controller
this->camera.log_error( function, "connection not open to controller" );
return ERROR;
}
if (this->archon_busy) { // only one command at a time
message.str(""); message << "Archon busy: ignored command " << cmd;
this->camera.log_error( function, message.str() );
return BUSY;
}
/**
* Hold a scoped lock for the duration of this function,
* to prevent multiple threads from accessing the Archon.
*/
const std::lock_guard<std::mutex> lock(this->archon_mutex);
this->archon_busy = true;
// build command: ">xxCOMMAND\n" where xx=hex msgref and COMMAND=command
//
this->msgref = (this->msgref + 1) % 256; // increment msgref for each new command sent
std::stringstream ssprefix;
ssprefix << ">"
<< std::setfill('0')
<< std::setw(2)
<< std::hex
<< this->msgref;
std::string prefix=ssprefix.str();
try {
std::transform( prefix.begin(), prefix.end(), prefix.begin(), ::toupper ); // make uppercase
} catch (...) {
message.str(""); message << "converting Archon command: " << prefix << " to uppercase";
this->camera.log_error( function, message.str() );
return ERROR;
}
// This allows sending commands that don't get logged,
// by prepending QUIET, which gets removed here if present.
//
bool quiet=false;
if ( cmd.find(QUIET)==0 ) {
cmd.erase(0, QUIET.length());
quiet=true;
}
std::stringstream sscmd; // sscmd = stringstream, building command
sscmd << prefix << cmd << "\n";
std::string scmd = sscmd.str(); // scmd = string, command to send
// build the command checksum: msgref used to check that reply matches command
//
SNPRINTF(check, "<%02X", this->msgref)
// log the command as long as it's not a STATUS, TIMER, WCONFIG or FRAME command
//
if ( !quiet && (cmd.compare(0,7,"WCONFIG") != 0) &&
(cmd.compare(0,5,"TIMER") != 0) &&
(cmd.compare(0,6,"STATUS") != 0) &&
(cmd.compare(0,5,"FRAME") != 0) ) {
// erase newline for logging purposes
std::string fcmd = scmd;
try {
fcmd.erase(fcmd.find('\n'), 1);
} catch(...) { }
message.str(""); message << "sending command: " << fcmd;
logwrite(function, message.str());
}
// optionally log WCONFIG commands
//
if ( this->logwconfig && cmd.find("WCONFIG")!=std::string::npos ) logwrite( function, strip_newline(cmd) );
// send the command
//
if ( (this->archon.Write(scmd)) == -1) {
this->camera.log_error( function, "writing to camera socket");
}
// For the FETCH command we don't wait for a reply, but return immediately.
// FETCH results in a binary response which is handled elsewhere (in read_frame).
// Must also distinguish this from the FETCHLOG command, for which we do wait
// for a normal reply.
//
// The scoped mutex lock will be released automatically upon return.
//
if ( (cmd.compare(0,5,"FETCH")==0)
&& (cmd.compare(0,8,"FETCHLOG")!=0) ) return (NO_ERROR);
// For all other commands, receive the reply
//
reply.clear(); // zero reply buffer
do {
if ( (retval=this->archon.Poll()) <= 0) {
if (retval==0) {
message.str("");
message << "Poll timeout waiting for response from Archon command (maybe unrecognized command?)";
error = TIMEOUT;
}
if (retval<0) {
message.str("");
message << "Poll error waiting for response from Archon command";
error = ERROR;
}
if ( error != NO_ERROR ) this->camera.log_error( function, message.str() );
break;
}
memset(buffer, '\0', 2048); // init temporary buffer
retval = this->archon.Read(buffer, 2048); // read into temp buffer
if (retval <= 0) {
this->camera.log_error( function, "reading Archon" );