Long time readers will have realized that I have been spending a lot of time with the technology that was formally known as Perceptual Computing (PerC). You may also know that this technology is now known as RealSense and that it will be rolling out to a device near you soon. What you might not know is that I’m currently writing a course on this technology for Pluralsight. As part of writing this course, I’ve been creating a few little wrapper utilities that will make your life easier when developing apps with the SDK.
In this post, I’m going to show you a handy little method for working with API methods. Pretty much every RealSense API method returns a status code to indicate whether or not it was successful. Now, it can get pretty tedious writing code that looks like this:
pxcmStatus status = Session.CreateImpl<PXCMVoiceRecognition>
(PXCMVoiceRecognition.CUID, out voiceRecognition);
if (status < pxcmStatus.pxcmStatus.PXCM_STATUS_NO_ERROR)
{
throw new InvalidStatusException("Could not create session");
}
status = _voiceRecognition.QueryProfile(out pInfo);
if (status < pxcmStatus.pxcmStatus.PXCM_STATUS_NO_ERROR)
{
throw new InvalidStatusException("Could not query profile");
}
As you can imagine, the more calls you make, the more status checks you have to do. Well, I like to log information about what I’m attempting to do and what I have successfully managed to do, so this simple method really helps to write information about the methods being invoked, and to throw an exception if things go wrong.
public void PipelineInvoke(Func<pxcmStatus> pipelineMethod, string loggingInfo = "")
{
if (!string.IsNullOrWhiteSpace(loggingInfo))
{
Debug.WriteLine("Start " + loggingInfo);
}
pxcmStatus status = pipelineMethod();
if (status < pxcmStatus.PXCM_STATUS_NO_ERROR)
{
throw new InvalidStatusException(loggingInfo, status);
}
if (!string.IsNullOrWhiteSpace(loggingInfo))
{
Debug.WriteLine("Finished " + loggingInfo);
}
}
This makes it easier to work with the API and gives us code that looks like this:
PipelineInvoke(() =>
Session.CreateImpl<PXCMVoiceRecognition>(PXCMVoiceRecognition.CUID,
out _voiceRecognition), "creating voice recognition module");
And this is what InvalidStatusException
looks like:
public class InvalidStatusException : Exception
{
public InvalidStatusException(string alertMessage, pxcmStatus status)
{
Message = alertMessage;
Status = status;
}
public string Message { get; private set; }
public pxcmStatus Status { get; private set; }
}
Over the course of the next couple of months, I’ll share more posts with you showing the little tricks and techniques that I use to make working with the RealSense SDK a real joy.
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/peteohanlon.wordpress.com/572/" /> <img alt="" border="0" height="1" src="http://stats.wordpress.com/b.gif?host=peteohanlon.wordpress.com&blog=1230283&post=572&subd=peteohanlon&ref=&feed=1" width="1" />