-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathDeviceBase.h
2505 lines (2194 loc) · 72.6 KB
/
DeviceBase.h
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: DeviceBase.h
// PROJECT: Micro-Manager
// SUBSYSTEM: MMDevice - Device adapter kit
//-----------------------------------------------------------------------------
// DESCRIPTION: Generic functionality for implementing device adapters
//
// AUTHOR: Nenad Amodaj, [email protected], 08/18/2005
//
// COPYRIGHT: University of California, San Francisco, 2006
//
// LICENSE: This file is distributed under the BSD license.
// License text is included with the source distribution.
//
// This file is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES.
//
//
#ifndef _DEVICE_BASE_H_
#define _DEVICE_BASE_H_
#include "MMDevice.h"
#include "MMDeviceConstants.h"
#include "Property.h"
#include "DeviceUtils.h"
#include "ModuleInterface.h"
#include "DeviceThreads.h"
#include <math.h>
#include <assert.h>
#include <string>
#include <vector>
#include <iomanip>
#include <map>
#include <sstream>
// common error messages
const char* const g_Msg_ERR = "Unknown error in the device";
const char* const g_Msg_INVALID_PROPERTY = "Invalid property name encountered";
const char* const g_Msg_INVALID_PROPERTY_VALUE = "Invalid property value";
const char* const g_Msg_DUPLICATE_PROPERTY = "Duplicate property names are not allowed";
const char* const g_Msg_INVALID_PROPERTY_TYPE = "Invalid property type";
const char* const g_Msg_NATIVE_MODULE_FAILED = "Native module failed to load";
const char* const g_Msg_UNSUPPORTED_DATA_FORMAT = "Unsupported data format encountered";
const char* const g_Msg_INTERNAL_INCONSISTENCY = "Device adapter inconsistent with the actual device";
const char* const g_Msg_NOT_SUPPORTED = "Device not supported by the adapter";
const char* const g_Msg_UNKNOWN_LABEL = "Label not defined";
const char* const g_Msg_UNSUPPORTED_COMMAND = "Unsupported device command";
const char* const g_Msg_UNKNOWN_POSITION = "Invalid state (position) requested";
const char* const g_Msg_NO_CALLBACK_REGISTERED = "No callback registered";
const char* const g_Msg_SERIAL_BUFFER_OVERRUN = "Serial buffer overrun.";
const char* const g_Msg_SERIAL_INVALID_RESPONSE = "Unexpected response from serial port. Is the device connected to the correct serial port?";
const char* const g_Msg_SERIAL_TIMEOUT = "Serial timeout occurred.";
const char* const g_Msg_SELF_REFERENCE = "Self reference error.";
const char* const g_Msg_NO_PROPERTY_DATA = "No property data error.";
const char* const g_Msg_DEVICE_DUPLICATE_LABEL = "Position label already in use";
const char* const g_Msg_INVALID_INPUT_PARAM = "Invalid input parameter.";
const char* const g_Msg_BUFFER_OVERFLOW = "Buffer Overflow.";
const char* const g_Msg_SERIAL_COMMAND_FAILED = "Serial command failed. Is the device connected to the serial port?";
const char* const g_Msg_DEVICE_NONEXISTENT_CHANNEL = "Requested channel is not defined.";
const char* const g_Msg_DEVICE_INVALID_PROPERTY_LIMTS = "Specified property limits are not valid."
" Either the property already has a set of discrete values, or the range is invalid";
const char* const g_Msg_EXCEPTION_IN_THREAD = "Exception in the thread function.";
const char* const g_Msg_EXCEPTION_IN_ON_THREAD_EXITING = "Exception in the OnThreadExiting function.";
const char* const g_Msg_SEQUENCE_ACQUISITION_THREAD_EXITING="Sequence thread exiting";
const char* const g_Msg_DEVICE_CAMERA_BUSY_ACQUIRING="Camera is busy acquiring images. Stop camera activity before changing this property";
const char* const g_Msg_DEVICE_CAN_NOT_SET_PROPERTY="The device can not set this property at this moment";
const char* const g_Msg_DEVICE_NOT_CONNECTED="Unable to communicate with the device.";
const char* const g_Msg_DEVICE_COMM_HUB_MISSING= "Parent module (Hub) is not available or defined for this device!";
const char* const g_Msg_DEVICE_DUPLICATE_LIBRARY="Duplicate Device Library Name";
const char* const g_Msg_DEVICE_PROPERTY_NOT_SEQUENCEABLE="This property is not sequenceable";
const char* const g_Msg_DEVICE_SEQUENCE_TOO_LARGE="Sequence is too large for this device";
const char* const g_Msg_DEVICE_NOT_YET_IMPLEMENTED="This command has not yet been implemented for this device.";
inline long nint( double value )
{
return (long)floor( 0.5 + value);
};
/**
* Implements functionality common to all devices.
* Typically used as the base class for actual device adapters. In general,
* derived class do not override DeviceBase methods, but rather take advantage
* of using them to simplify development of specific drivers.
*/
template <class T, class U>
class CDeviceBase : public T
{
public:
typedef MM::Action<U> CPropertyAction;
typedef MM::ActionEx<U> CPropertyActionEx;
/**
* Returns the library handle (for use only by the calling code).
*/
virtual HDEVMODULE GetModuleHandle() const {return module_;}
/**
* Assigns a name for the module (for use only by the calling code).
*/
virtual void SetModuleName(const char* name)
{
moduleName_ = name;
}
/**
* Returns the module name (for use only by the calling code).
*/
virtual void GetModuleName(char* name) const
{
CDeviceUtils::CopyLimitedString(name, moduleName_.c_str());
}
/**
* Assigns description string for a device (for use only by the calling code).
*/
virtual void SetDescription(const char* descr)
{
description_ = descr;
}
/**
* Returns device description (for use only by the calling code).
*/
virtual void GetDescription(char* name) const
{
CDeviceUtils::CopyLimitedString(name, description_.c_str());
}
/**
* Sets the library handle (for use only by the calling code).
*/
virtual void SetModuleHandle(HDEVMODULE hModule) {module_ = hModule;}
/**
* Sets the device label (for use only by the calling code).
* Labels are usually manipulated by the parent application and used
* for high-level programming.
*/
virtual void SetLabel(const char* label)
{
label_ = label;
}
/**
* Returns the device label (for use only by the calling code).
* Labels are usually manipulated by the parent application and used
* for high-level programming.
*/
virtual void GetLabel(char* name) const
{
CDeviceUtils::CopyLimitedString(name, label_.c_str());
}
/**
* Returns device delay used for synchronization by the calling code.
* Delay of 0 means that the device should be synchronized by polling with the
* Busy() method.
*/
virtual double GetDelayMs() const {return delayMs_;}
/**
* Returns the device delay used for synchronization by the calling code.
* Delay of 0 means that the device should be synchronized by polling with the
* Busy() method.
*/
virtual void SetDelayMs(double delay) {delayMs_ = delay;}
/**
* Sets the callback for accessing parent functionality (used only by the calling code).
*/
virtual void SetCallback(MM::Core* cbk) {callback_ = cbk;}
/**
* Signals if the device responds to different delay settings.
* Default device behavior is to ignore delays and use busy signals instead.
*/
virtual bool UsesDelay() {return usesDelay_;}
/**
* Returns the number of properties.
*/
virtual unsigned GetNumberOfProperties() const {return (unsigned)properties_.GetSize();}
/**
* Obtains the value of the property.
* @param name - property identifier (name)
* @param value - the value of the property
*/
virtual int GetProperty(const char* name, char* value) const
{
std::string strVal;
// additional information for reporting invalid properties.
SetMorePropertyErrorInfo(name);
int nRet = properties_.Get(name, strVal);
if (nRet == DEVICE_OK)
CDeviceUtils::CopyLimitedString(value, strVal.c_str());
return nRet;
}
/**
* Obtains the value of the property.
* @param name - property identifier (name)
* @param value - the value of the property
*/
int GetProperty(const char* name, double& val)
{
std::string strVal;
int nRet = properties_.Get(name, strVal);
if (nRet == DEVICE_OK)
val = atof(strVal.c_str());
return nRet;
}
/**
* Obtains the value of the property.
* @param name - property identifier (name)
* @param value - the value of the property
*/
int GetProperty(const char* name, long& val)
{
std::string strVal;
int nRet = properties_.Get(name, strVal);
if (nRet == DEVICE_OK)
val = atol(strVal.c_str());
return nRet;
}
/**
* Check if the property value is equal to a specific string
* @return true only if property exists and is equal to, false otherwise
* @param name - property identifier (name)
* @param value - the value to compare to
*/
bool IsPropertyEqualTo(const char* name, const char* val) const
{
std::string strVal;
int nRet = properties_.Get(name, strVal);
if (nRet == DEVICE_OK)
return strcmp(val, strVal.c_str()) == 0;
else
return false;
}
/**
* Checks whether the property is read-only.
* @param name - property identifier (name)
* @param readOnly - read-only or not
*/
virtual int GetPropertyReadOnly(const char* name, bool& readOnly) const
{
MM::Property* pProp = properties_.Find(name);
if (!pProp)
{
// additional information for reporting invalid properties.
SetMorePropertyErrorInfo(name);
return DEVICE_INVALID_PROPERTY;
}
readOnly = pProp->GetReadOnly();
return DEVICE_OK;
}
/**
* Checks whether the property is read-only.
* @param name - property identifier (name)
* @param readOnly - read-only or not
*/
virtual int GetPropertyInitStatus(const char* name, bool& preInit) const
{
MM::Property* pProp = properties_.Find(name);
if (!pProp)
{
// additional information for reporting invalid properties.
SetMorePropertyErrorInfo(name);
return DEVICE_INVALID_PROPERTY;
}
preInit = pProp->GetInitStatus();
return DEVICE_OK;
}
virtual int HasPropertyLimits(const char* name, bool& hasLimits) const
{
MM::Property* pProp = properties_.Find(name);
if (!pProp)
{
// additional information for reporting invalid properties.
SetMorePropertyErrorInfo(name);
return DEVICE_INVALID_PROPERTY;
}
hasLimits = pProp->HasLimits();
return DEVICE_OK;
}
/**
* Provides lower limit for a property that has property limits
* @param name - property identifier (name)
* @param lowLimit - returns lower limit
*/
virtual int GetPropertyLowerLimit(const char* name, double& lowLimit) const
{
MM::Property* pProp = properties_.Find(name);
if (!pProp)
{
// additional information for reporting invalid properties.
SetMorePropertyErrorInfo(name);
return DEVICE_INVALID_PROPERTY;
}
lowLimit = pProp->GetLowerLimit();
return DEVICE_OK;
}
/**
* Provides upper limit for a property that has property limits
* @param name - property identifier (name)
* @param hiLimit - returns upper limit
*/
virtual int GetPropertyUpperLimit(const char* name, double& hiLimit) const
{
MM::Property* pProp = properties_.Find(name);
if (!pProp)
{
// additional information for reporting invalid properties.
SetMorePropertyErrorInfo(name);
return DEVICE_INVALID_PROPERTY;
}
hiLimit = pProp->GetUpperLimit();
return DEVICE_OK;
}
/**
* Checks whether the property can be run in a sequence
* @param name - property identifier (name)
* @param sequenceable - sequenceable or not
*/
virtual int IsPropertySequenceable(const char* name, bool& sequenceable) const
{
MM::Property* pProp = properties_.Find(name);
if (!pProp)
{
// additional information for reporting invalid properties.
SetMorePropertyErrorInfo(name);
return DEVICE_INVALID_PROPERTY;
}
sequenceable = pProp->IsSequenceable();
return DEVICE_OK;
}
/**
* Provides the maximum number of events that can be executed by this sequenceable property
* @param name - property identifier (name)
* @param nrEvents - maximum number of events that can be handles by the device
*/
virtual int GetPropertySequenceMaxLength(const char* name, long& nrEvents) const
{
MM::Property* pProp = properties_.Find(name);
if (!pProp)
{
// additional information for reporting invalid properties.
SetMorePropertyErrorInfo(name);
return DEVICE_INVALID_PROPERTY;
}
bool sequenceable;
int ret = IsPropertySequenceable(name, sequenceable);
if (ret != DEVICE_OK)
return ret;
if (!sequenceable) {
SetMorePropertyErrorInfo(name);
return DEVICE_PROPERTY_NOT_SEQUENCEABLE;
}
nrEvents = pProp->GetSequenceMaxSize();
return DEVICE_OK;
}
/**
* Starts a (TTL-triggered) sequence for the given property
* Should be overridden by the device adapter (when a sequence is implemented)
* @param name - property for which the sequence should be started
*/
virtual int StartPropertySequence(const char* name)
{
MM::Property* pProp = properties_.Find(name);
if (!pProp)
{
// additional information for reporting invalid properties.
SetMorePropertyErrorInfo(name);
return DEVICE_INVALID_PROPERTY;
}
bool sequenceable;
int ret = IsPropertySequenceable(name, sequenceable);
if (ret != DEVICE_OK)
return ret;
if (!sequenceable) {
SetMorePropertyErrorInfo(name);
return DEVICE_PROPERTY_NOT_SEQUENCEABLE;
}
return pProp->StartSequence();
}
/**
* Stops a (TTL-triggered) sequence for the given property
* Should be overridden by the device adapter (when a sequence is implemented)
* @param name - property for which the sequence should be started
*/
virtual int StopPropertySequence(const char* name)
{
MM::Property* pProp = properties_.Find(name);
if (!pProp)
{
SetMorePropertyErrorInfo(name);
return DEVICE_INVALID_PROPERTY;
}
bool sequenceable;
int ret = IsPropertySequenceable(name, sequenceable);
if (ret != DEVICE_OK)
return ret;
if (!sequenceable) {
SetMorePropertyErrorInfo(name);
return DEVICE_PROPERTY_NOT_SEQUENCEABLE;
}
return pProp->StopSequence();
}
/**
* This function is used by the Core to communicate a sequence to the device
* @param name - name of the sequenceable property
*/
virtual int ClearPropertySequence(const char* name)
{
MM::Property* pProp;
int ret = GetSequenceableProperty(&pProp, name);
if (ret != DEVICE_OK)
return ret;
return pProp->ClearSequence();
}
/**
* This function is used by the Core to communicate a sequence to the device
* @param name - name of the sequenceable property
*/
virtual int AddToPropertySequence(const char* name, const char* value)
{
MM::Property* pProp;
int ret = GetSequenceableProperty(&pProp, name);
if (ret != DEVICE_OK)
return ret;
return pProp->AddToSequence(value);
}
/**
* This function is used by the Core to communicate a sequence to the device
* Sends the sequence to the device by calling the properties functor
* @param name - name of the sequenceable property
*/
virtual int SendPropertySequence(const char* name)
{
MM::Property* pProp;
int ret = GetSequenceableProperty(&pProp, name);
if (ret != DEVICE_OK)
return ret;
return pProp->SendSequence();
}
/**
* Obtains the property name given the index.
* Can be used for enumerating properties.
* @param uIdx - property index
* @param name - property name
*/
virtual bool GetPropertyName(unsigned uIdx, char* name) const
{
std::string strName;
if (!properties_.GetName(uIdx, strName))
return false;
CDeviceUtils::CopyLimitedString(name, strName.c_str());
return true;
}
/**
* Obtain property type (string, float or integer)
*/
virtual int GetPropertyType(const char* name, MM::PropertyType& pt) const
{
MM::Property* pProp = properties_.Find(name);
if (!pProp)
return DEVICE_INVALID_PROPERTY;
pt = pProp->GetType();
return DEVICE_OK;
}
/**
* Sets the property value.
* @param name - property name
* @param value - property value
*/
virtual int SetProperty(const char* name, const char* value)
{
int ret = properties_.Set(name, value);
if( DEVICE_OK != ret)
{
// additional information for reporting invalid properties.
SetMorePropertyErrorInfo(name);
}
return ret;
}
/**
* Checks if device supports a given property.
*/
virtual bool HasProperty(const char* name) const
{
MM::Property* pProp = properties_.Find(name);
if (pProp)
return true;
else
return false;
}
/**
* Returns the number of allowed property values.
* If the set of property values is not defined, not bounded,
* or property does not exist, the call returns 0.
*/
virtual unsigned GetNumberOfPropertyValues(const char* propertyName) const
{
MM::Property* pProp = properties_.Find(propertyName);
if (!pProp)
return 0;
return (unsigned)pProp->GetAllowedValues().size();
}
/**
* Returns the allowed value of the property, given its index.
* Intended for enumerating allowed property values.
* @param propertyName
* @param index
* @param value
*/
virtual bool GetPropertyValueAt(const char* propertyName, unsigned index, char* value) const
{
MM::Property* pProp = properties_.Find(propertyName);
if (!pProp)
return false;
std::vector<std::string> values = pProp->GetAllowedValues();
if (values.size() < index)
return false;
CDeviceUtils::CopyLimitedString(value, values[index].c_str());
return true;
}
/**
* Creates a new property for the device.
* @param name - property name
* @param value - initial value
* @param eType - property type (string, integer or float)
* @param readOnly - is the property read-only or not
* @param pAct - function object called on the property actions
* @param isPreInitProperty - whether to create a "pre-init" property, whose
* value will be available before Initialize() is called
*/
int CreateProperty(const char* name, const char* value, MM::PropertyType eType, bool readOnly, MM::ActionFunctor* pAct=0, bool isPreInitProperty=false)
{
return properties_.CreateProperty(name, value, eType, readOnly, pAct, isPreInitProperty);
}
/**
* Creates a new property for the device.
* @param name - property name
* @param value - initial value
* @param eType - property type (string, integer or float)
* @param readOnly - is the property read-only or not
* @param memberFunction - Function pointer to the device object "OnProperty" member function, e.g. &MyDevice::OnState
* @param isPreInitProperty - whether to create a "pre-init" property, whose
* value will be available before Initialize() is called
*/
int CreatePropertyWithHandler(const char* name, const char* value, MM::PropertyType eType, bool readOnly,
int(U::*memberFunction)(MM::PropertyBase* pProp, MM::ActionType eAct), bool isPreInitProperty=false) {
CPropertyAction* pAct = new CPropertyAction((U*) this, memberFunction);
return CreateProperty(name, value, eType, readOnly, pAct, isPreInitProperty);
}
/**
* Create an integer-valued property for the device
*/
int CreateIntegerProperty(const char* name, long value, bool readOnly, MM::ActionFunctor* pAct = 0, bool isPreInitProperty = false)
{
// Note: in theory, we can avoid converting to string and back. At this
// moment, it is not worth the trouble.
std::ostringstream oss;
oss << value;
return CreateProperty(name, oss.str().c_str(), MM::Integer, readOnly, pAct, isPreInitProperty);
}
/**
* Create a float-valued property for the device
*/
int CreateFloatProperty(const char* name, double value, bool readOnly, MM::ActionFunctor* pAct = 0, bool isPreInitProperty = false)
{
// Note: in theory, we can avoid converting to string and back. At this
// moment, it is not worth the trouble.
//
// However, note the following assumption being made here: the default
// settings of std::ostream will return strings with a decimal precision
// of 6 digits. When this eventually gets passed to
// MM::FloatProperty::Set(double), it gets truncated to 4 digits before
// being stored. Thus, we do not loose any information.
std::ostringstream oss;
oss << value;
return CreateProperty(name, oss.str().c_str(), MM::Float, readOnly, pAct, isPreInitProperty);
}
/**
* Create a string-valued property for the device
*/
int CreateStringProperty(const char* name, const char* value, bool readOnly, MM::ActionFunctor* pAct = 0, bool isPreInitProperty = false)
{
return CreateProperty(name, value, MM::String, readOnly, pAct, isPreInitProperty);
}
/**
* Define limits for properties with continuous range of values
*/
int SetPropertyLimits(const char* name, double low, double high)
{
MM::Property* pProp = properties_.Find(name);
if (!pProp)
{
SetMorePropertyErrorInfo(name);
return DEVICE_INVALID_PROPERTY;
}
if (pProp->SetLimits(low, high))
return DEVICE_OK;
else {
std::ostringstream os;
os << "Device adapter requests invalid values ( " << low << ", ";
os << high << ") for property: " << name;
LogMessage(os.str().c_str(), false);
return DEVICE_INVALID_PROPERTY_LIMTS;
}
}
/**
* Sets an entire array of allowed values.
*/
int SetAllowedValues(const char* name, std::vector<std::string>& values)
{
return properties_.SetAllowedValues(name, values);
}
/**
* Clears allowed values, and makes any value valid.
*/
int ClearAllowedValues(const char* name)
{
return properties_.ClearAllowedValues(name);
}
/**
* Add a single allowed value.
*/
int AddAllowedValue(const char* name, const char* value)
{
return properties_.AddAllowedValue(name, value);
}
/**
* Add a single allowed value, plus an additional data.
*/
int AddAllowedValue(const char* name, const char* value, long data)
{
return properties_.AddAllowedValue(name, value, data);
}
/**
* Obtains data field associated with the allowed property value.
*/
int GetPropertyData(const char* name, const char* value, long& data)
{
int ret = properties_.GetPropertyData(name, value, data);
if( DEVICE_OK != ret)
// additional information for reporting invalid properties.
SetMorePropertyErrorInfo(name);
return ret;
}
/**
* Obtains data field associated with the currently applied property value.
*/
int GetCurrentPropertyData(const char* name, long& data)
{
int ret = properties_.GetCurrentPropertyData(name, data);
if( DEVICE_OK != ret)
// additional information for reporting invalid properties.
SetMorePropertyErrorInfo(name);
return ret;
}
/**
* Refresh the entire state of the device and synchronize property values with
* the actual state of the hardware.
*/
int UpdateStatus()
{
return properties_.UpdateAll();
}
/**
* Update property value from the hardware.
*/
int UpdateProperty(const char* name)
{
return properties_.Update(name);
}
/**
* Apply the current property value to the hardware.
*/
int ApplyProperty(const char* name)
{
return properties_.Apply(name);
}
/**
* Obtains the error text associated with the error code.
*/
virtual bool GetErrorText(int errorCode, char* text) const
{
std::map<int, std::string>::const_iterator it;
it = messages_.find(errorCode);
if (it == messages_.end())
{
// generic message
std::ostringstream osTxt;
osTxt << "Error code " << errorCode << " (" << std::setbase(16) << errorCode << " hex)";
CDeviceUtils::CopyLimitedString(text, osTxt.str().c_str());
return false; // message text not found
}
else
{
std::ostringstream stringStreamMessage;
stringStreamMessage << it->second.c_str();
// add the additional 'property' error info.
if( 2<=errorCode && errorCode<=5 )
{
stringStreamMessage << ": " << GetMorePropertyErrorInfo();
}
SetMorePropertyErrorInfo("");
// native message
CDeviceUtils::CopyLimitedString(text, stringStreamMessage.str().c_str());
return true; // message found
}
}
// device discovery (auto-configuration)
virtual bool SupportsDeviceDetection(void) {
return false;
}
virtual MM::DeviceDetectionStatus DetectDevice(void){
return MM::Unimplemented;
};
// hub - peripheral relationship
virtual void SetParentID(const char* parentId)
{
parentID_ = parentId;
// truncate if necessary
if (parentID_.size() >= (unsigned) MM::MaxStrLength)
parentID_ = parentID_.substr(MM::MaxStrLength-1);
if (this->HasProperty(MM::g_Keyword_HubID))
{
this->SetProperty(MM::g_Keyword_HubID, parentID_.c_str());
}
}
virtual void GetParentID(char* parentID) const
{
CDeviceUtils::CopyLimitedString(parentID, parentID_.c_str());
}
////////////////////////////////////////////////////////////////////////////
// Protected methods, for internal use by the device adapters
////////////////////////////////////////////////////////////////////////////
protected:
CDeviceBase() : module_(0), delayMs_(0), usesDelay_(false), callback_(0)
{
InitializeDefaultErrorMessages();
}
virtual ~CDeviceBase() {}
/**
* Defines the error text associated with the code.
*/
void SetErrorText(int errorCode, const char* text)
{
messages_[errorCode] = text;
}
const char* GetMorePropertyErrorInfo(void) const
{
return morePropertyErrorInfo_.c_str();
}
void SetMorePropertyErrorInfo( const char* ptext) const
{
morePropertyErrorInfo_ = ptext;
}
/**
* Output the specified text message to the log stream.
* @param msg - message text
* @param debugOnly - if true the message will be sent only in the log-debug mode
*/
int LogMessage(const char* msg, bool debugOnly = false) const
{
if (callback_)
return callback_->LogMessage(this, msg, debugOnly);
return DEVICE_NO_CALLBACK_REGISTERED;
}
/**
* Output the specified text message to the log stream.
* @param msg - message text
* @param debugOnly - if true the message will be sent only in the log-debug mode
*/
int LogMessage(const std::string& msg, bool debugOnly = false) const
{
if (callback_)
return callback_->LogMessage(this, msg.c_str(), debugOnly);
return DEVICE_NO_CALLBACK_REGISTERED;
}
/**
* Output the text message of specified code to the log stream.
* @param errorCode - error code
* @param debugOnly - if true the message will be sent only in the log-debug mode
*/
int LogMessageCode(const int errorCode, bool debugOnly = false) const
{
if (callback_)
{
char text[MM::MaxStrLength];
GetErrorText(errorCode, text);
return callback_->LogMessage(this, text, debugOnly);
}
return DEVICE_NO_CALLBACK_REGISTERED;
}
/**
* Outputs time difference between two time stamps.
* Handy for hardware profiling
* @param start - Time stamp for start of Process
* @param end - Time stamp for end of Process
* @param message - message that will be displayed in output
* @param debugOnly - if true the message will be sent only in the log-debug mode
*/
int LogTimeDiff(MM::MMTime start, MM::MMTime end, const std::string& message, bool debugOnly = false) const
{
std::ostringstream os;
MM::MMTime t = end-start;
os << message << t.toString() << " seconds";
if (callback_)
return callback_->LogMessage(this, os.str().c_str(), debugOnly);
return DEVICE_NO_CALLBACK_REGISTERED;
}
/**
* Outputs time difference between two time stamps.
* Handy for hardware profiling
* @param start - Time stamp for start of Process
* @param end - Time stamp for end of Process
* @param debugOnly - if true the message will be sent only in the log-debug mode
*/
int LogTimeDiff(MM::MMTime start, MM::MMTime end, bool debugOnly = false) const
{
return LogTimeDiff(start, end, "Process took: " , debugOnly);
}
/**
* Sets-up the standard set of error codes and error messages.
*/
void InitializeDefaultErrorMessages()
{
// initialize error codes
SetErrorText(DEVICE_ERR, g_Msg_ERR);
SetErrorText(DEVICE_INVALID_PROPERTY, g_Msg_INVALID_PROPERTY);
SetErrorText(DEVICE_INVALID_PROPERTY_VALUE, g_Msg_INVALID_PROPERTY_VALUE);
SetErrorText(DEVICE_DUPLICATE_PROPERTY, g_Msg_DUPLICATE_PROPERTY);
SetErrorText(DEVICE_INVALID_PROPERTY_TYPE, g_Msg_INVALID_PROPERTY_TYPE);
SetErrorText(DEVICE_NATIVE_MODULE_FAILED, g_Msg_NATIVE_MODULE_FAILED);
SetErrorText(DEVICE_UNSUPPORTED_DATA_FORMAT, g_Msg_UNSUPPORTED_DATA_FORMAT);
SetErrorText(DEVICE_INTERNAL_INCONSISTENCY, g_Msg_INTERNAL_INCONSISTENCY);
SetErrorText(DEVICE_NOT_SUPPORTED, g_Msg_NOT_SUPPORTED);
SetErrorText(DEVICE_UNKNOWN_LABEL, g_Msg_UNKNOWN_LABEL);
SetErrorText(DEVICE_UNSUPPORTED_COMMAND, g_Msg_UNSUPPORTED_COMMAND);
SetErrorText(DEVICE_UNKNOWN_POSITION, g_Msg_UNKNOWN_POSITION);
SetErrorText(DEVICE_NO_CALLBACK_REGISTERED, g_Msg_NO_CALLBACK_REGISTERED);
SetErrorText(DEVICE_SERIAL_COMMAND_FAILED, g_Msg_SERIAL_COMMAND_FAILED);
SetErrorText(DEVICE_SERIAL_BUFFER_OVERRUN, g_Msg_SERIAL_BUFFER_OVERRUN);
SetErrorText(DEVICE_SERIAL_INVALID_RESPONSE, g_Msg_SERIAL_INVALID_RESPONSE);
SetErrorText(DEVICE_SERIAL_TIMEOUT, g_Msg_SERIAL_TIMEOUT);
SetErrorText(DEVICE_SELF_REFERENCE, g_Msg_SELF_REFERENCE);
SetErrorText(DEVICE_NO_PROPERTY_DATA, g_Msg_NO_PROPERTY_DATA);
SetErrorText(DEVICE_DUPLICATE_LABEL, g_Msg_DEVICE_DUPLICATE_LABEL);
SetErrorText(DEVICE_INVALID_INPUT_PARAM, g_Msg_INVALID_INPUT_PARAM);
SetErrorText(DEVICE_BUFFER_OVERFLOW, g_Msg_BUFFER_OVERFLOW);
SetErrorText(DEVICE_NONEXISTENT_CHANNEL, g_Msg_DEVICE_NONEXISTENT_CHANNEL);
SetErrorText(DEVICE_INVALID_PROPERTY_LIMTS, g_Msg_DEVICE_INVALID_PROPERTY_LIMTS);
SetErrorText(DEVICE_CAMERA_BUSY_ACQUIRING, g_Msg_DEVICE_CAMERA_BUSY_ACQUIRING);
SetErrorText(DEVICE_CAN_NOT_SET_PROPERTY, g_Msg_DEVICE_CAN_NOT_SET_PROPERTY);
SetErrorText(DEVICE_LOCALLY_DEFINED_ERROR, "t.b.d.");
SetErrorText(DEVICE_NOT_CONNECTED, g_Msg_DEVICE_NOT_CONNECTED);
SetErrorText(DEVICE_COMM_HUB_MISSING, g_Msg_DEVICE_COMM_HUB_MISSING);
SetErrorText(DEVICE_DUPLICATE_LIBRARY, g_Msg_DEVICE_DUPLICATE_LIBRARY);
SetErrorText(DEVICE_PROPERTY_NOT_SEQUENCEABLE, g_Msg_DEVICE_PROPERTY_NOT_SEQUENCEABLE);
SetErrorText(DEVICE_SEQUENCE_TOO_LARGE, g_Msg_DEVICE_SEQUENCE_TOO_LARGE);
SetErrorText(DEVICE_NOT_YET_IMPLEMENTED, g_Msg_DEVICE_NOT_YET_IMPLEMENTED);
}
/**
* Gets the handle (pointer) to the specified device label.
* With this method we can get a handle to other devices loaded in the system,
* if we know the device name.
*/
MM::Device* GetDevice(const char* deviceLabel) const
{
if (callback_)
return callback_->GetDevice(this, deviceLabel);
return 0;
}
/**
* Provides access to the names of devices of a given type
* deviceIterator determines which device in the list of devices of the
* given type will become accessible in deviceName.
* If deviceIterator exceeds the number of devices of the given type,
* no action will be taked (i.e., the memory pointed to by deviceName
* will be unchanged).
*
*/
// Microsoft compiler has trouble generating code to transport stl objects across DLL boundary
// so we use char*. Other compilers could conceivably have similar trouble, if for example,
// a dynamic library is linked with a different CRT than its client.
void GetLoadedDeviceOfType(MM::DeviceType devType, char* deviceName, const unsigned int deviceIterator )
{
deviceName[0] = 0;
if (callback_)
{
callback_->GetLoadedDeviceOfType( this, devType, deviceName, deviceIterator);
}
}
/**
* Sends an array of bytes to the com port.
*/
int WriteToComPort(const char* portLabel, const unsigned char* buf, unsigned bufLength)
{
if (callback_)
return callback_->WriteToSerial(this, portLabel, buf, bufLength);
return DEVICE_NO_CALLBACK_REGISTERED;
}
/**
* Sends an ASCII string withe the specified terminating characters to the serial port.
* @param portName
* @param command - command string