From f1a1391866c34d346c7ccfe1ead73011fc0ef67c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Fr=C3=B6jdh?= Date: Wed, 15 May 2019 10:22:38 +0200 Subject: [PATCH] Factored out time conversion (#23) * time conversion * with comment * lround in slsDetectorCommand --- .../include/multiSlsDetector.h | 24 ++++--- slsDetectorSoftware/src/multiSlsDetector.cpp | 71 +++++-------------- .../src/slsDetectorCommand.cpp | 6 +- 3 files changed, 35 insertions(+), 66 deletions(-) diff --git a/slsDetectorSoftware/include/multiSlsDetector.h b/slsDetectorSoftware/include/multiSlsDetector.h index 2a945e38a..1ebec7292 100755 --- a/slsDetectorSoftware/include/multiSlsDetector.h +++ b/slsDetectorSoftware/include/multiSlsDetector.h @@ -2146,14 +2146,14 @@ class multiSlsDetector : public virtual slsDetectorDefs { - /** - * add gap pixels to the image (only for Eiger in 4 bit mode) - * @param image pointer to image without gap pixels - * @param gpImage poiner to image with gap pixels, if NULL, allocated - * inside function - * @returns number of data bytes of image with gap pixels - */ - int processImageWithGapPixels(char *image, char *&gpImage); + /** + * add gap pixels to the image (only for Eiger in 4 bit mode) + * @param image pointer to image without gap pixels + * @param gpImage poiner to image with gap pixels, if NULL, allocated + * inside function + * @returns number of data bytes of image with gap pixels + */ + int processImageWithGapPixels(char *image, char *&gpImage); /** * Set total progress (total number of frames/images in an acquisition) @@ -2209,6 +2209,14 @@ class multiSlsDetector : public virtual slsDetectorDefs { */ std::vector readPofFile(const std::string &fname); + /** + * Convert a double holding time in seconds to an int64_t with nano seconds + * Used for conversion when sending time to detector + * @param t time in seconds + * @returns time in nano seconds + */ + int64_t secondsToNanoSeconds(double t); + /** Multi detector Id */ const int multiId{0}; diff --git a/slsDetectorSoftware/src/multiSlsDetector.cpp b/slsDetectorSoftware/src/multiSlsDetector.cpp index 6da911c14..76a1b212f 100755 --- a/slsDetectorSoftware/src/multiSlsDetector.cpp +++ b/slsDetectorSoftware/src/multiSlsDetector.cpp @@ -1099,89 +1099,50 @@ int64_t multiSlsDetector::setTimer(timerIndex index, int64_t t, int detPos) { return ret; } +int64_t multiSlsDetector::secondsToNanoSeconds(double t){ + int64_t ns = lround(t * 1E9); + return (ns < 0) ? -1: ns; +} + double multiSlsDetector::setExposureTime(double t, bool inseconds, int detPos) { if (!inseconds) { return setTimer(ACQUISITION_TIME, (int64_t)t, detPos); } - - // + 0.5 to round for precision lost from converting double to int64_t - int64_t tms = (int64_t)(t * (1E+9) + 0.5); - if (t < 0) { - tms = -1; - } - tms = setTimer(ACQUISITION_TIME, tms, detPos); - if (tms < 0) { - return -1; - } - return ((1E-9) * (double)tms); + auto t_ns = setTimer(ACQUISITION_TIME, secondsToNanoSeconds(t), detPos); + return (t_ns < 0) ? -1 : 1E-9 * t_ns; } double multiSlsDetector::setExposurePeriod(double t, bool inseconds, int detPos) { if (!inseconds) { return setTimer(FRAME_PERIOD, (int64_t)t, detPos); } - - // + 0.5 to round for precision lost from converting double to int64_t - int64_t tms = (int64_t)(t * (1E+9) + 0.5); - if (t < 0) { - tms = -1; - } - tms = setTimer(FRAME_PERIOD, tms, detPos); - if (tms < 0) { - return -1; - } - return ((1E-9) * (double)tms); + auto t_ns = setTimer(FRAME_PERIOD, secondsToNanoSeconds(t), detPos); + return (t_ns < 0) ? -1 : 1E-9 * t_ns; } double multiSlsDetector::setDelayAfterTrigger(double t, bool inseconds, int detPos) { if (!inseconds) { return setTimer(DELAY_AFTER_TRIGGER, (int64_t)t, detPos); } - - // + 0.5 to round for precision lost from converting double to int64_t - int64_t tms = (int64_t)(t * (1E+9) + 0.5); - if (t < 0) { - tms = -1; - } - tms = setTimer(DELAY_AFTER_TRIGGER, tms, detPos); - if (tms < 0) { - return -1; - } - return ((1E-9) * (double)tms); + auto t_ns = setTimer(DELAY_AFTER_TRIGGER, secondsToNanoSeconds(t), detPos); + return (t_ns < 0) ? -1 : 1E-9 * t_ns; } double multiSlsDetector::setSubFrameExposureTime(double t, bool inseconds, int detPos) { if (!inseconds) { return setTimer(SUBFRAME_ACQUISITION_TIME, (int64_t)t, detPos); - } else { - // + 0.5 to round for precision lost from converting double to int64_t - int64_t tms = (int64_t)(t * (1E+9) + 0.5); - if (t < 0) { - tms = -1; - } - tms = setTimer(SUBFRAME_ACQUISITION_TIME, tms, detPos); - if (tms < 0) { - return -1; - } - return ((1E-9) * (double)tms); } + auto t_ns = setTimer(SUBFRAME_ACQUISITION_TIME, secondsToNanoSeconds(t), detPos); + return (t_ns < 0) ? -1 : 1E-9 * t_ns; } double multiSlsDetector::setSubFrameExposureDeadTime(double t, bool inseconds, int detPos) { if (!inseconds) { return setTimer(SUBFRAME_DEADTIME, (int64_t)t, detPos); - } else { - // + 0.5 to round for precision lost from converting double to int64_t - int64_t tms = (int64_t)(t * (1E+9) + 0.5); - if (t < 0) { - tms = -1; - } - tms = setTimer(SUBFRAME_DEADTIME, tms, detPos); - if (tms < 0) { - return -1; - } - return ((1E-9) * (double)tms); } + auto t_ns = setTimer(SUBFRAME_DEADTIME, secondsToNanoSeconds(t), detPos); + return (t_ns < 0) ? -1 : 1E-9 * t_ns; + } int64_t multiSlsDetector::setNumberOfFrames(int64_t t, int detPos) { diff --git a/slsDetectorSoftware/src/slsDetectorCommand.cpp b/slsDetectorSoftware/src/slsDetectorCommand.cpp index 4306b6d61..4208c0c25 100755 --- a/slsDetectorSoftware/src/slsDetectorCommand.cpp +++ b/slsDetectorSoftware/src/slsDetectorCommand.cpp @@ -3,6 +3,7 @@ #include "string_utils.h" #include +#include #include #include #include @@ -4496,10 +4497,9 @@ std::string slsDetectorCommand::cmdTimer(int narg, char *args[], int action, int if (index == ACQUISITION_TIME || index == SUBFRAME_ACQUISITION_TIME || index == FRAME_PERIOD || index == DELAY_AFTER_TRIGGER || index == SUBFRAME_DEADTIME || index == STORAGE_CELL_DELAY) { - // +0.5 for precision of eg.0.0000325 - t = (val * 1E9 + 0.5); + t = lround(val * 1E9); } else - t = (int64_t)val; + t = static_cast(val); } myDet->setOnline(ONLINE_FLAG, detPos);