Skip to content

Commit

Permalink
Require explicit construction of MMTime
Browse files Browse the repository at this point in the history
Forbid implicit conversion from double (so that, for example, arithmetic
and comparison operators won't treat double as microseconds but instead
require an explicit MMTime object). This is safer because there is a
lower risk of getting the units wrong.

While this change requires device code to be updated, it does not affect
the binary interface and therefore does not require the device interface
version to be incremented.

Changes to device adapters all belong to these categories:

- Comparison of an MMTime with a number -> replace number with MMTime
  using appropriate unit (`1000.0 * x` -> `MM::MMTime::fromMs(x)`).

- In a subset of the previous case, remove a redundant if statement and
  return a bool directly.

- Replace assignment of 0 with assignment of MM::MMTime{}.

- Replace `return 0` with `return {}` in functions returning MMTime.

- Subtraction of a number from MMTime -> replace with MMTime using
  appropriate unit (fromSeconds())

All of the above changes preserve behavior exactly. But a few obvious
bugs were fixed:

- K8601/K8601.cpp:539
  Fix `1000.0 < GetDelayMs()` which should have been `1000.0 *
  GetDelayMs()`; use `MM::MMTime::fromMs(GetDelayMs())`.
  Compare K8055 and others that use the same pattern.

- Yokogawa/CSUX.cpp:330
  Comment said '33 msec' but use of raw number meant it was interpreted
  as 33 us. Fixed to use 33 ms as this was almost certainly what was
  intended.

The following looks like a potential bug, but I'm leaving the behavior
unchanged in the absence of a means to test:

- PeCon2000/PeCon2000.cpp:378
  Comparison with `double delta` interprets `delta` as microseconds, but
  usage of `is_older_than` under variable name `is_older_than_a_minute`
  suggests `delta` (= 60.0) was meant to be in seconds.
  I cannot tell if the behavior is correct or the name, so I'm opting to
  preserve current behavior.
  • Loading branch information
marktsuchida committed Oct 25, 2022
1 parent e1f6543 commit 4e1518f
Show file tree
Hide file tree
Showing 43 changed files with 100 additions and 131 deletions.
5 changes: 1 addition & 4 deletions DeviceAdapters/ASIFW1000/ASIFW1000.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1001,10 +1001,7 @@ bool Shutter::Busy()
{
//TODO: using the SQ command and shutters with sensors, we can check wether the shutter is open or closed. This need checking for sensors on initialization, and will also need caching of the last requested position
MM::MMTime interval = GetCurrentMMTime() - changedTime_;
if (interval < (1000.0 * GetDelayMs() ))
return true;
else
return false;
return interval < MM::MMTime::fromMs(GetDelayMs());
}

int Shutter::Shutdown()
Expand Down
4 changes: 2 additions & 2 deletions DeviceAdapters/AmScope/SequenceThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ void SequenceThread::Start(long numImages, double intervalMs)
stop_ = false;
suspend_=false;
activate();
actualDuration_ = 0;
actualDuration_ = MM::MMTime{};
startTime_= camera_->GetCurrentMMTime();
lastFrameTime_ = 0;
lastFrameTime_ = MM::MMTime{};
}

bool SequenceThread::IsStopped(){
Expand Down
2 changes: 1 addition & 1 deletion DeviceAdapters/AndorSDK3/AndorSDK3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1845,7 +1845,7 @@ void MySequenceThread::Start(long numImages, double intervalMs)
stop_ = false;
suspend_ = false;
activate();
actualDuration_ = 0;
actualDuration_ = MM::MMTime{};
startTime_ = camera_->GetCurrentMMTime();
}

Expand Down
5 changes: 1 addition & 4 deletions DeviceAdapters/Arduino/Arduino.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1247,10 +1247,7 @@ bool CArduinoShutter::Busy()
{
MM::MMTime interval = GetCurrentMMTime() - changedTime_;

if (interval < (1000.0 * GetDelayMs() ))
return true;
else
return false;
return interval < MM::MMTime::fromMs(GetDelayMs());
}

int CArduinoShutter::Initialize()
Expand Down
5 changes: 1 addition & 4 deletions DeviceAdapters/Arduino32bitBoards/Arduino32bitBoards.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1335,10 +1335,7 @@ bool CArduino32Shutter::Busy()
{
MM::MMTime interval = GetCurrentMMTime() - changedTime_;

if (interval < (1000.0 * GetDelayMs() ))
return true;
else
return false;
return interval < MM::MMTime::fromMs(GetDelayMs());
}

