Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add async property callback to DemoCamera #160

Merged
merged 9 commits into from
Feb 25, 2022
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions DeviceAdapters/DemoCamera/DemoCamera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <algorithm>
#include "WriteCompactTiffRGB.h"
#include <iostream>
#include <future>



Expand Down Expand Up @@ -396,6 +397,17 @@ int CDemoCamera::Initialize()
}
}

// Test Property with an async callback
// When the leader is set the follower will be set to the same value
// with some delay (default 2 seconds).
// This is to allow downstream testing of callbacks originating from
// device threads.
pAct = new CPropertyAction (this, &CDemoCamera::OnAsyncLeader);
CreateStringProperty("AsyncPropertyLeader", "init", false, pAct);
pAct = new CPropertyAction (this, &CDemoCamera::OnAsyncFollower);
CreateStringProperty("AsyncPropertyFollower", "init", true, pAct);
CreateIntegerProperty("AsyncPropertyDelayMS", 2000, false);

//pAct = new CPropertyAction(this, &CDemoCamera::OnSwitch);
//nRet = CreateIntegerProperty("Switch", 0, false, pAct);
//SetPropertyLimits("Switch", 8, 1004);
Expand Down Expand Up @@ -1289,6 +1301,47 @@ int CDemoCamera::OnTestProperty(MM::PropertyBase* pProp, MM::ActionType eAct, lo

}

void CDemoCamera::SlowPropUpdate(std::string leaderValue)
marktsuchida marked this conversation as resolved.
Show resolved Hide resolved
{
// wait in order to simulate a device doing something slowly
// in a thread
long delay; GetProperty("AsyncPropertyDelayMS", delay);
CDeviceUtils::SleepMs(delay);
{
MMThreadGuard g(asyncFollowerLock_);
asyncFollower_ = leaderValue;
}
OnPropertyChanged("AsyncPropertyFollower", leaderValue.c_str());
}

int CDemoCamera::OnAsyncFollower(MM::PropertyBase* pProp, MM::ActionType eAct)
{
if (eAct == MM::BeforeGet){
MMThreadGuard g(asyncFollowerLock_);
pProp->Set(asyncFollower_.c_str());
}
// no AfterSet as this is a readonly property
return DEVICE_OK;
}

int CDemoCamera::OnAsyncLeader(MM::PropertyBase* pProp, MM::ActionType eAct)
{
if (eAct == MM::BeforeGet){
MMThreadGuard g(asyncLeaderLock_);
pProp->Set(asyncLeader_.c_str());
}
if (eAct == MM::AfterSet)
{
std::string asyncLeaderTmp;
{
MMThreadGuard g(asyncLeaderLock_);
pProp->Get(asyncLeader_);
asyncLeaderTmp = asyncLeader_;
}
marktsuchida marked this conversation as resolved.
Show resolved Hide resolved
fut_ = std::async(std::launch::async, &CDemoCamera::SlowPropUpdate, this, asyncLeaderTmp);
}
return DEVICE_OK;
}

/**
* Handles "Binning" property.
Expand Down
11 changes: 10 additions & 1 deletion DeviceAdapters/DemoCamera/DemoCamera.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include <map>
#include <algorithm>
#include <stdint.h>
#include <future>

//////////////////////////////////////////////////////////////////////////////
// Error codes
Expand Down Expand Up @@ -174,7 +175,10 @@ class CDemoCamera : public CCameraBase<CDemoCamera>
// action interface
// ----------------
int OnMaxExposure(MM::PropertyBase* pProp, MM::ActionType eAct);
int OnTestProperty(MM::PropertyBase* pProp, MM::ActionType eAct, long);
int OnTestProperty(MM::PropertyBase* pProp, MM::ActionType eAct, long);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just fixing the indenting from tab to spaces in order to match the rest of the file.

int OnAsyncFollower(MM::PropertyBase* pProp, MM::ActionType eAct);
int OnAsyncLeader(MM::PropertyBase* pProp, MM::ActionType eAct);
void SlowPropUpdate(std::string leaderValue);
int OnBinning(MM::PropertyBase* pProp, MM::ActionType eAct);
int OnPixelType(MM::PropertyBase* pProp, MM::ActionType eAct);
int OnBitDepth(MM::PropertyBase* pProp, MM::ActionType eAct);
Expand Down Expand Up @@ -265,10 +269,15 @@ class CDemoCamera : public CCameraBase<CDemoCamera>
std::vector<unsigned> multiROIHeights_;

double testProperty_[10];
std::string asyncLeader_;
std::string asyncFollower_;
MMThreadLock imgPixelsLock_;
MMThreadLock asyncLeaderLock_;
MMThreadLock asyncFollowerLock_;
friend class MySequenceThread;
int nComponents_;
MySequenceThread * thd_;
std::future<void> fut_;
int mode_;
ImgManipulator* imgManpl_;
double pcf_;
Expand Down