/
CLIManager.cpp
2903 lines (2358 loc) · 87.6 KB
/
CLIManager.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
/*
* TI Voxel Lib component.
*
* Copyright (c) 2014 Texas Instruments Inc.
*/
#include "CLIManager.h"
#include <functional>
#include "LineNoise.h"
#include "PCLGrabber.h"
#define _C(F) std::bind(F, this, std::placeholders::_1, std::placeholders::_2)
#define _P(F) std::bind(F, this, std::placeholders::_1)
#define _H(F) std::bind(F, this)
#include <string.h>
#include <fstream>
#include <iomanip>
using namespace LineNoise;
namespace Voxel
{
CLIManager::CLIManager(CameraSystem &sys): _sys(sys)
{
Configuration conf;
_commandHistoryFileName = "history.txt";
conf.getLocalFile("cli", _commandHistoryFileName);
_commandGroups = Vector<CommandGroup>({
{"CLI Controls", {"help", "exit"}},
{"Parameter/Register Handling", {"get", "set", "cap", "getr", "setr"}},
{"Device/Camera Handling", {"list", "connect", "disconnect", "reset"}},
{"Stream Commands", {"start", "stop", "status", "save", "vxltoraw"}},
{"Filter Controls", {"filterlist", "filteradd", "filterremove", "filterparam"}},
{"Camera Profile Handling", {"profilelist", "profileselect", "profileadd", "profileremove", "profileparam", "profileparamremove", "profilesetdefault", "profilehwsave", "profilehwfetch", "profilehwremovedefault"}},
});
_commands = Map<String, Command>({
{"list", Command(_H(&CLIManager::_listHelp), _P(&CLIManager::_list), nullptr)},
{"status", Command(_H(&CLIManager::_currentHelp), _P(&CLIManager::_current), nullptr)},
{"connect", Command(_H(&CLIManager::_connectHelp), _P(&CLIManager::_connect), _C(&CLIManager::_connectCompletion))},
{"start", Command(_H(&CLIManager::_startHelp), _P(&CLIManager::_start), nullptr)},
{"stop", Command(_H(&CLIManager::_stopHelp), _P(&CLIManager::_stop), nullptr)},
{"getr", Command(_H(&CLIManager::_getRegisterHelp), _P(&CLIManager::_getRegister), nullptr)},
{"setr", Command(_H(&CLIManager::_setRegisterHelp), _P(&CLIManager::_setRegister), nullptr)},
{"get", Command(_H(&CLIManager::_getParameterHelp), _P(&CLIManager::_getParameter), _C(&CLIManager::_getParameterCompletion))},
{"set", Command(_H(&CLIManager::_setParameterHelp), _P(&CLIManager::_setParameter), _C(&CLIManager::_setParameterCompletion))},
{"save", Command(_H(&CLIManager::_saveHelp), _P(&CLIManager::_save), _C(&CLIManager::_saveCompletion))},
{"vxltoraw", Command(_H(&CLIManager::_vxlToRawHelp), _P(&CLIManager::_vxlToRaw), _C(&CLIManager::_vxlToRawCompletion))},
{"cap", Command(_H(&CLIManager::_capabilitiesHelp), _P(&CLIManager::_capabilities), _C(&CLIManager::_capabilitiesCompletion))},
{"help", Command(_H(&CLIManager::_helpHelp), _P(&CLIManager::_help), _C(&CLIManager::_helpCompletion))},
{"disconnect", Command(_H(&CLIManager::_disconnectHelp), _P(&CLIManager::_disconnect), nullptr)},
{"reset", Command(_H(&CLIManager::_resetHelp), _P(&CLIManager::_reset), nullptr)},
{"exit", Command(_H(&CLIManager::_exitHelp), _P(&CLIManager::_exit), nullptr)},
{"filterlist", Command(_H(&CLIManager::_filtersHelp), _P(&CLIManager::_filters), nullptr)},
{"filteradd", Command(_H(&CLIManager::_addFilterHelp), _P(&CLIManager::_addFilter), _C(&CLIManager::_addFilterCompletion))},
{"filterremove", Command(_H(&CLIManager::_removeFilterHelp), _P(&CLIManager::_removeFilter), _C(&CLIManager::_removeFilterCompletion))},
{"filterparam", Command(_H(&CLIManager::_setFilterParamHelp), _P(&CLIManager::_setFilterParam), _C(&CLIManager::_setFilterParamCompletion))},
{"profilelist", Command(_H(&CLIManager::_profileListHelp), _P(&CLIManager::_profileList), nullptr)},
{"profileselect", Command(_H(&CLIManager::_profileSetHelp), _P(&CLIManager::_profileSet), _C(&CLIManager::_profileSetCompletion))},
{"profileadd", Command(_H(&CLIManager::_profileAddHelp), _P(&CLIManager::_profileAdd), _C(&CLIManager::_profileAddCompletion))},
{"profileremove", Command(_H(&CLIManager::_profileRemoveHelp), _P(&CLIManager::_profileRemove), _C(&CLIManager::_profileRemoveCompletion))},
{"profileparam", Command(_H(&CLIManager::_profileParamHelp), _P(&CLIManager::_profileParam), _C(&CLIManager::_profileParamCompletion))},
{"profileparamremove", Command(_H(&CLIManager::_profileParamRemoveHelp), _P(&CLIManager::_profileParamRemove), _C(&CLIManager::_profileParamRemoveCompletion))},
{"profilesetdefault", Command(_H(&CLIManager::_profileSetDefaultHelp), _P(&CLIManager::_profileSetDefault), _C(&CLIManager::_profileSetDefaultCompletion))},
{"profilehwsave", Command(_H(&CLIManager::_profileHWSaveHelp), _P(&CLIManager::_profileHWSave), _C(&CLIManager::_profileHWSaveCompletion))},
{"profilehwfetch", Command(_H(&CLIManager::_profileHWFetchHelp), _P(&CLIManager::_profileHWFetch), _C(&CLIManager::_profileHWFetchCompletion))},
{"profilehwremovedefault",Command(_H(&CLIManager::_profileHWRemoveDefaultHelp), _P(&CLIManager::_profileHWRemoveDefault), nullptr)},
});
_specialParameters = Map<String, Command>({
{"roi", Command(_H(&CLIManager::_roiCapabilities), _P(&CLIManager::_roi), nullptr)},
{"video_mode",Command(_H(&CLIManager::_videoModeCapabilities), _P(&CLIManager::_videoMode), nullptr)},
});
// Scan and connect to the first device available
Vector<DevicePtr> devices = sys.scan();
if(devices.size() > 0)
_currentDepthCamera = sys.connect(devices[0]);
if(_currentDepthCamera)
{
_viewer = Ptr<PCLViewer>(new PCLViewer());
_viewer->setDepthCamera(_currentDepthCamera);
_viewer->start();
}
}
void CLIManager::run()
{
_commandLoop();
}
void CLIManager::_getPrompt(String &prompt)
{
if(_currentDepthCamera)
prompt = "voxel:" + _currentDepthCamera->id() + "> ";
else
prompt = "voxel:> ";
}
void CLIManager::_commandLoop()
{
_keepRunning = true;
/* Load history from file. The history file is just a plain text file
* where entries are separated by newlines. */
linenoiseHistorySetMaxLen(1000);
linenoiseHistoryLoad(_commandHistoryFileName.c_str()); /* Load the history at startup */
linenoiseSetCompletionCallback(std::bind(&CLIManager::_completionCallback, this, std::placeholders::_1, std::placeholders::_2));
char *line;
Vector<String> tokens;
String prompt;
_getPrompt(prompt);
/* Now this is the main loop of the typical linenoise-based application.
* The call to linenoise() will block as long as the user types something
* and presses enter.
*
* The typed string is returned as a malloc() allocated string by
* linenoise, so the user needs to free() it. */
while(_keepRunning && (line = linenoise(prompt.c_str())) != NULL)
{
/* Do something with the string. */
if (line[0] != '\0')
{
linenoiseHistoryAdd(line); /* Add to the history. */
linenoiseHistorySave(_commandHistoryFileName.c_str()); /* Save the history on disk. */
_getTokens(line, tokens);
free(line);
if(tokens.size() == 0)
{
logger(LOG_ERROR) << "Seems like a parse error. Please re-enter your command" << std::endl;
continue;
}
auto c = _commands.find(tokens[0]);
if(c == _commands.end())
{
logger(LOG_ERROR) << "Unknown command '" << tokens[0] << "'" << std::endl;
continue;
}
if(!c->second.process)
{
logger(LOG_ERROR) << "Don't know how to execute command '" << tokens[0] << "'" << std::endl;
continue;
}
c->second.process(tokens);
_getPrompt(prompt);
continue;
}
free(line);
}
}
void CLIManager::_getTokens(const char *command, Vector<String> &tokens)
{
const char *c = command;
const char *previousEnd = command;
bool inQuote = false;
tokens.clear();
auto index = 0;
auto maxLength = strlen(command);
while(*c != '\0' && index < maxLength)
{
index++;
if(*c == ' ' || *c == '\t')
{
if(!inQuote)
{
if(previousEnd != c)
{
tokens.push_back(String(previousEnd, (size_t)(c - previousEnd)));
previousEnd = c;
}
previousEnd++;
}
c++;
}
else if(isalnum(*c) || *c == '_' || *c == '.' || *c == '-' || *c == ':')
{
c++;
}
else if(*c == '"')
{
if(previousEnd != c)
{
tokens.push_back(String(previousEnd, (size_t)(c - previousEnd)));
previousEnd = c;
}
else if(inQuote)
{
tokens.push_back(String()); // blank string for blank quote
}
c++;
previousEnd = c;
inQuote = !inQuote;
}
else if(!inQuote) // treat each non-alnum as a separate token
{
if(previousEnd != c)
{
tokens.push_back(String(previousEnd, (size_t)(c - previousEnd)));
previousEnd = c;
}
tokens.push_back(String(c, 1));
c++;
previousEnd = c;
}
else // inQuote and non-alphabetic character
c++;
}
if(previousEnd != c)
tokens.push_back(String(previousEnd, (size_t)(c - previousEnd)));
}
void CLIManager::_helpHelp() { std::cout << "help [cmd] Print this help and for any specified Voxel command 'cmd'" << std::endl; }
void CLIManager::_exitHelp() { std::cout << "exit Exit VoxelCLI" << std::endl; }
void CLIManager::_connectHelp() { std::cout << "connect <dev> Connect to a specific device. <dev> needs to be device ID\n"
<< " in the format INTERFACE::DEVICE::SERIAL. Use double-quotes for the ID if it contains spaces" << std::endl; }
void CLIManager::_listHelp() { std::cout << "list Scan and list all valid Voxel connectable devices" << std::endl; }
void CLIManager::_currentHelp() { std::cout << "status Show the current connected device ID if present" << std::endl; }
void CLIManager::_startHelp() { std::cout << "start Start streaming and showing the current device" << std::endl; }
void CLIManager::_stopHelp() { std::cout << "stop Stop streaming the current device" << std::endl; }
void CLIManager::_getRegisterHelp() { std::cout << "getr <reg> Get register value at <reg>. Use '0x' prefix for hexadecimal" << std::endl; }
void CLIManager::_setRegisterHelp() { std::cout << "setr <reg> = <value> Set register <reg> to <value>. Use '0x' prefix for hexadecimal" << std::endl; }
void CLIManager::_getParameterHelp() { std::cout << "get <param> Get parameter value given by name <param>" << std::endl; }
void CLIManager::_capabilitiesHelp() { std::cout << "cap [<param>][*] Get capabilities of the current depth camera.\n"
<< " Optionally a parameter name can be given to list only that parameter details given by name <param>.\n"
<< " A optional wildcard can be given to list all parameters beginning with name <param>" << std::endl; }
void CLIManager::_filtersHelp() { std::cout << "filterlist List available filters and currently in use filters" << std::endl; }
void CLIManager::_addFilterHelp() { std::cout << "filteradd <frametype> <name> <pos> Add filter <name> for <frametype> at <pos>.\n"
<< " Set <pos> = -1 to add the end.\n"
<< " <frametype> can be raw/raw_processed/depth."<< std::endl; }
void CLIManager::_removeFilterHelp() { std::cout << "filterremove <frametype> <pos> Remove filter with id <filterid> for <frametype>.\n"
<< " <frametype> can be raw/raw_processed/depth."<< std::endl; }
void CLIManager::_setFilterParamHelp() { std::cout << "filterparam <frametype> <filterid> <param> = <value> Set parameter <param> to <value> for filter\n"
<< " with id <filterid> present for frame type <frametype>.\n"
<< " <frametype> can be raw/raw_processed/depth" << std::endl; }
void CLIManager::_setParameterHelp() { std::cout << "set <param> = <value> Set parameter value given by name <param>. Use '0x' prefix for hexadecimal" << std::endl; }
void CLIManager::_saveHelp() { std::cout << "save <type> <count> <filename> Save current 'count' number of frames.\n"
<< " 'type' = raw/phase/ambient/amplitude/flags/depth/pointcloud/vxl" << std::endl; }
void CLIManager::_vxlToRawHelp() { std::cout << "vxltoraw <rawtype> <vxlfile> <rawfile> Get raw data from saved VXL file.\n"
<< " 'rawtype' = raw/phase/ambient/amplitude/flags/depth/pointcloud" << std::endl; }
void CLIManager::_disconnectHelp() { std::cout << "disconnect Disconnect the currently connected depth camera" << std::endl; }
void CLIManager::_resetHelp() { std::cout << "reset Reset and disconnect the currently connected depth camera" << std::endl; }
void CLIManager::_profileListHelp() { std::cout << "profilelist List all profiles associated with the currently connected depth camera" << std::endl; }
void CLIManager::_profileSetHelp() { std::cout << "profileselect <id> Select and apply a particular profile to currently connected depth camera" << std::endl; }
void CLIManager::_profileAddHelp() { std::cout << "profileadd <name> [parentid] Add a new profile to currently connected depth camera" << std::endl
<< " An optional parentid can be specified indicating the parent profile" << std::endl
<< " Enclose name in double-quotes when it has spaces" << std::endl; }
void CLIManager::_profileRemoveHelp() { std::cout << "profileremove <id> Remove profile with 'id' associated with currently connected depth camera" << std::endl; }
void CLIManager::_profileParamHelp() { std::cout << "profileparam <id> [section [paramname[=value]]] Read/Write a section/parameter in profile with 'id'" << std::endl; }
void CLIManager::_profileParamRemoveHelp() { std::cout << "profileparamremove <id> <section> [paramname] Remove a section/parameter in profile with 'id'" << std::endl; }
void CLIManager::_profileSetDefaultHelp() { std::cout << "profilesetdefault <id> Select and apply a particular profile as default to currently connected depth camera" << std::endl; }
void CLIManager::_profileHWSaveHelp() { std::cout << "profilehwsave <id> Save a particular profile on depth camera hardware." << std::endl
<< " The profile 'id' to be saved should be on local host computer where this CLI is being run" << std::endl; }
void CLIManager::_profileHWFetchHelp() { std::cout << "profilehwfetch <id> Save a particular hardware profile's configuration file on local host computer." << std::endl
<< " NOTE: The saved configuration is only for review purposes and is not usable directly as profile." << std::endl; }
void CLIManager::_profileHWRemoveDefaultHelp() { std::cout << "profilehwremovedefault Remove default setting of profile in depth camera hardware" << std::endl; }
void CLIManager::_help(const Vector<String> &tokens)
{
if(tokens.size() >= 2)
{
auto c = _commands.find(tokens[1]);
if(c == _commands.end())
{
logger(LOG_ERROR) << "Unknown command '" << tokens[1] << "'" << std::endl;
return;
}
if(!c->second.help)
{
logger(LOG_ERROR) << "No help available about command '" << tokens[1] << "'" << std::endl;
return;
}
c->second.help();
}
else
{
for(auto &g: _commandGroups)
{
std::cout << "## " << g.name << " ##" << std::endl;
for(auto &c: g.commands)
{
auto command = _commands.find(c);
if(command == _commands.end() || !command->second.help)
{
logger(LOG_ERROR) << "Unknown command '" << c << "'" << std::endl;
}
else
{
command->second.help();
std::cout << std::endl;
}
}
}
}
}
void CLIManager::_helpCompletion(const Vector<String> &tokens, linenoiseCompletions *lc)
{
if(tokens.size() == 1)
{
for(auto &g: _commandGroups)
{
for(auto &c: g.commands)
{
auto command = _commands.find(c);
if(command != _commands.end())
linenoiseAddCompletion(lc, (tokens[0] + " " + c).c_str());
}
}
}
else if(tokens.size() > 1)
{
for(auto &g: _commandGroups)
{
for(auto &c: g.commands)
{
if(c.size() < tokens[1].size() || c.compare(0, tokens[1].size(), tokens[1]) != 0)
continue;
auto command = _commands.find(c);
if(command != _commands.end())
linenoiseAddCompletion(lc, (tokens[0] + " " + c).c_str());
}
}
}
}
void CLIManager::_exit(const Vector<String> &tokens)
{
_keepRunning = false;
_disconnect(tokens);
}
void CLIManager::_list(const Vector<String> &tokens)
{
Vector<DevicePtr> devices = _sys.scan();
if(!devices.size())
{
logger(LOG_ERROR) << "Could not find any valid devices." << std::endl;
return;
}
for(auto &d: devices)
{
std::cout << d->id();
if(d->description().size())
std::cout << " -- " << d->description();
std::cout << std::endl;
}
}
void CLIManager::_connect(const Vector<String> &tokens)
{
if(tokens.size() < 2)
{
logger(LOG_ERROR) << "Please specify a device ID" << std::endl;
return;
}
std::cout << "Searching for '" << tokens[1] << "'..." << std::endl;
Vector<DevicePtr> devices = _sys.scan();
for(auto &d: devices)
{
if(d->id() == tokens[1])
{
DepthCameraPtr p = _sys.connect(d);
if(!p)
{
std::cout << "Couldn't connect to specific device ID" << std::endl;
return;
}
bool wasRunning = false;
if(_viewer && _viewer->isRunning())
{
_stop(tokens);
wasRunning = true;
}
if(_currentDepthCamera)
{
_disconnect(tokens); // disconnect currently connected depth camera
}
_currentDepthCamera = p;
if(wasRunning)
_start(tokens);
return;
}
}
logger(LOG_ERROR) << "Could not find a valid device with specified ID" << std::endl;
}
void CLIManager::_current(const Vector<String> &tokens)
{
if(_currentDepthCamera)
{
std::cout << "Current device ID = " << _currentDepthCamera->id() << std::endl;
if(_viewer && _viewer->isRunning())
std::cout << "It is currently streaming. Use 'stop' command to stop streaming" << std::endl;
else
std::cout << "It is currently not streaming. Use 'start' command to start streaming" << std::endl;
}
else
std::cout << "No device is current in use by this CLI" << std::endl;
}
void CLIManager::_start(const Vector<String> &tokens)
{
if(!_currentDepthCamera)
{
logger(LOG_ERROR) << "No device connected. Kindly 'connect' a device first" << std::endl;
return;
}
if(!_viewer)
{
_viewer = Ptr<PCLViewer>(new PCLViewer());
}
if(!_viewer->isRunning())
{
_viewer->setDepthCamera(_currentDepthCamera);
_viewer->start();
}
else
{
_viewer->stop();
_viewer->setDepthCamera(_currentDepthCamera);
_viewer->start();
}
}
void CLIManager::_stop(const Vector<String> &tokens)
{
if(!_viewer)
return;
_viewer->stop();
}
void CLIManager::_getParameter(const Vector<String> &tokens)
{
if(!_currentDepthCamera)
{
logger(LOG_ERROR) << "Please connect to a depth camera first" << std::endl;
return;
}
if(tokens.size() < 2)
{
logger(LOG_ERROR) << "Please specific a parameter name" << std::endl;
return;
}
auto sp = _specialParameters.find(tokens[1]);
// Is a special parameter?
if(sp != _specialParameters.end() && sp->second.process)
{
sp->second.process(tokens);
return;
}
ParameterPtr param = _currentDepthCamera->getParam(tokens[1]);
if(!param)
{
logger(LOG_ERROR) << "No valid parameter with name = '" << tokens[1] << "'" << std::endl;
return;
}
BoolParameter *boolParam = dynamic_cast<BoolParameter *>(param.get());
IntegerParameter *intParam = dynamic_cast<IntegerParameter *>(param.get());
UnsignedIntegerParameter *uintParam = dynamic_cast<UnsignedIntegerParameter *>(param.get());
FloatParameter *floatParam = dynamic_cast<FloatParameter *>(param.get());
EnumParameter *enumParam = dynamic_cast<EnumParameter *>(param.get());
if(boolParam)
{
bool value;
if(!boolParam->get(value))
{
logger(LOG_ERROR) << "Failed to get parameter '" << tokens[1] << "'" << std::endl;
return;
}
std::cout << tokens[1] << " = " << (value?"true":"false");
const Vector<String> &meaning = boolParam->valueMeaning();
if(meaning.size() == 2 && meaning[value].size())
std::cout << " (" << meaning[value] << ")";
std::cout << std::endl;
return;
}
if(intParam)
{
int value;
if(!intParam->get(value))
{
logger(LOG_ERROR) << "Failed to get parameter '" << tokens[1] << "'" << std::endl;
return;
}
std::cout << tokens[1] << " = " << std::dec << value << " " << intParam->unit() << std::endl;
return;
}
if(uintParam)
{
uint value;
if(!uintParam->get(value))
{
logger(LOG_ERROR) << "Failed to get parameter '" << tokens[1] << "'" << std::endl;
return;
}
std::cout << tokens[1] << " = " << std::dec << value << " " << uintParam->unit() << std::endl;
return;
}
if(floatParam)
{
float value;
if(!floatParam->get(value))
{
logger(LOG_ERROR) << "Failed to get parameter '" << tokens[1] << "'" << std::endl;
return;
}
std::cout << tokens[1] << " = " << std::dec << value << " " << floatParam->unit() << std::endl;
return;
}
if(enumParam)
{
int value;
if(!enumParam->get(value))
{
logger(LOG_ERROR) << "Failed to get parameter '" << tokens[1] << "'" << std::endl;
return;
}
std::cout << tokens[1] << " = " << std::dec << value;
const Vector<String> &meaning = enumParam->valueMeaning();
if(meaning.size() > value && meaning[value].size())
std::cout << " (" << meaning[value] << ")";
std::cout << std::endl;
return;
}
logger(LOG_ERROR) << "Unknown type of parameter '" << tokens[1] << "'. Don't know how to handle" << std::endl;
}
void CLIManager::_setParameter(const Vector<String> &tokens)
{
if(!_currentDepthCamera)
{
logger(LOG_ERROR) << "Please connect to a depth camera first" << std::endl;
return;
}
if(tokens.size() < 4 || tokens[2] != "=")
{
logger(LOG_ERROR) << "Please specify a parameter name and value to be set in the format given below." << std::endl;
_setParameterHelp();
return;
}
auto sp = _specialParameters.find(tokens[1]);
// Is a special parameter?
if(sp != _specialParameters.end() && sp->second.process)
{
sp->second.process(tokens);
return;
}
ParameterPtr param = _currentDepthCamera->getParam(tokens[1]);
if(!param)
{
logger(LOG_ERROR) << "No valid parameter with name = '" << tokens[1] << "'" << std::endl;
return;
}
BoolParameter *boolParam = dynamic_cast<BoolParameter *>(param.get());
IntegerParameter *intParam = dynamic_cast<IntegerParameter *>(param.get());
UnsignedIntegerParameter *uintParam = dynamic_cast<UnsignedIntegerParameter *>(param.get());
FloatParameter *floatParam = dynamic_cast<FloatParameter *>(param.get());
EnumParameter *enumParam = dynamic_cast<EnumParameter *>(param.get());
if(boolParam)
{
bool value;
if(tokens[3] == "true")
value = true;
else if(tokens[3] == "false")
value = false;
else
{
std::istringstream s(tokens[3]);
s.unsetf(std::ios_base::basefield);
s >> value;
}
std::cout << "Setting parameter '" << tokens[1] << "' = " << (value?"true":"false") << " ..." << std::endl;
if(!boolParam->set(value))
{
logger(LOG_ERROR) << "Failed to set parameter '" << tokens[1] << "'" << std::endl;
return;
}
std::cout << "Successfully set parameter '" << tokens[1] << "'" << std::endl;
return;
}
if(intParam)
{
int value;
std::istringstream s(tokens[3]);
s.unsetf(std::ios_base::basefield);
s >> value;
std::cout << "Setting parameter '" << tokens[1] << "' = " << std::dec << value << " " << intParam->unit() << " ..." << std::endl;
if(!intParam->set(value))
{
logger(LOG_ERROR) << "Failed to set parameter '" << tokens[1] << "'" << std::endl;
return;
}
std::cout << "Successfully set parameter '" << tokens[1] << "'" << std::endl;
return;
}
if(uintParam)
{
uint value;
std::istringstream s(tokens[3]);
s.unsetf(std::ios_base::basefield);
s >> value;
std::cout << "Setting parameter '" << tokens[1] << "' = " << std::dec << value << " " << uintParam->unit() << " ..." << std::endl;
if(!uintParam->set(value))
{
logger(LOG_ERROR) << "Failed to set parameter '" << tokens[1] << "'" << std::endl;
return;
}
std::cout << "Successfully set parameter '" << tokens[1] << "'" << std::endl;
return;
}
if(floatParam)
{
float value;
std::istringstream s(tokens[3]);
s >> value;
std::cout << "Setting parameter '" << tokens[1] << "' = " << std::dec << value << " " << floatParam->unit() << " ..." << std::endl;
if(!floatParam->set(value))
{
logger(LOG_ERROR) << "Failed to set parameter '" << tokens[1] << "'" << std::endl;
return;
}
std::cout << "Successfully set parameter '" << tokens[1] << "'" << std::endl;
return;
}
if(enumParam)
{
int value;
std::istringstream s(tokens[3]);
s.unsetf(std::ios_base::basefield);
s >> value;
std::cout << "Setting parameter '" << tokens[1] << "' = " << std::dec << value << " ..." << std::endl;
if(!enumParam->set(value))
{
logger(LOG_ERROR) << "Failed to set parameter '" << tokens[1] << "'" << std::endl;
return;
}
std::cout << "Successfully set parameter '" << tokens[1] << "'" << std::endl;
return;
}
logger(LOG_ERROR) << "Unknown type of parameter '" << tokens[1] << "'. Don't know how to handle" << std::endl;
}
void CLIManager::_getRegister(const Vector<String> &tokens)
{
if(!_currentDepthCamera)
{
logger(LOG_ERROR) << "Please connect to a depth camera first" << std::endl;
return;
}
if(!_currentDepthCamera->getProgrammer())
{
logger(LOG_ERROR) << "Current depth camera " << _currentDepthCamera-> id() << " does not have a register programmer." << std::endl;
return;
}
if(tokens.size() < 2)
{
logger(LOG_ERROR) << "Please specify a register address to read from" << std::endl;
return;
}
uint address, value;
std::istringstream s(tokens[1]);
s.unsetf(std::ios_base::basefield);
s >> address;
if(!_currentDepthCamera->getProgrammer()->readRegister(address, value))
{
logger(LOG_ERROR) << "Could not read register @0x" << std::hex << address << std::endl;
}
else
std::cout << "Register @0x" << std::hex << address << " = 0x" << std::hex << value << std::endl;
}
void CLIManager::_setRegister(const Vector<String> &tokens)
{
if(!_currentDepthCamera)
{
logger(LOG_ERROR) << "Please connect to a depth camera first" << std::endl;
return;
}
if(!_currentDepthCamera->getProgrammer())
{
logger(LOG_ERROR) << "Current depth camera " << _currentDepthCamera-> id() << " does not have a register programmer." << std::endl;
return;
}
if(tokens.size() < 4 || tokens[2] != "=")
{
logger(LOG_ERROR) << "Please specify a register address to write to and value to set in the following format" << std::endl;
_setRegisterHelp();
return;
}
uint address, value;
{
std::istringstream s(tokens[1]);
s.unsetf(std::ios_base::basefield);
s >> address;
}
{
std::istringstream s(tokens[3]);
s.unsetf(std::ios_base::basefield);
s >> value;
}
std::cout << "Setting register @0x" << std::hex << address << " = " << std::hex << "0x" << value << " ..." << std::endl;
if(!_currentDepthCamera->getProgrammer()->writeRegister(address, value))
{
logger(LOG_ERROR) << "Could not write to register @0x" << std::hex << address << std::endl;
}
else
std::cout << "Successfully set register @0x" << std::hex << address << std::endl;
}
void CLIManager::_capabilities(const Vector<String> &tokens)
{
if(!_currentDepthCamera)
{
logger(LOG_ERROR) << "Please connect to a depth camera first" << std::endl;
return;
}
if(tokens.size() < 2)
{
// Show special parameters first
for(auto &sp: _specialParameters)
{
if(sp.second.help)
sp.second.help();
}
const Map<String, ParameterPtr> ¶meters = _currentDepthCamera->getParameters();
for(auto &p: parameters)
{
_showParameterInfo(p.second);
}
}
else if(tokens.size() == 2)
{
auto sp = _specialParameters.find(tokens[1]);
// Is a special parameter?
if(sp != _specialParameters.end() && sp->second.help)
{
sp->second.help();
return;
}
_showParameterInfo(_currentDepthCamera->getParam(tokens[1]));
}
else if(tokens[2] == "*") // wildcard match?
{
for(auto &sp: _specialParameters)
{
const String &n = sp.first;
if(n.size() < tokens[1].size()) // parameter name smaller than search name?
continue;
if(n.compare(0, tokens[1].size(), tokens[1]) == 0) // matches first part in the name?
sp.second.help();
}
const Map<String, ParameterPtr> ¶meters = _currentDepthCamera->getParameters();
for(auto &p: parameters)
{
const String &n = p.second->name();
if(n.size() < tokens[1].size()) // parameter name smaller than search name?
continue;
if(n.compare(0, tokens[1].size(), tokens[1]) == 0) // matches first part in the name?
_showParameterInfo(p.second);
}
}
}
void CLIManager::_showParameterInfo(const ParameterPtr ¶m)
{
if(!param)
{
logger(LOG_ERROR) << "Null parameter given for display" << std::endl;
return;
}
std::cout << std::endl;
BoolParameter *boolParam = dynamic_cast<BoolParameter *>(param.get());
IntegerParameter *intParam = dynamic_cast<IntegerParameter *>(param.get());
UnsignedIntegerParameter *uintParam = dynamic_cast<UnsignedIntegerParameter *>(param.get());
FloatParameter *floatParam = dynamic_cast<FloatParameter *>(param.get());
EnumParameter *enumParam = dynamic_cast<EnumParameter *>(param.get());
String n = param->name() + ((param->ioType() == Parameter::IO_READ_ONLY)?" [R]":" [RW]");
std::cout << n;
for(auto i = 0; i < 4 - n.size()/8; i++)
std::cout << "\t";
bool descShown = true;
if(param->displayName().size())
std::cout << "(" << param->displayName() << "). " << param->description() << std::endl;
else if(param->description().size())
std::cout << param->description() << std::endl;
else
descShown = false;
if(param->address())
{
if(descShown)
std::cout << "\t\t\t\t";
std::cout << "Register: 0x" << std::hex << param->address() << "[" << std::dec << (uint)param->msb() << ":" << (uint)param->lsb() << "]" << std::endl;
}
if(boolParam) // FIXME: Handle strobe type later
{
bool value;
if(boolParam->get(value))
std::cout << "\t\t\t\tCurrent value: " << (value?"true":"false") << std::endl;
if(boolParam->valueMeaning().size())
{
std::cout << "\t\t\t\tPossible values:" << std::endl;
for(auto i = 0; i < boolParam->valueMeaning().size(); i++)
{
std::cout << "\t\t\t\t" << ((i == 0)?"false":"true");
if(boolParam->valueMeaning()[i].size())
std::cout << " -> " << boolParam->valueMeaning()[i];
if(boolParam->valueDescription().size() > i &&boolParam->valueDescription()[i].size())
{
std::cout << " (" << boolParam->valueDescription()[i] << ")";
}
std::cout << std::endl;
}
}
return;
}
if(intParam)
{
int value;
if(intParam->get(value))
std::cout << "\t\t\t\tCurrent value: " << std::dec << value << " " << intParam->unit() << std::endl;
std::cout << "\t\t\t\tLimits: [" << std::dec << intParam->lowerLimit() << " " << intParam->unit() << ", " << intParam->upperLimit() << " " << intParam->unit() << "]" << std::endl;
return;
}
if(uintParam)
{
uint value;
if(uintParam->get(value))
std::cout << "\t\t\t\tCurrent value: " << std::dec << value << " " << uintParam->unit() << std::endl;
std::cout << "\t\t\t\tLimits: [" << std::dec << uintParam->lowerLimit() << " " << uintParam->unit() << ", " << uintParam->upperLimit() << " " << uintParam->unit() << "]" << std::endl;
return;
}
if(floatParam)
{
float value;
if(floatParam->get(value))
std::cout << "\t\t\t\tCurrent value: " << std::dec << value << " " << floatParam->unit() << std::endl;
std::cout << "\t\t\t\tLimits: [" << std::dec << floatParam->lowerLimit() << " " << floatParam->unit() << ", " << floatParam->upperLimit() << " " << floatParam->unit() << "]" << std::endl;
return;
}