int CArduino32Shutter::Initialize()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,10 +332,7 @@ bool CArduinoNeoPixelShutter::Busy()
{
MM::MMTime interval = GetCurrentMMTime() - changedTime_;

if (interval < (1000.0 * GetDelayMs() ))
return true;
else
return false;
return interval < MM::MMTime::fromMs(GetDelayMs());
}

int CArduinoNeoPixelShutter::Initialize()
Expand Down
10 changes: 2 additions & 8 deletions DeviceAdapters/CSUW1/CSUW1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -617,10 +617,7 @@ int Shutter::Initialize()
bool Shutter::Busy()
{
MM::MMTime interval = GetCurrentMMTime() - changedTime_;
if (interval < (1000.0 * GetDelayMs() ))
return true;

return false;
return interval < MM::MMTime::fromMs(GetDelayMs());
}

int Shutter::Shutdown()
Expand Down Expand Up @@ -1471,10 +1468,7 @@ int NIRShutter::Initialize()
bool NIRShutter::Busy()
{
MM::MMTime interval = GetCurrentMMTime() - changedTime_;
if (interval < (1000.0 * GetDelayMs() ))
return true;

return false;
return interval < MM::MMTime::fromMs(GetDelayMs());
}

int NIRShutter::Shutdown()
Expand Down
6 changes: 1 addition & 5 deletions DeviceAdapters/Conix/Conix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -417,11 +417,7 @@ int HexaFluor::Shutdown()
bool HexaFluor::Busy()
{
MM::MMTime interval = GetCurrentMMTime() - changedTime_;

if (interval < (1000.0 * GetDelayMs()) )
return true;

return false;
return interval < MM::MMTime::fromMs(GetDelayMs());
}


Expand Down
4 changes: 2 additions & 2 deletions DeviceAdapters/DemoCamera/DemoCamera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1219,9 +1219,9 @@ void MySequenceThread::Start(long numImages, double intervalMs)
stop_ = false;
suspend_=false;
activate();
actualDuration_ = 0;
actualDuration_ = MM::MMTime{};
startTime_= camera_->GetCurrentMMTime();
lastFrameTime_ = 0;
lastFrameTime_ = MM::MMTime{};
}

bool MySequenceThread::IsStopped(){
Expand Down
4 changes: 2 additions & 2 deletions DeviceAdapters/IDS_uEye/IDS_uEye.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1445,9 +1445,9 @@ void MySequenceThread::Start(long numImages, double intervalMs)
stop_ = false;
suspend_=false;
activate();
actualDuration_ = 0;
actualDuration_ = MM::MMTime{};
startTime_= camera_->GetCurrentMMTime();
lastFrameTime_ = 0;
lastFrameTime_ = MM::MMTime{};
}

bool MySequenceThread::IsStopped(){
Expand Down
6 changes: 1 addition & 5 deletions DeviceAdapters/K8055/K8055.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -506,11 +506,7 @@ void CK8055Shutter::GetName(char* name) const
bool CK8055Shutter::Busy()
{
MM::MMTime interval = GetCurrentMMTime() - changedTime_;

if (interval < (1000.0 * GetDelayMs() ))
return true;
else
return false;
return interval < MM::MMTime::fromMs(GetDelayMs());
}

int CK8055Shutter::Initialize()
Expand Down
6 changes: 1 addition & 5 deletions DeviceAdapters/K8061/K8061.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -536,11 +536,7 @@ void CK8061Shutter::GetName(char* name) const
bool CK8061Shutter::Busy()
{
MM::MMTime interval = GetCurrentMMTime() - changedTime_;

if (interval < (1000.0 < GetDelayMs() ))
return true;
else
return false;
return interval < MM::MMTime::fromMs(GetDelayMs());
}

int CK8061Shutter::Initialize()
Expand Down
20 changes: 8 additions & 12 deletions DeviceAdapters/LeicaDMR/LeicaDMR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -453,8 +453,8 @@ int Lamp::Initialize()
return ret;

// Set timer for the Busy signal, or we'll get a time-out the first time we check the state of the shutter, for good measure, go back 5s into the past
changedTime_ = GetCurrentMMTime() - 5000000;
changedTime_ = GetCurrentMMTime() - MM::MMTime::fromSeconds(5);

// Check current intensity of lamp
ret = g_hub.GetLampIntensity(*this, *GetCoreCallback(), intensity_);
if (DEVICE_OK != ret)
Expand Down Expand Up @@ -497,10 +497,7 @@ int Lamp::Initialize()
bool Lamp::Busy()
{
MM::MMTime interval = GetCurrentMMTime() - changedTime_;
if (interval < (1000.0 * GetDelayMs() ))
return true;

return false;
return interval < MM::MMTime::fromMs(GetDelayMs());
}

int Lamp::Shutdown()
Expand Down Expand Up @@ -654,8 +651,8 @@ int RLShutter::Initialize()
return ret;

// Set timer for the Busy signal, or we'll get a time-out the first time we check the state of the shutter, for good measure, go back 5s into the past
changedTime_ = GetCurrentMMTime() - 5000000;
changedTime_ = GetCurrentMMTime() - MM::MMTime::fromSeconds(5);

// State
CPropertyAction* pAct = new CPropertyAction (this, &RLShutter::OnState);
ret = CreateProperty(MM::g_Keyword_State, "0", MM::Integer, false, pAct);
Expand All @@ -681,8 +678,7 @@ bool RLShutter::Busy()
// TODO: determine whether we need to use delay
/*
MM::MMTime interval = GetCurrentMMTime() - changedTime_;
if (interval < (1000.0 * GetDelayMs() ))
return true;
return interval < MM::MMTime::fromMs(GetDelayMs());
*/
return false;
}
Expand Down Expand Up @@ -801,8 +797,8 @@ int ZStage::Initialize()
return ret;

// Set timer for the Busy signal, or we'll get a time-out the first time we check the state of the shutter, for good measure, go back 5s into the past
changedTime_ = GetCurrentMMTime() - 5000000;
changedTime_ = GetCurrentMMTime() - MM::MMTime::fromSeconds(5);

// Position
// There are two reference frames. An absolute reference frame (implemeted here)
// and a relative reference frame, implemented with a upper and lower lower limit
Expand Down
3 changes: 2 additions & 1 deletion DeviceAdapters/LeicaDMR/LeicaDMRHub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ int LeicaDMRHub::GetRLModulePosition(MM::Device& device, MM::Core& core, int& po
pos = 0;
MM::MMTime start = core.GetCurrentMMTime();
int ret;
while (pos == 0 && (core.GetCurrentMMTime() - start < 500000) ) {
while (pos == 0 &&
(core.GetCurrentMMTime() - start < MM::MMTime::fromMs(500))) {
ret = GetCommand(device, core, rLFA_, 10, pos);
if (ret != DEVICE_OK)
return ret;
Expand Down
2 changes: 1 addition & 1 deletion DeviceAdapters/LeicaDMSTC/LeicaDMSTC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ int XYStage::Initialize()
// Do not implement a position property as it can lead to trouble!

// Set timer for the Busy signal, or we'll get a time-out the first time we check the state of the shutter, for good measure, go back 5s into the past
changedTime_ = GetCurrentMMTime() - 5000000;
changedTime_ = GetCurrentMMTime() - MM::MMTime::fromSeconds(5);

initialized_ = true;

Expand Down
2 changes: 1 addition & 1 deletion DeviceAdapters/Ludl/Ludl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1405,7 +1405,7 @@ bool Shutter::Busy()
{
// First check delay, then query controller
MM::MMTime interval = GetCurrentMMTime() - changedTime_;
if (interval < (1000.0 * GetDelayMs()))
if (interval < MM::MMTime::fromMs(GetDelayMs()))
return true;

clearPort(*this, *GetCoreCallback(), GetPort().c_str());
Expand Down
4 changes: 2 additions & 2 deletions DeviceAdapters/MatrixVision/mvIMPACT_Acquire_Device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1214,9 +1214,9 @@ void MySequenceThread::Start( long numImages, double intervalMs )
stop_ = false;
suspend_ = false;
activate();
actualDuration_ = 0;
actualDuration_ = MM::MMTime{};
startTime_ = pmvIMPACT_Acquire_Device_->GetCurrentMMTime();
lastFrameTime_ = 0;
lastFrameTime_ = MM::MMTime{};
}

//-----------------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions DeviceAdapters/Mightex_C_Cam/Mightex_USBCamera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1668,9 +1668,9 @@ void MySequenceThread::Start(long numImages, double intervalMs)
stop_ = false;
suspend_=false;
activate();
actualDuration_ = 0;
actualDuration_ = MM::MMTime{};
startTime_= camera_->GetCurrentMMTime();
lastFrameTime_ = 0;
lastFrameTime_ = MM::MMTime{};
}

bool MySequenceThread::IsStopped(){
Expand Down
6 changes: 1 addition & 5 deletions DeviceAdapters/Neos/Neos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,7 @@ int Neos::Shutdown()
bool Neos::Busy()
{
MM::MMTime interval = GetCurrentMMTime() - changedTime_;

if (interval < (1000.0 * GetDelayMs()))
return true;

return false;
return interval < MM::MMTime::fromMs(GetDelayMs());
}


Expand Down
3 changes: 2 additions & 1 deletion DeviceAdapters/NikonTE2000/TEHub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,8 @@ int TEHub::ExecuteCommand(MM::Device& device, MM::Core& core, const char* type,
else
{
CDeviceUtils::SleepMs(delayMs);
if ( (core.GetCurrentMMTime() - startTime) > (maxTimeMs*1000.0) )
if ((core.GetCurrentMMTime() - startTime) >
MM::MMTime::fromMs(maxTimeMs))
done = true;
}
}
Expand Down
4 changes: 2 additions & 2 deletions DeviceAdapters/Okolab/OkolabDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1016,9 +1016,9 @@ void OkolabThread::Start(double intervalMs)
_stop = false;
_suspend = false;
activate();
_actualDuration = 0;
_actualDuration = MM::MMTime{};
_startTime = _device->GetCurrentMMTime();
_lastFrameTime = 0;
_lastFrameTime = MM::MMTime{};
Sleep(THREAD_SLEEP_MS);
}

Expand Down
4 changes: 2 additions & 2 deletions DeviceAdapters/OpenCVgrabber/OpenCVgrabber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -920,9 +920,9 @@ void MySequenceThread::Start(long numImages, double intervalMs)
stop_ = false;
suspend_=false;
activate();
actualDuration_ = 0;
actualDuration_ = MM::MMTime{};
startTime_= camera_->GetCurrentMMTime();
lastFrameTime_ = 0;
lastFrameTime_ = MM::MMTime{};
}

bool MySequenceThread::IsStopped(){
Expand Down
6 changes: 4 additions & 2 deletions DeviceAdapters/OxxiusCombiner/Oxxius_combiner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -565,9 +565,11 @@ int OxxiusCombinerHub::QueryCommand(MM::Device* device, MM::Core* core, const un
while (!done) {
counter++;
ret = core->GetSerialAnswer(device, port_.c_str(), RCV_BUF_LENGTH, rcvBuf_, "\r\n");
if ( (ret == DEVICE_OK) || ( (core->GetCurrentMMTime() - startTime) > (maxTimeMs*1000.0) ) )
if ((ret == DEVICE_OK) ||
((core->GetCurrentMMTime() - startTime) >
MM::MMTime::fromMs(maxTimeMs))) {
done = true;
else {
} else {
CDeviceUtils::SleepMs(delayMs);
delayMs *= 2;
}
Expand Down
2 changes: 1 addition & 1 deletion DeviceAdapters/PeCon2000/PeCon2000.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ struct is_older_than {
inline is_older_than(MM::MMTime now, double delta) : now(now), delta(delta) {};

inline bool operator()(const MM::MMTime& then) {
return (now - then) > delta;
return (now - then) > MM::MMTime(delta);
}
};

Expand Down
4 changes: 2 additions & 2 deletions DeviceAdapters/RaptorEPIX/RaptorEPIX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6098,9 +6098,9 @@ void MySequenceThread::Start(long numImages, double intervalMs)
stop_ = false;
suspend_=false;
activate();
actualDuration_ = 0;
actualDuration_ = MM::MMTime{};
startTime_= camera_->GetCurrentMMTime();
lastFrameTime_ = 0;
lastFrameTime_ = MM::MMTime{};
}

bool MySequenceThread::IsStopped(){
Expand Down
4 changes: 1 addition & 3 deletions DeviceAdapters/SpectralLMM5/SpectralLMM5.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -568,9 +568,7 @@ void LMM5Shutter::GetName(char* pszName) const
bool LMM5Shutter::Busy()
{
MM::MMTime interval = GetCurrentMMTime() - changedTime_;
if (interval < (1000.0 * GetDelayMs() ))
return true;
return false;
return interval < MM::MMTime::fromMs(GetDelayMs());
}

int LMM5Shutter::SetOpen(bool open)
Expand Down
4 changes: 2 additions & 2 deletions DeviceAdapters/Spinnaker/SpinnakerCamera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1717,9 +1717,9 @@ void SpinnakerAcquisitionThread::Start(long numImages, double intervalMs)
m_stop = false;
m_suspend = false;
activate();
m_actualDuration = 0;
m_actualDuration = MM::MMTime{};
m_startTime = m_spkrCam->GetCurrentMMTime();
m_lastFrameTime = 0;
m_lastFrameTime = MM::MMTime{};
m_spkrCam->allocateImageBuffer(m_spkrCam->GetImageBufferSize(), m_spkrCam->m_cam->PixelFormat.GetValue());

if (numImages == -1)
Expand Down
Loading

0 comments on commit 4e1518f

Please sign in to comment.